diff --git a/cmd/install.go b/cmd/install.go index 480453a..dd76937 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -23,6 +23,11 @@ func runInstall(_ *cobra.Command, _ []string) error { return errors.Wrap(basicAuthErr, "cannot create basic-auth-* files") } + wd := "/run/faasd" + if err := ensureWorkingDir(wd); err != nil { + return err + } + err := binExists("/usr/local/bin/", "faas-containerd") if err != nil { return err @@ -38,12 +43,12 @@ func runInstall(_ *cobra.Command, _ []string) error { return err } - err = systemd.InstallUnit("faas-containerd") + err = systemd.InstallUnit("faas-containerd", wd) if err != nil { return err } - err = systemd.InstallUnit("faasd") + err = systemd.InstallUnit("faasd", wd) if err != nil { return err } @@ -83,3 +88,14 @@ func binExists(folder, name string) error { } return nil } + +func ensureWorkingDir(folder string) error { + if _, err := os.Stat(folder); err != nil { + err = os.MkdirAll("/run/faasd", 0600) + if err != nil { + return err + } + } + + return nil +} diff --git a/hack/faas-containerd.service b/hack/faas-containerd.service index 57eba46..7796e47 100644 --- a/hack/faas-containerd.service +++ b/hack/faas-containerd.service @@ -7,7 +7,7 @@ MemoryLimit=500M ExecStart=/usr/local/bin/faas-containerd Restart=on-failure RestartSec=10s -WorkingDirectory=/usr/local/bin/ +WorkingDirectory={{.Cwd}} [Install] WantedBy=multi-user.target diff --git a/pkg/systemd/systemd.go b/pkg/systemd/systemd.go index 373f583..6b8b47c 100644 --- a/pkg/systemd/systemd.go +++ b/pkg/systemd/systemd.go @@ -64,7 +64,12 @@ func DaemonReload() error { return nil } -func InstallUnit(name string) error { +func InstallUnit(name, folder string) error { + if len(folder) == 0 { + wd, _ := os.Getwd() + folder = wd + } + tmplName := "./hack/" + name + ".service" tmpl, err := template.ParseFiles(tmplName) @@ -72,12 +77,11 @@ func InstallUnit(name string) error { return fmt.Errorf("error loading template %s, error %s", tmplName, err) } - wd, _ := os.Getwd() var tpl bytes.Buffer userData := struct { Cwd string }{ - Cwd: wd, + Cwd: folder, } err = tmpl.Execute(&tpl, userData)