From 2ff1ba07c7cda65d6f20f3e0858429e5d775de11 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Fri, 31 May 2024 14:37:39 +0200 Subject: [PATCH 1/2] Fix for NPE in superuserpassword --- cmd/bios.go | 10 +++++----- cmd/install.go | 20 ++++++++++---------- cmd/reinstall.go | 2 +- cmd/root.go | 39 ++++++++++++++++++--------------------- cmd/supwd.go | 4 ++-- 5 files changed, 36 insertions(+), 39 deletions(-) diff --git a/cmd/bios.go b/cmd/bios.go index 96fe40e..6c08b2a 100644 --- a/cmd/bios.go +++ b/cmd/bios.go @@ -10,11 +10,11 @@ import ( // ConfigureBIOS ensures that UEFI boot is enabled and CSM-support is disabled. // It then reboots the machine. func (h *Hammer) ConfigureBIOS() error { - if h.Hal.Board().VM { + if h.hal.Board().VM { return nil } - reboot, err := h.Hal.ConfigureBIOS() + reboot, err := h.hal.ConfigureBIOS() if err != nil { return err } @@ -22,7 +22,7 @@ func (h *Hammer) ConfigureBIOS() error { if reboot { msg := "BIOS configuration requires a reboot" - h.EventEmitter.Emit(event.ProvisioningEventPlannedReboot, msg) + h.eventEmitter.Emit(event.ProvisioningEventPlannedReboot, msg) h.log.Info("bios", msg, "reboot in 1 sec") time.Sleep(1 * time.Second) err = kernel.Reboot() @@ -37,11 +37,11 @@ func (h *Hammer) ConfigureBIOS() error { // EnsureBootOrder ensures that the BIOS boot order is properly set, // i.e. first boot from OS image and then PXE boot func (h *Hammer) EnsureBootOrder(bootloaderID string) error { - if h.Hal.Board().VM { + if h.hal.Board().VM { return nil } - err := h.Hal.EnsureBootOrder(bootloaderID) + err := h.hal.EnsureBootOrder(bootloaderID) if err != nil { return err } diff --git a/cmd/install.go b/cmd/install.go index 80879cc..3b8a679 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -23,7 +23,7 @@ import ( // Install a given image to the disk by using genuinetools/img func (h *Hammer) Install(machine *models.V1MachineResponse) (*api.Bootinfo, error) { - s := storage.New(h.log, h.ChrootPrefix, *h.FilesystemLayout) + s := storage.New(h.log, h.chrootPrefix, *h.filesystemLayout) err := s.Run() if err != nil { return nil, err @@ -31,17 +31,17 @@ func (h *Hammer) Install(machine *models.V1MachineResponse) (*api.Bootinfo, erro image := machine.Allocation.Image.URL - err = img.NewImage(h.log).Pull(image, h.OsImageDestination) + err = img.NewImage(h.log).Pull(image, h.osImageDestination) if err != nil { return nil, err } - err = img.NewImage(h.log).Burn(h.ChrootPrefix, image, h.OsImageDestination) + err = img.NewImage(h.log).Burn(h.chrootPrefix, image, h.osImageDestination) if err != nil { return nil, err } - info, err := h.install(h.ChrootPrefix, machine, s.RootUUID) + info, err := h.install(h.chrootPrefix, machine, s.RootUUID) if err != nil { return nil, err } @@ -150,8 +150,8 @@ func (h *Hammer) install(prefix string, machine *models.V1MachineResponse, rootU // will only be written if lvm is installed in the target image func (h *Hammer) writeLVMLocalConf() error { srclvmlocal := "/etc/lvm/lvmlocal.conf" - dstlvm := path.Join(h.ChrootPrefix, "/etc/lvm") - dstlvmlocal := path.Join(h.ChrootPrefix, srclvmlocal) + dstlvm := path.Join(h.chrootPrefix, "/etc/lvm") + dstlvmlocal := path.Join(h.chrootPrefix, srclvmlocal) _, err := os.Stat(srclvmlocal) // FIXME use fileExists below if os.IsNotExist(err) { @@ -177,7 +177,7 @@ func (h *Hammer) writeLVMLocalConf() error { } func (h *Hammer) writeUserData(machine *models.V1MachineResponse) error { - configdir := path.Join(h.ChrootPrefix, "etc", "metal") + configdir := path.Join(h.chrootPrefix, "etc", "metal") destination := path.Join(configdir, "userdata") base64UserData := machine.Allocation.UserData @@ -194,7 +194,7 @@ func (h *Hammer) writeUserData(machine *models.V1MachineResponse) error { func (h *Hammer) writeInstallerConfig(machine *models.V1MachineResponse, rootUUiD string) error { h.log.Info("write installation configuration") - configdir := path.Join(h.ChrootPrefix, "etc", "metal") + configdir := path.Join(h.chrootPrefix, "etc", "metal") err := os.MkdirAll(configdir, 0755) if err != nil { return fmt.Errorf("mkdir of %s target os failed %w", configdir, err) @@ -223,8 +223,8 @@ func (h *Hammer) writeInstallerConfig(machine *models.V1MachineResponse, rootUUi Hostname: *alloc.Hostname, SSHPublicKey: sshPubkeys, Networks: alloc.Networks, - MachineUUID: h.Spec.MachineUUID, - Password: h.Spec.ConsolePassword, + MachineUUID: h.spec.MachineUUID, + Password: h.spec.ConsolePassword, Console: console, Timestamp: time.Now().Format(time.RFC3339), Nics: h.onlyNicsWithNeighbors(machine.Hardware.Nics), diff --git a/cmd/reinstall.go b/cmd/reinstall.go index 70ba8b0..ff96ae6 100644 --- a/cmd/reinstall.go +++ b/cmd/reinstall.go @@ -16,7 +16,7 @@ func (h *Hammer) abortReinstall(reason error, machineID string, primaryDiskWiped ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - resp, err := h.MetalAPIClient.BootService().AbortReinstall(ctx, &v1.BootServiceAbortReinstallRequest{Uuid: machineID, PrimaryDiskWiped: primaryDiskWiped}) + resp, err := h.metalAPIClient.BootService().AbortReinstall(ctx, &v1.BootServiceAbortReinstallRequest{Uuid: machineID, PrimaryDiskWiped: primaryDiskWiped}) if err != nil { h.log.Error("failed to abort reinstall", "error", err) time.Sleep(5 * time.Second) diff --git a/cmd/root.go b/cmd/root.go index b52e5aa..38b8132 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,18 +22,15 @@ import ( // Hammer is the machine which forms a bare metal to a working server type Hammer struct { - Spec *Specification log *slog.Logger - Hal hal.InBand - MetalAPIClient *MetalAPIClient - EventEmitter *event.EventEmitter - LLDPClient *network.LLDPClient - FilesystemLayout *models.V1FilesystemLayoutResponse + spec *Specification + hal hal.InBand + metalAPIClient *MetalAPIClient + eventEmitter *event.EventEmitter + filesystemLayout *models.V1FilesystemLayoutResponse // IPAddress is the ip of the eth0 interface during installation - IPAddress string - Started time.Time - ChrootPrefix string - OsImageDestination string + chrootPrefix string + osImageDestination string } // Run orchestrates the whole register/wipe/format/burn and reboot process @@ -57,13 +54,13 @@ func Run(log *slog.Logger, spec *Specification, hal hal.InBand) (*event.EventEmi } hammer := &Hammer{ - Hal: hal, - Spec: spec, + hal: hal, + spec: spec, log: log, - IPAddress: spec.IP, - EventEmitter: eventEmitter, - ChrootPrefix: "/rootfs", - OsImageDestination: "/tmp/os.tgz", + eventEmitter: eventEmitter, + chrootPrefix: "/rootfs", + osImageDestination: "/tmp/os.tgz", + metalAPIClient: metalAPIClient, } // Reboot after 24Hours if no allocation was requested. @@ -71,7 +68,7 @@ func Run(log *slog.Logger, spec *Specification, hal hal.InBand) (*event.EventEmi eventEmitter.Emit(event.ProvisioningEventPlannedReboot, "autoreboot after 24h") }) - hammer.Spec.ConsolePassword = password.Generate(16) + hammer.spec.ConsolePassword = password.Generate(16) err = hammer.createBmcSuperuser() if err != nil { @@ -111,7 +108,7 @@ func Run(log *slog.Logger, spec *Specification, hal hal.InBand) (*event.EventEmi } m := resp.Payload if m != nil && m.Allocation != nil && m.Allocation.Reinstall != nil && *m.Allocation.Reinstall { - hammer.FilesystemLayout = m.Allocation.Filesystemlayout + hammer.filesystemLayout = m.Allocation.Filesystemlayout primaryDiskWiped := false if m.Allocation.Image == nil || m.Allocation.Image.ID == nil { err = fmt.Errorf("no image specified") @@ -149,7 +146,7 @@ func Run(log *slog.Logger, spec *Specification, hal hal.InBand) (*event.EventEmi m = resp.Payload log.Info("perform install", "machineID", m.ID, "imageID", *m.Allocation.Image.ID) - hammer.FilesystemLayout = m.Allocation.Filesystemlayout + hammer.filesystemLayout = m.Allocation.Filesystemlayout err = hammer.installImage(eventEmitter, bootService, m) return eventEmitter, err } @@ -165,9 +162,9 @@ func (h *Hammer) installImage(eventEmitter *event.EventEmitter, bootService v1.B } rep := &report.Report{ - MachineUUID: h.Spec.MachineUUID, + MachineUUID: h.spec.MachineUUID, Client: bootService, - ConsolePassword: h.Spec.ConsolePassword, + ConsolePassword: h.spec.ConsolePassword, Initrd: info.Initrd, Cmdline: info.Cmdline, Kernel: info.Kernel, diff --git a/cmd/supwd.go b/cmd/supwd.go index eaf4065..3b175e5 100644 --- a/cmd/supwd.go +++ b/cmd/supwd.go @@ -11,7 +11,7 @@ import ( // createBmcSuperuser creates the bmc super user. func (h *Hammer) createBmcSuperuser() error { req := &v1.BootServiceSuperUserPasswordRequest{} - resp, err := h.MetalAPIClient.BootService().SuperUserPassword(context.Background(), req) + resp, err := h.metalAPIClient.BootService().SuperUserPassword(context.Background(), req) if err != nil { return fmt.Errorf("failed to fetch SuperUser password %w", err) } @@ -21,7 +21,7 @@ func (h *Hammer) createBmcSuperuser() error { return nil } - bmcConn := h.Hal.BMCConnection() + bmcConn := h.hal.BMCConnection() err = bmcConn.CreateUser(bmcConn.SuperUser(), api.AdministratorPrivilege, resp.SuperUserPassword) if err != nil { From ff3baf00c036f5d67f5e7d8905e6af22b98a5d0f Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Fri, 31 May 2024 14:39:44 +0200 Subject: [PATCH 2/2] unexport hammer --- cmd/bios.go | 4 ++-- cmd/install.go | 12 ++++++------ cmd/install_test.go | 2 +- cmd/reinstall.go | 2 +- cmd/root.go | 8 ++++---- cmd/supwd.go | 2 +- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/cmd/bios.go b/cmd/bios.go index 6c08b2a..2105159 100644 --- a/cmd/bios.go +++ b/cmd/bios.go @@ -9,7 +9,7 @@ import ( // ConfigureBIOS ensures that UEFI boot is enabled and CSM-support is disabled. // It then reboots the machine. -func (h *Hammer) ConfigureBIOS() error { +func (h *hammer) ConfigureBIOS() error { if h.hal.Board().VM { return nil } @@ -36,7 +36,7 @@ func (h *Hammer) ConfigureBIOS() error { // EnsureBootOrder ensures that the BIOS boot order is properly set, // i.e. first boot from OS image and then PXE boot -func (h *Hammer) EnsureBootOrder(bootloaderID string) error { +func (h *hammer) EnsureBootOrder(bootloaderID string) error { if h.hal.Board().VM { return nil } diff --git a/cmd/install.go b/cmd/install.go index 3b8a679..1b30a0d 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -22,7 +22,7 @@ import ( ) // Install a given image to the disk by using genuinetools/img -func (h *Hammer) Install(machine *models.V1MachineResponse) (*api.Bootinfo, error) { +func (h *hammer) Install(machine *models.V1MachineResponse) (*api.Bootinfo, error) { s := storage.New(h.log, h.chrootPrefix, *h.filesystemLayout) err := s.Run() if err != nil { @@ -60,7 +60,7 @@ func (h *Hammer) Install(machine *models.V1MachineResponse) (*api.Bootinfo, erro // install will execute /install.sh in the pulled docker image which was extracted onto disk // to finish installation e.g. install mbr, grub, write network and filesystem config -func (h *Hammer) install(prefix string, machine *models.V1MachineResponse, rootUUID string) (*api.Bootinfo, error) { +func (h *hammer) install(prefix string, machine *models.V1MachineResponse, rootUUID string) (*api.Bootinfo, error) { h.log.Info("install", "image", machine.Allocation.Image.URL) err := h.writeInstallerConfig(machine, rootUUID) @@ -148,7 +148,7 @@ func (h *Hammer) install(prefix string, machine *models.V1MachineResponse, rootU // writeLVMLocalConf to make lvm more compatible with os without udevd // will only be written if lvm is installed in the target image -func (h *Hammer) writeLVMLocalConf() error { +func (h *hammer) writeLVMLocalConf() error { srclvmlocal := "/etc/lvm/lvmlocal.conf" dstlvm := path.Join(h.chrootPrefix, "/etc/lvm") dstlvmlocal := path.Join(h.chrootPrefix, srclvmlocal) @@ -176,7 +176,7 @@ func (h *Hammer) writeLVMLocalConf() error { return nil } -func (h *Hammer) writeUserData(machine *models.V1MachineResponse) error { +func (h *hammer) writeUserData(machine *models.V1MachineResponse) error { configdir := path.Join(h.chrootPrefix, "etc", "metal") destination := path.Join(configdir, "userdata") @@ -192,7 +192,7 @@ func (h *Hammer) writeUserData(machine *models.V1MachineResponse) error { return nil } -func (h *Hammer) writeInstallerConfig(machine *models.V1MachineResponse, rootUUiD string) error { +func (h *hammer) writeInstallerConfig(machine *models.V1MachineResponse, rootUUiD string) error { h.log.Info("write installation configuration") configdir := path.Join(h.chrootPrefix, "etc", "metal") err := os.MkdirAll(configdir, 0755) @@ -242,7 +242,7 @@ func (h *Hammer) writeInstallerConfig(machine *models.V1MachineResponse, rootUUi return os.WriteFile(destination, yamlContent, 0600) } -func (h *Hammer) onlyNicsWithNeighbors(nics []*models.V1MachineNic) []*models.V1MachineNic { +func (h *hammer) onlyNicsWithNeighbors(nics []*models.V1MachineNic) []*models.V1MachineNic { noNeighbors := func(neighbors []*models.V1MachineNic) bool { if len(neighbors) == 0 { return true diff --git a/cmd/install_test.go b/cmd/install_test.go index f4ab377..c1f302b 100644 --- a/cmd/install_test.go +++ b/cmd/install_test.go @@ -45,7 +45,7 @@ func TestHammer_onlyNicsWithNeighbors(t *testing.T) { for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { - h := &Hammer{ + h := &hammer{ log: slog.Default(), } if got := h.onlyNicsWithNeighbors(tt.nics); !reflect.DeepEqual(got, tt.want) { diff --git a/cmd/reinstall.go b/cmd/reinstall.go index ff96ae6..e594b42 100644 --- a/cmd/reinstall.go +++ b/cmd/reinstall.go @@ -9,7 +9,7 @@ import ( "github.com/metal-stack/metal-hammer/pkg/kernel" ) -func (h *Hammer) abortReinstall(reason error, machineID string, primaryDiskWiped bool) error { +func (h *hammer) abortReinstall(reason error, machineID string, primaryDiskWiped bool) error { h.log.Error("reinstall cancelled => boot into existing OS...", "reason", reason) var bootInfo *kernelapi.Bootinfo diff --git a/cmd/root.go b/cmd/root.go index 38b8132..28da520 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -20,8 +20,8 @@ import ( "github.com/metal-stack/v" ) -// Hammer is the machine which forms a bare metal to a working server -type Hammer struct { +// hammer is the machine which forms a bare metal to a working server +type hammer struct { log *slog.Logger spec *Specification hal hal.InBand @@ -53,7 +53,7 @@ func Run(log *slog.Logger, spec *Specification, hal hal.InBand) (*event.EventEmi return eventEmitter, err } - hammer := &Hammer{ + hammer := &hammer{ hal: hal, spec: spec, log: log, @@ -151,7 +151,7 @@ func Run(log *slog.Logger, spec *Specification, hal hal.InBand) (*event.EventEmi return eventEmitter, err } -func (h *Hammer) installImage(eventEmitter *event.EventEmitter, bootService v1.BootServiceClient, m *models.V1MachineResponse) error { +func (h *hammer) installImage(eventEmitter *event.EventEmitter, bootService v1.BootServiceClient, m *models.V1MachineResponse) error { eventEmitter.Emit(event.ProvisioningEventInstalling, "start installation") installationStart := time.Now() info, err := h.Install(m) diff --git a/cmd/supwd.go b/cmd/supwd.go index 3b175e5..de193a5 100644 --- a/cmd/supwd.go +++ b/cmd/supwd.go @@ -9,7 +9,7 @@ import ( ) // createBmcSuperuser creates the bmc super user. -func (h *Hammer) createBmcSuperuser() error { +func (h *hammer) createBmcSuperuser() error { req := &v1.BootServiceSuperUserPasswordRequest{} resp, err := h.metalAPIClient.BootService().SuperUserPassword(context.Background(), req) if err != nil {