diff --git a/cmd/prepNode.go b/cmd/prepNode.go index b48fae2..5211f25 100644 --- a/cmd/prepNode.go +++ b/cmd/prepNode.go @@ -68,7 +68,8 @@ func init() { prepNodeCmd.Flags().StringVar(&util.KubeVersion, "kube-version", "", "Specific version of pf9-kube to install") prepNodeCmd.Flags().MarkHidden("kube-version") prepNodeCmd.Flags().BoolVar(&util.CheckIfOnboarded, "skip-connected", false, "If the node is already connected to the PMK control plane, prep-node will be skipped") - + prepNodeCmd.Flags().BoolVar(&util.SkipInstallerDownload, "skip-installer-download", false, "If set then installer will not be downloaded") + prepNodeCmd.Flags().MarkHidden("skip-installer-download") rootCmd.AddCommand(prepNodeCmd) } diff --git a/pkg/pmk/node.go b/pkg/pmk/node.go index feb70d7..08cd4c5 100644 --- a/pkg/pmk/node.go +++ b/pkg/pmk/node.go @@ -31,6 +31,11 @@ const ( HostAgentLegacy = 404 ) +var ( + // Building our new spinner + s = spinner.New(spinner.CharSets[9], 100*time.Millisecond) +) + // Sends an event to segment based on the input string and uses auth as keystone UUID property. func sendSegmentEvent(allClients client.Client, eventStr string, auth keystone.KeystoneAuth, isError bool) { @@ -58,8 +63,6 @@ func sendSegmentEvent(allClients client.Client, eventStr string, auth keystone.K // PrepNode sets up prerequisites for k8s stack func PrepNode(ctx objects.Config, allClients client.Client, auth keystone.KeystoneAuth) error { - // Building our new spinner - s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) s.Color("red") zap.S().Debug("Received a call to start preparing node(s).") @@ -109,7 +112,7 @@ func PrepNode(ctx objects.Config, allClients client.Client, auth keystone.Keysto } sendSegmentEvent(allClients, "Installing hostagent - 2", auth, false) - s.Suffix = " Downloading the Hostagent (this might take a few minutes...)" + if err := installHostAgent(ctx, auth, hostOS, allClients.Executor); err != nil { errStr := "Error: Unable to install hostagent. " + err.Error() sendSegmentEvent(allClients, errStr, auth, true) @@ -264,28 +267,43 @@ func installHostAgent(ctx objects.Config, auth keystone.KeystoneAuth, hostOS str } func installHostAgentCertless(ctx objects.Config, regionURL string, auth keystone.KeystoneAuth, hostOS string, exec cmdexec.Executor) error { - zap.S().Debug("Downloading the installer (this might take a few minutes...)") - - url := fmt.Sprintf( - "https://%s/clarity/platform9-install-%s.sh", - regionURL, hostOS) - insecureDownload := "" - if ctx.AllowInsecure { - insecureDownload = "-k" - } - homeDir, err := createDirToDownloadInstaller(exec) + var err error + homeDir, err = createDirToDownloadInstaller(exec) if err != nil { return err } - cmd := fmt.Sprintf(`curl %s --silent --show-error %s -o %s/pf9/installer.sh`, insecureDownload, url, homeDir) - _, err = exec.RunWithStdout("bash", "-c", cmd) + if !util.SkipInstallerDownload { + s.Suffix = " Downloading the Hostagent (this might take a few minutes...)" + zap.S().Debug("Downloading the installer (this might take a few minutes...)") + err = downloadCertLessInstaller(ctx, regionURL, hostOS, exec) + if err != nil { + return err + } + } else { + s.Suffix = " Skipped Installer Download, Installing hostagent" + zap.S().Debug("User skiped installer download") + } + + err = callCertLessInstaller(ctx, regionURL, auth, exec) + + removeTempDirAndInstaller(exec) + if err != nil { - return err + _, exitCode := cmdexec.ExitCodeChecker(err) + fmt.Printf("\n") + fmt.Println("Error :", util.InstallerErrors[exitCode]) + zap.S().Debugf("Error:%s", util.InstallerErrors[exitCode]) + return fmt.Errorf("error while running installer script: %s", util.InstallerErrors[exitCode]) } - zap.S().Debug("Hostagent download completed successfully") + // TODO: here we actually need additional validation by checking /tmp/agent_install. log + zap.S().Debug("Platform9 packages installed successfully") + return nil +} + +func callCertLessInstaller(ctx objects.Config, regionURL string, auth keystone.KeystoneAuth, exec cmdexec.Executor) error { var installOptions string //Pass keystone token if MFA token is provided @@ -299,11 +317,12 @@ func installHostAgentCertless(ctx objects.Config, regionURL string, auth keyston } changePermission := fmt.Sprintf("chmod +x %s/pf9/installer.sh", homeDir) - _, err = exec.RunWithStdout("bash", "-c", changePermission) + _, err := exec.RunWithStdout("bash", "-c", changePermission) if err != nil { return err } + var cmd string if ctx.ProxyURL != "" { cmd = fmt.Sprintf(`%s/pf9/installer.sh --proxy %s --skip-os-check --no-ntp`, homeDir, ctx.ProxyURL) } else { @@ -317,19 +336,24 @@ func installHostAgentCertless(ctx objects.Config, regionURL string, auth keyston cmd = fmt.Sprintf(`%s %s`, cmd, installOptions) _, err = exec.RunWithStdout("bash", "-c", cmd) } + return err +} - removeTempDirAndInstaller(exec) +func downloadCertLessInstaller(ctx objects.Config, regionURL string, hostOS string, exec cmdexec.Executor) error { + url := fmt.Sprintf( + "https://%s/clarity/platform9-install-%s.sh", + regionURL, hostOS) + insecureDownload := "" + if ctx.AllowInsecure { + insecureDownload = "-k" + } + cmd := fmt.Sprintf(`curl %s --silent --show-error %s -o %s/pf9/installer.sh`, insecureDownload, url, homeDir) + _, err := exec.RunWithStdout("bash", "-c", cmd) if err != nil { - _, exitCode := cmdexec.ExitCodeChecker(err) - fmt.Printf("\n") - fmt.Println("Error :", util.InstallerErrors[exitCode]) - zap.S().Debugf("Error:%s", util.InstallerErrors[exitCode]) - return fmt.Errorf("error while running installer script: %s", util.InstallerErrors[exitCode]) + return err } - - // TODO: here we actually need additional validation by checking /tmp/agent_install. log - zap.S().Debug("Platform9 packages installed successfully") + zap.S().Debug("Hostagent download completed successfully") return nil } @@ -420,36 +444,58 @@ func pf9PackagesPresent(hostOS string, exec cmdexec.Executor) bool { } func installHostAgentLegacy(ctx objects.Config, regionURL string, auth keystone.KeystoneAuth, hostOS string, exec cmdexec.Executor) error { - zap.S().Debug("Downloading Hostagent Installer Legacy") - - url := fmt.Sprintf("https://%s/private/platform9-install-%s.sh", regionURL, hostOS) - homeDir, err := createDirToDownloadInstaller(exec) + var err error + homeDir, err = createDirToDownloadInstaller(exec) if err != nil { return err } - installOptions := fmt.Sprintf("--insecure --project-name=%s 2>&1 | tee -a %s/pf9/agent_install", auth.ProjectID, homeDir) - //use insecure by default - cmd := fmt.Sprintf(`curl --insecure --silent --show-error -H 'X-Auth-Token:%s' %s -o %s/pf9/installer.sh`, auth.Token, url, homeDir) - _, err = exec.RunWithStdout("bash", "-c", cmd) + if !util.SkipInstallerDownload { + s.Suffix = " Downloading the Hostagent (this might take a few minutes...)" + zap.S().Debug("Downloading Hostagent Installer Legacy") + err = downloadLegacyInstaller(regionURL, auth, hostOS, exec) + if err != nil { + return err + } + } else { + s.Suffix = " Skipped Installer Download, Installing hostagent" + zap.S().Debug("User skiped installer download") + } + + err = callLegacyInstaller(ctx, auth, exec) + + removeTempDirAndInstaller(exec) + if err != nil { - return err + _, exitCode := cmdexec.ExitCodeChecker(err) + fmt.Printf("\n") + zap.S().Debugf("Error:%s", util.InstallerErrors[exitCode]) + fmt.Println("Error :", util.InstallerErrors[exitCode]) + return fmt.Errorf("error while running installer script: %s", util.InstallerErrors[exitCode]) } - zap.S().Debug("Hostagent download completed successfully") + // TODO: here we actually need additional validation by checking /tmp/agent_install. log + zap.S().Debug("Hostagent installed successfully") + return nil +} + +func callLegacyInstaller(ctx objects.Config, auth keystone.KeystoneAuth, exec cmdexec.Executor) error { changePermission := fmt.Sprintf("chmod +x %s/pf9/installer.sh", homeDir) - _, err = exec.RunWithStdout("bash", "-c", changePermission) + _, err := exec.RunWithStdout("bash", "-c", changePermission) if err != nil { return err } + var cmd string if ctx.ProxyURL != "" { cmd = fmt.Sprintf(`%s/pf9/installer.sh --proxy %s --skip-os-check --no-ntp`, homeDir, ctx.ProxyURL) } else { cmd = fmt.Sprintf(`%s/pf9/installer.sh --no-proxy --skip-os-check --no-ntp`, homeDir) } + installOptions := fmt.Sprintf("--insecure --project-name=%s 2>&1 | tee -a %s/pf9/agent_install", auth.ProjectID, homeDir) + if IsRemoteExecutor { cmd = fmt.Sprintf(`bash %s %s`, cmd, installOptions) _, err = exec.RunWithStdout(cmd) @@ -457,19 +503,18 @@ func installHostAgentLegacy(ctx objects.Config, regionURL string, auth keystone. cmd = fmt.Sprintf(`%s %s`, cmd, installOptions) _, err = exec.RunWithStdout("bash", "-c", cmd) } + return err +} - removeTempDirAndInstaller(exec) - +func downloadLegacyInstaller(regionURL string, auth keystone.KeystoneAuth, hostOS string, exec cmdexec.Executor) error { + url := fmt.Sprintf("https://%s/private/platform9-install-%s.sh", regionURL, hostOS) + //use insecure by default + cmd := fmt.Sprintf(`curl --insecure --silent --show-error -H 'X-Auth-Token:%s' %s -o %s/pf9/installer.sh`, auth.Token, url, homeDir) + _, err := exec.RunWithStdout("bash", "-c", cmd) if err != nil { - _, exitCode := cmdexec.ExitCodeChecker(err) - fmt.Printf("\n") - zap.S().Debugf("Error:%s", util.InstallerErrors[exitCode]) - fmt.Println("Error :", util.InstallerErrors[exitCode]) - return fmt.Errorf("error while running installer script: %s", util.InstallerErrors[exitCode]) + return err } - - // TODO: here we actually need additional validation by checking /tmp/agent_install. log - zap.S().Debug("Hostagent installed successfully") + zap.S().Debug("Hostagent download completed successfully") return nil } diff --git a/pkg/util/constants.go b/pkg/util/constants.go index 5e8302c..d44638c 100644 --- a/pkg/util/constants.go +++ b/pkg/util/constants.go @@ -14,6 +14,7 @@ var ProcessesList []string // Kubernetes clusters processes list var SwapOffDisabled bool // If this is true the swapOff functionality will be disabled. var SkipPrepNode bool var CheckIfOnboarded bool +var SkipInstallerDownload bool // SkipKube skips authorizing kube role during prep-node. Not applicable to bootstrap command var SkipKube bool