• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
12import argparse
13import glob
14import os
15import shutil
16import subprocess
17
18# See https://cloud.google.com/sdk/downloads#versioned for documentation on
19# scripting gcloud and also for updates.
20BASE_URL = 'https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/%s'
21GCLOUD_BASE_NAME='google-cloud-sdk'
22GCLOUD_ARCHIVE = '%s-250.0.0-linux-x86_64.tar.gz' % GCLOUD_BASE_NAME
23GCLOUD_URL = BASE_URL % GCLOUD_ARCHIVE
24
25def 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',
50                         'install', 'beta', 'cloud-firestore-emulator',
51                         '--quiet'], env=env)
52  # As of gcloud v250.0.0 and Cloud Firestore Emulator v1.4.6, there is a bug
53  # that something expects the JAR to be executable, but it isn't.
54  fs_jar = 'platform/cloud-firestore-emulator/cloud-firestore-emulator.jar'
55  subprocess.check_call(['chmod', '+x', os.path.join(target_dir, fs_jar)])
56  subprocess.check_call([gcloud_exe, 'components','update', '--quiet'], env=env)
57
58  # Remove the tarball.
59  os.remove(GCLOUD_ARCHIVE)
60
61def main():
62  parser = argparse.ArgumentParser()
63  parser.add_argument('--target_dir', '-t', required=True)
64  args = parser.parse_args()
65  create_asset(args.target_dir)
66
67if __name__ == '__main__':
68  main()
69