Home | History | Annotate | Download | only in gcloud_linux
      1 #!/usr/bin/env python
      2 #
      3 # Copyright 2017 Google Inc.
      4 #
      5 # Use of this source code is governed by a BSD-style license that can be
      6 # found in the LICENSE file.
      7 
      8 
      9 """Create the asset."""
     10 
     11 
     12 import argparse
     13 import glob
     14 import os
     15 import shutil
     16 import subprocess
     17 
     18 # See https://cloud.google.com/sdk/downloads#versioned for documentation on
     19 # scripting gcloud and also for updates.
     20 BASE_URL = 'https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/%s'
     21 GCLOUD_BASE_NAME='google-cloud-sdk'
     22 GCLOUD_ARCHIVE = '%s-215.0.0-linux-x86_64.tar.gz' % GCLOUD_BASE_NAME
     23 GCLOUD_URL = BASE_URL % GCLOUD_ARCHIVE
     24 
     25 def create_asset(target_dir):
     26   """Create the asset."""
     27   target_dir = os.path.abspath(target_dir)
     28   subprocess.check_call(['curl', GCLOUD_URL, '-o', GCLOUD_ARCHIVE])
     29 
     30   # Extract the arcive to the target directory and remove it.
     31   subprocess.check_call(['tar', '-xzf', GCLOUD_ARCHIVE,
     32                          '--strip-components=1',
     33                          '-C', target_dir])
     34 
     35   # Substitute the HOME directory in the environment so we don't overwrite
     36   # an existing gcloud configuration in $HOME/.config/gcloud
     37   env = os.environ.copy()
     38   env["HOME"] = target_dir
     39   gcloud_exe = os.path.join(target_dir, 'bin', 'gcloud')
     40   subprocess.check_call([gcloud_exe, 'components',
     41                          'install', 'beta', 'cloud-datastore-emulator',
     42                          '--quiet'], env=env)
     43   subprocess.check_call([gcloud_exe, 'components',
     44                          'install', 'beta', 'bigtable',
     45                          '--quiet'], env=env)
     46   subprocess.check_call([gcloud_exe, 'components',
     47                          'install', 'pubsub-emulator',
     48                          '--quiet'], env=env)
     49   subprocess.check_call([gcloud_exe, 'components','update', '--quiet'], env=env)
     50 
     51   # Remove the tarball.
     52   os.remove(GCLOUD_ARCHIVE)
     53 
     54 def main():
     55   parser = argparse.ArgumentParser()
     56   parser.add_argument('--target_dir', '-t', required=True)
     57   args = parser.parse_args()
     58   create_asset(args.target_dir)
     59 
     60 if __name__ == '__main__':
     61   main()
     62