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: ' + args.image_dir + '/*.img')
18    subprocess.check_call(
19      'tar -c -f - --lzop -S ' + ' '.join(images) +
20        ' | ssh %s@%s -- tar -x -f - --lzop -S' % (
21          args.user,
22          args.ip),
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      'ssh %s@%s -- tar -x -z -f - < %s' % (
31          args.user,
32          args.ip,
33          host_package),
34      shell=True)
35
36
37def launch_cvd(args):
38  subprocess.check_call(
39      'ssh %s@%s -- bin/launch_cvd %s' % (
40          args.user,
41          args.ip,
42          ' '.join(args.runner_args)
43      ),
44      shell=True)
45
46
47def stop_cvd(args):
48  subprocess.call(
49      'ssh %s@%s -- bin/stop_cvd' % (
50          args.user,
51          args.ip),
52      shell=True)
53
54
55def main():
56  parser = argparse.ArgumentParser(
57      description='Upload a local build to Google Compute Engine and run it')
58  parser.add_argument(
59      '-host_dir',
60      type=str,
61      default=os.environ.get('ANDROID_HOST_OUT', '.'),
62      help='path to the dist directory')
63  parser.add_argument(
64      '-image_dir',
65      type=str,
66      default=os.environ.get('ANDROID_PRODUCT_OUT', '.'),
67      help='path to the img files')
68  parser.add_argument(
69      '-user', type=str, default='vsoc-01',
70      help='user to update on the instance')
71  parser.add_argument(
72      '-ip', type=str,
73      help='ip address of the board')
74  parser.add_argument(
75      '-launch', default=False,
76      action='store_true',
77      help='launch the device')
78  parser.add_argument('runner_args', nargs='*', help='launch_cvd arguments')
79  args = parser.parse_args()
80  stop_cvd(args)
81  upload_artifacts(args)
82  if args.launch:
83    launch_cvd(args)
84
85
86if __name__ == '__main__':
87  main()
88