From fe1e9fc52e01d290447a639f8f387123fd057601 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 23 Feb 2017 09:09:22 +0000 Subject: [PATCH] Create your own functionk working. --- gateway/assets/newfunction.html | 8 +++--- gateway/assets/script/bootstrap.js | 7 +++++- gateway/handlers/functionshandler.go | 37 ++++++++++++++++++++++++++++ gateway/requests/requests.go | 8 +++--- 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/gateway/assets/newfunction.html b/gateway/assets/newfunction.html index d0d6e856..bb841613 100644 --- a/gateway/assets/newfunction.html +++ b/gateway/assets/newfunction.html @@ -1,4 +1,4 @@ - +
@@ -10,14 +10,14 @@
- - + +
- +
diff --git a/gateway/assets/script/bootstrap.js b/gateway/assets/script/bootstrap.js index ef44240d..efb5d724 100644 --- a/gateway/assets/script/bootstrap.js +++ b/gateway/assets/script/bootstrap.js @@ -10,9 +10,14 @@ app.controller("home", ['$scope', '$log', '$http', '$location', '$timeout', '$md $scope.invocation = { contentType: "text" }; + $scope.functionTemplate = { - image: "" + image: "", + envProcess: "", + network: "", + service: "" }; + $scope.invocation.request = "" setInterval(function() { refreshData(); diff --git a/gateway/handlers/functionshandler.go b/gateway/handlers/functionshandler.go index 428cf28f..d62743f1 100644 --- a/gateway/handlers/functionshandler.go +++ b/gateway/handlers/functionshandler.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "log" "net/http" "io/ioutil" @@ -12,6 +13,7 @@ import ( "github.com/alexellis/faas/gateway/requests" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" + "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/client" io_prometheus_client "github.com/prometheus/client_model/go" ) @@ -78,5 +80,40 @@ func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Clie fmt.Println(request) w.WriteHeader(http.StatusNotImplemented) + options := types.ServiceCreateOptions{} + spec := makeSpec(&request) + + response, err := c.ServiceCreate(context.Background(), spec, options) + if err != nil { + log.Println(err) + } + log.Println(response.ID, response.Warnings) } } + +func makeSpec(request *requests.CreateFunctionRequest) swarm.ServiceSpec { + max := uint64(1) + + nets := []swarm.NetworkAttachmentConfig{ + swarm.NetworkAttachmentConfig{Target: request.Network}, + } + + spec := swarm.ServiceSpec{ + TaskTemplate: swarm.TaskSpec{ + RestartPolicy: &swarm.RestartPolicy{ + MaxAttempts: &max, + Condition: swarm.RestartPolicyConditionNone, + }, + ContainerSpec: swarm.ContainerSpec{ + Image: request.Image, + Env: []string{fmt.Sprintf("fprocess=%s", request.EnvProcess)}, + Labels: map[string]string{"function": "true"}, + }, + Networks: nets, + }, + Annotations: swarm.Annotations{ + Name: request.Service, + }, + } + return spec +} diff --git a/gateway/requests/requests.go b/gateway/requests/requests.go index 1b1e24c5..243b73ae 100644 --- a/gateway/requests/requests.go +++ b/gateway/requests/requests.go @@ -2,10 +2,10 @@ package requests // CreateFunctionRequest create a function in the swarm. type CreateFunctionRequest struct { - Service string `json:"service"` - FProcess string `json:"fprocess"` - Image string `json:"image"` - Network string `json:"network"` + Service string `json:"service"` + Image string `json:"image"` + Network string `json:"network"` + EnvProcess string `json:"envProcess"` } type AlexaSessionApplication struct {