| 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 |
|
| 19 |
# See https://cloud.google.com/sdk/downloads#versioned for documentation on |
| 20 |
# scripting gcloud and also for updates. |
| 21 |
BASE_URL = 'https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/%s' |
| 22 |
GCLOUD_BASE_NAME='google-cloud-sdk' |
| 23 |
GCLOUD_ARCHIVE = '%s-343.0.0-linux-x86_64.tar.gz' % GCLOUD_BASE_NAME |
| 24 |
GCLOUD_URL = BASE_URL % GCLOUD_ARCHIVE |
| 25 |
|
| 26 |
|
| 27 |
def create_asset(target_dir): |
| 28 |
"""Create the asset.""" |
| 29 |
target_dir = os.path.abspath(target_dir) |
| 30 |
subprocess.check_call(['curl', GCLOUD_URL, '-o', GCLOUD_ARCHIVE]) |
| 31 |
|
| 32 |
# Extract the arcive to the target directory and remove it. |
| 33 |
subprocess.check_call(['tar', '-xzf', GCLOUD_ARCHIVE, |
| 34 |
'--strip-components=1', |
| 35 |
'-C', target_dir]) |
| 36 |
|
| 37 |
# Substitute the HOME directory in the environment so we don't overwrite |
| 38 |
# an existing gcloud configuration in $HOME/.config/gcloud |
| 39 |
env = os.environ.copy() |
| 40 |
env["HOME"] = target_dir |
| 41 |
gcloud_exe = os.path.join(target_dir, 'bin', 'gcloud') |
| 42 |
subprocess.check_call([gcloud_exe, 'components', |
| 43 |
'install', 'beta', 'cloud-datastore-emulator', |
| 44 |
'--quiet'], env=env) |
| 45 |
subprocess.check_call([gcloud_exe, 'components', |
| 46 |
'install', 'beta', 'bigtable', |
| 47 |
'--quiet'], env=env) |
| 48 |
subprocess.check_call([gcloud_exe, 'components', |
| 49 |
'install', 'pubsub-emulator', |
| 50 |
'--quiet'], env=env) |
| 51 |
subprocess.check_call([gcloud_exe, 'components', |
| 52 |
'install', 'beta', 'cloud-firestore-emulator', |
| 53 |
'--quiet'], env=env) |
| 54 |
# As of gcloud v250.0.0 and Cloud Firestore Emulator v1.4.6, there is a bug |
| 55 |
# that something expects the JAR to be executable, but it isn't. |
| 56 |
fs_jar = 'platform/cloud-firestore-emulator/cloud-firestore-emulator.jar' |
| 57 |
subprocess.check_call(['chmod', '+x', os.path.join(target_dir, fs_jar)]) |
| 58 |
subprocess.check_call([gcloud_exe, 'components','update', '--quiet'], env=env) |
| 59 |
|
| 60 |
# Remove the tarball. |
| 61 |
os.remove(GCLOUD_ARCHIVE) |
| 62 |
|
| 63 |
|
| 64 |
def main(): |
| 65 |
parser = argparse.ArgumentParser() |
| 66 |
parser.add_argument('--target_dir', '-t', required=True) |
| 67 |
args = parser.parse_args() |
| 68 |
create_asset(args.target_dir) |
| 69 |
|
| 70 |
|
| 71 |
if __name__ == '__main__': |
| 72 |
main() |