-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for dependencies before installing
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |