Skip to content

Commit fcd2925

Browse files
committed
f
1 parent 120b47c commit fcd2925

File tree

1 file changed

+83
-21
lines changed

1 file changed

+83
-21
lines changed

cmd/installer/cli/install.go

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,11 @@ func preRunInstall(cmd *cobra.Command, flags *InstallCmdFlags) error {
250250

251251
flags.isAirgap = flags.airgapBundle != ""
252252

253-
if err := ensureAdminConsolePassword(flags); err != nil {
254-
return err
253+
// restore command doesn't have a password flag
254+
if cmd.Flags().Lookup("admin-console-password") != nil {
255+
if err := ensureAdminConsolePassword(flags); err != nil {
256+
return err
257+
}
255258
}
256259

257260
if flags.guidedUI {
@@ -449,11 +452,88 @@ func waitForInstallAPI(ctx context.Context, addr string) error {
449452
}
450453
}
451454

455+
func newAPIClient(flags InstallCmdFlags) (apiclient.Client, error) {
456+
apiClient := apiclient.New(fmt.Sprintf("http://localhost:%d", flags.managerPort))
457+
if err := apiClient.Login(flags.adminConsolePassword); err != nil {
458+
return nil, fmt.Errorf("unable to login to api: %w", err)
459+
}
460+
return apiClient, nil
461+
}
462+
463+
func setupStatusUpdates(ctx context.Context, flags InstallCmdFlags, successChan <-chan struct{}) (chan spinner.GlobalMessage, error) {
464+
if !flags.guidedUI {
465+
return nil, nil
466+
}
467+
468+
statusChan := make(chan spinner.GlobalMessage)
469+
spinner.SetGlobalChannel(statusChan)
470+
471+
apiClient, err := newAPIClient(flags)
472+
if err != nil {
473+
return nil, fmt.Errorf("unable to create api client: %w", err)
474+
}
475+
476+
// start a goroutine to handle status updates
477+
go func(apiClient apiclient.Client) {
478+
for {
479+
select {
480+
case <-ctx.Done():
481+
return
482+
case <-successChan:
483+
status := apitypes.InstallationStatus{
484+
State: apitypes.InstallationStateSucceeded,
485+
Description: "Install Complete",
486+
LastUpdated: time.Now(),
487+
}
488+
if _, err := apiClient.SetInstallStatus(status); err != nil {
489+
logrus.Errorf("Unable to update installation status: %v", err)
490+
}
491+
return
492+
case msg := <-statusChan:
493+
status := apitypes.InstallationStatus{
494+
State: apitypes.InstallationStateRunning,
495+
Description: msg.Message,
496+
LastUpdated: time.Now(),
497+
}
498+
if msg.Err {
499+
status.State = apitypes.InstallationStateFailed
500+
}
501+
if _, err := apiClient.SetInstallStatus(status); err != nil {
502+
logrus.Errorf("Unable to update installation status: %v", err)
503+
}
504+
}
505+
}
506+
}(apiClient)
507+
508+
return statusChan, nil
509+
}
510+
511+
func markUIInstallComplete(statusChan chan spinner.GlobalMessage) {
512+
// Send final success message before closing
513+
statusChan <- spinner.GlobalMessage{
514+
Message: "Install Complete",
515+
Err: false,
516+
}
517+
close(statusChan)
518+
}
519+
452520
func runInstall(ctx context.Context, name string, flags InstallCmdFlags, metricsReporter preflights.MetricsReporter) error {
453521
if err := runInstallVerifyAndPrompt(ctx, name, &flags); err != nil {
454522
return err
455523
}
456524

525+
apiClient, err := newAPIClient(flags)
526+
if err != nil {
527+
return fmt.Errorf("unable to create api client: %w", err)
528+
}
529+
530+
// set up status update channel for guided UI mode
531+
statusChan, err := setupStatusUpdates(flags, apiClient)
532+
if err != nil {
533+
return fmt.Errorf("unable to setup status updates: %w", err)
534+
}
535+
defer close(statusChan)
536+
457537
logrus.Debug("initializing install")
458538
if err := initializeInstall(ctx, flags); err != nil {
459539
return fmt.Errorf("unable to initialize install: %w", err)
@@ -571,9 +651,7 @@ func runInstall(ctx context.Context, name string, flags InstallCmdFlags, metrics
571651
}
572652

573653
if flags.guidedUI {
574-
if err := markUIInstallComplete(flags.adminConsolePassword, flags.managerPort); err != nil {
575-
return fmt.Errorf("unable to mark ui install complete: %w", err)
576-
}
654+
markUIInstallComplete(statusChan)
577655
} else {
578656
if err := printSuccessMessage(flags.license, flags.networkInterface); err != nil {
579657
return err
@@ -583,22 +661,6 @@ func runInstall(ctx context.Context, name string, flags InstallCmdFlags, metrics
583661
return nil
584662
}
585663

586-
func markUIInstallComplete(password string, managerPort int) error {
587-
apiClient := apiclient.New(fmt.Sprintf("http://localhost:%d", managerPort))
588-
if err := apiClient.Login(password); err != nil {
589-
return fmt.Errorf("unable to login: %w", err)
590-
}
591-
_, err := apiClient.SetInstallStatus(apitypes.InstallationStatus{
592-
State: apitypes.InstallationStateSucceeded,
593-
Description: "Install Complete",
594-
LastUpdated: time.Now(),
595-
})
596-
if err != nil {
597-
return fmt.Errorf("unable to set install status: %w", err)
598-
}
599-
return nil
600-
}
601-
602664
func runInstallVerifyAndPrompt(ctx context.Context, name string, flags *InstallCmdFlags) error {
603665
logrus.Debugf("checking if k0s is already installed")
604666
err := verifyNoInstallation(name, "reinstall")

0 commit comments

Comments
 (0)