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

feat(tools): create prototype for inventory management #39

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions cmd/hwctl/enroll.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"fmt"

"github.com/spf13/cobra"
tinkv1 "github.com/tinkerbell/tink/api/v1alpha2"
"gopkg.in/yaml.v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
LabelCores = "baremetal.nicklasfrahm.dev/cpus"
LabelMemory = "baremetal.nicklasfrahm.dev/memory"
LabelManufacturer = "baremetal.nicklasfrahm.dev/manufacturer"
LabelModel = "baremetal.nicklasfrahm.dev/model"
)

var cpus int
var memoryGB int
var manufacturer string
var model string

var enrollCmd = &cobra.Command{
Use: "enroll",
Short: "Enroll a new server",
Long: `Enroll a new piece of hardware.

This command will create a configuration
file for the new server in the repository
and generate a random passphrase for the
encrypted disk.`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
serverName := args[0]

vendorData := fmt.Sprintf(`{
"cpus": %d,
"memory": %d,
"manufacturer": "%s",
"model": "%s"
}`, cpus, memoryGB, manufacturer, model)

hw := tinkv1.Hardware{
ObjectMeta: metav1.ObjectMeta{
Name: serverName,
Labels: map[string]string{
LabelCores: fmt.Sprint(cpus),
LabelMemory: fmt.Sprint(memoryGB),
LabelManufacturer: manufacturer,
LabelModel: model,
},
},
Spec: tinkv1.HardwareSpec{
NetworkInterfaces: tinkv1.NetworkInterfaces{
"00:e0:4c:88:00:f1": tinkv1.NetworkInterface{},
},
Instance: &tinkv1.Instance{
Vendordata: &vendorData,
},
},
}

yamlBytes, err := yaml.Marshal(hw)
if err != nil {
return err
}

fmt.Printf("Enrolling new server:\n%s\n", string(yamlBytes))

return nil
},
}

func init() {
enrollCmd.Flags().IntVarP(&cpus, "cpus", "c", 4, "number of CPU cores")
enrollCmd.MarkFlagRequired("cpus")
enrollCmd.Flags().IntVarP(&memoryGB, "memory", "m", 16, "amount of memory in GB")
enrollCmd.MarkFlagRequired("memory")
enrollCmd.Flags().StringVarP(&manufacturer, "manufacturer", "M", "raspberrypi", "manufacturer of the server")
enrollCmd.MarkFlagRequired("manufacturer")
enrollCmd.Flags().StringVarP(&model, "model", "B", "4b", "model of the server")
enrollCmd.MarkFlagRequired("model")

rootCmd.AddCommand(enrollCmd)
}
46 changes: 46 additions & 0 deletions cmd/hwctl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"os"

"github.com/spf13/cobra"
)

var version = "dev"
var help bool

var rootCmd = &cobra.Command{
Use: "hwctl",
Short: "A command line tool to manage physical servers.",
Long: ` _ _ _
| |____ _____| |_| |
| '_ \ \ /\ / / __| __| |
| | | \ V V / (__| |_| |
|_| |_|\_/\_/ \___|\__|_|

A command line tool to manage a physical server fleet.
The tool provides helpers to manage an inventory and
credentials for the servers.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if help {
cmd.Help()
os.Exit(0)
}
},
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
os.Exit(0)
},
Version: version,
SilenceUsage: true,
}

func init() {
rootCmd.PersistentFlags().BoolVarP(&help, "help", "h", false, "display help for command")
}

func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
12 changes: 12 additions & 0 deletions configs/hardware/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Hardware

This directory contains the hardware configuration files for the physical servers that host my infrastructure.

## Usage

- **Enroll a new server**
To register a new piece of hardware, run the following command from within this repository.

```bash
hwctl enroll alpha -c 4 -m 16
```
7 changes: 7 additions & 0 deletions configs/hardware/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: nicklasfrahm.dev/v1alpha1
kind: Hardware
metadata:
name: example
spec:
cores: 4
memory: 8Gi
39 changes: 27 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ require (
github.com/gofiber/fiber/v2 v2.45.0
github.com/pulumi/pulumi-cloudflare/sdk/v5 v5.0.0
github.com/pulumi/pulumi/sdk/v3 v3.59.1
github.com/spf13/cobra v1.7.0
github.com/tinkerbell/tink v0.9.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/apimachinery v0.28.2
)

require (
Expand All @@ -24,41 +27,45 @@ require (
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
github.com/go-git/go-git/v5 v5.6.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.1.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/leodido/go-urn v1.2.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/opentracing/basictracer-go v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/term v1.1.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 // indirect
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0 // indirect
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/skeema/knownhosts v1.1.0 // indirect
github.com/spf13/cobra v1.6.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/texttheater/golang-levenshtein v1.0.1 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
Expand All @@ -70,17 +77,25 @@ require (
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20230323212658-478b75c54725 // indirect
google.golang.org/grpc v1.54.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/tools v0.9.3 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.56.2 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/api v0.27.3 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
lukechampine.com/frand v1.4.2 // indirect
sigs.k8s.io/controller-runtime v0.15.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sourcegraph.com/sourcegraph/appdash v0.0.0-20211028080628-e2786a622600 // indirect
)
Loading