github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/scenarios/canarypush.py (about)

     1  #!/usr/bin/env python
     2  
     3  # Copyright 2017 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # Need to figure out why this only fails on travis
    18  # pylint: disable=bad-continuation
    19  
    20  """Executes a command."""
    21  
    22  import argparse
    23  import os
    24  import subprocess
    25  import sys
    26  
    27  def check_with_log(*cmd):
    28      """Log and run the command, raising on errors."""
    29      print >>sys.stderr, 'Run:', cmd
    30      print >>sys.stderr, subprocess.check_call(cmd)
    31  
    32  def check_no_log(*cmd):
    33      """Run the command, raising on errors, no logs"""
    34      try:
    35          subprocess.check_call(cmd)
    36      except:
    37          raise subprocess.CalledProcessError(cmd='subprocess.check_call', returncode=1)
    38  
    39  def check_output(*cmd):
    40      """Log and run the command, return output, raising on errors."""
    41      print >>sys.stderr, 'Run:', cmd
    42      return subprocess.check_output(cmd)
    43  
    44  
    45  def main(target, buildfile):
    46      """Build & push to canary."""
    47      check_with_log(
    48        'docker', 'build', '-t', target, '--no-cache=true',
    49        '--pull=true', '--file=%s' % buildfile, '.'
    50      )
    51      check_with_log('docker', 'inspect', target)
    52  
    53      user = os.environ.get('DOCKER_USER')
    54      pwd = os.environ.get('DOCKER_PASSWORD')
    55      print >>sys.stderr, 'Logging in as %r' % user
    56      check_no_log('docker', 'login', '--username=%s' % user, '--password=%s' % pwd)
    57  
    58      os.environ.pop('DOCKER_USER', None)
    59      os.environ.pop('DOCKER_PASSWORD', None)
    60  
    61      check_with_log('docker', 'push', target)
    62      check_with_log('docker', 'logout')
    63  
    64  
    65  if __name__ == '__main__':
    66      PARSER = argparse.ArgumentParser()
    67      PARSER.add_argument(
    68          '--owner', help='Owner of the job')
    69      PARSER.add_argument(
    70          '--target', help='Build target')
    71      PARSER.add_argument(
    72          '--file', help='Build files')
    73      ARGS = PARSER.parse_args()
    74      if not ARGS.target or not ARGS.file:
    75          raise ValueError('--target and --file must be set!')
    76      if ARGS.owner:
    77          os.environ['OWNER'] = ARGS.owner
    78      main(ARGS.target, ARGS.file)