diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 3148ce9..23ba409 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -30,5 +30,6 @@ jobs: file: Dockerfile platforms: linux/amd64, linux/arm64 push: true + target: kairos-init tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index d55927e..5d2f756 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,5 @@ +ARG BASE_IMAGE=ubuntu:24.04 + FROM golang AS build WORKDIR /app COPY go.mod go.sum . @@ -7,5 +9,16 @@ ENV CGO_ENABLED=0 RUN go build -o /app/kairos-init . -FROM scratch AS default -COPY --from=build /app/kairos-init /kairos-init \ No newline at end of file +FROM scratch AS kairos-init +COPY --from=build /app/kairos-init /kairos-init + +FROM ${BASE_IMAGE} +ARG MODEL=generic +ARG VARIANT=core +ARG FRAMEWORK_VERSION="" +ARG TRUSTED_BOOT=false + +COPY --from=kairos-init /kairos-init /kairos-init +RUN /kairos-init -l debug -s install -m "${MODEL}" -v "${VARIANT}" -f "${FRAMEWORK_VERSION}" -t "${TRUSTED_BOOT}" +RUN /kairos-init -l debug -s init -m "${MODEL}" -v "${VARIANT}" -f "${FRAMEWORK_VERSION}" -t "${TRUSTED_BOOT}" +RUN rm /kairos-init \ No newline at end of file diff --git a/main.go b/main.go index 140a0c0..b78dafb 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/mudler/yip/pkg/schema" "os" + "strings" "github.com/kairos-io/kairos-init/pkg/config" "github.com/kairos-io/kairos-init/pkg/stages" @@ -14,16 +15,36 @@ import ( ) func main() { - flag.StringVar(&config.DefaultConfig.Level, "level", "info", "set the log level (shorthand: -l)") - flag.StringVar(&config.DefaultConfig.Level, "l", "info", "set the log level (shorthand: -l)") - flag.StringVar(&config.DefaultConfig.Stage, "stage", "", "set the stage to run (shorthand: -s)") - flag.StringVar(&config.DefaultConfig.Stage, "s", "", "set the stage to run (shorthand: -s)") - flag.StringVar(&config.DefaultConfig.Model, "model", "generic", "model to build for, like generic or rpi4 (shorthand: -m)") - flag.StringVar(&config.DefaultConfig.Model, "m", "generic", "model to build for, like generic or rpi4 (shorthand: -m)") - flag.BoolVar(&config.DefaultConfig.TrustedBoot, "trustedboot", false, "init the system for Trusted Boot, changes bootloader to systemd (shorthand: -t)") - flag.BoolVar(&config.DefaultConfig.TrustedBoot, "t", false, "init the system for Trusted Boot, changes bootloader to systemd (shorthand: -t)") + var trusted string + flag.StringVar(&config.DefaultConfig.Level, "l", "info", "set the log level") + flag.StringVar(&config.DefaultConfig.Stage, "s", "all", "set the stage to run") + flag.StringVar(&config.DefaultConfig.Model, "m", "generic", "model to build for, like generic or rpi4") + flag.StringVar(&config.DefaultConfig.Variant, "v", "core", "variant to build (core or standard for k3s flavor) (shorthand: -v)") + flag.StringVar(&trusted, "t", "false", "init the system for Trusted Boot, changes bootloader to systemd") + flag.StringVar(&config.DefaultConfig.FrameworkVersion, "f", values.GetFrameworkVersion(), "set the framework version to use") + showHelp := flag.Bool("help", false, "show help") + + if strings.ToLower(trusted) == "true" { + config.DefaultConfig.TrustedBoot = true + } + + // Custom usage function + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) + flag.VisitAll(func(f *flag.Flag) { + if f.Name != "cpuprofile" && f.Name != "memprofile" && f.Name != "stubs" && f.Name != "help" && f.Name != "pkg" && f.Name != "log" && f.Name != "e" && f.Name != "out" { + fmt.Fprintf(os.Stderr, " -%s: %s (default: %s)\n", f.Name, f.Usage, f.DefValue) + } + }) + } + flag.Parse() + if *showHelp { + flag.Usage() + os.Exit(0) + } + logger := types.NewKairosLogger("kairos-init", config.DefaultConfig.Level, false) logger.Infof("Starting kairos-init version %s", values.GetVersion()) logger.Debug(litter.Sdump(values.GetFullVersion())) @@ -39,12 +60,12 @@ func main() { runStages, err = stages.RunInstallStage(logger) case "init": runStages, err = stages.RunInitStage(logger) + case "all": + runStages, err = stages.RunAllStages(logger) default: - logger.Errorf("Unknown stage %s", config.DefaultConfig.Stage) + logger.Errorf("Unknown stage %s. Valid values are install, init and all", config.DefaultConfig.Stage) os.Exit(1) } - } else { - runStages, err = stages.RunAllStages(logger) } if err != nil { diff --git a/pkg/config/config.go b/pkg/config/config.go index e044e4b..9b54e17 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -3,10 +3,12 @@ package config // Config is the struct to track the config of the init image // So we can access it from anywhere type Config struct { - Level string - Stage string - Model string - TrustedBoot bool + Level string + Stage string + Model string + FrameworkVersion string + Variant string + TrustedBoot bool } var DefaultConfig = Config{} diff --git a/pkg/stages/stages.go b/pkg/stages/stages.go index 8214579..61442d3 100644 --- a/pkg/stages/stages.go +++ b/pkg/stages/stages.go @@ -167,6 +167,13 @@ func GetCleanupStage(_ values.System, _ types.KairosLogger) []schema.Stage { } func GetInstallFrameworkStage(_ values.System, _ types.KairosLogger) []schema.Stage { + var frameworkVersion string + // If the framework version is set in the config use that, otherwise use the version from the values which usually its the latest + if config.DefaultConfig.FrameworkVersion != "" { + frameworkVersion = config.DefaultConfig.FrameworkVersion + } else { + frameworkVersion = values.GetFrameworkVersion() + } return []schema.Stage{ { Name: "Create kairos directory", @@ -182,7 +189,7 @@ func GetInstallFrameworkStage(_ values.System, _ types.KairosLogger) []schema.St Name: "Install framework", UnpackImages: []schema.UnpackImageConf{ { - Source: fmt.Sprintf("quay.io/kairos/framework:%s", values.GetFrameworkVersion()), + Source: fmt.Sprintf("quay.io/kairos/framework:%s", frameworkVersion), Target: "/", }, },