kairos-agent/internal/utils/system.go
Ettore Di Giacinto 63cd28d1cb Split off cli into separate binaries (#37)
* 🎨 Split off cli into separate binaries

This commit splits off the cli into 3 binaries:
- agent
- cli
- provider

The provider now is a separate component that can be tested by itself
and have its own lifecycle. This paves the way to a ligher c3os variant,
HA support and other features that can be provided on runtime.

This is working, but still there are low hanging fruit to care about.

Fixes #14

* 🤖 Add provider bin to releases

* ⚙️ Handle signals

* ⚙️ Reduce buildsize footprint

* 🎨 Scan for providers also in /system/providers

* 🤖 Run goreleaser

* 🎨 Refactoring
2022-07-04 22:39:34 +02:00

83 lines
1.4 KiB
Go

package utils
import (
"fmt"
"net"
"strings"
"github.com/joho/godotenv"
"github.com/pterm/pterm"
)
func Reboot() {
pterm.Info.Println("Rebooting node")
SH("reboot")
}
func PowerOFF() {
pterm.Info.Println("Shutdown node")
if IsOpenRCBased() {
SH("poweroff")
} else {
SH("shutdown")
}
}
func Version() string {
release, _ := godotenv.Read("/etc/os-release")
v := release["VERSION"]
v = strings.ReplaceAll(v, "+k3s1-c3OS", "-")
return strings.ReplaceAll(v, "+k3s-c3OS", "-")
}
func OSRelease(key string) (string, error) {
release, err := godotenv.Read("/etc/os-release")
if err != nil {
return "", err
}
v, exists := release[key]
if !exists {
return "", fmt.Errorf("key not found")
}
return v, nil
}
func Flavor() string {
release, _ := godotenv.Read("/etc/os-release")
v := release["NAME"]
return strings.ReplaceAll(v, "c3os-", "")
}
func IsOpenRCBased() bool {
f := Flavor()
return f == "alpine" || f == "alpine-arm-rpi"
}
func GetInterfaceIP(in string) string {
ifaces, err := net.Interfaces()
if err != nil {
fmt.Println("failed getting system interfaces")
return ""
}
for _, i := range ifaces {
if i.Name == in {
addrs, _ := i.Addrs()
// handle err
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip != nil {
return ip.String()
}
}
}
}
return ""
}