Last active
June 9, 2020 18:25
-
-
Save jacobweinstock/586cbba370f638010dee55c2b60c08bf to your computer and use it in GitHub Desktop.
serverless and cake deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## FYI docker needs at least a few GBs of memory | |
if [ "$1" == "cleanup" ]; then | |
docker rm -f $(docker ps -a -q -f "name=nuclio-") | |
docker volume rm nuclio-local-storage | |
rm -rf cake.tar.gz nuclio_spec.yaml main.go | |
exit 0 | |
fi | |
cakespec="./spec.yaml" | |
cakebinary="./cake-linux" | |
if [ ! -f "./spec.yaml" ]; then | |
echo -e "what's the full path to the cake spec.yaml file?" | |
read cakespec | |
cp -a $cakespec ./spec.yaml | |
fi | |
if [ ! -f "./cake-linux" ]; then | |
echo -e "what's the full path to the cake-linux binary?" | |
read cakebinary | |
cp -a $cakebinary ./cake-linux | |
fi | |
# start nuclio server | |
docker run -d -p 8070:8070 -v /tmp:/tmp -v /var/run/docker.sock:/var/run/docker.sock --name nuclio-dashboard quay.io/nuclio/dashboard:unstable-amd64 | |
# write out nuclio spec file | |
cat <<EOF > nuclio_spec.yaml | |
metadata: | |
name: cake | |
labels: | |
nuclio.io/project-name: default | |
spec: | |
triggers: | |
myHttpTrigger: | |
maxWorkers: 4 | |
kind: "http" | |
attributes: | |
port: 32002 | |
maxRequestBodySize: 3221225472 | |
readBufferSize: 3221225472 | |
handler: "main:Handler" | |
runtime: golang | |
build: | |
codeEntryType: sourceCode | |
onbuildImage: quay.io/nuclio/handler-builder-golang-onbuild:unstable-amd64-alpine | |
path: main.go | |
loggerSinks: | |
- level: debug | |
EOF | |
# write out function | |
cat <<EOF > main.go | |
package main | |
import ( | |
"archive/tar" | |
"bytes" | |
"compress/gzip" | |
"github.com/nuclio/nuclio-sdk-go" | |
"io" | |
"io/ioutil" | |
"os" | |
"path/filepath" | |
"os/exec" | |
) | |
func Handler(context *nuclio.Context, event nuclio.Event) (interface{}, error) { | |
var err error | |
context.Logger.DebugWith("Got request", "path", event.GetPath(), "ctype", event.GetContentType()) | |
dir, err := ioutil.TempDir("", "cake") | |
if err != nil { | |
context.Logger.DebugWith("error creating temp dir", "error", err) | |
return nuclio.Response{ | |
StatusCode: 500, | |
ContentType: "application/json", | |
}, err | |
} | |
defer os.RemoveAll(dir) | |
r := bytes.NewReader(event.GetBody()) | |
err = tarExtract(dir, r) | |
cmd := exec.Command(filepath.Join(dir, "cake-linux"), "deploy", "rancher", "-j", "-f", filepath.Join(dir, "spec.yaml")) | |
out, err := cmd.CombinedOutput() | |
if err != nil { | |
context.Logger.DebugWith("error running command", "error", err) | |
return nuclio.Response{ | |
StatusCode: 500, | |
ContentType: "application/json", | |
Body: out, | |
}, nil | |
} | |
return nuclio.Response{ | |
StatusCode: 200, | |
Body: out, | |
}, nil | |
} | |
func tarExtract(dst string, r io.Reader) error { | |
gzr, err := gzip.NewReader(r) | |
if err != nil { | |
return err | |
} | |
defer gzr.Close() | |
tr := tar.NewReader(gzr) | |
for { | |
header, err := tr.Next() | |
switch { | |
case err == io.EOF: | |
return nil | |
case err != nil: | |
return err | |
case header == nil: | |
continue | |
} | |
target := filepath.Join(dst, header.Name) | |
switch header.Typeflag { | |
case tar.TypeDir: | |
if _, err := os.Stat(target); err != nil { | |
if err := os.MkdirAll(target, 0755); err != nil { | |
return err | |
} | |
} | |
case tar.TypeReg: | |
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)) | |
if err != nil { | |
return err | |
} | |
if _, err := io.Copy(f, tr); err != nil { | |
return err | |
} | |
f.Close() | |
} | |
} | |
} | |
EOF | |
# install nuctl | |
which nuctl || (wget -O /usr/local/bin/nuctl https://github.com/nuclio/nuclio/releases/download/1.4.6/nuctl-1.4.6-$(go env GOOS)-$(go env GOARCH); chmod +x /usr/local/bin/nuctl) | |
# deploy function | |
nuctl deploy --platform local -f nuclio_spec.yaml --path main.go | |
# tar the cake binary and a spec file | |
rm -rf cake.tar.gz | |
tar zcvf cake.tar.gz cake-linux spec.yaml | |
# call the function endpoint | |
curl --data-binary @cake.tar.gz http://localhost:32002 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment