1cb93a386Sopenharmony_ci#!/usr/bin/env python
2cb93a386Sopenharmony_ci#
3cb93a386Sopenharmony_ci# Copyright 2017 Google Inc.
4cb93a386Sopenharmony_ci#
5cb93a386Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
6cb93a386Sopenharmony_ci# found in the LICENSE file.
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci
9cb93a386Sopenharmony_ci"""Create the asset."""
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ciimport argparse
13cb93a386Sopenharmony_ciimport glob
14cb93a386Sopenharmony_ciimport os
15cb93a386Sopenharmony_ciimport shutil
16cb93a386Sopenharmony_ciimport subprocess
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ci# See https://cloud.google.com/sdk/downloads#versioned for documentation on
20cb93a386Sopenharmony_ci# scripting gcloud and also for updates.
21cb93a386Sopenharmony_ciBASE_URL = 'https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/%s'
22cb93a386Sopenharmony_ciGCLOUD_BASE_NAME='google-cloud-sdk'
23cb93a386Sopenharmony_ciGCLOUD_ARCHIVE = '%s-343.0.0-linux-x86_64.tar.gz' % GCLOUD_BASE_NAME
24cb93a386Sopenharmony_ciGCLOUD_URL = BASE_URL % GCLOUD_ARCHIVE
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_ci
27cb93a386Sopenharmony_cidef create_asset(target_dir):
28cb93a386Sopenharmony_ci  """Create the asset."""
29cb93a386Sopenharmony_ci  target_dir = os.path.abspath(target_dir)
30cb93a386Sopenharmony_ci  subprocess.check_call(['curl', GCLOUD_URL, '-o', GCLOUD_ARCHIVE])
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_ci  # Extract the arcive to the target directory and remove it.
33cb93a386Sopenharmony_ci  subprocess.check_call(['tar', '-xzf', GCLOUD_ARCHIVE,
34cb93a386Sopenharmony_ci                         '--strip-components=1',
35cb93a386Sopenharmony_ci                         '-C', target_dir])
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci  # Substitute the HOME directory in the environment so we don't overwrite
38cb93a386Sopenharmony_ci  # an existing gcloud configuration in $HOME/.config/gcloud
39cb93a386Sopenharmony_ci  env = os.environ.copy()
40cb93a386Sopenharmony_ci  env["HOME"] = target_dir
41cb93a386Sopenharmony_ci  gcloud_exe = os.path.join(target_dir, 'bin', 'gcloud')
42cb93a386Sopenharmony_ci  subprocess.check_call([gcloud_exe, 'components',
43cb93a386Sopenharmony_ci                         'install', 'beta', 'cloud-datastore-emulator',
44cb93a386Sopenharmony_ci                         '--quiet'], env=env)
45cb93a386Sopenharmony_ci  subprocess.check_call([gcloud_exe, 'components',
46cb93a386Sopenharmony_ci                         'install', 'beta', 'bigtable',
47cb93a386Sopenharmony_ci                         '--quiet'], env=env)
48cb93a386Sopenharmony_ci  subprocess.check_call([gcloud_exe, 'components',
49cb93a386Sopenharmony_ci                         'install', 'pubsub-emulator',
50cb93a386Sopenharmony_ci                         '--quiet'], env=env)
51cb93a386Sopenharmony_ci  subprocess.check_call([gcloud_exe, 'components',
52cb93a386Sopenharmony_ci                         'install', 'beta', 'cloud-firestore-emulator',
53cb93a386Sopenharmony_ci                         '--quiet'], env=env)
54cb93a386Sopenharmony_ci  # As of gcloud v250.0.0 and Cloud Firestore Emulator v1.4.6, there is a bug
55cb93a386Sopenharmony_ci  # that something expects the JAR to be executable, but it isn't.
56cb93a386Sopenharmony_ci  fs_jar = 'platform/cloud-firestore-emulator/cloud-firestore-emulator.jar'
57cb93a386Sopenharmony_ci  subprocess.check_call(['chmod', '+x', os.path.join(target_dir, fs_jar)])
58cb93a386Sopenharmony_ci  subprocess.check_call([gcloud_exe, 'components','update', '--quiet'], env=env)
59cb93a386Sopenharmony_ci
60cb93a386Sopenharmony_ci  # Remove the tarball.
61cb93a386Sopenharmony_ci  os.remove(GCLOUD_ARCHIVE)
62cb93a386Sopenharmony_ci
63cb93a386Sopenharmony_ci
64cb93a386Sopenharmony_cidef main():
65cb93a386Sopenharmony_ci  parser = argparse.ArgumentParser()
66cb93a386Sopenharmony_ci  parser.add_argument('--target_dir', '-t', required=True)
67cb93a386Sopenharmony_ci  args = parser.parse_args()
68cb93a386Sopenharmony_ci  create_asset(args.target_dir)
69cb93a386Sopenharmony_ci
70cb93a386Sopenharmony_ci
71cb93a386Sopenharmony_ciif __name__ == '__main__':
72cb93a386Sopenharmony_ci  main()
73