diff --git a/machine/machine.go b/machine/machine.go index d61d9dc..2431add 100644 --- a/machine/machine.go +++ b/machine/machine.go @@ -107,6 +107,37 @@ func K3sEnvUnit(unit string) string { return fmt.Sprintf("/etc/sysconfig/%s", unit) } +func K0s() (Service, error) { + if utils.IsOpenRCBased() { + return openrc.NewService( + openrc.WithName("k0scontroller"), + ) + } + + return systemd.NewService( + systemd.WithName("k0scontroller"), + ) +} + +func K0sWorker() (Service, error) { + if utils.IsOpenRCBased() { + return openrc.NewService( + openrc.WithName("k0sworker"), + ) + } + + return systemd.NewService( + systemd.WithName("k0sworker"), + ) +} + +func K0sEnvUnit(unit string) string { + if utils.IsOpenRCBased() { + return fmt.Sprintf("/etc/k0s/%s.env", unit) + } + + return fmt.Sprintf("/etc/sysconfig/%s", unit) +} func UUID() string { if os.Getenv("UUID") != "" { return os.Getenv("UUID") diff --git a/utils/utils.go b/utils/utils.go index dc39f00..fd890fd 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -135,8 +135,21 @@ func K3sBin() string { return "" } +func K0sBin() string { + for _, p := range []string{"/usr/bin/k0s", "/usr/local/bin/k0s"} { + if _, err := os.Stat(p); err == nil { + return p + } + } + + return "" +} + func WriteEnv(envFile string, config map[string]string) error { - content, _ := os.ReadFile(envFile) + content, err := os.ReadFile(envFile) + if err != nil && !os.IsNotExist(err) { + return err + } env, _ := godotenv.Unmarshal(string(content)) for key, val := range config { @@ -157,18 +170,14 @@ func Flavor() string { // GetInit Return the init system used by the OS func GetInit() string { - for _, file := range []string{"/run/systemd/system", "/sbin/systemctl", "/usr/bin/systemctl", "/usr/sbin/systemctl", "/usr/bin/systemctl"} { - _, err := os.Stat(file) - // Found systemd - if err == nil { + for _, file := range []string{"/run/systemd/system", "/sbin/systemctl", "/usr/bin/systemctl", "/usr/sbin/systemctl"} { + if _, err := os.Stat(file); err == nil { return systemd } } for _, file := range []string{"/sbin/openrc", "/usr/sbin/openrc", "/bin/openrc", "/usr/bin/openrc"} { - _, err := os.Stat(file) - // Found openrc - if err == nil { + if _, err := os.Stat(file); err == nil { return openrc } } @@ -262,16 +271,6 @@ func PowerOFF() { } } -func Version() string { - v, err := OSRelease("VERSION") - if err != nil { - return "" - } - v = strings.ReplaceAll(v, "+k3s1-Kairos", "-") - v = strings.ReplaceAll(v, "+k3s-Kairos", "-") - return strings.ReplaceAll(v, "Kairos", "") -} - func ListToOutput(rels []string, output string) []string { switch strings.ToLower(output) { case "yaml":