Skip to content

Commit

Permalink
Add systemd utility package
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 5bb68e1 commit ad97b6d
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 62 deletions.
2 changes: 2 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 5 additions & 27 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
55 changes: 22 additions & 33 deletions cmd/install.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package cmd

import (
"bytes"
"fmt"
"os"
"path"
"path/filepath"
"text/template"

systemd "github.com/alexellis/faasd/pkg/systemd"

"github.com/spf13/cobra"
)
Expand All @@ -29,58 +28,48 @@ func runInstall(_ *cobra.Command, _ []string) error {
return err
}

err = installUnit("faas-containerd")
err = systemd.InstallUnit("faas-containerd")
if err != nil {
return err
}

err = installUnit("faasd")
err = systemd.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)
err = systemd.DaemonReload()
if err != nil {
return err
}
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 = systemd.Enable("faas-containerd")
if err != nil {
return err
}

err = tmpl.Execute(&tpl, userData)
err = systemd.Enable("faasd")
if err != nil {
return err
}

err = writeUnit(name+".service", tpl.Bytes())
err = systemd.Start("faas-containerd")
if err != nil {
return err
}

err = systemd.Start("faasd")
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
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)
}
defer f.Close()
_, err = f.Write(data)
return err
return nil
}
4 changes: 2 additions & 2 deletions hack/faas-containerd.service
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Description=faasd-containerd

[Service]
MemoryLimit=500M
ExecStart="/usr/local/bin/faas-containerd"
ExecStart=/usr/local/bin/faas-containerd
Restart=on-failure
RestartSec=10s
WorkingDirectory="/usr/local/bin/"
WorkingDirectory=/usr/local/bin/

[Install]
WantedBy=multi-user.target
100 changes: 100 additions & 0 deletions pkg/systemd/systemd.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ad97b6d

Please sign in to comment.