From 5bb68e15f59f4847f5811e5a5aeb5c473de41b42 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Tue, 24 Dec 2019 09:01:34 +0000 Subject: [PATCH] Check for dependencies before installing Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- cmd/install.go | 86 ++++++++++++++++++++++++++++++++++++ cmd/root.go | 1 + hack/faas-containerd.service | 12 +++++ hack/faasd.service | 13 ++++++ 4 files changed, 112 insertions(+) create mode 100644 cmd/install.go create mode 100644 hack/faas-containerd.service create mode 100644 hack/faasd.service diff --git a/cmd/install.go b/cmd/install.go new file mode 100644 index 0000000..b0cbc0d --- /dev/null +++ b/cmd/install.go @@ -0,0 +1,86 @@ +package cmd + +import ( + "bytes" + "fmt" + "os" + "path" + "path/filepath" + "text/template" + + "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 = installUnit("faas-containerd") + if err != nil { + return err + } + + err = installUnit("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 +} + +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 +} diff --git a/cmd/root.go b/cmd/root.go index 0d8de48..c649319 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,6 +22,7 @@ const WelcomeMessage = "Welcome to faasd" func init() { rootCommand.AddCommand(versionCmd) rootCommand.AddCommand(upCmd) + rootCommand.AddCommand(installCmd) } var rootCommand = &cobra.Command{ diff --git a/hack/faas-containerd.service b/hack/faas-containerd.service new file mode 100644 index 0000000..e7af68d --- /dev/null +++ b/hack/faas-containerd.service @@ -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 diff --git a/hack/faasd.service b/hack/faasd.service new file mode 100644 index 0000000..c09a735 --- /dev/null +++ b/hack/faasd.service @@ -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