mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-06-03 01:44:53 +00:00
art: Allow the agent to self-restart on error
In init systems that don't support automatic restart this might be handy when we fail because we boot up too fast. This is an attempt to fix https://github.com/c3os-io/c3os/issues/47
This commit is contained in:
parent
b62a3fc892
commit
f185430669
@ -14,12 +14,16 @@ import (
|
||||
)
|
||||
|
||||
// setup needs edgevpn and k3s installed locally (both k3s and k3s-agent systemd services).
|
||||
func Run(apiAddress string, dir []string, force bool) error {
|
||||
func Run(opts ...Option) error {
|
||||
o := &Options{}
|
||||
if err := o.Apply(opts...); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
os.MkdirAll("/usr/local/.c3os", 0600) //nolint:errcheck
|
||||
|
||||
// Reads config
|
||||
c, err := config.Scan(config.Directories(dir...))
|
||||
c, err := config.Scan(config.Directories(o.Dir...))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -63,6 +67,11 @@ func Run(apiAddress string, dir []string, force bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
_, err = bus.Manager.Publish(events.EventBootstrap, events.BootstrapPayload{APIAddress: apiAddress, Config: c.String(), Logfile: fileName})
|
||||
_, 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
|
||||
}
|
||||
|
50
internal/agent/agent_options.go
Normal file
50
internal/agent/agent_options.go
Normal file
@ -0,0 +1,50 @@
|
||||
package agent
|
||||
|
||||
// Options yields the options for the running agent
|
||||
type Options struct {
|
||||
ApiAddress string
|
||||
Dir []string
|
||||
Force bool
|
||||
Restart bool
|
||||
}
|
||||
|
||||
// Apply applies option to the options struct
|
||||
func (o *Options) Apply(opts ...Option) error {
|
||||
for _, oo := range opts {
|
||||
if err := oo(o); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Option is a generic option for the Agent
|
||||
type Option func(o *Options) error
|
||||
|
||||
// ForceAgent forces the agent to run
|
||||
var ForceAgent Option = func(o *Options) error {
|
||||
o.Force = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// RestartAgent makes the agent restart on error
|
||||
var RestartAgent Option = func(o *Options) error {
|
||||
o.Restart = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// WithAPI sets the API address
|
||||
func WithAPI(address string) Option {
|
||||
return func(o *Options) error {
|
||||
o.ApiAddress = address
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithDirectory sets the Agent config directories
|
||||
func WithDirectory(dirs ...string) Option {
|
||||
return func(o *Options) error {
|
||||
o.Dir = dirs
|
||||
return nil
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user