From 54e13ceaad704809071596bf01b919a39b0d6361 Mon Sep 17 00:00:00 2001
From: "Yasin, Siraj (SY495P)" <sirajudeen.yasin@gmail.com>
Date: Thu, 28 May 2020 13:39:21 -0500
Subject: [PATCH] Added new command for Image Build.

* command: airshipctl image build
* For now it is just a wrapper for baremetal isogen.
* Removed isogen subcommand from baremetal

Change-Id: I5c1260c4b167e5768eba27a7cd83c9b60ab29184
Relates-To:#251
Closes:#251
---
 cmd/baremetal/baremetal.go                    |  3 --
 cmd/baremetal/baremetal_test.go               |  5 ---
 .../baremetal-isogen-with-help.golden         |  7 ----
 .../baremetal-with-help.golden                |  1 -
 cmd/{baremetal/isogen.go => image/build.go}   | 10 ++---
 cmd/image/image.go                            | 41 +++++++++++++++++++
 cmd/image/image_test.go                       | 36 ++++++++++++++++
 .../image-with-help.golden                    | 13 ++++++
 cmd/root.go                                   |  2 +
 .../rootCmd-with-default-subcommands.golden   |  1 +
 docs/source/cli/airshipctl.md                 |  1 +
 docs/source/cli/airshipctl_baremetal.md       |  1 -
 docs/source/cli/airshipctl_image.md           | 27 ++++++++++++
 docs/source/cli/airshipctl_image_build.md     | 30 ++++++++++++++
 .../tasks/main.yml                            |  2 +-
 15 files changed, 157 insertions(+), 23 deletions(-)
 delete mode 100644 cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-isogen-with-help.golden
 rename cmd/{baremetal/isogen.go => image/build.go} (75%)
 create mode 100644 cmd/image/image.go
 create mode 100644 cmd/image/image_test.go
 create mode 100644 cmd/image/testdata/TestImageGoldenOutput/image-with-help.golden
 create mode 100644 docs/source/cli/airshipctl_image.md
 create mode 100644 docs/source/cli/airshipctl_image_build.md

diff --git a/cmd/baremetal/baremetal.go b/cmd/baremetal/baremetal.go
index 2f51b723e..5cdc86a2f 100644
--- a/cmd/baremetal/baremetal.go
+++ b/cmd/baremetal/baremetal.go
@@ -51,9 +51,6 @@ func NewBaremetalCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Co
 	ejectMediaCmd := NewEjectMediaCommand(rootSettings)
 	baremetalRootCmd.AddCommand(ejectMediaCmd)
 
-	isoGenCmd := NewISOGenCommand(rootSettings)
-	baremetalRootCmd.AddCommand(isoGenCmd)
-
 	powerOffCmd := NewPowerOffCommand(rootSettings)
 	baremetalRootCmd.AddCommand(powerOffCmd)
 
diff --git a/cmd/baremetal/baremetal_test.go b/cmd/baremetal/baremetal_test.go
index ce0f237d8..ec6d3221f 100644
--- a/cmd/baremetal/baremetal_test.go
+++ b/cmd/baremetal/baremetal_test.go
@@ -35,11 +35,6 @@ func TestBaremetal(t *testing.T) {
 			CmdLine: "-h",
 			Cmd:     baremetal.NewEjectMediaCommand(nil),
 		},
-		{
-			Name:    "baremetal-isogen-with-help",
-			CmdLine: "-h",
-			Cmd:     baremetal.NewISOGenCommand(nil),
-		},
 		{
 			Name:    "baremetal-poweroff-with-help",
 			CmdLine: "-h",
diff --git a/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-isogen-with-help.golden b/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-isogen-with-help.golden
deleted file mode 100644
index 94013e188..000000000
--- a/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-isogen-with-help.golden
+++ /dev/null
@@ -1,7 +0,0 @@
-Generate baremetal host ISO image
-
-Usage:
-  isogen [flags]
-
-Flags:
-  -h, --help   help for isogen
diff --git a/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-with-help.golden b/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-with-help.golden
index 7710d630c..5fe569010 100644
--- a/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-with-help.golden
+++ b/cmd/baremetal/testdata/TestBaremetalGoldenOutput/baremetal-with-help.golden
@@ -6,7 +6,6 @@ Usage:
 Available Commands:
   ejectmedia   Eject media attached to a baremetal host
   help         Help about any command
-  isogen       Generate baremetal host ISO image
   poweroff     Shutdown a baremetal host
   poweron      Power on a host
   powerstatus  Retrieve the power status of a baremetal host
diff --git a/cmd/baremetal/isogen.go b/cmd/image/build.go
similarity index 75%
rename from cmd/baremetal/isogen.go
rename to cmd/image/build.go
index e7330da8c..f4aca7b57 100644
--- a/cmd/baremetal/isogen.go
+++ b/cmd/image/build.go
@@ -12,7 +12,7 @@
  limitations under the License.
 */
 
-package baremetal
+package image
 
 import (
 	"github.com/spf13/cobra"
@@ -21,11 +21,11 @@ import (
 	"opendev.org/airship/airshipctl/pkg/environment"
 )
 
-// NewISOGenCommand creates a new command with the capability to generate the ephemeral node ISO image.
-func NewISOGenCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
+// NewImageBuildCommand creates a new command with the capability to build an ISO image.
+func NewImageBuildCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
 	cmd := &cobra.Command{
-		Use:   "isogen",
-		Short: "Generate baremetal host ISO image",
+		Use:   "build",
+		Short: "Build ISO image",
 		RunE: func(cmd *cobra.Command, args []string) error {
 			return isogen.GenerateBootstrapIso(rootSettings)
 		},
diff --git a/cmd/image/image.go b/cmd/image/image.go
new file mode 100644
index 000000000..aec253b9b
--- /dev/null
+++ b/cmd/image/image.go
@@ -0,0 +1,41 @@
+/*
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+     https://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+package image
+
+import (
+	"github.com/spf13/cobra"
+
+	"opendev.org/airship/airshipctl/pkg/environment"
+	"opendev.org/airship/airshipctl/pkg/log"
+)
+
+// NewImageCommand creates a new command for managing ISO images using airshipctl.
+func NewImageCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
+	imageRootCmd := &cobra.Command{
+		Use:   "image",
+		Short: "Manage ISO image creation",
+		PersistentPreRun: func(cmd *cobra.Command, args []string) {
+			log.Init(rootSettings.Debug, cmd.OutOrStderr())
+
+			// Load or Initialize airship Config
+			rootSettings.InitConfig()
+		},
+	}
+
+	imageBuildCmd := NewImageBuildCommand(rootSettings)
+	imageRootCmd.AddCommand(imageBuildCmd)
+
+	return imageRootCmd
+}
diff --git a/cmd/image/image_test.go b/cmd/image/image_test.go
new file mode 100644
index 000000000..5d919d878
--- /dev/null
+++ b/cmd/image/image_test.go
@@ -0,0 +1,36 @@
+/*
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+     https://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+package image_test
+
+import (
+	"testing"
+
+	"opendev.org/airship/airshipctl/cmd/image"
+	"opendev.org/airship/airshipctl/testutil"
+)
+
+func TestImage(t *testing.T) {
+	tests := []*testutil.CmdTest{
+		{
+			Name:    "image-with-help",
+			CmdLine: "-h",
+			Cmd:     image.NewImageCommand(nil),
+		},
+	}
+
+	for _, tt := range tests {
+		testutil.RunTest(t, tt)
+	}
+}
diff --git a/cmd/image/testdata/TestImageGoldenOutput/image-with-help.golden b/cmd/image/testdata/TestImageGoldenOutput/image-with-help.golden
new file mode 100644
index 000000000..fc06a0708
--- /dev/null
+++ b/cmd/image/testdata/TestImageGoldenOutput/image-with-help.golden
@@ -0,0 +1,13 @@
+Manage ISO image creation
+
+Usage:
+  image [command]
+
+Available Commands:
+  build       Build ISO image
+  help        Help about any command
+
+Flags:
+  -h, --help   help for image
+
+Use "image [command] --help" for more information about a command.
diff --git a/cmd/root.go b/cmd/root.go
index 370816fee..82e1157f1 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -27,6 +27,7 @@ import (
 	"opendev.org/airship/airshipctl/cmd/completion"
 	"opendev.org/airship/airshipctl/cmd/config"
 	"opendev.org/airship/airshipctl/cmd/document"
+	"opendev.org/airship/airshipctl/cmd/image"
 	"opendev.org/airship/airshipctl/cmd/phase"
 	"opendev.org/airship/airshipctl/cmd/secret"
 	"opendev.org/airship/airshipctl/pkg/environment"
@@ -64,6 +65,7 @@ func AddDefaultAirshipCTLCommands(cmd *cobra.Command, settings *environment.Airs
 	cmd.AddCommand(completion.NewCompletionCommand())
 	cmd.AddCommand(document.NewDocumentCommand(settings))
 	cmd.AddCommand(config.NewConfigCommand(settings))
+	cmd.AddCommand(image.NewImageCommand(settings))
 	cmd.AddCommand(secret.NewSecretCommand())
 	cmd.AddCommand(phase.NewPhaseCommand(settings))
 
diff --git a/cmd/testdata/TestRootGoldenOutput/rootCmd-with-default-subcommands.golden b/cmd/testdata/TestRootGoldenOutput/rootCmd-with-default-subcommands.golden
index a8a29358d..11dcece40 100644
--- a/cmd/testdata/TestRootGoldenOutput/rootCmd-with-default-subcommands.golden
+++ b/cmd/testdata/TestRootGoldenOutput/rootCmd-with-default-subcommands.golden
@@ -10,6 +10,7 @@ Available Commands:
   config      Manage the airshipctl config file
   document    Manage deployment documents
   help        Help about any command
+  image       Manage ISO image creation
   phase       Manage phases
   secret      Manage secrets
   version     Show the version number of airshipctl
diff --git a/docs/source/cli/airshipctl.md b/docs/source/cli/airshipctl.md
index 314b7624b..847411b9a 100644
--- a/docs/source/cli/airshipctl.md
+++ b/docs/source/cli/airshipctl.md
@@ -22,6 +22,7 @@ A unified entrypoint to various airship components
 * [airshipctl completion](airshipctl_completion.md)	 - Generate completion script for the specified shell (bash or zsh)
 * [airshipctl config](airshipctl_config.md)	 - Manage the airshipctl config file
 * [airshipctl document](airshipctl_document.md)	 - Manage deployment documents
+* [airshipctl image](airshipctl_image.md)	 - Manage ISO image creation
 * [airshipctl phase](airshipctl_phase.md)	 - Manage phases
 * [airshipctl secret](airshipctl_secret.md)	 - Manage secrets
 * [airshipctl version](airshipctl_version.md)	 - Show the version number of airshipctl
diff --git a/docs/source/cli/airshipctl_baremetal.md b/docs/source/cli/airshipctl_baremetal.md
index 635cc49ba..8449596fd 100644
--- a/docs/source/cli/airshipctl_baremetal.md
+++ b/docs/source/cli/airshipctl_baremetal.md
@@ -24,7 +24,6 @@ Perform actions on baremetal hosts
 
 * [airshipctl](airshipctl.md)	 - A unified entrypoint to various airship components
 * [airshipctl baremetal ejectmedia](airshipctl_baremetal_ejectmedia.md)	 - Eject media attached to a baremetal host
-* [airshipctl baremetal isogen](airshipctl_baremetal_isogen.md)	 - Generate baremetal host ISO image
 * [airshipctl baremetal poweroff](airshipctl_baremetal_poweroff.md)	 - Shutdown a baremetal host
 * [airshipctl baremetal poweron](airshipctl_baremetal_poweron.md)	 - Power on a host
 * [airshipctl baremetal powerstatus](airshipctl_baremetal_powerstatus.md)	 - Retrieve the power status of a baremetal host
diff --git a/docs/source/cli/airshipctl_image.md b/docs/source/cli/airshipctl_image.md
new file mode 100644
index 000000000..9365ac684
--- /dev/null
+++ b/docs/source/cli/airshipctl_image.md
@@ -0,0 +1,27 @@
+## airshipctl image
+
+Manage ISO image creation
+
+### Synopsis
+
+Manage ISO image creation
+
+### Options
+
+```
+  -h, --help   help for image
+```
+
+### Options inherited from parent commands
+
+```
+      --airshipconf string   Path to file for airshipctl configuration. (default "$HOME/.airship/config")
+      --debug                enable verbose output
+      --kubeconfig string    Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
+```
+
+### SEE ALSO
+
+* [airshipctl](airshipctl.md)	 - A unified entrypoint to various airship components
+* [airshipctl image build](airshipctl_image_build.md)	 - Build ISO image
+
diff --git a/docs/source/cli/airshipctl_image_build.md b/docs/source/cli/airshipctl_image_build.md
new file mode 100644
index 000000000..0c143ba39
--- /dev/null
+++ b/docs/source/cli/airshipctl_image_build.md
@@ -0,0 +1,30 @@
+## airshipctl image build
+
+Build ISO image
+
+### Synopsis
+
+Build ISO image
+
+```
+airshipctl image build [flags]
+```
+
+### Options
+
+```
+  -h, --help   help for build
+```
+
+### Options inherited from parent commands
+
+```
+      --airshipconf string   Path to file for airshipctl configuration. (default "$HOME/.airship/config")
+      --debug                enable verbose output
+      --kubeconfig string    Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
+```
+
+### SEE ALSO
+
+* [airshipctl image](airshipctl_image.md)	 - Manage ISO image creation
+
diff --git a/roles/airshipctl-build-ephemeral-iso/tasks/main.yml b/roles/airshipctl-build-ephemeral-iso/tasks/main.yml
index 923b65cfd..309ddbcd9 100644
--- a/roles/airshipctl-build-ephemeral-iso/tasks/main.yml
+++ b/roles/airshipctl-build-ephemeral-iso/tasks/main.yml
@@ -19,7 +19,7 @@
   become: yes
 
 - name: build ephemeral node iso
-  command: airshipctl baremetal isogen
+  command: airshipctl image build
   environment:
     http_proxy: "{{ proxy.http }}"
     https_proxy: "{{ proxy.http }}"