diff --git a/FaaS/OpenFaaS/template/README.md b/FaaS/OpenFaaS/template/README.md index 2f19816..656b950 100644 --- a/FaaS/OpenFaaS/template/README.md +++ b/FaaS/OpenFaaS/template/README.md @@ -93,6 +93,29 @@ Steps could refer to Then you can invoke your python function by OpenFaas UI or faas-cli. +### go-clearlinux +1. mkdir test && cd test +2. `cp -r ""/FaaS/OpenFaaS/template/. template/` +3. `faas-cli new --lang go-clearlinux go-openfaas --prefix=""` + + Files tree as below. +> + ├── go-openfaas + │   ├── handler.go + ├── go-openfaas.yml + └── template + + + * Put the required Clear Linux bundles in the "bundles.txt". + For example, + + `echo "openblas" >> go-openfaas/bundles.txt` + `echo "wget" >> go-openfaas/bundles.txt` +4. `faas-cli up -f go-openfaas.yml` + + Then you can invoke your go function by OpenFaas UI or faas-cli. + + ## Proxy and Clear Linux mirror When working behind a Proxy, you can pass proxy settings to faas-cli commands by "--build-arg". diff --git a/FaaS/OpenFaaS/template/go-clearlinux/Dockerfile b/FaaS/OpenFaaS/template/go-clearlinux/Dockerfile new file mode 100644 index 0000000..90f40c0 --- /dev/null +++ b/FaaS/OpenFaaS/template/go-clearlinux/Dockerfile @@ -0,0 +1,56 @@ +FROM openfaas/classic-watchdog:0.18.1 as watchdog + +FROM clearlinux/golang as builder + +# Allows you to add additional packages via build-arg +ARG CGO_ENABLED=0 + +COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog +RUN chmod +x /usr/bin/fwatchdog + +WORKDIR /go/src/handler +COPY . . + +# install bundles if required +# PLEASE input bundle line by line +COPY bundles.txt . +RUN for bundle in $(cat bundles.txt); do \ + swupd bundle-add $bundle $swupd_args; \ + done + +# Run a gofmt and exclude all vendored code. +RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./function/vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; } + +RUN CGO_ENABLED=${CGO_ENABLED} GOOS=linux \ + go build --ldflags "-s -w" -a -installsuffix cgo -o handler . && \ + go test $(go list ./... | grep -v /vendor/) -cover + +FROM clearlinux:base + +# Add non root user +RUN groupadd app && useradd -g app app +RUN mkdir -p /home/app + +WORKDIR /home/app + +# install bundles if required +# PLEASE input bundle line by line +COPY bundles.txt . +RUN for bundle in $(cat bundles.txt); do \ + swupd bundle-add $bundle $swupd_args; \ + done + +COPY --from=builder /usr/bin/fwatchdog . +COPY --from=builder /go/src/handler/function/ . +COPY --from=builder /go/src/handler/handler . + +RUN chown -R app /home/app + +USER app + +ENV fprocess="./handler" +EXPOSE 8080 + +HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1 + +CMD ["./fwatchdog"] diff --git a/FaaS/OpenFaaS/template/go-clearlinux/bundles.txt b/FaaS/OpenFaaS/template/go-clearlinux/bundles.txt new file mode 100644 index 0000000..e69de29 diff --git a/FaaS/OpenFaaS/template/go-clearlinux/function/handler.go b/FaaS/OpenFaaS/template/go-clearlinux/function/handler.go new file mode 100644 index 0000000..0c3f15f --- /dev/null +++ b/FaaS/OpenFaaS/template/go-clearlinux/function/handler.go @@ -0,0 +1,10 @@ +package function + +import ( + "fmt" +) + +// Handle a serverless request +func Handle(req []byte) string { + return fmt.Sprintf("Hello, Go. You said: %s", string(req)) +} diff --git a/FaaS/OpenFaaS/template/go-clearlinux/main.go b/FaaS/OpenFaaS/template/go-clearlinux/main.go new file mode 100644 index 0000000..9b6e4bc --- /dev/null +++ b/FaaS/OpenFaaS/template/go-clearlinux/main.go @@ -0,0 +1,22 @@ +// Copyright (c) Alex Ellis 2017. All rights reserved. +// Copyright (c) OpenFaaS Author(s) 2018. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +package main + +import ( + "fmt" + "handler/function" + "io/ioutil" + "log" + "os" +) + +func main() { + input, err := ioutil.ReadAll(os.Stdin) + if err != nil { + log.Fatalf("Unable to read standard input: %s", err.Error()) + } + + fmt.Println(function.Handle(input)) +} diff --git a/FaaS/OpenFaaS/template/go-clearlinux/template.yml b/FaaS/OpenFaaS/template/go-clearlinux/template.yml new file mode 100644 index 0000000..5d341d5 --- /dev/null +++ b/FaaS/OpenFaaS/template/go-clearlinux/template.yml @@ -0,0 +1,2 @@ +language: go +fprocess: ./handler