Skip to content

Commit

Permalink
Rework reboot/shutdown to use the hooks (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
Itxaka authored Jul 24, 2023
1 parent 4e5d116 commit 956f86f
Show file tree
Hide file tree
Showing 22 changed files with 101 additions and 467 deletions.
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ require (
github.com/pterm/pterm v0.12.63
github.com/sanity-io/litter v1.5.5
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
github.com/twpayne/go-vfs v1.7.2
github.com/urfave/cli/v2 v2.25.7
Expand Down Expand Up @@ -96,7 +95,6 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/itchyny/gojq v0.12.12 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/jaypipes/pcidb v1.0.0 // indirect
Expand Down Expand Up @@ -131,7 +129,6 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
github.com/packethost/packngo v0.29.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/phayes/permbits v0.0.0-20190612203442-39d7c581d2ee // indirect
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
Expand All @@ -140,7 +137,6 @@ require (
github.com/qeesung/image2ascii v1.0.1 // indirect
github.com/rancher-sandbox/linuxkit v1.0.1-0.20230517173613-432a87ba3e09 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/samber/lo v1.37.0 // indirect
github.com/santhosh-tekuri/jsonschema/v5 v5.3.0 // indirect
Expand Down
184 changes: 3 additions & 181 deletions go.sum

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package agent

import (
"fmt"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
"os"
"path/filepath"

hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
"github.com/kairos-io/kairos-agent/v2/internal/bus"
config "github.com/kairos-io/kairos-agent/v2/pkg/config"
events "github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/collector"
"github.com/kairos-io/kairos-sdk/machine"
"github.com/kairos-io/kairos-sdk/utils"
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
"github.com/kairos-io/kairos-agent/v2/internal/bus"
config "github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/nxadm/tail"
)

Expand Down Expand Up @@ -63,8 +64,8 @@ func Run(opts ...Option) error {
}()

if !machine.SentinelExist("firstboot") {

if err := hook.Run(*c, hook.FirstBoot...); err != nil {
spec := v1.EmptySpec{}
if err := hook.Run(*c, &spec, hook.FirstBoot...); err != nil {
return err
}

Expand Down
7 changes: 4 additions & 3 deletions internal/agent/hooks/bundles.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package hook

import (
config "github.com/kairos-io/kairos-agent/v2/pkg/config"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
"github.com/kairos-io/kairos-sdk/bundles"
"github.com/kairos-io/kairos-sdk/machine"
config "github.com/kairos-io/kairos-agent/v2/pkg/config"
)

type BundleOption struct{}

func (b BundleOption) Run(c config.Config) error {
func (b BundleOption) Run(c config.Config, _ v1.Spec) error {

machine.Mount("COS_PERSISTENT", "/usr/local") //nolint:errcheck
defer func() {
Expand All @@ -31,7 +32,7 @@ func (b BundleOption) Run(c config.Config) error {

type BundlePostInstall struct{}

func (b BundlePostInstall) Run(c config.Config) error {
func (b BundlePostInstall) Run(c config.Config, _ v1.Spec) error {
opts := c.Bundles.Options()
err := bundles.RunBundles(opts...)
if c.FailOnBundleErrors && err != nil {
Expand Down
7 changes: 4 additions & 3 deletions internal/agent/hooks/gruboptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package hook

import (
"fmt"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"

"github.com/kairos-io/kairos-sdk/system"
config "github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/kairos-io/kairos-sdk/system"
)

type GrubOptions struct{}

func (b GrubOptions) Run(c config.Config) error {
func (b GrubOptions) Run(c config.Config, _ v1.Spec) error {
err := system.Apply(system.SetGRUBOptions(c.Install.GrubOptions))
if err != nil {
fmt.Println(err)
Expand All @@ -19,7 +20,7 @@ func (b GrubOptions) Run(c config.Config) error {

type GrubPostInstallOptions struct{}

func (b GrubPostInstallOptions) Run(c config.Config) error {
func (b GrubPostInstallOptions) Run(c config.Config, _ v1.Spec) error {
err := system.Apply(system.SetGRUBOptions(c.GrubOptions))
if err != nil {
fmt.Println(err)
Expand Down
15 changes: 11 additions & 4 deletions internal/agent/hooks/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package hook

import (
config "github.com/kairos-io/kairos-agent/v2/pkg/config"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
)

type Interface interface {
Run(c config.Config) error
Run(c config.Config, spec v1.Spec) error
}

var AfterInstall = []Interface{
Expand All @@ -17,16 +18,22 @@ var AfterInstall = []Interface{
&Lifecycle{}, // Handles poweroff/reboot by config options
}

var AfterReset = []Interface{}
var AfterReset = []Interface{
&Lifecycle{},
}

var AfterUpgrade = []Interface{
&Lifecycle{},
}

var FirstBoot = []Interface{
&BundlePostInstall{},
&GrubPostInstallOptions{},
}

func Run(c config.Config, hooks ...Interface) error {
func Run(c config.Config, spec v1.Spec, hooks ...Interface) error {
for _, h := range hooks {
if err := h.Run(c); err != nil {
if err := h.Run(c, spec); err != nil {
return err
}
}
Expand Down
5 changes: 3 additions & 2 deletions internal/agent/hooks/kcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package hook

import (
"fmt"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
"github.com/kairos-io/kairos-sdk/machine"
"time"

"github.com/kairos-io/kairos-sdk/utils"
"github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/kairos-io/kairos-sdk/utils"
)

type Kcrypt struct{}

func (k Kcrypt) Run(c config.Config) error {
func (k Kcrypt) Run(c config.Config, _ v1.Spec) error {

if len(c.Install.Encrypt) == 0 {
return nil
Expand Down
12 changes: 8 additions & 4 deletions internal/agent/hooks/lifecycle.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package hook

import (
"github.com/kairos-io/kairos-sdk/utils"
"github.com/kairos-io/kairos-agent/v2/pkg/config"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
"github.com/kairos-io/kairos-sdk/utils"
"time"
)

type Lifecycle struct{}

func (s Lifecycle) Run(c config.Config) error {
if c.Install.Reboot {
func (s Lifecycle) Run(c config.Config, spec v1.Spec) error {
if spec.ShouldReboot() {
time.Sleep(5)
utils.Reboot()
}

if c.Install.Poweroff {
if spec.ShouldShutdown() {
time.Sleep(5)
utils.PowerOFF()
}
return nil
Expand Down
5 changes: 3 additions & 2 deletions internal/agent/hooks/mounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package hook

import (
"fmt"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
"os"
"path/filepath"
"strings"

"github.com/kairos-io/kairos-sdk/machine"
config "github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/kairos-io/kairos-sdk/machine"
"github.com/mudler/yip/pkg/schema"
yip "github.com/mudler/yip/pkg/schema"
"gopkg.in/yaml.v1"
Expand All @@ -26,7 +27,7 @@ func saveCloudConfig(name config.Stage, yc yip.YipConfig) error {
// Read the keys sections ephemeral_mounts and bind mounts from install key in the cloud config.
// If not empty write an environment file to /run/cos/custom-layout.env.
// That env file is in turn read by /overlay/files/system/oem/11_persistency.yaml in fs.after stage.
func (cm CustomMounts) Run(c config.Config) error {
func (cm CustomMounts) Run(c config.Config, _ v1.Spec) error {

//fmt.Println("Custom mounts hook")
//fmt.Println(strings.Join(c.Install.BindMounts, " "))
Expand Down
3 changes: 2 additions & 1 deletion internal/agent/hooks/runstage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package hook
import (
"github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/kairos-io/kairos-agent/v2/pkg/elementalConfig"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
"github.com/kairos-io/kairos-agent/v2/pkg/utils"

events "github.com/kairos-io/kairos-sdk/bus"
)

type RunStage struct{}

func (r RunStage) Run(c config.Config) error {
func (r RunStage) Run(c config.Config, _ v1.Spec) error {
cfg, err := elementalConfig.ReadConfigRunFromAgentConfig(&c)
if err != nil {
cfg.Logger.Errorf("Error reading config: %s\n", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func RunInstall(options map[string]string) error {
os.Exit(1)
}

return hook.Run(*c, hook.AfterInstall...)
return hook.Run(*c, installSpec, hook.AfterInstall...)
}

func ensureDataSourceReady() {
Expand Down
33 changes: 1 addition & 32 deletions internal/agent/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/kairos-io/kairos-sdk/utils"

"github.com/mudler/go-pluggable"
"github.com/pterm/pterm"
)

func Reset(dir ...string) error {
Expand Down Expand Up @@ -103,37 +102,7 @@ func Reset(dir ...string) error {
os.Exit(1)
}

if err := hook.Run(*c, hook.AfterReset...); err != nil {
return err
}

bus.Manager.Publish(sdk.EventAfterReset, sdk.EventPayload{}) //nolint:errcheck

if !agentConfig.Fast {
pterm.Info.Println("Rebooting in 60 seconds, press Enter to abort...")
}

// We don't close the lock, as none of the following actions are expected to return
lock2 := sync.Mutex{}
go func() {
// Wait for user input and go back to shell
utils.Prompt("") //nolint:errcheck
// give tty1 back
svc, err := machine.Getty(1)
if err == nil {
svc.Start() //nolint:errcheck
}

lock2.Lock()
fmt.Println("Reboot aborted")
panic(utils.Shell().Run())
}()

if !agentConfig.Fast {
time.Sleep(60 * time.Second)
}
lock2.Lock()
utils.Reboot()

return nil
return hook.Run(*c, resetSpec, hook.AfterReset...)
}
16 changes: 15 additions & 1 deletion internal/agent/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
"sort"

"github.com/Masterminds/semver/v3"
Expand Down Expand Up @@ -119,7 +120,20 @@ func Upgrade(

upgradeAction := action.NewUpgradeAction(upgradeConfig, upgradeSpec)

return upgradeAction.Run()
err = upgradeAction.Run()
if err != nil {
return err
}

if upgradeSpec.Reboot {
utils.Reboot()
}

if upgradeSpec.PowerOff {
utils.PowerOFF()
}

return hook.Run(*c, upgradeSpec, hook.AfterUpgrade...)
}

// determineUpgradeImage asks the provider plugin for an image or constructs
Expand Down
8 changes: 0 additions & 8 deletions pkg/action/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,5 @@ func (i InstallAction) Run() (err error) {
}
}

// Reboot, poweroff or nothing
if i.cfg.Reboot {
i.cfg.Logger.Infof("Rebooting in 5 seconds")
return utils.Reboot(i.cfg.Runner, 5)
} else if i.cfg.PowerOff {
i.cfg.Logger.Infof("Shutting down in 5 seconds")
return utils.Shutdown(i.cfg.Runner, 5)
}
return err
}
4 changes: 0 additions & 4 deletions pkg/action/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ var _ = Describe("Install action tests", func() {

It("Successfully installs", func() {
spec.Target = device
config.Reboot = true
Expect(installer.Run()).To(BeNil())
Expect(runner.IncludesCmds([][]string{{"reboot", "-f"}}))
})

It("Sets the executable /run/cos/ejectcd so systemd can eject the cd on restart", func() {
Expand Down Expand Up @@ -248,9 +246,7 @@ var _ = Describe("Install action tests", func() {
It("Successfully installs despite hooks failure", Label("hooks"), func() {
cloudInit.Error = true
spec.Target = device
config.PowerOff = true
Expect(installer.Run()).To(BeNil())
Expect(runner.IncludesCmds([][]string{{"poweroff", "-f"}}))
})

It("Successfully installs without formatting despite detecting a previous installation", Label("no-format", "disk"), func() {
Expand Down
8 changes: 0 additions & 8 deletions pkg/action/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,5 @@ func (r ResetAction) Run() (err error) {
return err
}

// Reboot, poweroff or nothing
if r.cfg.Reboot {
r.cfg.Logger.Infof("Rebooting in 5 seconds")
return utils.Reboot(r.cfg.Runner, 5)
} else if r.cfg.PowerOff {
r.cfg.Logger.Infof("Shutting down in 5 seconds")
return utils.Shutdown(r.cfg.Runner, 5)
}
return err
}
4 changes: 0 additions & 4 deletions pkg/action/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,12 @@ var _ = Describe("Reset action tests", func() {
})

It("Successfully resets on non-squashfs recovery", func() {
config.Reboot = true
Expect(reset.Run()).To(BeNil())
Expect(runner.IncludesCmds([][]string{{"reboot", "-f"}}))
})
It("Successfully resets on non-squashfs recovery including persistent data", func() {
config.PowerOff = true
spec.FormatPersistent = true
spec.FormatOEM = true
Expect(reset.Run()).To(BeNil())
Expect(runner.IncludesCmds([][]string{{"poweroff", "-f"}}))
})
It("Successfully resets from a squashfs recovery image", Label("channel"), func() {
err := utils.MkdirAll(config.Fs, constants.IsoBaseTree, constants.DirPerm)
Expand Down
Loading

0 comments on commit 956f86f

Please sign in to comment.