Skip to content

Commit

Permalink
adds ability for preflight to create config (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-rash authored Apr 23, 2024
1 parent 996f4ff commit 316166b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/models/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ type NodeOptions struct {
OtelTraces bool `json:"-"`
OtelTracesExporter string `json:"-"`

PreflightInit string `json:"-"`

Errors []error `json:"errors,omitempty"`
}

Expand Down
65 changes: 65 additions & 0 deletions internal/node/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log/slog"
"os"

nexmodels "github.com/synadia-io/nex/internal/models"
)
Expand All @@ -20,6 +21,33 @@ func CmdUp(opts *nexmodels.Options, nodeopts *nexmodels.NodeOptions, ctx context
}

func CmdPreflight(opts *nexmodels.Options, nodeopts *nexmodels.NodeOptions, ctx context.Context, cancel context.CancelFunc, log *slog.Logger) error {
if nodeopts.PreflightInit != "" {
fi, err := os.Stat(nodeopts.ConfigFilepath)
if os.IsNotExist(err) {
f, err := os.Create(nodeopts.ConfigFilepath)
if err != nil {
return err
}
defer f.Close()
if nodeopts.PreflightInit == "sandbox" {
_, err = f.WriteString(defaultNodeConfig)
if err != nil {
return err
}
} else if nodeopts.PreflightInit == "nosandbox" {
_, err = f.WriteString(defaultNoSandboxNodeConfig)
if err != nil {
return err
}
}
log.Debug("Configuration file created.", slog.String("path", nodeopts.ConfigFilepath))
} else if err != nil || fi.IsDir() {
return err
} else {
log.Warn("Configuration file already exists, skipping init.", slog.String("path", nodeopts.ConfigFilepath))
}
}

config, err := LoadNodeConfiguration(nodeopts.ConfigFilepath)
if err != nil {
return fmt.Errorf("failed to load configuration file: %s", err)
Expand All @@ -34,3 +62,40 @@ func CmdPreflight(opts *nexmodels.Options, nodeopts *nexmodels.NodeOptions, ctx

return nil
}

const defaultNodeConfig string = `{
"default_resource_dir":"/tmp/nex",
"machine_pool_size": 1,
"internal_node_host":"10.10.10.1",
"cni": {
"network_name": "fcnet",
"interface_name": "veth0",
"subnet": "10.10.10.0/24"
},
"machine_template": {
"vcpu_count": 1,
"memsize_mib": 256
},
"tags": {
"simple": "true"
}
}`

const defaultNoSandboxNodeConfig string = `{
"default_resource_dir":"/tmp/nex",
"machine_pool_size": 1,
"internal_node_host":"10.10.10.1",
"cni": {
"network_name": "fcnet",
"interface_name": "veth0",
"subnet": "10.10.10.0/24"
},
"machine_template": {
"vcpu_count": 1,
"memsize_mib": 256
},
"tags": {
"simple": "true"
},
"no_sandbox": true
}`
1 change: 1 addition & 0 deletions nex/node_up_preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func setConditionalCommands() {
nodePreflight = nodes.Command("preflight", "Checks system for node requirements and installs missing")
nodePreflight.Flag("force", "installs missing dependencies without prompt").Default("false").BoolVar(&NodeOpts.ForceDepInstall)
nodePreflight.Flag("config", "configuration file for the node").Default("./config.json").StringVar(&NodeOpts.ConfigFilepath)
nodePreflight.Flag("init", "creates the configuration file if it does not exist").EnumVar(&NodeOpts.PreflightInit, "sandbox", "nosandbox")
}

func RunNodeUp(ctx context.Context, logger *slog.Logger) error {
Expand Down

0 comments on commit 316166b

Please sign in to comment.