Skip to content

Commit

Permalink
Adding preflight command to check deps
Browse files Browse the repository at this point in the history
  • Loading branch information
autodidaddict committed Nov 28, 2023
1 parent 3443dd2 commit 10bbfe1
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
10 changes: 8 additions & 2 deletions nex-node/cmd/nex-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@ func main() {
switch cmd {
case "up":
cmdUp(opts, ctx, cancel, log)
<-ctx.Done()
case "preflight":
cmdPreflight(opts, ctx, cancel, log)
}

<-ctx.Done()
}

func cmdUp(opts *nexnode.CliOptions, ctx context.Context, cancel context.CancelFunc, log *logrus.Logger) {
Expand Down Expand Up @@ -112,6 +111,13 @@ func cmdUp(opts *nexnode.CliOptions, ctx context.Context, cancel context.CancelF
}

func cmdPreflight(opts *nexnode.CliOptions, ctx context.Context, cancel context.CancelFunc, log *logrus.Logger) {
config, err := nexnode.LoadNodeConfiguration(opts.NodeConfigFile)
if err != nil {
fmt.Printf("Failed to load configuration file: %s\n", err)
os.Exit(1)
}

nexnode.CheckPreRequisites(config)
}

// TODO : look into also pre-removing /var/lib/cni/networks/fcnet/ during startup sequence
Expand Down
85 changes: 85 additions & 0 deletions nex-node/prereq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package nexnode

import (
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"

"github.com/fatih/color"

_ "embed"
)

var (
requiredPlugins = [...]fileSpec{
{name: "/opt/cni/bin/host-local", description: "host-local CNI plugin"},
{name: "/opt/cni/bin/ptp", description: "ptp CNI plugin"},
{name: "/opt/cni/bin/tc-redirect-tap", description: "tc-redirect-tap CNI plugin"},
}
)

type fileSpec struct {
name string
description string
}

func CheckPreRequisites(config *NodeConfiguration) {
cyan := color.New(color.FgCyan).SprintFunc()
red := color.New(color.FgRed).SprintFunc()

fmt.Printf("\nChecking for required files and dependencies...\n\n")
for _, p := range requiredPlugins {
if _, err := os.Stat(p.name); errors.Is(err, os.ErrNotExist) {
fmt.Printf("⛔ %s was not found (%s)\n", p.description, red(p.name))
} else {
fmt.Printf("✅ %s found (%s)\n", p.description, cyan(p.name))
}
}

cniConfig := fmt.Sprintf("/etc/cni/conf.d/%s.conflist", config.CNI.NetworkName)
if _, err := os.Stat(cniConfig); errors.Is(err, os.ErrNotExist) {
err = writeCniConf(cniConfig, config.CNI.NetworkName)
if err != nil {
fmt.Printf("⛔ CNI network configuration file not found (%s), Unable to write a default\n", red(cniConfig))
} else {
fmt.Printf("✅ Created a default CNI configuration for %s (%s)\n", cyan(config.CNI.NetworkName), cyan(cniConfig))
}

} else {
fmt.Printf("✅ CNI network configuration file found (%s)\n", cyan(cniConfig))
}

firecrackerBinary, err := exec.LookPath("firecracker")
if err != nil {
fmt.Println("⛔ Could not locate the 'firecracker' binary in path")
} else {
fmt.Printf("✅ Located the firecracker executable in path (%s)\n", cyan(firecrackerBinary))
}
}

func writeCniConf(fileName string, networkName string) error {
f, err := os.Create(fileName)
if err != nil {
return err
}

var fcConfig map[string]interface{}
err = json.Unmarshal(defaultFcNetConf, &fcConfig)
if err != nil {
return err
}
fcConfig["name"] = networkName
raw, _ := json.Marshal(fcConfig)

_, err = f.Write(raw)
if err != nil {
return nil
}
f.Close()
return nil
}

//go:embed templates/fcnet.conflist
var defaultFcNetConf []byte
18 changes: 18 additions & 0 deletions nex-node/templates/fcnet.conflist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "fcnet",
"cniVersion": "0.4.0",
"plugins": [
{
"type": "ptp",
"ipMasq": true,
"ipam": {
"type": "host-local",
"subnet": "192.168.127.0/24",
"resolvConf": "/etc/resolv.conf"
}
},
{
"type": "tc-redirect-tap"
}
]
}

0 comments on commit 10bbfe1

Please sign in to comment.