-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows to keep them on subsequent boots. Also refactor systemd unit which was pretty messy
- Loading branch information
Showing
8 changed files
with
185 additions
and
75 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
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 |
---|---|---|
@@ -1,12 +1,11 @@ | ||
package main | ||
|
||
import ( | ||
"os/exec" | ||
|
||
"github.com/mudler/c3os/installer/utils" | ||
"github.com/pterm/pterm" | ||
) | ||
|
||
func Reboot() { | ||
pterm.Info.Println("Rebooting node") | ||
exec.Command("reboot").Start() | ||
utils.SH("reboot") | ||
} |
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,96 @@ | ||
package systemd | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/mudler/c3os/installer/utils" | ||
) | ||
|
||
type ServiceUnit struct { | ||
content string | ||
name, instance string | ||
} | ||
|
||
const overrideCmdTemplate string = ` | ||
[Service] | ||
ExecStart= | ||
ExecStart=%s | ||
` | ||
|
||
type ServiceOpts func(*ServiceUnit) error | ||
|
||
func WithName(n string) ServiceOpts { | ||
return func(su *ServiceUnit) error { | ||
su.name = n | ||
return nil | ||
} | ||
} | ||
|
||
func WithInstance(n string) ServiceOpts { | ||
return func(su *ServiceUnit) error { | ||
su.instance = n | ||
return nil | ||
} | ||
} | ||
|
||
func WithUnitContent(n string) ServiceOpts { | ||
return func(su *ServiceUnit) error { | ||
su.content = n | ||
return nil | ||
} | ||
} | ||
|
||
func NewService(opts ...ServiceOpts) (ServiceUnit, error) { | ||
s := &ServiceUnit{} | ||
for _, o := range opts { | ||
if err := o(s); err != nil { | ||
return *s, err | ||
} | ||
} | ||
return *s, nil | ||
} | ||
|
||
func (s ServiceUnit) WriteUnit() error { | ||
uname := s.name | ||
if s.instance != "" { | ||
uname = fmt.Sprintf("%s@", s.name) | ||
} | ||
return ioutil.WriteFile(fmt.Sprintf("/etc/systemd/system/%s.service", uname), []byte(s.content), 0600) | ||
} | ||
|
||
func (s ServiceUnit) OverrideCmd(cmd string) error { | ||
svcDir := fmt.Sprintf("/etc/systemd/system/%s.service.d/", s.name) | ||
os.MkdirAll(svcDir, 0600) | ||
|
||
return ioutil.WriteFile(filepath.Join(svcDir, "override.conf"), []byte(fmt.Sprintf(overrideCmdTemplate, cmd)), 0600) | ||
} | ||
|
||
func (s ServiceUnit) Start() error { | ||
uname := s.name | ||
if s.instance != "" { | ||
uname = fmt.Sprintf("%s@%s", s.name, s.instance) | ||
} | ||
_, err := utils.SH(fmt.Sprintf("systemctl start --no-block %s", uname)) | ||
return err | ||
} | ||
|
||
func (s ServiceUnit) Enable() error { | ||
uname := s.name | ||
if s.instance != "" { | ||
uname = fmt.Sprintf("%s@%s", s.name, s.instance) | ||
} | ||
_, err := utils.SH(fmt.Sprintf("systemctl enable %s", uname)) | ||
return err | ||
} | ||
|
||
func (s ServiceUnit) StartBlocking() error { | ||
uname := s.name | ||
if s.instance != "" { | ||
uname = fmt.Sprintf("%s@%s", s.name, s.instance) | ||
} | ||
_, err := utils.SH(fmt.Sprintf("systemctl start %s", uname)) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package utils | ||
|
||
import ( | ||
"io/ioutil" | ||
"os/exec" | ||
|
||
"github.com/joho/godotenv" | ||
) | ||
|
||
func SH(c string) (string, error) { | ||
o, err := exec.Command("/bin/sh", "-c", c).CombinedOutput() | ||
return string(o), err | ||
} | ||
|
||
func WriteEnv(envFile string, config map[string]string) error { | ||
content, _ := ioutil.ReadFile(envFile) | ||
env, _ := godotenv.Unmarshal(string(content)) | ||
|
||
for key, val := range config { | ||
env[key] = val | ||
} | ||
|
||
return godotenv.Write(env, envFile) | ||
} |