diff --git a/sample-functions/NodeBaseFunction/Dockerfile b/sample-functions/NodeBaseFunction/Dockerfile new file mode 100644 index 00000000..c0dcd2dc --- /dev/null +++ b/sample-functions/NodeBaseFunction/Dockerfile @@ -0,0 +1,18 @@ +FROM node:6.9.1-alpine + +ADD https://github.com/alexellis/faas/releases/download/0.5.1-alpha/fwatchdog /usr/bin +RUN chmod +x /usr/bin/fwatchdog + +WORKDIR /root/ + +COPY package.json . + +# Provides a boot-strap into your function, just add handler.js to derived image +RUN npm i +COPY faas_index.js . + +ENV fprocess="node faas_index.js" + +HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1 + +CMD ["fwatchdog"] diff --git a/sample-functions/NodeBaseFunction/build.sh b/sample-functions/NodeBaseFunction/build.sh new file mode 100755 index 00000000..7eb1e34a --- /dev/null +++ b/sample-functions/NodeBaseFunction/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +echo "Building functions/nodebase:alpine-6.9.1" +docker build -t functions/nodebase:alpine-6.9.1 . + diff --git a/sample-functions/NodeBaseFunction/faas_index.js b/sample-functions/NodeBaseFunction/faas_index.js new file mode 100644 index 00000000..de57c707 --- /dev/null +++ b/sample-functions/NodeBaseFunction/faas_index.js @@ -0,0 +1,29 @@ +"use strict" + +let getStdin = require('get-stdin'); + +let handler = require('./handler'); + +getStdin().then(val => { + + let req; + if(process.env.json) { + req = JSON.parse(val); + } else { + req = val + } + + handler(req, (err, res) => { + if(err) { + return console.error(err); + } + + if(process.env.json) { + console.log(JSON.stringify(res)); + } else { + console.log(res); + } + }); +}).catch(e => { + console.error(e.stack); +}); diff --git a/sample-functions/NodeBaseFunction/package.json b/sample-functions/NodeBaseFunction/package.json new file mode 100644 index 00000000..d35e9625 --- /dev/null +++ b/sample-functions/NodeBaseFunction/package.json @@ -0,0 +1,15 @@ +{ + "name": "NodejsBase", + "version": "1.0.0", + "description": "", + "main": "faas_index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "get-stdin": "^5.0.1" + } +} diff --git a/sample-functions/NodeInfo/Dockerfile.func b/sample-functions/NodeInfo/Dockerfile.func new file mode 100644 index 00000000..6854ea19 --- /dev/null +++ b/sample-functions/NodeInfo/Dockerfile.func @@ -0,0 +1,3 @@ +FROM functions/nodebase:alpine-6.9.1 + +COPY handler.js . diff --git a/sample-functions/NodeInfo/build.func.sh b/sample-functions/NodeInfo/build.func.sh new file mode 100755 index 00000000..262ef55f --- /dev/null +++ b/sample-functions/NodeInfo/build.func.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +docker build -t functions/nodeinfo:func . -f Dockerfile.func diff --git a/sample-functions/NodeInfo/handler.js b/sample-functions/NodeInfo/handler.js new file mode 100644 index 00000000..77ba8b2d --- /dev/null +++ b/sample-functions/NodeInfo/handler.js @@ -0,0 +1,23 @@ +'use strict' +let os = require('os'); +let fs = require('fs'); +let util = require('util'); + +module.exports = (content, callback) => { + fs.readFile("/etc/hostname", "utf8", (err, data) => { + let val = ""; + val += "Hostname: " + data +"\n"; + val += "Platform: " + os.platform()+"\n"; + val += "Arch: " + os.arch() + "\n"; + val += "CPU count: " + os.cpus().length+ "\n"; + + val += "Uptime: " + os.uptime()+ "\n"; + + if (content && content.length && content.indexOf("verbose") > -1) { + val += util.inspect(os.cpus()) + "\n"; + val += util.inspect(os.networkInterfaces())+ "\n"; + } + + callback(null, val); + }); +}; diff --git a/sample-functions/NodeInfo/oneshot.txt b/sample-functions/NodeInfo/oneshot.txt deleted file mode 100644 index ba574b78..00000000 --- a/sample-functions/NodeInfo/oneshot.txt +++ /dev/null @@ -1 +0,0 @@ -docker build -t nodeinfo . ; docker service rm NodeInfo ; docker service create --network=functions --name NodeInfo nodeinfo