From e77dcfa2f0e34192ed6727ad765a87e04b402ba4 Mon Sep 17 00:00:00 2001 From: ratanasovvmw <86951923+ratanasovvmw@users.noreply.github.com> Date: Mon, 1 Nov 2021 12:23:14 +0200 Subject: [PATCH] CLI for the installer library (#175) CLI for the installer library --- agent/installer/cli.go | 134 ++++++++++++++++++++++++++++++++++++ agent/installer/cli/main.go | 12 ++++ 2 files changed, 146 insertions(+) create mode 100644 agent/installer/cli.go create mode 100644 agent/installer/cli/main.go diff --git a/agent/installer/cli.go b/agent/installer/cli.go new file mode 100644 index 000000000..0a281f835 --- /dev/null +++ b/agent/installer/cli.go @@ -0,0 +1,134 @@ +// Copyright 2021 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package installer + +import ( + "flag" + "fmt" + "k8s.io/klog" + "k8s.io/klog/klogr" +) + +var ( + listFlag = flag.Bool("list", false, "List all supported OS and Kubernetes versions") + listBundlesFlag = flag.Bool("listBundles", false, "List the BYOH Bundle names for all supported OS and Kubernetes versions") + detectOSFlag = flag.Bool("detect", false, "Detects the current operating system") + installFlag = flag.Bool("install", false, "Install a BYOH Bundle") + uninstallFlag = flag.Bool("uninstall", false, "Unnstall a BYOH Bundle") + bundleRepoFlag = flag.String("bundleRepo", "https://projects.registry.vmware.com", "BYOH Bundle Repository. If not set, will look for bundles locally") + k8sFlag = flag.String("k8s", "1.22.1", "Kubernetes version") + osFlag = flag.String("os", "", "OS. If used with install/uninstall, skip os detection") + previewOSChangesFlag = flag.Bool("previewOSChanges", false, "Preview the install and uninstall changes for the specified OS") +) + +const ( + doInstall = true + doUninstall = false +) + +func Main() { + flag.Parse() + + if *listFlag { + list() + } + + if *listBundlesFlag { + listBundles() + } + + if *detectOSFlag { + detectOS() + } + + if *installFlag { + runInstaller(doInstall) + } + + if *uninstallFlag { + runInstaller(doUninstall) + } + + if *previewOSChangesFlag { + previewOSChanges() + } +} + +func list() { + for _, os := range ListSupportedOS() { + for _, k8s := range ListSupportedK8s(os) { + fmt.Printf("%s %s\n", os, k8s) + } + } +} + +func listBundles() { + for _, os := range ListSupportedOS() { + for _, k8s := range ListSupportedK8s(os) { + fmt.Println(GetBundleName(os, k8s)) + } + } +} + +func detectOS() { + osd := osDetector{} + os, err := osd.Detect() + if err != nil { + fmt.Printf("Error detecting OS %s", err) + return + } + + fmt.Printf("Detected OS as: %s", os) +} + +func runInstaller(install bool) { + klog.InitFlags(nil) + klogger := klogr.New() + + if *bundleRepoFlag == "" { + bd := bundleDownloader{"", "."} + fmt.Printf("Bundle repo not specified. Provide bundle contents in %s\n", bd.GetBundleDirPath(*k8sFlag)) + } + + var i *installer + var err error + if *osFlag != "" { + // Override current OS detection + i, err = newUnchecked(*osFlag, *bundleRepoFlag, ".", klogger, &logPrinter{klogger}) + if err != nil { + fmt.Println(err) + return + } + } else { + i, err = New("norepo", ".", klogger) + if err != nil { + fmt.Println(err) + return + } + + // Override preview mode + i.downloadPath = "." + i.repoAddr = *bundleRepoFlag + } + + if install { + err = i.Install(*k8sFlag) + } else { + err = i.Uninstall(*k8sFlag) + } + if err != nil { + fmt.Println(err) + } +} + +func previewOSChanges() { + installChanges, uninstallChanges, err := PreviewChanges(*osFlag, *k8sFlag) + if err != nil { + fmt.Printf("Error previewing changes for os '%s' k8s '%s' %s", *osFlag, *k8sFlag, err) + return + } + + fmt.Printf("Install changes:\n%s\n\n", installChanges) + fmt.Printf("Uninstall changes:\n%s", uninstallChanges) +} diff --git a/agent/installer/cli/main.go b/agent/installer/cli/main.go new file mode 100644 index 000000000..4611ec373 --- /dev/null +++ b/agent/installer/cli/main.go @@ -0,0 +1,12 @@ +// Copyright 2021 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "github.com/vmware-tanzu/cluster-api-provider-byoh/agent/installer" +) + +func main() { + installer.Main() +}