Add node and numpy-mp openfaas templates

Signed-off-by: Qi Zheng <qi.zheng@intel.com>
This commit is contained in:
Qi Zheng
2020-01-13 15:18:00 +08:00
committed by qzheng527
parent c4423637f8
commit 97385e2349
25 changed files with 512 additions and 84 deletions
+12 -72
View File
@@ -39,86 +39,26 @@ Steps could refer to
[OpenFaaS workshop](https://github.com/openfaas/workshop)
### dockerfile-clearlinux
1. `mkdir test && cd test`
2. `cp -r "<your-dockerfiles-path>"/FaaS/OpenFaaS/template/. template/`
3. `faas-cli new --lang dockerfile-clearlinux hello-dockerfile --prefix="<your-docker-username-here>"`
### Get Clear Linux based templates
Files tree as below.
>
├── hello-dockerfile
│   ├── bundles.txt
│   └── Dockerfile
├── hello-dockerfile.yml
```shell
$ mkdir test && cd test
$ git clone https://github.com/clearlinux/dockerfiles.git
$ cp -r dockerfiles/FaaS/OpenFaaS/template .
```
* Put the required Clear Linux bundles in the "bundles.txt".
For example,
### For each template how-to, please refer to
`echo "openblas" >> hello-dockerfile/bundles.txt`
`echo "wget" >> hello-dockerfile/bundles.txt`
4. Replace the "cat" in the Dockerfile line below per your requirement.
ENV fprocess="cat"
5. `faas-cli up -f hello-dockerfile.yml`
* [dockerfile-clearlinux](./dockerfile-clearlinux/README.md)
Then you can invoke your python function by OpenFaas UI or faas-cli.
* [go-clearlinux](./go-clearlinux/README.md)
### python3-clearlinux
1. `mkdir test && cd test`
2. `cp -r "<your-dockerfiles-path>"/FaaS/OpenFaaS/template/. template/`
3. `faas-cli new --lang python3-clearlinux hello-openfaas --prefix="<your-docker-username-here>"`
* [node-clearlinux](./node-clearlinux/README.md)
Files tree as below.
>
├── hello-openfaas
│   ├── bundles.txt
│   ├── handler.py
│   ├── helper_script.sh
│   ├── __init__.py
│   └── requirements.txt
├── hello-openfaas.yml
└── template
* [numpy-mp](./numpy-mp/README.md)
* [python3-clearlinux](./python3-clearlinux/README.md)
* Put the required python packages in the "requirements.txt".
For example,
`echo "redis" >> hello-openfaas/requirements.txt`
`echo "flask" >> hello-openfaas/requirements.txt`
* Put the required Clear Linux bundles in the "bundles.txt".
For example,
`echo "openblas" >> hello-openfaas/bundles.txt`
`echo "wget" >> hello-openfaas/bundles.txt`
* Put any initial/helper operation in the "help_script.sh"
4. `faas-cli up -f hello-openfaas.yml`
Then you can invoke your python function by OpenFaas UI or faas-cli.
### go-clearlinux
1. mkdir test && cd test
2. `cp -r "<your-dockerfiles-path>"/FaaS/OpenFaaS/template/. template/`
3. `faas-cli new --lang go-clearlinux go-openfaas --prefix="<your-docker-username-here>"`
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
@@ -0,0 +1,23 @@
Based on clearlinux:base image
### dockerfile-clearlinux
1. `faas-cli new --lang dockerfile-clearlinux hello-openfaas --prefix="<your-docker-username-here>"`
Files tree as below.
>
├── hello-openfaas
│   ├── bundles.txt
│   └── Dockerfile
├── hello-openfaas.yml
└── template
* Put the required Clear Linux bundles in the "bundles.txt".
For example,
`echo "openblas" >> hello-openfaas/bundles.txt`
`echo "wget" >> hello-openfaas/bundles.txt`
2. `faas-cli up -f hello-openfaas.yml`
Then you can invoke your python function by OpenFaas UI or faas-cli.
@@ -13,7 +13,7 @@ COPY . .
# install bundles if required
# PLEASE input bundle line by line
COPY bundles.txt .
COPY function/bundles.txt .
RUN for bundle in $(cat bundles.txt); do \
swupd bundle-add $bundle $swupd_args; \
done
@@ -35,7 +35,7 @@ WORKDIR /home/app
# install bundles if required
# PLEASE input bundle line by line
COPY bundles.txt .
COPY function/bundles.txt .
RUN for bundle in $(cat bundles.txt); do \
swupd bundle-add $bundle $swupd_args; \
done
@@ -0,0 +1,22 @@
Based on clearlinux/golang container image
### go-clearlinux
1. `faas-cli new --lang go-clearlinux go-openfaas --prefix="<your-docker-username-here>"`
Files tree as below.
>
.
├── go-openfaas
│   ├── bundles.txt
│   └── 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`
2. `faas-cli up -f go-openfaas.yml`
Then you can invoke your go function by OpenFaas UI or faas-cli.
@@ -0,0 +1,62 @@
FROM openfaas/of-watchdog:0.7.2 as watchdog
FROM clearlinux/node:12 as ship
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog
RUN useradd app
WORKDIR /root/
# install bundles if required
# PLEASE input bundle line by line
COPY function/bundles.txt .
RUN for bundle in $(cat bundles.txt); do \
swupd bundle-add $bundle $swupd_args; \
done
# Turn down the verbosity to default level.
ENV NPM_CONFIG_LOGLEVEL warn
# Wrapper/boot-strapper
WORKDIR /home/app
COPY package.json ./
# This ordering means the npm installation is cached for the outer function handler.
RUN npm i
# Copy outer function handler
COPY index.js ./
# COPY function node packages and install, adding this as a separate
# entry allows caching of npm install
WORKDIR /home/app/function
COPY function/*.json ./
RUN npm i || :
# COPY function files and folders
COPY function/ ./
# Set correct permissions to use non root user
WORKDIR /home/app/
# chmod for tmp is for a buildkit issue (@alexellis)
RUN chown app:app -R /home/app \
&& chmod 777 /tmp
USER app
ENV cgi_headers="true"
ENV fprocess="node index.js"
ENV mode="http"
ENV upstream_url="http://127.0.0.1:3000"
ENV exec_timeout="10s"
ENV write_timeout="15s"
ENV read_timeout="15s"
HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1
CMD ["fwatchdog"]
@@ -0,0 +1,28 @@
Based on clearlinux/node:12 container image.
### node-clearlinux
1. `faas-cli new --lang node-clearlinux node-openfaas --prefix="<your-docker-username-here>"`
Files tree as below.
>
├── node-openfaas
│   ├── bundles.txt
│   ├── handler.js
│   └── package.json
├── node-openfaas.yml
└── template
* The entry is "handler.js", do any change you want in this file.
* Put the required Clear Linux bundles in the "bundles.txt".
For example,
`echo "openblas" >> node-openfaas/bundles.txt`
`echo "wget" >> node-openfaas/bundles.txt`
2. `faas-cli up -f node-openfaas.yml`
Then you can invoke your python function by OpenFaas UI or faas-cli.
@@ -0,0 +1,12 @@
"use strict"
module.exports = async (event, context) => {
let err;
const result = {
status: "Received input: " + JSON.stringify(event.body)
};
return context
.status(200)
.succeed(result);
}
@@ -0,0 +1,12 @@
{
"name": "function",
"version": "1.0.0",
"description": "",
"main": "handler.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
@@ -0,0 +1,113 @@
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
"use strict"
const express = require('express')
const app = express()
const handler = require('./function/handler');
const bodyParser = require('body-parser')
// app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(bodyParser.raw());
app.use(bodyParser.text({ type : "text/*" }));
app.disable('x-powered-by');
class FunctionEvent {
constructor(req) {
this.body = req.body;
this.headers = req.headers;
this.method = req.method;
this.query = req.query;
this.path = req.path;
}
}
class FunctionContext {
constructor(cb) {
this.value = 200;
this.cb = cb;
this.headerValues = {};
this.cbCalled = 0;
}
status(value) {
if(!value) {
return this.value;
}
this.value = value;
return this;
}
headers(value) {
if(!value) {
return this.headerValues;
}
this.headerValues = value;
return this;
}
succeed(value) {
let err;
this.cbCalled++;
this.cb(err, value);
}
fail(value) {
let message;
this.cbCalled++;
this.cb(value, message);
}
}
var middleware = async (req, res) => {
let cb = (err, functionResult) => {
if (err) {
console.error(err);
return res.status(500).send(err.toString ? err.toString() : err);
}
if(isArray(functionResult) || isObject(functionResult)) {
res.set(fnContext.headers()).status(fnContext.status()).send(JSON.stringify(functionResult));
} else {
res.set(fnContext.headers()).status(fnContext.status()).send(functionResult);
}
};
let fnEvent = new FunctionEvent(req);
let fnContext = new FunctionContext(cb);
Promise.resolve(handler(fnEvent, fnContext, cb))
.then(res => {
if(!fnContext.cbCalled) {
fnContext.succeed(res);
}
})
.catch(e => {
cb(e);
});
};
app.post('/*', middleware);
app.get('/*', middleware);
app.patch('/*', middleware);
app.put('/*', middleware);
app.delete('/*', middleware);
const port = process.env.http_port || 3000;
app.listen(port, () => {
console.log(`OpenFaaS Node.js listening on port: ${port}`)
});
let isArray = (a) => {
return (!!a) && (a.constructor === Array);
};
let isObject = (a) => {
return (!!a) && (a.constructor === Object);
};
@@ -0,0 +1,16 @@
{
"name": "node10-express",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.2"
}
}
@@ -0,0 +1,7 @@
language: node-clearlinux
fprocess: node index.js
welcome_message: |
You have created a new function which uses Node.js 12 and the OpenFaaS
of-watchdog.
npm i --save can be used to add third-party packages like request or cheerio
npm documentation: https://docs.npmjs.com/
@@ -0,0 +1,39 @@
FROM openfaas/classic-watchdog:0.18.1 as watchdog
FROM clearlinux/python:3
ARG swupd_args
RUN swupd bundle-add python-extras $swupd_args
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog
RUN useradd numpy-func
WORKDIR /home/numpy-func
COPY function /home/numpy-func/function
WORKDIR /home/numpy-func/function
# pip install python packages if required
RUN pip install -r requirements.txt
# install bundles if required
# PLEASE input bundle line by line
RUN for bundle in $(cat bundles.txt); do \
swupd bundle-add $bundle $swupd_args; \
done
# give a chance to run help script
RUN chmod +x helper_script.sh && ./helper_script.sh
RUN chmod +x numpy-entry.sh && chmod +x set-num-threads.sh \
&& chown numpy-func -R /home/numpy-func \
&& mkdir /usr/local/bin \
&& mv numpy-entry.sh /usr/local/bin \
&& mv set-num-threads.sh /usr/local/bin
USER numpy-func
ENV fprocess="numpy-entry.sh"
EXPOSE 8080
HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1
CMD ["fwatchdog"]
+40
View File
@@ -0,0 +1,40 @@
Based on clearlinux/python:3 container image.
Current version is 3.8.
It can set OMP_NUM_THREADS dynamically at runtime to accelerate
performance according to assigned computing resources.
### numpy-clearlinux
1. `faas-cli new --lang numpy-mp numpy-openfaas --prefix="<your-docker-username-here>"`
Files tree as below.
>
├── numpy-openfaas
│   ├── bundles.txt
│   ├── handler.py
│   ├── helper_script.sh
│   ├── main.py
│   ├── numpy-entry.sh
│   ├── requirements.txt
│   └── set-num-threads.sh
├── numpy-openfaas.yml
└── template
* Put the required python packages in the "requirements.txt".
For example,
`echo "redis" >> numpy-openfaas/requirements.txt`
`echo "flask" >> numpy-openfaas/requirements.txt`
* Put the required Clear Linux bundles in the "bundles.txt".
For example,
`echo "openblas" >> numpy-openfaas/bundles.txt`
`echo "wget" >> numpy-openfaas/bundles.txt`
* Put any initial/helper operation in the "help_script.sh"
* Entry is "handler.py", do any change you want here.
2. `faas-cli up -f numpy-openfaas.yml`
Then you can invoke your python function by OpenFaas UI or faas-cli.
@@ -0,0 +1,7 @@
def handle(req):
"""handle a request to the function
Args:
req (str): request body
"""
return req
@@ -0,0 +1 @@
#!/bin/bash
@@ -0,0 +1,14 @@
import sys
import os
import handler
def print_info():
num = os.getenv('OMP_NUM_THREADS', 'calculated dynamically')
return "OMP_NUM_THREADS is " + num
if __name__ == "__main__":
st = print_info()
ret = handler.handle(st)
if ret != None:
print(ret)
@@ -0,0 +1,9 @@
#!/bin/sh
if [ -n "$OMP_NUM_THREADS" ]; then
echo "Warning: Manual set of \"OMP_NUM_THREADS=$OMP_NUM_THREADS\" may be not better than the calculated value by script!"
else
source /usr/local/bin/set-num-threads.sh
echo "runtime calculated and exported OMP_NUM_THREADS=${OMP_NUM_THREADS}"
fi
python3 main.py
@@ -0,0 +1,57 @@
#!/bin/sh
# get core numbers
lcores=$(grep "^processor" -c /proc/cpuinfo)
htn=$(lscpu | grep "^Thread(s) per core" | awk '{print $NF}')
pcores=$(awk 'BEGIN{ x="'$lcores'" / "'$htn'"; printf("%.0f\n", (x == int(x))?x:int(x)+1) }')
csets=$((0x$(cat /proc/self/status | grep "^Cpus_allowed:" | awk '{print $NF}')))
cfull=$(((1<<cores) - 1))
if (( csets == cfull )); then
# calculate physical core numbers
pnums=$pcores
lnums=$lcores
else
# if set cpu affinity
# get hyperthread sibling topology
st=0; i=0
until [[ $st > 0 || $i > $lcores ]]; do
if [ -r /sys/devices/system/cpu/cpu$i/topology/thread_siblings_list ]; then
x=$(awk -F, '{print $1}' /sys/devices/system/cpu/cpu$i/topology/thread_siblings_list)
y=$(awk -F, '{print $2}' /sys/devices/system/cpu/cpu$i/topology/thread_siblings_list)
st=$((y-x))
fi
((i++))
done
# calculate logical & physical core number by the toplogy
lnums=0; pnums=0; mask=0; n=csets
while (( n != 0 ))
do
m=$((n & (~n + 1)))
if (( (m & mask) == 0 )); then
mask=$((mask|m|m<<st))
((pnums++))
fi
n=$((n - m))
((lnums++))
done
fi
# cpu quota
qus=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us)
pus=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us)
# calculate nums by assigned logical core and total physical core numbers
nums=$(((lnums < pcores)?lnums:pcores))
# calculate qnums by CPU quota and round up
if [[ $qus != "-1" ]]; then
qnums=$(awk 'BEGIN{ x="'$qus'" / "'$pus'"; printf("%.0f\n", (x == int(x))?x:int(x)+1) }')
nums=$(((nums < qnums)?nums:qnums))
fi
# for special single physical core case
nums=$(((nums == 2 && pnums == 1)?pnums:nums))
export OMP_NUM_THREADS=$nums
@@ -0,0 +1,2 @@
language: numpy-mp
fprocess: numpy-entry.sh
@@ -10,16 +10,6 @@ RUN chmod +x /usr/bin/fwatchdog
WORKDIR /root/
COPY index.py .
# pip install python packages if required
COPY requirements.txt .
RUN pip install -r requirements.txt
# 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 mkdir -p function
RUN touch ./function/__init__.py
@@ -0,0 +1,34 @@
Based on clearlinux/python:3 container image.
Current version is 3.8.
### python3-clearlinux
1. `faas-cli new --lang python3-clearlinux hello-openfaas --prefix="<your-docker-username-here>"`
Files tree as below.
>
├── hello-openfaas
│   ├── bundles.txt
│   ├── handler.py
│   ├── helper_script.sh
│   ├── __init__.py
│   └── requirements.txt
├── hello-openfaas.yml
└── template
* Put the required python packages in the "requirements.txt".
For example,
`echo "redis" >> hello-openfaas/requirements.txt`
`echo "flask" >> hello-openfaas/requirements.txt`
* Put the required Clear Linux bundles in the "bundles.txt".
For example,
`echo "openblas" >> hello-openfaas/bundles.txt`
`echo "wget" >> hello-openfaas/bundles.txt`
* Put any initial/helper operation in the "help_script.sh"
2. `faas-cli up -f hello-openfaas.yml`
Then you can invoke your python function by OpenFaas UI or faas-cli.