• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2
3"""Upload a local build to Google Compute Engine and run it."""
4
5import argparse
6import glob
7import os
8import subprocess
9
10
11def upload_artifacts(args):
12  dir = os.getcwd()
13  try:
14    os.chdir(args.image_dir)
15    images = glob.glob('*.img')
16    if len(images) == 0:
17      raise OSError('File not found: %s' + image_pat)
18    subprocess.check_call(
19      'tar -c -f - --lzop -S ' + ' '.join(images) +
20        ' | gcloud compute ssh %s@%s -- tar -x -f - --lzop -S' % (
21          args.user,
22          args.instance),
23      shell=True)
24  finally:
25    os.chdir(dir)
26
27  host_package = os.path.join(args.host_dir, 'cvd-host_package.tar.gz')
28  # host_package
29  subprocess.check_call(
30      'gcloud compute ssh %s@%s -- tar -x -z -f - < %s' % (
31          args.user,
32          args.instance,
33          host_package),
34      shell=True)
35
36
37def launch_cvd(args):
38  if args.data_image:
39    subprocess.check_call(
40        'gcloud compute ssh %s@%s -- bin/launch_cvd --data-image %s '
41        '--data-policy create_if_missing --blank-data-image-mb %d' % (
42            args.user,
43            args.instance,
44            args.data_image,
45            args.blank_data_image_mb),
46        shell=True)
47  else:
48    subprocess.check_call(
49        'gcloud compute ssh %s@%s -- bin/launch_cvd' % (
50            args.user,
51            args.instance),
52        shell=True)
53
54
55def stop_cvd(args):
56  subprocess.call(
57      'gcloud compute ssh %s@%s -- bin/stop_cvd' % (
58          args.user,
59          args.instance),
60      shell=True)
61
62
63def main():
64  parser = argparse.ArgumentParser(
65      description='Upload a local build to Google Compute Engine and run it')
66  parser.add_argument(
67      '-host_dir',
68      type=str,
69      default=os.environ.get('ANDROID_HOST_OUT', '.'),
70      help='path to the dist directory')
71  parser.add_argument(
72      '-image_dir',
73      type=str,
74      default=os.environ.get('ANDROID_PRODUCT_OUT', '.'),
75      help='path to the img files')
76  parser.add_argument(
77      '-instance', type=str, required=True,
78      help='instance to update')
79  parser.add_argument(
80      '-user', type=str, default='vsoc-01',
81      help='user to update on the instance')
82  parser.add_argument(
83      '-data-image', type=str, default=None,
84      help='userdata image file name, this file will be used instead of default one')
85  parser.add_argument(
86      '-blank-data-image-mb', type=int, default=4098,
87      help='custom userdata image size in megabytes')
88  parser.add_argument(
89      '-launch', default=False,
90      action='store_true',
91      help='launch the device')
92  args = parser.parse_args()
93  stop_cvd(args)
94  upload_artifacts(args)
95  if args.launch:
96    launch_cvd(args)
97
98
99if __name__ == '__main__':
100  main()
101