Check for dependencies before installing

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (OpenFaaS Ltd) 2019-12-24 09:01:34 +00:00
parent 0662605756
commit 5bb68e15f5
4 changed files with 112 additions and 0 deletions

86
cmd/install.go Normal file
View File

@ -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
}

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

@ -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