kairos-agent/internal/agent/agent.go

69 lines
1.7 KiB
Go
Raw Normal View History

2022-07-17 08:42:12 +00:00
package agent
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/c3os-io/c3os/internal/bus"
machine "github.com/c3os-io/c3os/internal/machine"
events "github.com/c3os-io/c3os/pkg/bus"
config "github.com/c3os-io/c3os/pkg/config"
"github.com/nxadm/tail"
)
2022-07-25 22:26:10 +00:00
// setup needs edgevpn and k3s installed locally (both k3s and k3s-agent systemd services).
2022-07-17 08:42:12 +00:00
func Run(apiAddress string, dir []string, force bool) error {
2022-07-25 22:26:10 +00:00
os.MkdirAll("/usr/local/.c3os", 0600) //nolint:errcheck
2022-07-17 08:42:12 +00:00
// Reads config
c, err := config.Scan(config.Directories(dir...))
if err != nil {
return err
}
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
}
2022-07-17 08:42:12 +00:00
2022-07-25 22:26:10 +00:00
os.MkdirAll("/var/log/c3os", 0600) //nolint:errcheck
2022-07-17 08:42:12 +00:00
fileName := filepath.Join("/var/log/c3os", "agent-provider.log")
err = ioutil.WriteFile(fileName, []byte{}, os.ModePerm)
if err != nil {
return err
}
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("bundles") {
opts := c.Bundles.Options()
err := machine.RunBundles(opts...)
if !c.IgnoreBundleErrors && err != nil {
return err
}
// Re-load providers
bus.Manager.LoadProviders()
2022-07-25 22:26:10 +00:00
err = machine.CreateSentinel("bundles")
if !c.IgnoreBundleErrors && err != nil {
return err
}
2022-07-17 08:42:12 +00:00
}
_, err = bus.Manager.Publish(events.EventBootstrap, events.BootstrapPayload{APIAddress: apiAddress, Config: c.String(), Logfile: fileName})
return err
}