mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-08-18 00:07:08 +00:00
* 🌱 Add webui Signed-off-by: mudler <mudler@c3os.io> * 🌱 Re-read config files after loading bundles Signed-off-by: mudler <mudler@c3os.io> * [check-spelling] Update metadata Update for https://github.com/kairos-io/kairos/actions/runs/3806058276/attempts/1 Accepted in https://github.com/kairos-io/kairos/pull/587#issuecomment-1367859480 Signed-off-by: check-spelling-bot <check-spelling-bot@users.noreply.github.com> Signed-off-by: mudler <mudler@c3os.io> * 🎨 Beautify index page Signed-off-by: mudler <mudler@c3os.io> * Do not rerun if we were successful or we are already running Signed-off-by: mudler <mudler@c3os.io> * Add syntax highlight Signed-off-by: mudler <mudler@c3os.io> * Add error message Signed-off-by: mudler <mudler@c3os.io> * Add YAML validation and highlight Signed-off-by: mudler <mudler@c3os.io> * Fixup terminal output Signed-off-by: mudler <mudler@c3os.io> * Fix newlines Signed-off-by: mudler <mudler@c3os.io> * fixups Signed-off-by: mudler <mudler@c3os.io> * 🎨 Fixup lint issues Signed-off-by: mudler <mudler@c3os.io> * Mark dependencies Signed-off-by: mudler <mudler@c3os.io> * Let configure the listening address Signed-off-by: mudler <mudler@c3os.io> Signed-off-by: mudler <mudler@c3os.io> Signed-off-by: check-spelling-bot <check-spelling-bot@users.noreply.github.com>
94 lines
2.1 KiB
Go
94 lines
2.1 KiB
Go
package agent
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/kairos-io/kairos/pkg/utils"
|
|
|
|
events "github.com/kairos-io/kairos/sdk/bus"
|
|
|
|
hook "github.com/kairos-io/kairos/internal/agent/hooks"
|
|
"github.com/kairos-io/kairos/internal/bus"
|
|
config "github.com/kairos-io/kairos/pkg/config"
|
|
machine "github.com/kairos-io/kairos/pkg/machine"
|
|
"github.com/nxadm/tail"
|
|
)
|
|
|
|
// setup needs edgevpn and k3s installed locally (both k3s and k3s-agent systemd services).
|
|
func Run(opts ...Option) error {
|
|
o := &Options{}
|
|
if err := o.Apply(opts...); err != nil {
|
|
return err
|
|
}
|
|
|
|
os.MkdirAll("/usr/local/.kairos", 0600) //nolint:errcheck
|
|
|
|
// Reads config
|
|
c, err := config.Scan(config.Directories(o.Dir...))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
utils.SetEnv(c.Env)
|
|
bf := machine.BootFrom()
|
|
if c.Install != nil && c.Install.Auto && (bf == machine.NetBoot || bf == machine.LiveCDBoot) {
|
|
// Don't go ahead if we are asked to install from a booting live medium
|
|
fmt.Println("Agent run aborted. Installation being performed from live medium")
|
|
return nil
|
|
}
|
|
|
|
os.MkdirAll("/var/log/kairos", 0600) //nolint:errcheck
|
|
|
|
fileName := filepath.Join("/var/log/kairos", "agent-provider.log")
|
|
|
|
// Create if not exist
|
|
if _, err := os.Stat(fileName); err != nil {
|
|
err = os.WriteFile(fileName, []byte{}, os.ModePerm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Tail to the log
|
|
t, err := tail.TailFile(fileName, tail.Config{Follow: true})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
go func() {
|
|
for line := range t.Lines {
|
|
fmt.Println(line.Text)
|
|
}
|
|
}()
|
|
|
|
if !machine.SentinelExist("firstboot") {
|
|
|
|
if err := hook.Run(*c, hook.FirstBoot...); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Re-load providers
|
|
bus.Reload()
|
|
err = machine.CreateSentinel("firstboot")
|
|
if c.FailOnBundleErrors && err != nil {
|
|
return err
|
|
}
|
|
|
|
// Re-read config files
|
|
c, err = config.Scan(config.Directories(o.Dir...))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
_, err = bus.Manager.Publish(events.EventBootstrap, events.BootstrapPayload{APIAddress: o.APIAddress, Config: c.String(), Logfile: fileName})
|
|
|
|
if o.Restart && err != nil {
|
|
fmt.Println("Warning: Agent failed, restarting: ", err.Error())
|
|
return Run(opts...)
|
|
}
|
|
return err
|
|
}
|