1#!/usr/bin/python
2#
3# Copyright 2018 The Android Open-Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18"""Upload a local build to Google Compute Engine and run it."""
19
20import argparse
21import glob
22import os
23import subprocess
24
25
26def upload_artifacts(args):
27  dir = os.getcwd()
28  try:
29    os.chdir(args.image_dir)
30    images = glob.glob('*.img') + ["bootloader"]
31    if len(images) == 0:
32      raise OSError('File not found: ' + args.image_dir + '/*.img')
33    subprocess.check_call(
34      'tar -c -f - --lzop -S ' + ' '.join(images) +
35        ' | ssh %s@%s -- tar -x -f - --lzop -S' % (
36          args.user,
37          args.ip),
38      shell=True)
39  finally:
40    os.chdir(dir)
41
42  host_package = os.path.join(args.host_dir, 'cvd-host_package.tar.gz')
43  # host_package
44  subprocess.check_call(
45      'ssh %s@%s -- tar -x -z -f - < %s' % (
46          args.user,
47          args.ip,
48          host_package),
49      shell=True)
50
51
52def launch_cvd(args):
53  subprocess.check_call(
54      'ssh %s@%s -- bin/launch_cvd %s' % (
55          args.user,
56          args.ip,
57          ' '.join(args.runner_args)
58      ),
59      shell=True)
60
61
62def stop_cvd(args):
63  subprocess.call(
64      'ssh %s@%s -- bin/stop_cvd' % (
65          args.user,
66          args.ip),
67      shell=True)
68
69
70def main():
71  parser = argparse.ArgumentParser(
72      description='Upload a local build to Google Compute Engine and run it')
73  parser.add_argument(
74      '-host_dir',
75      type=str,
76      default=os.environ.get('ANDROID_HOST_OUT', '.'),
77      help='path to soong host out directory')
78  parser.add_argument(
79      '-image_dir',
80      type=str,
81      default=os.environ.get('ANDROID_PRODUCT_OUT', '.'),
82      help='path to the img files')
83  parser.add_argument(
84      '-user', type=str, default='vsoc-01',
85      help='user to update on the instance')
86  parser.add_argument(
87      '-ip', type=str,
88      help='ip address of the board')
89  parser.add_argument(
90      '-launch', default=False,
91      action='store_true',
92      help='launch the device')
93  parser.add_argument('runner_args', nargs='*', help='launch_cvd arguments')
94  args = parser.parse_args()
95  stop_cvd(args)
96  upload_artifacts(args)
97  if args.launch:
98    launch_cvd(args)
99
100
101if __name__ == '__main__':
102  main()
103