Skip to content

Commit

Permalink
Check for dependencies before installing
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Dec 24, 2019
1 parent 0662605 commit 5bb68e1
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
86 changes: 86 additions & 0 deletions cmd/install.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const WelcomeMessage = "Welcome to faasd"
func init() {
rootCommand.AddCommand(versionCmd)
rootCommand.AddCommand(upCmd)
rootCommand.AddCommand(installCmd)
}

var rootCommand = &cobra.Command{
Expand Down
12 changes: 12 additions & 0 deletions hack/faas-containerd.service
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions hack/faasd.service
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5bb68e1

Please sign in to comment.