From 067fb6a53af5c53818a5bc48b7be97b41705c844 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Mon, 5 Sep 2022 20:06:18 +0100 Subject: [PATCH] Add example container source for ASCII cows Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- README.md | 3 ++- cows.yaml | 5 ++--- examples/cows/.gitignore | 3 +++ examples/cows/Dockerfile | 23 +++++++++++++++++++++++ examples/cows/Makefile | 3 +++ examples/cows/index.js | 21 +++++++++++++++++++++ examples/cows/package-lock.json | 30 ++++++++++++++++++++++++++++++ examples/cows/package.json | 15 +++++++++++++++ examples/cows/stack.yml | 10 ++++++++++ 9 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 examples/cows/.gitignore create mode 100644 examples/cows/Dockerfile create mode 100644 examples/cows/Makefile create mode 100644 examples/cows/index.js create mode 100644 examples/cows/package-lock.json create mode 100644 examples/cows/package.json create mode 100644 examples/cows/stack.yml diff --git a/README.md b/README.md index a3c5e7f..fb75e39 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,8 @@ cows.yaml: ```yaml $ cat < cows.yaml -image: alexellis2/cows:latest +# Multi-arch image for arm64, amd64 and armv7l +image: alexellis2/cows:2022-09-05-1955 name: cows EOF ``` diff --git a/cows.yaml b/cows.yaml index eb02487..fd4a62a 100644 --- a/cows.yaml +++ b/cows.yaml @@ -1,4 +1,3 @@ -# Note: this is not a multi-arch image -# and won't run on your Raspberry Pi -image: alexellis2/cows:latest +# Multi-arch image for arm64, amd64 and armv7l +image: alexellis2/cows:2022-09-05-1955 name: cows diff --git a/examples/cows/.gitignore b/examples/cows/.gitignore new file mode 100644 index 0000000..4ea4ffc --- /dev/null +++ b/examples/cows/.gitignore @@ -0,0 +1,3 @@ +template +build +node_modules diff --git a/examples/cows/Dockerfile b/examples/cows/Dockerfile new file mode 100644 index 0000000..403e44f --- /dev/null +++ b/examples/cows/Dockerfile @@ -0,0 +1,23 @@ +FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.16 as builder + +ARG TARGETPLATFORM +ARG BUILDPLATFORM +ARG TARGETOS +ARG TARGETARCH + +RUN mkdir -p /home/app + +RUN apk add --no-cache nodejs npm + +# Add non root user +RUN addgroup -S app && adduser app -S -G app +RUN chown app /home/app + +WORKDIR /home/app +USER app + +COPY package.json . +RUN npm install --omit=dev +COPY index.js . + +CMD ["/usr/bin/node", "./index.js"] diff --git a/examples/cows/Makefile b/examples/cows/Makefile new file mode 100644 index 0000000..730bb53 --- /dev/null +++ b/examples/cows/Makefile @@ -0,0 +1,3 @@ +.PHONY: build +build: + faas-cli publish --platforms linux/amd64,linux/arm64,linux/arm/v7 -f cows.yml \ No newline at end of file diff --git a/examples/cows/index.js b/examples/cows/index.js new file mode 100644 index 0000000..41e543b --- /dev/null +++ b/examples/cows/index.js @@ -0,0 +1,21 @@ +var cows = require('cows') +var all = cows(); + +function show(cow) { + var cowText = all[cow]; + console.log(cowText); +} + +var cowArgument = process.argv[2] +if(!cowArgument || isNaN(Number(cowArgument))) { + var randomCow = Math.floor((Math.random() * all.length) + 1); + show(randomCow); +} +else { + var cowNumber = Number(cowArgument); + if(cowNumber && cowNumber <= all.length) { + show(cowNumber); + } else { + console.log("Enter cow number between [1-"+all.length+"]") + } +} diff --git a/examples/cows/package-lock.json b/examples/cows/package-lock.json new file mode 100644 index 0000000..0dc4f72 --- /dev/null +++ b/examples/cows/package-lock.json @@ -0,0 +1,30 @@ +{ + "name": "cows", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "cows": "^2.1.1" + } + }, + "node_modules/cows": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/cows/-/cows-2.1.1.tgz", + "integrity": "sha512-AqXwB77mg4+bvr6Qo2N97qJy6Ky0mHvH51v9KiiaXL+aJ5jaNtgcgF54LI59HE34MTnKM9WnipKPJVStCInWUg==", + "engines": { + "node": ">=6" + } + } + }, + "dependencies": { + "cows": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/cows/-/cows-2.1.1.tgz", + "integrity": "sha512-AqXwB77mg4+bvr6Qo2N97qJy6Ky0mHvH51v9KiiaXL+aJ5jaNtgcgF54LI59HE34MTnKM9WnipKPJVStCInWUg==" + } + } +} diff --git a/examples/cows/package.json b/examples/cows/package.json new file mode 100644 index 0000000..099e7e7 --- /dev/null +++ b/examples/cows/package.json @@ -0,0 +1,15 @@ +{ + "name": "cows", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "cows": "^2.1.1" + } +} diff --git a/examples/cows/stack.yml b/examples/cows/stack.yml new file mode 100644 index 0000000..157b583 --- /dev/null +++ b/examples/cows/stack.yml @@ -0,0 +1,10 @@ +version: 1.0 +provider: + name: openfaas + gateway: http://127.0.0.1:8080 + +functions: + cows: + lang: dockerfile + handler: ./ + image: alexellis2/cows:2022-09-05-1955