kairos-agent/internal/machine/machine.go
Ettore Di Giacinto 09b41735f4 gear: Be sure to run datasource before install
Seems in certain condition the datasource is kicking late and serving the datasource not fast as wanted.

We make sure to pull it before install, so we also give chance to convoluted setups to pull configs.

Also wires up autoinstall tests to CI
2022-07-21 21:38:07 +00:00

158 lines
3.5 KiB
Go

package machine
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/c3os-io/c3os/internal/machine/openrc"
"github.com/c3os-io/c3os/internal/machine/systemd"
"github.com/denisbrodbeck/machineid"
"github.com/c3os-io/c3os/internal/utils"
)
type Service interface {
WriteUnit() error
Start() error
OverrideCmd(string) error
Enable() error
Restart() error
}
const (
PassiveBoot = "passive"
ActiveBoot = "active"
RecoveryBoot = "recovery"
LiveCDBoot = "liveCD"
NetBoot = "netboot"
UnknownBoot = "unknown"
)
// BootFrom returns the booting partition of the SUT
func BootFrom() string {
out, err := utils.SH("cat /proc/cmdline")
if err != nil {
return UnknownBoot
}
switch {
case strings.Contains(out, "COS_ACTIVE"):
return ActiveBoot
case strings.Contains(out, "COS_PASSIVE"):
return PassiveBoot
case strings.Contains(out, "COS_RECOVERY"), strings.Contains(out, "COS_SYSTEM"):
return RecoveryBoot
case strings.Contains(out, "live:CDLABEL"):
return LiveCDBoot
case strings.Contains(out, "netboot"):
return NetBoot
default:
return UnknownBoot
}
}
func EdgeVPN(instance, rootDir string) (Service, error) {
if utils.IsOpenRCBased() {
return openrc.NewService(
openrc.WithName("edgevpn"),
openrc.WithUnitContent(openrc.EdgevpnUnit),
openrc.WithRoot(rootDir),
)
} else {
return systemd.NewService(
systemd.WithName("edgevpn"),
systemd.WithInstance(instance),
systemd.WithUnitContent(systemd.EdgevpnUnit),
systemd.WithRoot(rootDir),
)
}
}
const EdgeVPNDefaultInstance string = "c3os"
type fakegetty struct{}
func (fakegetty) Restart() error { return nil }
func (fakegetty) Enable() error { return nil }
func (fakegetty) OverrideCmd(string) error { return nil }
func (fakegetty) SetEnvFile(string) error { return nil }
func (fakegetty) WriteUnit() error { return nil }
func (fakegetty) Start() error {
utils.SH("chvt 2")
return nil
}
func Getty(i int) (Service, error) {
if utils.IsOpenRCBased() {
return &fakegetty{}, nil
} else {
return systemd.NewService(
systemd.WithName("getty"),
systemd.WithInstance(fmt.Sprintf("tty%d", i)),
)
}
}
func K3s() (Service, error) {
if utils.IsOpenRCBased() {
return openrc.NewService(
openrc.WithName("k3s"),
)
} else {
return systemd.NewService(
systemd.WithName("k3s"),
)
}
}
func K3sAgent() (Service, error) {
if utils.IsOpenRCBased() {
return openrc.NewService(
openrc.WithName("k3s-agent"),
)
} else {
return systemd.NewService(
systemd.WithName("k3s-agent"),
)
}
}
func K3sEnvUnit(unit string) string {
if utils.IsOpenRCBased() {
return fmt.Sprintf("/etc/rancher/k3s/%s.env", unit)
} else {
return fmt.Sprintf("/etc/sysconfig/%s", unit)
}
}
func UUID() string {
if os.Getenv("UUID") != "" {
return os.Getenv("UUID")
}
id, _ := machineid.ID()
hostname, _ := os.Hostname()
return fmt.Sprintf("%s-%s", id, hostname)
}
func CreateSentinel(f string) error {
return ioutil.WriteFile(fmt.Sprintf("/usr/local/.c3os/sentinel_%s", f), []byte{}, os.ModePerm)
}
func SentinelExist(f string) bool {
if _, err := os.Stat(fmt.Sprintf("/usr/local/.c3os/sentinel_%s", f)); err == nil {
return true
}
return false
}
func ExecuteInlineCloudConfig(cloudConfig, stage string) error {
_, err := utils.ShellSTDIN(cloudConfig, fmt.Sprintf("elemental run-stage -s %s -", stage))
return err
}
func ExecuteCloudConfig(file, stage string) error {
_, err := utils.SH(fmt.Sprintf("elemental run-stage -s %s %s", stage, file))
return err
}