Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce --bootstrap-token-stdin and --{username,password} #160

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ linters:
- gocyclo
- godot
- godox
- goerr113
- gofmt
- goheader
- gomodguard
Expand Down Expand Up @@ -116,6 +115,9 @@ linters:
# Needs package whitelists
- depguard

# This is mostly a CLI tool, not a package, so it's OK to have dynamic errors
- goerr113

issues:
# Don't hide multiple issues that belong to one class since GitHub annotations can handle them all nicely.
max-issues-per-linter: 0
Expand Down
8 changes: 8 additions & 0 deletions internal/command/create/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var memory uint64
var netSoftnet bool
var netBridged string
var headless bool
var username string
var password string
var resources map[string]string
var restartPolicy string
var startupScript string
Expand All @@ -38,6 +40,10 @@ func newCreateVMCommand() *cobra.Command {
command.PersistentFlags().BoolVar(&netSoftnet, "net-softnet", false, "whether to use Softnet network isolation")
command.PersistentFlags().StringVar(&netBridged, "net-bridged", "", "whether to use Bridged network mode")
command.PersistentFlags().BoolVar(&headless, "headless", true, "whether to run without graphics")
command.PersistentFlags().StringVar(&username, "username", "admin",
"SSH username to use when executing a startup script on the VM")
command.PersistentFlags().StringVar(&password, "password", "admin",
"SSH password to use when executing a startup script on the VM")
command.PersistentFlags().StringToStringVar(&resources, "resources", map[string]string{},
"resources to request for this VM")
command.PersistentFlags().StringVar(&restartPolicy, "restart-policy", string(v1.RestartPolicyNever),
Expand Down Expand Up @@ -82,6 +88,8 @@ func runCreateVM(cmd *cobra.Command, args []string) error {
NetSoftnet: netSoftnet,
NetBridged: netBridged,
Headless: headless,
Username: username,
Password: password,
HostDirs: hostDirs,
}

Expand Down
43 changes: 38 additions & 5 deletions internal/command/worker/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"io"
"os"
"strings"
)

var ErrRunFailed = errors.New("failed to run worker")

var ErrBootstrapTokenNotProvided = errors.New("no bootstrap token provided")
var (
ErrRunFailed = errors.New("failed to run worker")
ErrNoBootstrapTokenProvided = errors.New("no bootstrap token was provided")
ErrEmptyBootstrapTokenProvided = errors.New("empty bootstrap token was provided")
)

var bootstrapTokenRaw string
var bootstrapTokenStdin bool
var logFilePath string
var stringToStringResources map[string]string
var noPKI bool
Expand All @@ -33,7 +39,9 @@ func newRunCommand() *cobra.Command {
}

cmd.PersistentFlags().StringVar(&bootstrapTokenRaw, "bootstrap-token", "",
"a bootstrap token retrieved via `orchard get bootstrap-token <service-account-name-for-workers>`")
"a bootstrap token retrieved via \"orchard get bootstrap-token <service-account-name-for-workers>\"")
cmd.PersistentFlags().BoolVar(&bootstrapTokenStdin, "bootstrap-token-stdin", false,
"use this flag to provide a bootstrap token via the standard input")
cmd.PersistentFlags().StringVar(&logFilePath, "log-file", "",
"optional path to a file where logs (up to 100 Mb) will be written.")
cmd.PersistentFlags().StringToStringVar(&stringToStringResources, "resources", map[string]string{},
Expand All @@ -55,8 +63,12 @@ func runWorker(cmd *cobra.Command, args []string) (err error) {
}

// Parse bootstrap token
bootstrapTokenRaw, err := readBootstrapToken()
if err != nil {
return err
}
if bootstrapTokenRaw == "" {
return ErrBootstrapTokenNotProvided
return ErrEmptyBootstrapTokenProvided
}
bootstrapToken, err := bootstraptoken.NewFromString(bootstrapTokenRaw)
if err != nil {
Expand Down Expand Up @@ -110,6 +122,27 @@ func runWorker(cmd *cobra.Command, args []string) (err error) {
return workerInstance.Run(cmd.Context())
}

func readBootstrapToken() (string, error) {
if bootstrapTokenRaw != "" && bootstrapTokenStdin {
return "", fmt.Errorf("--bootstrap-token and --bootstrap-token-stdin are mutually exclusive")
}

if bootstrapTokenRaw != "" {
return bootstrapTokenRaw, nil
}

if bootstrapTokenStdin {
stdinBytes, err := io.ReadAll(os.Stdin)
if err != nil {
return "", fmt.Errorf("failed to read the bootstrap token from the standard input: %w", err)
}

return strings.TrimSuffix(string(stdinBytes), "\n"), nil
}

return "", ErrNoBootstrapTokenProvided
}

func createLogger() (*zap.Logger, error) {
level := zap.InfoLevel
if debug {
Expand Down