github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/kubetest/stage.go (about)

     1  /*
     2  Copyright 2017 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 main
    18  
    19  import (
    20  	"fmt"
    21  	"os/exec"
    22  	"regexp"
    23  	"strings"
    24  )
    25  
    26  type stageStrategy struct {
    27  	bucket         string
    28  	ci             bool
    29  	gcsSuffix      string
    30  	versionSuffix  string
    31  	dockerRegistry string
    32  }
    33  
    34  // Return something like gs://bucket/ci/suffix
    35  func (s *stageStrategy) String() string {
    36  	p := "devel"
    37  	if s.ci {
    38  		p = "ci"
    39  	}
    40  	return fmt.Sprintf("%v%v%v", s.bucket, p, s.gcsSuffix)
    41  }
    42  
    43  // Parse bucket, ci, suffix from gs://BUCKET/ci/SUFFIX
    44  func (s *stageStrategy) Set(value string) error {
    45  	re := regexp.MustCompile(`^(gs://[\w-]+)/(devel|ci)(/.*)?`)
    46  	mat := re.FindStringSubmatch(value)
    47  	if mat == nil {
    48  		return fmt.Errorf("Invalid stage location: %v. Use gs://bucket/ci/optional-suffix", value)
    49  	}
    50  	s.bucket = mat[1]
    51  	s.ci = mat[2] == "ci"
    52  	s.gcsSuffix = mat[3]
    53  	return nil
    54  }
    55  
    56  // True when this kubetest invocation wants to stage the release
    57  func (s *stageStrategy) Enabled() bool {
    58  	return s.bucket != ""
    59  }
    60  
    61  // Stage the release build to GCS.
    62  // Essentially release/push-build.sh --bucket=B --ci? --gcs-suffix=S --federation? --noupdatelatest
    63  func (s *stageStrategy) Stage(fed bool) error {
    64  	name := k8s("release", "push-build.sh")
    65  	b := s.bucket
    66  	if strings.HasPrefix(b, "gs://") {
    67  		b = b[len("gs://"):]
    68  	}
    69  	args := []string{
    70  		"--nomock",
    71  		"--verbose",
    72  		"--noupdatelatest", // we may need to expose control of this if build jobs start using kubetest
    73  		fmt.Sprintf("--bucket=%v", b),
    74  	}
    75  	if s.ci {
    76  		args = append(args, "--ci")
    77  	}
    78  	if len(s.gcsSuffix) > 0 {
    79  		args = append(args, fmt.Sprintf("--gcs-suffix=%v", s.gcsSuffix))
    80  	}
    81  	if len(s.versionSuffix) > 0 {
    82  		args = append(args, fmt.Sprintf("--version-suffix=%s", s.versionSuffix))
    83  	}
    84  	if len(s.dockerRegistry) > 0 {
    85  		args = append(args, fmt.Sprintf("--docker-registry=%s", s.dockerRegistry))
    86  	}
    87  	if fed {
    88  		args = append(args, "--federation")
    89  	}
    90  
    91  	cmd := exec.Command(name, args...)
    92  	cmd.Dir = k8s("kubernetes")
    93  	return finishRunning(cmd)
    94  }