--- /dev/null
+// Copyright © 2019 Red Hat <yroblamo@redhat.com>
+//
+// 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
+//
+// http://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 cmd
+
+import (
+ "fmt"
+ "log"
+ "os"
+
+ "gerrit.akraino.org/kni/installer/pkg/site"
+ "github.com/spf13/cobra"
+)
+
+// applyWorkloadsCmd represents the apply_workloads command
+var applyWorkloadsCmd = &cobra.Command{
+ Use: "apply_workloads siteName [--build_path=<local_build_path>]",
+ Short: "Command to apply the workloads after deploying a site",
+ Long: ``,
+ TraverseChildren: true,
+ Run: func(cmd *cobra.Command, args []string) {
+ // retrieve config values and start fetching
+ var siteName string
+ if len(args) == 0 {
+ log.Fatal("Please specify site name as first argument")
+ os.Exit(1)
+ } else {
+ siteName = args[0]
+ }
+
+ buildPath, _ := cmd.Flags().GetString("build_path")
+ if len(buildPath) == 0 {
+ // will generate a temporary directory
+ buildPath = fmt.Sprintf("%s/.kni", os.Getenv("HOME"))
+ }
+
+ // define a site object and proceed with applying workloads
+ s := site.NewWithName(siteName, buildPath)
+ s.ApplyWorkloads()
+ },
+}
+
+func init() {
+ rootCmd.AddCommand(applyWorkloadsCmd)
+
+ applyWorkloadsCmd.Flags().StringP("build_path", "", "", "Directory to use as build path. If that not exists, the installer will generate a default directory")
+
+}
}
}
+
+// using the site contents, applies the workloads on it
+func (s Site) ApplyWorkloads() {
+ siteBuildPath := fmt.Sprintf("%s/%s", s.buildPath, s.siteName)
+
+ // check if we have the needed kubeconfig file
+ kubeconfigFile := fmt.Sprintf("%s/final_manifests/auth/kubeconfig", siteBuildPath)
+ binariesPath := fmt.Sprintf("%s/requirements", siteBuildPath)
+
+ if _, err := os.Stat(kubeconfigFile); err == nil {
+ log.Println("Applying workloads from %s", fmt.Sprintf("%s/blueprint/sites/site/02_cluster-addons", siteBuildPath))
+ out := utils.ApplyKustomize(fmt.Sprintf("%s/kustomize", binariesPath), fmt.Sprintf("%s/blueprint/sites/site/02_cluster-addons", siteBuildPath))
+ if string(out) != "" {
+ // now we can apply it
+ utils.ApplyKubectl(fmt.Sprintf("%s/kubectl", binariesPath), out, kubeconfigFile)
+ } else {
+ log.Println(fmt.Sprintf("No manifests found for %s/blueprint/sites/site/02_cluster-addons", siteBuildPath))
+ }
+
+ log.Println("Applying workloads from %s", fmt.Sprintf("%s/blueprint/sites/site/03_services", siteBuildPath))
+ out = utils.ApplyKustomize(fmt.Sprintf("%s/kustomize", binariesPath), fmt.Sprintf("%s/blueprint/sites/site/03_services", siteBuildPath))
+ if string(out) != "" {
+ // now we can apply it
+ utils.ApplyKubectl(fmt.Sprintf("%s/kubectl", binariesPath), out, kubeconfigFile)
+ } else {
+ log.Println(fmt.Sprintf("No manifests found for %s/blueprint/sites/site/03_services", siteBuildPath))
+ }
+
+ } else {
+ // no kubeconfig, abort the workload creation
+ log.Fatal("Error, no kubeconfig file found")
+ os.Exit(1)
+ }
+
+}
"os"
"os/exec"
"path/filepath"
+ "time"
)
// utility to validate pre-requisites for deploying
return out
}
+
+// utility to apply kubectl for a given output
+func ApplyKubectl(kubectlBinary string, kubectlContent []byte, kubeconfigPath string) {
+ var out []byte
+ var err string
+ for i := 1; i <= 10; i++ {
+ cmd := exec.Command(kubectlBinary, "apply", "-f", "-")
+
+ cmd.Env = os.Environ()
+ cmd.Env = append(cmd.Env, fmt.Sprintf("KUBECONFIG_PATH=%s", kubeconfigPath))
+
+ // add output to stdin
+ stdin, err := cmd.StdinPipe()
+ stdin.Write(kubectlContent)
+ stdin.Close()
+
+ out, err = cmd.Output()
+
+ // show output for user to see progress
+ log.Println(string(out))
+
+ if err == nil {
+ // it is ok, stop the loop
+ break
+ } else {
+ // sleep and retry
+ time.Sleep(60 * time.Second)
+ }
+ }
+}