Move checkRoot to main

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
Dimitris Karakasilis 2023-09-14 15:53:27 +03:00
parent fddbf3f657
commit bf40c48812
No known key found for this signature in database
GPG Key ID: 286DCAFD2C97DDE3
6 changed files with 29 additions and 36 deletions

View File

@ -1,7 +1,6 @@
package agent package agent
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -96,11 +95,3 @@ func Run(opts ...Option) error {
} }
return err return err
} }
func checkRoot() error {
if os.Geteuid() != 0 {
return errors.New("this command requires root privileges")
}
return nil
}

View File

@ -55,10 +55,6 @@ func displayInfo(agentConfig *Config) {
} }
func ManualInstall(c, device string, reboot, poweroff, strictValidations bool) error { func ManualInstall(c, device string, reboot, poweroff, strictValidations bool) error {
if err := checkRoot(); err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -89,10 +85,6 @@ func ManualInstall(c, device string, reboot, poweroff, strictValidations bool) e
} }
func Install(dir ...string) error { func Install(dir ...string) error {
if err := checkRoot(); err != nil {
return err
}
var cc *config.Config var cc *config.Config
var err error var err error

View File

@ -130,10 +130,6 @@ func detectDevice() string {
} }
func InteractiveInstall(debug, spawnShell bool) error { func InteractiveInstall(debug, spawnShell bool) error {
if err := checkRoot(); err != nil {
return err
}
var sshUsers []string var sshUsers []string
bus.Manager.Initialize() bus.Manager.Initialize()

View File

@ -21,10 +21,6 @@ import (
) )
func Reset(reboot, unattended bool, dir ...string) error { func Reset(reboot, unattended bool, dir ...string) error {
if err := checkRoot(); err != nil {
return err
}
bus.Manager.Initialize() bus.Manager.Initialize()
// This config is only for reset branding. // This config is only for reset branding.

View File

@ -53,10 +53,6 @@ func ListReleases(includePrereleases bool) semver.Collection {
func Upgrade( func Upgrade(
version, source string, force, strictValidations bool, dirs []string, preReleases, upgradeRecovery bool) error { version, source string, force, strictValidations bool, dirs []string, preReleases, upgradeRecovery bool) error {
if err := checkRoot(); err != nil {
return err
}
bus.Manager.Initialize() bus.Manager.Initialize()
if version == "" && source == "" { if version == "" && source == "" {

36
main.go
View File

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -119,7 +120,8 @@ See https://kairos.io/docs/upgrade/manual/ for documentation.
return fmt.Errorf("source %s does not match any of oci:, dir: or file: ", source) return fmt.Errorf("source %s does not match any of oci:, dir: or file: ", source)
} }
} }
return nil
return checkRoot()
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
var v string var v string
@ -230,6 +232,9 @@ E.g. kairos-agent install-bundle container:quay.io/kairos/kairos...
}, },
}, },
UsageText: "Install a bundle manually in the node", UsageText: "Install a bundle manually in the node",
Before: func(c *cli.Context) error {
return checkRoot()
},
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
if c.Args().Len() != 1 { if c.Args().Len() != 1 {
return fmt.Errorf("bundle name required") return fmt.Errorf("bundle name required")
@ -382,6 +387,9 @@ This command is meant to be used from the boot GRUB menu, but can be also starte
}, },
}, },
Usage: "Starts interactive installation", Usage: "Starts interactive installation",
Before: func(c *cli.Context) error {
return checkRoot()
},
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return agent.InteractiveInstall(c.Bool("debug"), c.Bool("shell")) return agent.InteractiveInstall(c.Bool("debug"), c.Bool("shell"))
}, },
@ -403,6 +411,9 @@ This command is meant to be used from the boot GRUB menu, but can be also starte
Name: "reboot", Name: "reboot",
}, },
}, },
Before: func(c *cli.Context) error {
return checkRoot()
},
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
if c.NArg() == 0 { if c.NArg() == 0 {
return fmt.Errorf("expect one argument. the config file - if you don't have it, use the interactive-install") return fmt.Errorf("expect one argument. the config file - if you don't have it, use the interactive-install")
@ -424,6 +435,9 @@ See also https://kairos.io/docs/installation/qrcode/ for documentation.
This command is meant to be used from the boot GRUB menu, but can be started manually`, This command is meant to be used from the boot GRUB menu, but can be started manually`,
Aliases: []string{"i"}, Aliases: []string{"i"},
Before: func(c *cli.Context) error {
return checkRoot()
},
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return agent.Install(configScanDir...) return agent.Install(configScanDir...)
}, },
@ -457,6 +471,9 @@ This command is meant to be used from the boot GRUB menu, but can likely be used
Usage: "Do not wait for user input and provide ttys after reset. Also sets the fast mode (do not wait 60 seconds before reset)", Usage: "Do not wait for user input and provide ttys after reset. Also sets the fast mode (do not wait 60 seconds before reset)",
}, },
}, },
Before: func(c *cli.Context) error {
return checkRoot()
},
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
reboot := c.Bool("reboot") reboot := c.Bool("reboot")
unattended := c.Bool("unattended") unattended := c.Bool("unattended")
@ -535,7 +552,8 @@ The validate command expects a configuration file as its only argument. Local fi
_ = cli.ShowSubcommandHelp(c) _ = cli.ShowSubcommandHelp(c)
return fmt.Errorf("") return fmt.Errorf("")
} }
return nil
return checkRoot()
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
stage := c.Args().First() stage := c.Args().First()
@ -574,11 +592,7 @@ The validate command expects a configuration file as its only argument. Local fi
return fmt.Errorf("") return fmt.Errorf("")
} }
if os.Geteuid() != 0 { return checkRoot()
return fmt.Errorf("this command requires root privileges")
}
return nil
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
image := c.Args().Get(0) image := c.Args().Get(0)
@ -664,3 +678,11 @@ The kairos agent is a component to abstract away node ops, providing a common fe
os.Exit(1) os.Exit(1)
} }
} }
func checkRoot() error {
if os.Geteuid() != 0 {
return errors.New("this command requires root privileges")
}
return nil
}