1
0
mirror of https://github.com/kairos-io/immucore.git synced 2025-05-11 01:26:35 +00:00

Run command directly without subcommand ()

If immucore is run we should just execute our normal dag stuff directly
instead of having a subcommand.

Keep the version subcommand and dry-flag

Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>
This commit is contained in:
Itxaka 2023-02-20 11:14:26 +01:00 committed by GitHub
parent 97752aaa53
commit bf34119ece
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 111 deletions
dracut/28immucore
internal/cmd
main.go

View File

@ -10,4 +10,4 @@ Conflicts=initrd-switch-root.target
Type=oneshot
RemainAfterExit=yes
StandardOutput=journal+console
ExecStart=/usr/bin/immucore start
ExecStart=/usr/bin/immucore

View File

@ -1,97 +0,0 @@
package cmd
import (
"context"
"os"
"github.com/kairos-io/immucore/internal/utils"
"github.com/kairos-io/immucore/internal/version"
"github.com/kairos-io/immucore/pkg/mount"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spectrocloud-labs/herd"
"github.com/urfave/cli/v2"
)
var Commands = []*cli.Command{
{
Name: "start",
Usage: "start",
UsageText: "starts",
Description: `
Sends a generic event payload with the configuration found in the scanned directories.
`,
Aliases: []string{},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dry-run",
EnvVars: []string{"IMMUCORE_DRY_RUN"},
Required: false,
},
},
Action: func(c *cli.Context) (err error) {
debug := len(utils.ReadCMDLineArg("rd.immucore.debug")) > 0
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger()
zerolog.SetGlobalLevel(zerolog.InfoLevel)
debugFromEnv := os.Getenv("IMMUCORE_DEBUG") != ""
if debug || debugFromEnv {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger()
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
v := version.Get()
log.Logger.Info().Str("commit", v.GitCommit).Str("compiled with", v.GoVersion).Str("version", v.Version).Msg("Immucore")
cmdline, _ := os.ReadFile("/proc/cmdline")
log.Logger.Debug().Msg(string(cmdline))
g := herd.DAG(herd.EnableInit)
// Get targets and state
targetImage, targetDevice := utils.GetTarget(c.Bool("dry-run"))
s := &mount.State{
Logger: log.Logger,
Rootdir: utils.GetRootDir(),
TargetDevice: targetDevice,
TargetImage: targetImage,
RootMountMode: utils.RootRW(),
}
if utils.DisableImmucore() {
log.Logger.Info().Msg("Stanza rd.cos.disable on the cmdline or booting from CDROM/Netboot/Squash recovery. Disabling immucore.")
err = s.RegisterLiveMedia(g)
} else {
log.Logger.Info().Msg("Booting on active/passive/recovery.")
err = s.RegisterNormalBoot(g)
}
if err != nil {
s.Logger.Err(err)
return err
}
log.Info().Msg(s.WriteDAG(g))
// Once we print the dag we can exit already
if c.Bool("dry-run") {
return nil
}
err = g.Run(context.Background())
log.Info().Msg(s.WriteDAG(g))
return err
},
},
{
Name: "version",
Usage: "version",
Action: func(c *cli.Context) error {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger()
zerolog.SetGlobalLevel(zerolog.InfoLevel)
v := version.Get()
log.Logger.Info().Str("commit", v.GitCommit).Str("compiled with", v.GoVersion).Str("version", v.Version).Msg("Immucore")
return nil
},
},
}

93
main.go
View File

@ -1,26 +1,93 @@
package main
import (
"context"
"fmt"
"os"
"github.com/kairos-io/immucore/internal/cmd"
"github.com/kairos-io/immucore/internal/utils"
"github.com/kairos-io/immucore/internal/version"
"github.com/kairos-io/immucore/pkg/mount"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spectrocloud-labs/herd"
"github.com/urfave/cli/v2"
"os"
)
// Apply Immutability profiles.
func main() {
app := &cli.App{
Name: "immucore",
Version: "0.1",
Authors: []*cli.Author{{Name: "Kairos authors"}},
Usage: "kairos agent start",
Description: `
`,
UsageText: ``,
Copyright: "kairos authors",
app := cli.NewApp()
app.Version = version.GetVersion()
app.Authors = []*cli.Author{{Name: "Kairos authors"}}
app.Copyright = "kairos authors"
app.Action = func(c *cli.Context) (err error) {
debug := len(utils.ReadCMDLineArg("rd.immucore.debug")) > 0
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger()
zerolog.SetGlobalLevel(zerolog.InfoLevel)
debugFromEnv := os.Getenv("IMMUCORE_DEBUG") != ""
if debug || debugFromEnv {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger()
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
Commands: cmd.Commands,
v := version.Get()
log.Logger.Info().Str("commit", v.GitCommit).Str("compiled with", v.GoVersion).Str("version", v.Version).Msg("Immucore")
cmdline, _ := os.ReadFile("/proc/cmdline")
log.Logger.Debug().Msg(string(cmdline))
g := herd.DAG(herd.EnableInit)
// Get targets and state
targetImage, targetDevice := utils.GetTarget(c.Bool("dry-run"))
s := &mount.State{
Logger: log.Logger,
Rootdir: utils.GetRootDir(),
TargetDevice: targetDevice,
TargetImage: targetImage,
RootMountMode: utils.RootRW(),
}
if utils.DisableImmucore() {
log.Logger.Info().Msg("Stanza rd.cos.disable on the cmdline or booting from CDROM/Netboot/Squash recovery. Disabling immucore.")
err = s.RegisterLiveMedia(g)
} else {
log.Logger.Info().Msg("Booting on active/passive/recovery.")
err = s.RegisterNormalBoot(g)
}
if err != nil {
s.Logger.Err(err)
return err
}
log.Info().Msg(s.WriteDAG(g))
// Once we print the dag we can exit already
if c.Bool("dry-run") {
return nil
}
err = g.Run(context.Background())
log.Info().Msg(s.WriteDAG(g))
return err
}
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "dry-run",
},
}
app.Commands = []*cli.Command{
{
Name: "version",
Usage: "version",
Action: func(c *cli.Context) error {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger()
zerolog.SetGlobalLevel(zerolog.InfoLevel)
v := version.Get()
log.Logger.Info().Str("commit", v.GitCommit).Str("compiled with", v.GoVersion).Str("version", v.Version).Msg("Immucore")
return nil
},
},
}
err := app.Run(os.Args)