adding utils components to handle k0s

Signed-off-by: William Rizzo <william.rizzo@gmail.com>
This commit is contained in:
William Rizzo
2025-01-12 07:05:41 +01:00
committed by Mauro Morales
parent a2d305dd6d
commit 474faf6370
4 changed files with 138 additions and 82 deletions

View File

@@ -120,6 +120,10 @@ func FindCommand(def string, options []string) string {
return path
}
}
// Additional check for K0s binary
if def == "k0s" {
return K0sBin()
}
// Otherwise return default
return def
@@ -135,8 +139,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 +174,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
}
}
@@ -258,18 +271,30 @@ func PowerOFF() {
if IsOpenRCBased() {
SH("poweroff") //nolint:errcheck
} else {
SH("shutdown") //nolint:errcheck
SH("shutdown now") //nolint:errcheck
}
}
func Version() string {
v, err := OSRelease("VERSION")
if err != nil {
return ""
// Check for K3s version
k3sBin := K3sBin()
if k3sBin != "" {
v, err := OSRelease("VERSION")
if err == nil {
return strings.ReplaceAll(v, "+k3s1-Kairos", "-")
}
}
v = strings.ReplaceAll(v, "+k3s1-Kairos", "-")
v = strings.ReplaceAll(v, "+k3s-Kairos", "-")
return strings.ReplaceAll(v, "Kairos", "")
// Check for K0s version
k0sBin := K0sBin()
if k0sBin != "" {
v, err := SH(fmt.Sprintf("%s version", k0sBin))
if err == nil {
return strings.TrimSpace(v)
}
}
return ""
}
func ListToOutput(rels []string, output string) []string {