Adapt machine to new sdk paths and format

Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>
This commit is contained in:
Itxaka 2023-03-17 14:00:02 +01:00
parent 750eed7f93
commit b3256fa436
10 changed files with 51 additions and 17 deletions

View File

@ -3,7 +3,7 @@ package machine_test
import ( import (
"os" "os"
. "github.com/kairos-io/kairos/pkg/machine" . "github.com/kairos-io/kairos-sdk/machine"
. "github.com/onsi/ginkgo/v2" . "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )

View File

@ -7,10 +7,10 @@ import (
"strings" "strings"
"github.com/denisbrodbeck/machineid" "github.com/denisbrodbeck/machineid"
"github.com/kairos-io/kairos/pkg/machine/openrc" "github.com/kairos-io/kairos-sdk/machine/openrc"
"github.com/kairos-io/kairos/pkg/machine/systemd" "github.com/kairos-io/kairos-sdk/machine/systemd"
"github.com/kairos-io/kairos/pkg/utils" "github.com/kairos-io/kairos-sdk/utils"
) )
type Service interface { type Service interface {

View File

@ -6,7 +6,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/kairos-io/kairos/pkg/utils" "github.com/kairos-io/kairos-sdk/utils"
) )
type ServiceUnit struct { type ServiceUnit struct {

View File

@ -5,7 +5,7 @@ import (
"os" "os"
"strings" "strings"
"github.com/kairos-io/kairos/pkg/utils" "github.com/kairos-io/kairos-sdk/utils"
) )
func Umount(path string) error { func Umount(path string) error {
@ -33,7 +33,7 @@ func Mount(label, mountpoint string) error {
part = strings.TrimSuffix(part, "\n") part = strings.TrimSuffix(part, "\n")
if !Exists(mountpoint) { if !utils.Exists(mountpoint) {
err := os.MkdirAll(mountpoint, 0755) err := os.MkdirAll(mountpoint, 0755)
if err != nil { if err != nil {
return err return err

View File

@ -6,7 +6,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/kairos-io/kairos/pkg/utils" "github.com/kairos-io/kairos-sdk/utils"
) )
type ServiceUnit struct { type ServiceUnit struct {

View File

@ -1,9 +0,0 @@
package machine
import "os"
func Exists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}

View File

@ -1,9 +1,11 @@
package utils package utils
import ( import (
"bytes"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"strings"
"github.com/denisbrodbeck/machineid" "github.com/denisbrodbeck/machineid"
"github.com/joho/godotenv" "github.com/joho/godotenv"
@ -63,3 +65,44 @@ func FindCommand(def string, options []string) string {
// Otherwise return default // Otherwise return default
return def return def
} }
func K3sBin() string {
for _, p := range []string{"/usr/bin/k3s", "/usr/local/bin/k3s"} {
if _, err := os.Stat(p); err == nil {
return p
}
}
return ""
}
func WriteEnv(envFile string, config map[string]string) error {
content, _ := os.ReadFile(envFile)
env, _ := godotenv.Unmarshal(string(content))
for key, val := range config {
env[key] = val
}
return godotenv.Write(env, envFile)
}
func Flavor() string {
release, _ := godotenv.Read("/etc/os-release")
v := release["NAME"]
return strings.ReplaceAll(v, "kairos-", "")
}
func IsOpenRCBased() bool {
f := Flavor()
return strings.Contains(f, "alpine")
}
func ShellSTDIN(s, c string) (string, error) {
cmd := exec.Command("/bin/sh", "-c", c)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = bytes.NewBuffer([]byte(s))
o, err := cmd.CombinedOutput()
return string(o), err
}