mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-06-27 16:46:59 +00:00
* Introduce config/collector package to split the collection of config sources out of the config package. Each consumer of the new package will take care of unmarshalling the yaml to a specific Config struct, do validations etc. * Add tests and remove garbage * Follow all config_url chains and test it * Add missing options file and refactor cmdline code * Consolidate the way we merge configs no matter where they come from * Allow and use only files with valid headers Config is specific to Kairos while Collector is generic. This will allow us to do validations which are just related to Kairos at the config level, while including every type of key and querying of the full yaml at the Collector level splitting the responsibilities of each package. --------- Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
96 lines
2.2 KiB
Go
96 lines
2.2 KiB
Go
package agent
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
events "github.com/kairos-io/kairos-sdk/bus"
|
|
"github.com/kairos-io/kairos-sdk/machine"
|
|
"github.com/kairos-io/kairos-sdk/utils"
|
|
hook "github.com/kairos-io/kairos/internal/agent/hooks"
|
|
"github.com/kairos-io/kairos/internal/bus"
|
|
config "github.com/kairos-io/kairos/pkg/config"
|
|
"github.com/kairos-io/kairos/pkg/config/collector"
|
|
"github.com/nxadm/tail"
|
|
)
|
|
|
|
// Run starts the agent provider emitting the bootstrap event.
|
|
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(collector.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(collector.Directories(o.Dir...))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
configStr, err := c.Config.String()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
_, err = bus.Manager.Publish(events.EventBootstrap, events.BootstrapPayload{APIAddress: o.APIAddress, Config: configStr, Logfile: fileName})
|
|
|
|
if o.Restart && err != nil {
|
|
fmt.Println("Warning: Agent failed, restarting: ", err.Error())
|
|
return Run(opts...)
|
|
}
|
|
return err
|
|
}
|