Rename 'function-run' dir to 'environments'
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
FROM alpine:3.4
|
||||
|
||||
ADD fetcher /
|
||||
|
||||
EXPOSE 8000
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
GOOS=linux GOARCH=386 go build -o fetcher .
|
||||
@@ -0,0 +1,97 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type FetchRequest struct {
|
||||
Url string `json:"url"`
|
||||
Filename string `json:"filename"`
|
||||
}
|
||||
|
||||
type Fetcher struct {
|
||||
sharedVolumePath string
|
||||
}
|
||||
|
||||
func MakeFetcher(sharedVolumePath string) *Fetcher {
|
||||
return &Fetcher{sharedVolumePath: sharedVolumePath}
|
||||
}
|
||||
|
||||
func (fetcher *Fetcher) handler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "", 404)
|
||||
return
|
||||
}
|
||||
|
||||
// parse request
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
req := FetchRequest{}
|
||||
err = json.Unmarshal(body, &req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
// fetch the file and save it to tmp path
|
||||
resp, err := http.Get(req.Url)
|
||||
if err != nil {
|
||||
e := fmt.Sprintf("Failed to fetch from url: %v", err)
|
||||
http.Error(w, e, 400)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err = ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
e := fmt.Sprintf("Failed to read from url: %v", err)
|
||||
http.Error(w, e, 400)
|
||||
return
|
||||
}
|
||||
tmpFile := req.Filename + ".tmp"
|
||||
tmpPath := filepath.Join(fetcher.sharedVolumePath, tmpFile)
|
||||
err = ioutil.WriteFile(tmpPath, body, 0600)
|
||||
if err != nil {
|
||||
e := fmt.Sprintf("Failed to write file: %v", err)
|
||||
http.Error(w, e, 500)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: add signature verification
|
||||
|
||||
// move tmp file to requested filename
|
||||
err = os.Rename(tmpPath, filepath.Join(fetcher.sharedVolumePath, req.Filename))
|
||||
if err != nil {
|
||||
e := fmt.Sprintf("Failed to move file: %v", err)
|
||||
http.Error(w, e, 500)
|
||||
return
|
||||
}
|
||||
|
||||
// all done
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
// Usage: fetcher <shared volume path>
|
||||
func main() {
|
||||
dir := os.Args[1]
|
||||
if _, err := os.Stat(dir); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
err = os.MkdirAll(dir, os.ModeDir|0700)
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating directory: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
fetcher := MakeFetcher(dir)
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", fetcher.handler)
|
||||
http.ListenAndServe(":8000", mux)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
# A docker image for the func container.
|
||||
|
||||
FROM node:4-onbuild
|
||||
|
||||
ADD server.js /usr/src/app/server.js
|
||||
|
||||
EXPOSE 8888
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "fission-nodejs-runtime",
|
||||
"version": "0.0.0",
|
||||
"author": "Soam Vasani",
|
||||
"contributors": [
|
||||
{
|
||||
"name": "Soam Vasani",
|
||||
"email": "soamvasani@platform9.com"
|
||||
}
|
||||
],
|
||||
"description": "NodeJS run container for the fission framework",
|
||||
"engines": {
|
||||
"node": ">=4.2.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "",
|
||||
"minimist": "",
|
||||
"body-parser": "",
|
||||
"morgan": ""
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const process = require('process');
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const bodyParser = require('body-parser');
|
||||
const morgan = require('morgan');
|
||||
|
||||
// Command line opts
|
||||
const argv = require('minimist')(process.argv.slice(1));
|
||||
if (!argv.codepath) {
|
||||
argv.codepath = "/userfunc/user";
|
||||
console.log("Codepath defaulting to ", argv.codepath);
|
||||
}
|
||||
if (!argv.port) {
|
||||
console.log("Port defaulting to 8888");
|
||||
argv.port = 8888;
|
||||
}
|
||||
|
||||
// User function. Starts out undefined.
|
||||
let userFunction;
|
||||
|
||||
//
|
||||
// Specialize this server to a given user function. The user function
|
||||
// is read from argv.codepath; it's expected to be placed there by the
|
||||
// fission runtime.
|
||||
//
|
||||
function specialize(req, res) {
|
||||
// Make sure we're a generic container. (No reuse of containers.
|
||||
// Once specialized, the container remains specialized.)
|
||||
if (userFunction) {
|
||||
res.status(400).send("Not a generic container");
|
||||
return;
|
||||
}
|
||||
|
||||
// Read and load the code. It's placed there securely by the fission runtime.
|
||||
try {
|
||||
var startTime = process.hrtime();
|
||||
userFunction = require(argv.codepath);
|
||||
var elapsed = process.hrtime(startTime);
|
||||
console.log(`user code loaded in ${elapsed[0]}sec ${elapsed[1]/1000000}ms`);
|
||||
} catch(e) {
|
||||
console.error(`user code load error: ${e}`);
|
||||
res.status(500).send(JSON.stringify(e));
|
||||
return;
|
||||
}
|
||||
res.status(202).send();
|
||||
}
|
||||
|
||||
|
||||
// Request logger
|
||||
app.use(morgan('combined'))
|
||||
|
||||
app.use(bodyParser.urlencoded({ extended: false }));
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.raw());
|
||||
|
||||
app.post('/specialize', specialize);
|
||||
|
||||
// Generic route -- all http requests go to the user function.
|
||||
app.all('/', function (req, res) {
|
||||
if (!userFunction) {
|
||||
res.status(500).send("Generic container: no requests supported");
|
||||
return;
|
||||
}
|
||||
const context = {
|
||||
request: req,
|
||||
response: res
|
||||
// TODO: context should also have: URL template params, query string
|
||||
};
|
||||
function callback(status, body, headers) {
|
||||
if (!status)
|
||||
return;
|
||||
if (headers) {
|
||||
for (let name of Object.keys(headers)) {
|
||||
res.set(name, headers[name]);
|
||||
}
|
||||
}
|
||||
res.status(status).send(body);
|
||||
}
|
||||
try {
|
||||
userFunction(context, callback);
|
||||
} catch(e) {
|
||||
callback(500, "Internal server error")
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(argv.port);
|
||||
@@ -0,0 +1,6 @@
|
||||
module.exports = function (context, callback) {
|
||||
console.log("headers=", JSON.stringify(context.request.headers));
|
||||
console.log("body=", JSON.stringify(context.request.body));
|
||||
|
||||
callback(200, "Hello, world !\n");
|
||||
}
|
||||
Executable
+31
@@ -0,0 +1,31 @@
|
||||
#!/bin/sh
|
||||
|
||||
# TODO placeholder until we have better tests :)
|
||||
|
||||
set +x
|
||||
set -e
|
||||
|
||||
DIR=$(dirname $0)
|
||||
|
||||
echo "-- Starting server"
|
||||
node $DIR/../server.js --codepath $DIR/test.js --port 8888 &
|
||||
function cleanup() {
|
||||
echo "-- Cleanup"
|
||||
kill %1
|
||||
}
|
||||
trap cleanup EXIT
|
||||
sleep 2
|
||||
|
||||
echo "-- Specializing"
|
||||
curl -f -X POST http://localhost:8888/specialize
|
||||
|
||||
echo "-- Running user function"
|
||||
curl -f -X GET http://localhost:8888
|
||||
curl -f -X POST http://localhost:8888
|
||||
curl -f -X PUT http://localhost:8888
|
||||
curl -f -X DELETE http://localhost:8888
|
||||
curl -f -X TRACE http://localhost:8888
|
||||
curl -f -X OPTIONS http://localhost:8888
|
||||
|
||||
# -I causes curl to make a HEAD request.
|
||||
curl -f -I http://localhost:8888
|
||||
Reference in New Issue
Block a user