Skip to content

Commit 576be87

Browse files
Jiaxin FanJiaxin Fan
authored andcommitted
Explain Gradle and add one use case
1 parent 754c1ec commit 576be87

File tree

1 file changed

+109
-22
lines changed

1 file changed

+109
-22
lines changed

docs/users/standalone/README.md

Lines changed: 109 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,59 @@
1818
-->
1919

2020
# Tests for OpenWhisk NodeJS Runtime as Standalone Container
21+
[![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0)
22+
23+
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/)
2128
## 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)
2445

25-
- Run the `distDocker` command to generate the local Docker image for the desired runtime version.
2646
```
2747
./gradlew core:nodejs18Action:distDocker
2848
```
29-
This will return the following runtime image with the name `action-nodejs-v18`, which should be listed after using the `docker images`
3049

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+
```
3154
## 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):
3457
```
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
3659
```
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`.
3861

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.
4174
```json
4275
{
4376
"value": {
@@ -48,42 +81,96 @@ This example has prepared a `helloworld.json` file to post using `curl`.
4881
}
4982
}
5083
```
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.
5285

53-
### Initialze the Runtime
86+
## Initialze the Runtime
5487
Before issuing the action against the runtime, we first initialize the function with by invoking the ```/init``` endpoint.
5588
```
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}
5994
```
60-
being the expected response.
6195

6296
As mentioned above, if `port 80` on `localhost` was used, the command could simply be
6397
```
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
6599
```
66100

67-
#### Run the function
101+
## Run the function
68102

69103
Invoke the function using the ```/run``` endpoint.
70104

71105
```
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
73107
```
74108

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.
76110
```json
77111
{
78112
"value": {}
79113
}
80114
```
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+
81120
The expected response should be
82121
```
83122
{"payload":"Hello World!"}
84123
```
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
133+
```json
134+
{
135+
"value": {
136+
"name": "js-helloworld-with-params",
137+
"main" : "main",
138+
"binary" : false,
139+
"code" : "function main(params) { return {payload: 'Hello ' + params.name + ' from ' + params.place + '!!!'} }"
140+
}
141+
}
142+
```
143+
144+
Also, create a file called `js-run-params.json` which contains the parameters for triggering the function.
145+
```json
146+
{
147+
"value": {
148+
"name": "Visitor",
149+
"place": "Earth"
150+
}
151+
}
152+
```
153+
These files shall be sent via the `init` API and via the `run` API respectively.
154+
155+
To initialize the function, please make sure your NodeJS runtime container is running.
156+
First, issue a `POST` request against the `init` API using curl:
157+
```
158+
curl -H "Content-Type:application/json" -X POST --data '@/$FILEPATH/js-init-params.json' http://localhost:3008/init
159+
```
160+
161+
Next, trigger the function by issuing this request against the `run` API using curl:
162+
```
163+
curl -H ""Content-Type:application/json" -X POST --data '/$FILEPATH/js-run-params.json' http://localhost:3008/run
164+
```
165+
166+
You should expect the following client response:
167+
```
168+
{"payload": "Hello Visitor from Earth!!!"}
169+
```
170+
171+
And this response from the container:
86172
```
87173
XXX_THE_END_OF_A_WHISK_ACTIVATION_XXX
88174
```
89-
in the terminal
175+
176+
## Thanks for following along!

0 commit comments

Comments
 (0)