|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "path" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/sirupsen/logrus" |
| 26 | + "github.com/spf13/cobra" |
| 27 | + |
| 28 | + "k8s.io/release/pkg/gcp/build" |
| 29 | + "k8s.io/release/pkg/log" |
| 30 | +) |
| 31 | + |
| 32 | +// rootCmd represents the base command when called without any subcommands |
| 33 | +var rootCmd = &cobra.Command{ |
| 34 | + Use: "gcbuilder", |
| 35 | + Short: "gcbuilder", |
| 36 | + PersistentPreRunE: initLogging, |
| 37 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 38 | + return run() |
| 39 | + }, |
| 40 | +} |
| 41 | + |
| 42 | +type rootOptions struct { |
| 43 | + logLevel string |
| 44 | +} |
| 45 | + |
| 46 | +var ( |
| 47 | + rootOpts = &rootOptions{} |
| 48 | + buildOpts = &build.Options{} |
| 49 | +) |
| 50 | + |
| 51 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 52 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 53 | +func Execute() { |
| 54 | + if err := rootCmd.Execute(); err != nil { |
| 55 | + logrus.Fatal(err) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func init() { |
| 60 | + rootCmd.PersistentFlags().StringVar(&buildOpts.ConfigDir, "config-dir", "", "Configuration directory") |
| 61 | + rootCmd.PersistentFlags().StringVar(&buildOpts.BuildDir, "build-dir", "", "If provided, this directory will be uploaded as the source for the Google Cloud Build run.") |
| 62 | + rootCmd.PersistentFlags().StringVar(&buildOpts.CloudbuildFile, "gcb-config", "cloudbuild.yaml", "If provided, this will be used as the name of the Google Cloud Build config file.") |
| 63 | + rootCmd.PersistentFlags().StringVar(&buildOpts.LogDir, "log-dir", "", "If provided, build logs will be sent to files in this directory instead of to stdout/stderr.") |
| 64 | + rootCmd.PersistentFlags().StringVar(&buildOpts.ScratchBucket, "scratch-bucket", "", "The complete GCS path for Cloud Build to store scratch files (sources, logs).") |
| 65 | + rootCmd.PersistentFlags().StringVar(&buildOpts.Project, "project", "", "If specified, use a non-default GCP project.") |
| 66 | + rootCmd.PersistentFlags().BoolVar(&buildOpts.AllowDirty, "allow-dirty", false, "If true, allow pushing dirty builds.") |
| 67 | + rootCmd.PersistentFlags().BoolVar(&buildOpts.NoSource, "no-source", false, "If true, no source will be uploaded with this build.") |
| 68 | + rootCmd.PersistentFlags().StringVar(&buildOpts.Variant, "variant", "", "If specified, build only the given variant. An error if no variants are defined.") |
| 69 | + rootCmd.PersistentFlags().StringVar(&buildOpts.EnvPassthrough, "env-passthrough", "", "Comma-separated list of specified environment variables to be passed to GCB as substitutions with an _ prefix. If the variable doesn't exist, the substitution will exist but be empty.") |
| 70 | + rootCmd.PersistentFlags().StringVar(&rootOpts.logLevel, "log-level", "info", "the logging verbosity, either 'panic', 'fatal', 'error', 'warn', 'warning', 'info', 'debug' or 'trace'") |
| 71 | + |
| 72 | + buildOpts.ConfigDir = strings.TrimSuffix(buildOpts.ConfigDir, "/") |
| 73 | +} |
| 74 | + |
| 75 | +// TODO: Clean up error handling |
| 76 | +func run() error { |
| 77 | + if buildOpts.ConfigDir == "" { |
| 78 | + logrus.Fatal("expected a config directory to be provided") |
| 79 | + } |
| 80 | + |
| 81 | + if bazelWorkspace := os.Getenv("BUILD_WORKSPACE_DIRECTORY"); bazelWorkspace != "" { |
| 82 | + if err := os.Chdir(bazelWorkspace); err != nil { |
| 83 | + logrus.Fatalf("Failed to chdir to bazel workspace (%s): %v", bazelWorkspace, err) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if buildOpts.BuildDir == "" { |
| 88 | + buildOpts.BuildDir = buildOpts.ConfigDir |
| 89 | + } |
| 90 | + |
| 91 | + logrus.Infof("Build directory: %s\n", buildOpts.BuildDir) |
| 92 | + |
| 93 | + // Canonicalize the config directory to be an absolute path. |
| 94 | + // As we're about to cd into the build directory, we need a consistent way to reference the config files |
| 95 | + // when the config directory is not the same as the build directory. |
| 96 | + absConfigDir, absErr := filepath.Abs(buildOpts.ConfigDir) |
| 97 | + if absErr != nil { |
| 98 | + logrus.Fatalf("Could not resolve absolute path for config directory: %v", absErr) |
| 99 | + } |
| 100 | + |
| 101 | + buildOpts.ConfigDir = absConfigDir |
| 102 | + buildOpts.CloudbuildFile = path.Join(buildOpts.ConfigDir, buildOpts.CloudbuildFile) |
| 103 | + |
| 104 | + configDirErr := buildOpts.ValidateConfigDir() |
| 105 | + if configDirErr != nil { |
| 106 | + logrus.Fatalf("Could not validate config directory: %v", configDirErr) |
| 107 | + } |
| 108 | + |
| 109 | + logrus.Infof("Config directory: %s\n", buildOpts.ConfigDir) |
| 110 | + |
| 111 | + logrus.Infof("cd-ing to build directory: %s\n", buildOpts.BuildDir) |
| 112 | + if err := os.Chdir(buildOpts.BuildDir); err != nil { |
| 113 | + logrus.Fatalf("Failed to chdir to build directory (%s): %v", buildOpts.BuildDir, err) |
| 114 | + } |
| 115 | + |
| 116 | + errors := build.RunBuildJobs(buildOpts) |
| 117 | + if len(errors) != 0 { |
| 118 | + logrus.Fatalf("Failed to run some build jobs: %v", errors) |
| 119 | + } |
| 120 | + logrus.Info("Finished.") |
| 121 | + |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func initLogging(*cobra.Command, []string) error { |
| 126 | + return log.SetupGlobalLogger(rootOpts.logLevel) |
| 127 | +} |
0 commit comments