-
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.
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
- Loading branch information
Showing
5 changed files
with
131 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,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 | ||
} |