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

Fix for NPE in superuserpassword #129

Merged
merged 2 commits into from
May 31, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions cmd/bios.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ 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 {
func (h *hammer) ConfigureBIOS() error {
if h.hal.Board().VM {
return nil
}

reboot, err := h.Hal.ConfigureBIOS()
reboot, err := h.hal.ConfigureBIOS()
if err != nil {
return err
}
h.log.Info("bios", "message", "successfully configured BIOS")

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()
Expand All @@ -36,12 +36,12 @@ 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 {
func (h *hammer) EnsureBootOrder(bootloaderID string) error {
if h.hal.Board().VM {
return nil
}

err := h.Hal.EnsureBootOrder(bootloaderID)
err := h.hal.EnsureBootOrder(bootloaderID)
if err != nil {
return err
}
Expand Down
32 changes: 16 additions & 16 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ 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)
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 {
return nil, err
}

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
}
Expand All @@ -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)
Expand Down Expand Up @@ -148,10 +148,10 @@ 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)
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) {
Expand All @@ -176,8 +176,8 @@ func (h *Hammer) writeLVMLocalConf() error {
return nil
}

func (h *Hammer) writeUserData(machine *models.V1MachineResponse) error {
configdir := path.Join(h.ChrootPrefix, "etc", "metal")
func (h *hammer) writeUserData(machine *models.V1MachineResponse) error {
configdir := path.Join(h.chrootPrefix, "etc", "metal")
destination := path.Join(configdir, "userdata")

base64UserData := machine.Allocation.UserData
Expand All @@ -192,9 +192,9 @@ 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")
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)
Expand Down Expand Up @@ -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),
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/reinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ 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

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)
Expand Down
47 changes: 22 additions & 25 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,17 @@ import (
"github.com/metal-stack/v"
)

// Hammer is the machine which forms a bare metal to a working server
type Hammer struct {
Spec *Specification
// hammer is the machine which forms a bare metal to a working server
type hammer struct {
log *slog.Logger
Hal hal.InBand
MetalAPIClient *MetalAPIClient
EventEmitter *event.EventEmitter
LLDPClient *network.LLDPClient
FilesystemLayout *models.V1FilesystemLayoutResponse
spec *Specification
hal hal.InBand
metalAPIClient *MetalAPIClient
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field was deleted

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
Expand All @@ -56,22 +53,22 @@ func Run(log *slog.Logger, spec *Specification, hal hal.InBand) (*event.EventEmi
return eventEmitter, err
}

hammer := &Hammer{
Hal: hal,
Spec: spec,
hammer := &hammer{
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.
go kernel.AutoReboot(log, 1*24*time.Hour, 24*time.Hour, func() {
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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -149,12 +146,12 @@ 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
}

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)
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions cmd/supwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ 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)
resp, err := h.metalAPIClient.BootService().SuperUserPassword(context.Background(), req)
if err != nil {
return fmt.Errorf("failed to fetch SuperUser password %w", err)
}
Expand All @@ -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 {
Expand Down
Loading