You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This README walks you through how to build, customise and test Apache OpenWhisk Node.js runtime images.
24
+
## Pre-requisites
25
+
-[Gradle](https://gradle.org/)
26
+
-[Docker](https://www.docker.com/)
27
+
-[curl](https://curl.se/)
21
28
## Building the Runtime Container
22
-
After a runtime container is built, one should be able to run it as a standalone container.
23
-
The following example shows how to generate a Docker image for the Node.js 18 runtime version and test the standalone runtime using the `curl` command. Testing other runtime versions can be done in the same manner.
29
+
Choose a NodeJS version. All build files reside inside `core/nodejsActionBase`. If you take a look into `core/nodejsActionBase/Dockerfile` you’ll see a line that looks like:
30
+
```
31
+
FROM node:lts-stretch
32
+
```
33
+
This will use the latest NodeJS version. But we want to be more specific. Now if you look into each of the Dockerfile within `core/nodejs14Action`, `core/nodejs16Action`, `core/nodejs18Action`, you’ll notice different NodeJS versions. Let’s go ahead with the 18 version. We are going to use this version throughout the README, for the others, you merely have to modify the version number.
34
+
35
+
Gradle will a create `build` folder that will contain all the necessary files to build our NodeJS container. Next, it will copy the NodeJS application (server used to implement the [action interface](https://github.com/apache/openwhisk/blob/master/docs/actions-new.md#action-interface)) as well as the target Dockerfile with the NodeJS version 18.
36
+
37
+
What Gradle does is equivalent to running these commands
38
+
```
39
+
mkdir build
40
+
cp -r core/nodejsActionBase/* build
41
+
cp core/nodejs18Action/Dockerfile build
42
+
```
43
+
44
+
Now, run the `distDocker` command to generate a local Docker image for the chosen runtime version. (Make sure docker daemon is running)
24
45
25
-
- Run the `distDocker` command to generate the local Docker image for the desired runtime version.
26
46
```
27
47
./gradlew core:nodejs18Action:distDocker
28
48
```
29
-
This will return the following runtime image with the name `action-nodejs-v18`, which should be listed after using the `docker images`
30
49
50
+
This will return the following runtime image with the name `action-nodejs-v18`. Since a docker image is created, you can check the `IMAGE ID` for `nodejs-action-v18`
51
+
```
52
+
docker images
53
+
```
31
54
## Running the Container
32
-
For the purpose of the test. We are going to start the container that has a web service running inside it built using Node/Express. In order to access this service within the container from the outside, as we are about to do using `curl`, port mapping needs to be done next. As a result, we can now access the web service inside docker by first reaching an IP port on `localhost`, which subsequently forwards the request to the docker container's designated port.
33
-
In our example, the `Action` container exposes `port 8080` (see the Dockerfile for the associated Docker image), thus we publish the container's `port 8080` to the `localhost` (here, `port 3008` on `localhost` is chosen arbitrarily, as long as the port is not already assigned for something else):
55
+
For the testing purpose, we are going to start the container locally that has Node.js app server inside. The Apache OpenWhisk platform uses this server to inject action code into the runtime and fire invocation requests. In order to access this service within the container from the outside, as we are about to do using `curl`, port mapping needs to be done next. As a result, we can now access the web service inside docker by first reaching an IP port on `localhost`, which subsequently forwards the request to the docker container's designated port.
56
+
In our example, the `Action` container exposes `port 8080` (see the Dockerfile for the associated Docker image), thus we publish the container's `port 8080` to the `localhost` (here, `port 3008` on `localhost` is chosen arbitrarily, as long as the port is not already used for something else):
34
57
```
35
-
docker run --publish 3008:8080 -i -t action-nodejs-v18:latest
58
+
docker run --publish 3008:8080 --name=bloom_whisker -i -t action-nodejs-v18:latest
36
59
```
37
-
A simpler way is to map `port 80` on `localhost` to the container's `port 8080`. The port number assigned to the HTTP protocol is `80` Since we will be sending actions against the runtime using HTTP, using this number will allow us to omit the port in the request later. Without loss of generality, the following examples will use the arbitrarily chosen `port 3008`
60
+
A simpler way is to map `port 80` on `localhost` to the container's `port 8080`. The port number assigned to the HTTP protocol is `80`. Since we will be sending actions against the runtime using HTTP, using this number will allow us to omit the port in the request later. Oftentimes, `port 80 ` could already be occupied by another process. Without loss of generality, the following examples will use the arbitrarily chosen `port 3008`.
38
61
39
-
## Testing
40
-
This example has prepared a `helloworld.json` file to post using `curl`.
62
+
Lists all running containers
63
+
```
64
+
docker ps
65
+
```
66
+
or
67
+
```
68
+
docker ps -a
69
+
```
70
+
You should see a container named `bloom_whisker` being run.
71
+
72
+
## Create your function
73
+
A container can only hold one function. This first example prepared a `js-init.json` file which contains the function.
41
74
```json
42
75
{
43
76
"value": {
@@ -48,42 +81,96 @@ This example has prepared a `helloworld.json` file to post using `curl`.
48
81
}
49
82
}
50
83
```
51
-
The json file contains a simple JavaScript function, which is the actual payload.
84
+
The json file contains a simple JavaScript (the target runtime language) function, which is the actual payload.
52
85
53
-
###Initialze the Runtime
86
+
## Initialze the Runtime
54
87
Before issuing the action against the runtime, we first initialize the function with by invoking the ```/init``` endpoint.
55
88
```
56
-
curl -H "Content-Type:application/json" -X POST --data '@openwhisk-runtime-nodejs/tests/src/test/standalone/helloworld/helloworld.json' http://localhost:3008/init
57
-
58
-
{"OK":true}
89
+
curl -H "Content-Type:application/json" -X POST --data '@/$FILEPATH/js-init.json' http://localhost:3008/init
90
+
```
91
+
the expected response being
92
+
```
93
+
{"ok":true}
59
94
```
60
-
being the expected response.
61
95
62
96
As mentioned above, if `port 80` on `localhost` was used, the command could simply be
63
97
```
64
-
curl -H "Content-Type:application/json" -X POST --data '@openwhisk-runtime-nodejs/tests/src/test/standalone/helloworld/helloworld.json' http://localhost/init
98
+
curl -H "Content-Type:application/json" -X POST --data '@/$FILEPATH/js-init.json' http://localhost/init
65
99
```
66
100
67
-
####Run the function
101
+
## Run the function
68
102
69
103
Invoke the function using the ```/run``` endpoint.
70
104
71
105
```
72
-
curl -H ""Content-Type:application/json" -X POST --data '@openwhisk-runtime-nodejs/tests/src/test/standalone/helloworld/helloworld.json' http://localhost:3008/run
106
+
curl -H ""Content-Type:application/json" -X POST --data '@/$FILEPATH/js-init.json' http://localhost:3008/run
73
107
```
74
108
75
-
The JavaScript function in this example is one without arguments. Using the same json file as during initialization won't be a problem. Strictly speaking, we should have provided another json file with the arguments. In our case, it should simply be
109
+
The JavaScript function in this example is one without arguments (nullary function). Using the same json file as during initialization won't be a problem. Ideally, we should have provided another file `js-params.json`with the arguments to trigger the function.
76
110
```json
77
111
{
78
112
"value": {}
79
113
}
80
114
```
115
+
In this case the command to trigger the function should be
116
+
```
117
+
curl -H ""Content-Type:application/json" -X POST --data '/$FILEPATH/js-params.json' http://localhost:3008/run
118
+
```
119
+
81
120
The expected response should be
82
121
```
83
122
{"payload":"Hello World!"}
84
123
```
85
-
Also the running container will print
124
+
Also the running container will print the following message in the terminal
125
+
```
126
+
XXX_THE_END_OF_A_WHISK_ACTIVATION_XXX
127
+
```
128
+
129
+
## Functions with arguments
130
+
If your container still running from the previous example you must stop it before proceeding. Because each NodeJS runtime can only hold one function which cannot be overridden.
131
+
132
+
Create a file called `js-init-params.json` that contains the function to be initialized
0 commit comments