github.com/jenkins-x/test-infra@v0.0.7/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 = None
    54      if os.path.exists(os.environ.get('DOCKER_USER')):
    55          with open(os.environ.get('DOCKER_USER'), 'r') as content_file:
    56              user = content_file.read()
    57  
    58      pwd = None
    59      if os.path.exists(os.environ.get('DOCKER_PASSWORD')):
    60          with open(os.environ.get('DOCKER_PASSWORD'), 'r') as content_file:
    61              pwd = content_file.read()
    62  
    63      if not user or not pwd:
    64          print >>sys.stderr, 'Logging info not exist'
    65          sys.exit(1)
    66      print >>sys.stderr, 'Logging in as %r' % user
    67      check_no_log('docker', 'login', '--username=%s' % user, '--password=%s' % pwd)
    68  
    69      os.environ.pop('DOCKER_USER', None)
    70      os.environ.pop('DOCKER_PASSWORD', None)
    71  
    72      check_with_log('docker', 'push', target)
    73      check_with_log('docker', 'logout')
    74  
    75  
    76  if __name__ == '__main__':
    77      PARSER = argparse.ArgumentParser()
    78      PARSER.add_argument(
    79          '--owner', help='Owner of the job')
    80      PARSER.add_argument(
    81          '--target', help='Build target')
    82      PARSER.add_argument(
    83          '--file', help='Build files')
    84      ARGS = PARSER.parse_args()
    85      if not ARGS.target or not ARGS.file:
    86          raise ValueError('--target and --file must be set!')
    87      if ARGS.owner:
    88          os.environ['OWNER'] = ARGS.owner
    89      main(ARGS.target, ARGS.file)