Compare commits

..

3 Commits
0.1.5 ... 0.1.7

Author SHA1 Message Date
ad97b6db58 Add systemd utility package
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
2019-12-24 09:12:34 +00:00
5bb68e15f5 Check for dependencies before installing
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
2019-12-24 09:01:34 +00:00
0662605756 Bump latest gateway / queue-worker versions
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
2019-12-23 21:18:06 +00:00
8 changed files with 210 additions and 29 deletions

2
Gopkg.lock generated
View File

@ -440,6 +440,7 @@
analyzer-name = "dep"
analyzer-version = 1
input-imports = [
"github.com/alexellis/go-execute",
"github.com/containerd/containerd",
"github.com/containerd/containerd/cio",
"github.com/containerd/containerd/containers",
@ -451,6 +452,7 @@
"github.com/spf13/cobra",
"github.com/vishvananda/netlink",
"github.com/vishvananda/netns",
"golang.org/x/sys/unix",
]
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -1,30 +1,3 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[[constraint]]
name = "github.com/containerd/containerd"
version = "1.3.2"
@ -37,6 +10,11 @@
name = "github.com/spf13/cobra"
version = "0.0.5"
[[constraint]]
name = "github.com/alexellis/go-execute"
version = "0.3.0"
[prune]
go-tests = true
unused-packages = true

75
cmd/install.go Normal file
View File

@ -0,0 +1,75 @@
package cmd
import (
"fmt"
"os"
"path"
systemd "github.com/alexellis/faasd/pkg/systemd"
"github.com/spf13/cobra"
)
var installCmd = &cobra.Command{
Use: "install",
Short: "Install faasd",
RunE: runInstall,
}
func runInstall(_ *cobra.Command, _ []string) error {
err := binExists("/usr/local/bin/", "faas-containerd")
if err != nil {
return err
}
err = binExists("/usr/local/bin/", "netns")
if err != nil {
return err
}
err = systemd.InstallUnit("faas-containerd")
if err != nil {
return err
}
err = systemd.InstallUnit("faasd")
if err != nil {
return err
}
err = systemd.DaemonReload()
if err != nil {
return err
}
err = systemd.Enable("faas-containerd")
if err != nil {
return err
}
err = systemd.Enable("faasd")
if err != nil {
return err
}
err = systemd.Start("faas-containerd")
if err != nil {
return err
}
err = systemd.Start("faasd")
if err != nil {
return err
}
return nil
}
func binExists(folder, name string) error {
findPath := path.Join(folder, name)
if _, err := os.Stat(findPath); err != nil {
return fmt.Errorf("unable to stat %s, install this binary before continuing", findPath)
}
return nil
}

View File

@ -22,6 +22,7 @@ const WelcomeMessage = "Welcome to faasd"
func init() {
rootCommand.AddCommand(versionCmd)
rootCommand.AddCommand(upCmd)
rootCommand.AddCommand(installCmd)
}
var rootCommand = &cobra.Command{

View File

@ -51,7 +51,7 @@ func runUp(_ *cobra.Command, _ []string) error {
"faas_nats_address=nats",
"faas_nats_port=4222",
},
Image: "docker.io/openfaas/gateway:0.17.4",
Image: "docker.io/openfaas/gateway:0.18.7",
Mounts: []pkg.Mount{},
Caps: []string{"CAP_NET_RAW"},
},
@ -66,7 +66,7 @@ func runUp(_ *cobra.Command, _ []string) error {
"max_inflight=1",
"faas_print_body=true",
},
Image: "docker.io/openfaas/queue-worker:0.8.4",
Image: "docker.io/openfaas/queue-worker:0.9.0",
Mounts: []pkg.Mount{},
Caps: []string{"CAP_NET_RAW"},
},

View File

@ -0,0 +1,12 @@
[Unit]
Description=faasd-containerd
[Service]
MemoryLimit=500M
ExecStart=/usr/local/bin/faas-containerd
Restart=on-failure
RestartSec=10s
WorkingDirectory=/usr/local/bin/
[Install]
WantedBy=multi-user.target

13
hack/faasd.service Normal file
View File

@ -0,0 +1,13 @@
[Unit]
Description=faasd
After=faas-containerd.service
[Service]
MemoryLimit=500M
ExecStart={{.Cwd}}/faasd up
Restart=on-failure
RestartSec=10s
WorkingDirectory={{.Cwd}}
[Install]
WantedBy=multi-user.target

100
pkg/systemd/systemd.go Normal file
View File

@ -0,0 +1,100 @@
package systemd
import (
"bytes"
"fmt"
"os"
"path/filepath"
"text/template"
execute "github.com/alexellis/go-execute/pkg/v1"
)
func Enable(unit string) error {
task := execute.ExecTask{Command: "systemctl",
Args: []string{"enable", unit},
StreamStdio: false,
}
res, err := task.Execute()
if err != nil {
return err
}
if res.ExitCode != 0 {
return fmt.Errorf("error executing task %s %v, stderr: %s", task.Command, task.Args, res.Stderr)
}
return nil
}
func Start(unit string) error {
task := execute.ExecTask{Command: "systemctl",
Args: []string{"start", unit},
StreamStdio: false,
}
res, err := task.Execute()
if err != nil {
return err
}
if res.ExitCode != 0 {
return fmt.Errorf("error executing task %s %v, stderr: %s", task.Command, task.Args, res.Stderr)
}
return nil
}
func DaemonReload() error {
task := execute.ExecTask{Command: "systemctl",
Args: []string{"daemon-reload"},
StreamStdio: false,
}
res, err := task.Execute()
if err != nil {
return err
}
if res.ExitCode != 0 {
return fmt.Errorf("error executing task %s %v, stderr: %s", task.Command, task.Args, res.Stderr)
}
return nil
}
func InstallUnit(name string) error {
tmpl, err := template.ParseFiles("./hack/" + name + ".service")
wd, _ := os.Getwd()
var tpl bytes.Buffer
userData := struct {
Cwd string
}{
Cwd: wd,
}
err = tmpl.Execute(&tpl, userData)
if err != nil {
return err
}
err = writeUnit(name+".service", tpl.Bytes())
if err != nil {
return err
}
return nil
}
func writeUnit(name string, data []byte) error {
f, err := os.Create(filepath.Join("/lib/systemd/system", name))
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(data)
return err
}