Make sure to copy site yamls to automation repo 19/1819/1
authorAndrew Bays <andrew.bays@gmail.com>
Tue, 22 Oct 2019 12:55:53 +0000 (08:55 -0400)
committerAndrew Bays <andrew.bays@gmail.com>
Tue, 22 Oct 2019 12:55:53 +0000 (08:55 -0400)
Change-Id: I9cba91b07acba4cbd98346f58ce0a023f19e0c5d

pkg/automation/automation.go
pkg/automation/baremetal.go
pkg/site/site.go

index d965c2c..a6e4e4f 100644 (file)
@@ -17,7 +17,7 @@ type AutomatedDeploymentParams struct {
 
 type AutomatedDeploymentInterface interface {
        PrepareAutomation(map[string]string) error // Initial host preparation for automation, before finalizing manifests
-       FinalizeAutomation() error                 // Final host preparation for automation, after finalizing manifests
+       FinalizeAutomationPreparation() error      // Final host preparation for automation, after finalizing manifests
        DeployMasters() error                      // Deploy cluster masters
        DeployWorkers() error                      // Deploy cluster workers
        DestroyCluster() error                     // Destroy the cluster
index 536e2e7..9a15e74 100644 (file)
@@ -179,24 +179,83 @@ func (bad baremetalAutomatedDeployment) PrepareAutomation(requirements map[strin
        return nil
 }
 
-func (bad baremetalAutomatedDeployment) FinalizeAutomation() error {
+func (bad baremetalAutomatedDeployment) FinalizeAutomationPreparation() error {
+       // Copy finalized manifests into the baremetal automation repo directory
+       automationManifestSource := fmt.Sprintf("%s/%s/automation", bad.siteBuildPath, bad.siteName)
        automationDestination := fmt.Sprintf("%s/%s/baremetal_automation", bad.siteBuildPath, bad.siteName)
+       automationManifestDestination := fmt.Sprintf("%s/cluster", automationDestination)
 
-       // Execute automation's prep_bm_host script
+       // Need to remove all files copied to the site's cluster manifests staging dir that do not
+       // have associated "kind" content within them, as the baremetal automation repo logic will
+       // not tolerate anything without a "kind"
+       err := filepath.Walk(automationManifestSource, func(path string, info os.FileInfo, err error) error {
+               if err != nil {
+                       return err
+               }
+
+               // Not interested in directories, so just keep walking
+               if info.IsDir() {
+                       return nil
+               }
+
+               filename, _ := filepath.Abs(path)
+               fileBytes, err := ioutil.ReadFile(filename)
+
+               if err != nil {
+                       // File can't be read, so skip it
+                       return nil
+               }
+
+               // Empty file is useless, so just keep walking
+               if len(fileBytes) == 0 {
+                       return nil
+               }
+
+               var fileObject map[string]interface{}
+
+               err = yaml.Unmarshal(fileBytes, &fileObject)
+
+               if err != nil {
+                       // File does not have the proper YAML format we are looking for, so skip it
+                       return nil
+               }
+
+               // Check unmarshalled file for a "kind" key
+               if _, ok := fileObject["kind"]; ok {
+                       // Kind found, so we need to copy this into the baremetal automation
+                       // cluster manifests directory
+                       err = ioutil.WriteFile(fmt.Sprintf("%s/%s", automationManifestDestination, info.Name()), fileBytes, 0644)
+
+                       if err != nil {
+                               return err
+                       }
+
+                       log.Printf("baremetalAutomatedDeployment: FinalizeAutomationPreparation: copied %s to baremetal repo\n", info.Name())
+               }
+
+               return nil
+       })
+
+       if err != nil {
+               return fmt.Errorf("baremetalAutomatedDeployment: FinalizeAutomationPreparation: error copying finalized manifests to automation repo: %s", err)
+       }
+
+       // Execute automation's prep_bm_host script now that all manifests have been
+       // copied to the baremetal automation repo's cluster manifests directory
        cmd := exec.Command(fmt.Sprintf("%s/prep_bm_host.sh", automationDestination))
        cmd.Dir = automationDestination
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
 
-       log.Println("baremetalAutomatedDeployment: FinalizeAutomation: running baremetal automation host preparation script...")
+       log.Println("baremetalAutomatedDeployment: FinalizeAutomationPreparation: running baremetal automation host preparation script...")
 
-       err := cmd.Run()
+       err = cmd.Run()
 
        if err != nil {
-               return fmt.Errorf("baremetalAutomatedDeployment: FinalizeAutomation: error running baremetal automation host preparation script")
+               return fmt.Errorf("baremetalAutomatedDeployment: FinalizeAutomationPreparation: error running baremetal automation host preparation script: %s", err)
        }
 
-       log.Println("baremetalAutomatedDeployment: FinalizeAutomation: finished running automation host preparation script")
+       log.Println("baremetalAutomatedDeployment: FinalizeAutomationPreparation: finished running automation host preparation script")
 
        return nil
 }
index 3c090fd..29034ae 100644 (file)
@@ -349,11 +349,9 @@ func (s Site) PrepareManifests() {
        // create automation sub-directory to store a copy of anything that might be
        // needed in the case of potential automation
        automationPath := fmt.Sprintf("%s/automation", sitePath)
-       automationRepoPath := fmt.Sprintf("%s/baremetal_automation", sitePath)
        os.Mkdir(automationPath, 0755)
 
-       // copy 00_install-config directory contents (minus kustomization.yaml)
-       // into automation sub-directory
+       // copy 00_install-config directory contents into automation sub-directory
        installConfigDirPath := fmt.Sprintf("%s/blueprint/sites/site/00_install-config", sitePath)
        err := copy.Copy(installConfigDirPath, automationPath)
 
@@ -362,10 +360,6 @@ func (s Site) PrepareManifests() {
                os.Exit(1)
        }
 
-       // Remove kustomization from automation sub-directory (the copy library used
-       // above does not allow for filtering files when copying directories)
-       os.Remove(fmt.Sprintf("%s/kustomization.yaml", automationPath))
-
        // generate openshift-install manifests based on phase 00_install-config
        assetsPath := fmt.Sprintf("%s/generated_assets", sitePath)
        os.RemoveAll(assetsPath)
@@ -387,15 +381,6 @@ func (s Site) PrepareManifests() {
                        log.Fatal(fmt.Sprintf("Error writing final install-config file to automation assets directory: %s", err))
                        os.Exit(1)
                }
-
-               automationRepoClusterPath := fmt.Sprintf("%s/cluster", automationRepoPath)
-               if _, err = os.Stat(automationRepoClusterPath); err == nil {
-                       err = ioutil.WriteFile(fmt.Sprintf("%s/install-config.yaml", automationRepoClusterPath), out, 0644)
-                       if err != nil {
-                               log.Fatal(fmt.Sprintf("Error writing final install-config file to automation repo directory: %s", err))
-                               os.Exit(1)
-                       }
-               }
        } else {
                log.Fatal("Error, kustomize did not return any content")
                os.Exit(1)
@@ -757,5 +742,5 @@ func (s Site) finalizeHostForAutomation(profileName string) error {
        }
 
        // Tell the automated deployment instance to prepare the host for automation
-       return automatedDeployment.FinalizeAutomation()
+       return automatedDeployment.FinalizeAutomationPreparation()
 }