This is an automated email from the ASF dual-hosted git repository. dubeejw pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git
The following commit(s) were added to refs/heads/master by this push: new 1cbd2bf Document Go actions. (#3194) 1cbd2bf is described below commit 1cbd2bf7811c87abf717f1dd1eeeb517981498c3 Author: rodric rabbah <rod...@gmail.com> AuthorDate: Thu Jan 18 00:55:33 2018 -0500 Document Go actions. (#3194) --- docs/actions.md | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/docs/actions.md b/docs/actions.md index d922140..22f117b 100644 --- a/docs/actions.md +++ b/docs/actions.md @@ -2,9 +2,16 @@ # Creating and invoking OpenWhisk actions -Actions are stateless code snippets that run on the OpenWhisk platform. An action can be written as a JavaScript, Swift, or Python function, a Java method, or a custom executable program packaged in a Docker container. For example, an action can be used to detect the faces in an image, respond to a database change, aggregate a set of API calls, or post a Tweet. +Actions are stateless code snippets that run on the OpenWhisk platform. +For example, an action can be used to detect the faces in an image, respond to a database change, +aggregate a set of API calls, or post a Tweet. +An action can be written as a JavaScript, Swift, Python or PHP function, a Java method, +any binary-compatible executable including Go programs and custom executables packaged as Docker containers. -Actions can be explicitly invoked, or run in response to an event. In either case, each run of an action results in an activation record that is identified by a unique activation ID. The input to an action and the result of an action are a dictionary of key-value pairs, where the key is a string and the value a valid JSON value. Actions can also be composed of calls to other actions or a defined sequence of actions. +Actions can be explicitly invoked, or run in response to an event. +In either case, each run of an action results in an activation record that is identified by a unique activation ID. +The input to an action and the result of an action are a dictionary of key-value pairs, where the key is a string and the value a valid JSON value. +Actions can also be composed of calls to other actions or a defined sequence of actions. ## Prerequisites @@ -18,6 +25,8 @@ Learn how to create, invoke, and debug actions in your preferred development env * [Java](#creating-java-actions) * [PHP](#creating-php-actions) * [Docker](#creating-docker-actions) +* [Go](#creating-go-actions) +* [Native binaries](#creating-native-actions) In addition, learn about: @@ -1107,6 +1116,70 @@ For the instructions that follow, assume that the Docker user ID is `janesmith` wsk action create example exec.zip --docker openwhisk/dockerskeleton ``` +## Creating Go actions + +The `--native` option allows for packaging of any executable as an action. This works for Go as an example. +As with Docker actions, the Go executable receives a single argument from the command line. +It is a string serialization of the JSON object representing the arguments to the action. +The program may log to `stdout` or `stderr`. +By convention, the last line of output _must_ be a stringified JSON object which represents the result of the action. + +Here is an example Go action. +```go +package main + +import "encoding/json" +import "fmt" +import "os" + +func main() { + //program receives one argument: the JSON object as a string + arg := os.Args[1] + + // unmarshal the string to a JSON object + var obj map[string]interface{} + json.Unmarshal([]byte(arg), &obj) + + // can optionally log to stdout (or stderr) + fmt.Println("hello Go action") + + name, ok := obj["name"].(string) + if !ok { name = "Stranger" } + + // last line of stdout is the result JSON object as a string + msg := map[string]string{"msg": ("Hello, " + name + "!")} + res, _ := json.Marshal(msg) + fmt.Println(string(res)) +} +``` + +Save the code above to a file `sample.go` and cross compile it for OpenWhisk. The executable must be called `exec`. +```bash +GOOS=linux GOARCH=amd64 go build -o exec +zip exec.zip exec +wsk action create helloGo --native exec.zip +``` + +The action may be run as any other action. +```bash +wsk action invoke helloGo -r -p name gopher +{ + "msg": "Hello, gopher!" +} +``` + +Logs are retrieved in a similar way as well. +```bash +wsk activation logs --last --strip +my first Go action. +``` + +## Creating native actions + +Using `--native`, you can see that any executable may be run as an OpenWhisk action. This includes `bash` scripts, +or cross compiled binaries. For the latter, the constraint is that the binary must be compatible with the +`openwhisk/dockerskeleton` image. + ## Watching action output OpenWhisk actions might be invoked by other users, in response to various events, or as part of an action sequence. In such cases it can be useful to monitor the invocations. -- To stop receiving notification emails like this one, please contact ['"commits@openwhisk.apache.org" <commits@openwhisk.apache.org>'].