From cef15ea8ad0731c4044de7fdcb0c9bcc4c0ecc2f Mon Sep 17 00:00:00 2001 From: Rousko Atanasov Date: Thu, 28 Oct 2021 13:31:12 +0300 Subject: [PATCH 1/6] CLI for the installer library --- agent/installer/cli.go | 101 ++++++++++++++++++++++++++++++++++++ agent/installer/cli/main.go | 12 +++++ 2 files changed, 113 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..75b7e0581 --- /dev/null +++ b/agent/installer/cli.go @@ -0,0 +1,101 @@ +// 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("k8sVer", "1.22.1", "Kubernetes version to install") +) + +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) + } +} + +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) { + if *bundleRepoFlag == "" { + bd := bundleDownloader{"", "."} + fmt.Printf("Bundle repo not specified. Provide bundle contents in %s\n", bd.GetBundleDirPath(*k8sFlag)) + } + + klog.InitFlags(nil) + klogr.New() + + i, err := New("norepo", ".", klogr.New()) + if err != nil { + fmt.Println(err) + } + + if install { + err = i.Install(*k8sFlag) + } else { + err = i.Uninstall(*k8sFlag) + } + if err != nil { + fmt.Println(err) + } +} 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() +} From ff557650d405fa60f0ec5c0834a9ba403ad80ad2 Mon Sep 17 00:00:00 2001 From: Rousko Atanasov Date: Thu, 28 Oct 2021 17:43:56 +0300 Subject: [PATCH 2/6] Add preview flag --- agent/installer/cli.go | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/agent/installer/cli.go b/agent/installer/cli.go index 75b7e0581..1b8cf9dfa 100644 --- a/agent/installer/cli.go +++ b/agent/installer/cli.go @@ -11,13 +11,15 @@ import ( ) 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("k8sVer", "1.22.1", "Kubernetes version to install") + 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") + previewOSChangesFlag = flag.Bool("previewOSChanges", false, "Preview the install and uninstall changes for the specified OS") ) const ( @@ -47,6 +49,10 @@ func Main() { if *uninstallFlag { runInstaller(doUninstall) } + + if *previewOSChangesFlag { + previewOSChanges() + } } func list() { @@ -99,3 +105,14 @@ func runInstaller(install bool) { 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) +} From 2e914e5c0f89eaf3ab36ea318ce98a57f2a99195 Mon Sep 17 00:00:00 2001 From: Rousko Atanasov Date: Thu, 28 Oct 2021 17:56:43 +0300 Subject: [PATCH 3/6] Override preview mode --- agent/installer/cli.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/agent/installer/cli.go b/agent/installer/cli.go index 1b8cf9dfa..232e64bb1 100644 --- a/agent/installer/cli.go +++ b/agent/installer/cli.go @@ -96,6 +96,10 @@ func runInstaller(install bool) { fmt.Println(err) } + //Override preview mode + i.downloadPath = "." + i.repoAddr = *bundleRepoFlag + if install { err = i.Install(*k8sFlag) } else { From d20a18488adb6b66bbf919eee7a92af665ff8f5e Mon Sep 17 00:00:00 2001 From: Rousko Atanasov Date: Thu, 28 Oct 2021 18:28:55 +0300 Subject: [PATCH 4/6] Rename cmd option --- agent/installer/cli.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agent/installer/cli.go b/agent/installer/cli.go index 232e64bb1..fb8ea11ba 100644 --- a/agent/installer/cli.go +++ b/agent/installer/cli.go @@ -12,7 +12,7 @@ import ( 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") + 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") From b4f2832dc07ce51f7464b6a778a5429199655ec7 Mon Sep 17 00:00:00 2001 From: Rousko Atanasov Date: Fri, 29 Oct 2021 11:32:36 +0300 Subject: [PATCH 5/6] fix linter --- agent/installer/cli.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agent/installer/cli.go b/agent/installer/cli.go index fb8ea11ba..307da7a7a 100644 --- a/agent/installer/cli.go +++ b/agent/installer/cli.go @@ -96,7 +96,7 @@ func runInstaller(install bool) { fmt.Println(err) } - //Override preview mode + // Override preview mode i.downloadPath = "." i.repoAddr = *bundleRepoFlag From 14c45ed019ca038f003e12d383efd43b9354ec00 Mon Sep 17 00:00:00 2001 From: Rousko Atanasov Date: Fri, 29 Oct 2021 16:15:43 +0300 Subject: [PATCH 6/6] Allow bypass detecting OS --- agent/installer/cli.go | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/agent/installer/cli.go b/agent/installer/cli.go index 307da7a7a..0a281f835 100644 --- a/agent/installer/cli.go +++ b/agent/installer/cli.go @@ -18,7 +18,7 @@ var ( 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") + 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") ) @@ -83,23 +83,35 @@ func detectOS() { } 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)) } - klog.InitFlags(nil) - klogr.New() + 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 + } - i, err := New("norepo", ".", klogr.New()) - if err != nil { - fmt.Println(err) + // Override preview mode + i.downloadPath = "." + i.repoAddr = *bundleRepoFlag } - // Override preview mode - i.downloadPath = "." - i.repoAddr = *bundleRepoFlag - if install { err = i.Install(*k8sFlag) } else {