xref: /aosp_15_r20/external/autotest/server/cros/tradefed/push_arc_image.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Copyright 2021 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import os
7
8_BUILDS_BUCKET = 'gs://chromeos-arc-images/builds'
9
10_ABI_MAP = {
11        'armeabi-v7a': 'arm',
12        'arm64-v8a': 'arm64',
13        'x86': 'x86',
14        'x86_64': 'x86_64'
15}
16
17# This version and beyond contains logic in push_to_device.py that supports
18# HOST:PORT format for specifying the remote machine.
19_PTD_MIN_VERSION_MAP = {
20        'pi-arc': 7740639,
21        'rvc-arc': 7741959,
22        'sc-arc-dev': 7743996,
23}
24
25
26def push_userdebug_image(host, branch_prefix, lunch_target, download_func,
27                         install_bundle_func, run_func):
28    """This pushes a userdebug android image to the host.
29
30    This downloads the userdebug android image and push_to_device.py tool
31    from a google storage bucket.
32    This uses the push_to_device.py tool to push the image onto the host.
33
34    @param host: target host to push the image to.
35    @param branch_prefix: the branch name prefix of where the image is
36                          (e.g. rvc-arc, pi-arc). This does not have to be
37                          the exact branch name for a particular release
38                          (e.g. rvc-arc-m91).
39    @param lunch_target: the target lunch name (e.g. cheets, bertha)
40    @param download_func: function for downloading an object. This shall be
41                          self._download_to_cache when invoking from TradefedTest class.
42    @param install_bundle_func: function for downloading and unarchiving files.
43                                This shall be self._install_bundle when invoking
44                                from TradefedTest class.
45    @param run_func: function for running a command. This shall be
46                     self._run when invoking from TradefedTest class.
47
48    @returns True on success, False otherwise.
49    """
50    arc_version = host.get_arc_version()
51    if not arc_version:
52        logging.error('Failed to determine ARC version.')
53        return False
54
55    # The split is necessary because push_to_device.py puts the whole image name
56    # in CHROMEOS_ARC_VERSION, e.g. bertha_x86_64-img-7759413.
57    # The split won't hurt even if it is just a number e.g.
58    # CHROMEOS_ARC_VERSION=7759413.
59    arc_version = int(arc_version.split('-')[-1])
60
61    abi = _ABI_MAP[host.get_arc_primary_abi()]
62
63    # Using '*' here to let gsutil figure out the release branch name.
64    # arc_version is unique and will not show multiple branches.
65    image_base_uri = '{}/git_{}-*linux-{}_{}-userdebug'.format(
66            _BUILDS_BUCKET, branch_prefix, lunch_target, abi)
67
68    image_uri = '{}/{}/{}_{}-img-{}.zip'.format(image_base_uri, arc_version,
69                                                lunch_target, abi, arc_version)
70    se_policy_uri = '{}/{}/sepolicy.zip'.format(image_base_uri, arc_version)
71
72    image_file = download_func(image_uri)
73    se_policy_file = download_func(se_policy_uri)
74
75    if branch_prefix in _PTD_MIN_VERSION_MAP:
76        ptd_version = max(arc_version, _PTD_MIN_VERSION_MAP[branch_prefix])
77    else:
78        logging.warning(
79                '{} is not in _PTD_MIN_VERSION_MAP. This might fail to fetch '
80                'the push_to_device tool.'.format(branch_prefix))
81        ptd_version = arc_version
82
83    push_to_device_uri = '{}/{}/push_to_device.zip'.format(
84            image_base_uri, ptd_version)
85
86    push_to_device_dir = install_bundle_func(push_to_device_uri)
87    push_to_device_tool = os.path.join(push_to_device_dir, 'push_to_device.py')
88
89    # This file on the device tells the infrastructure
90    # that the device has to be reprovisioned before running other tasks.
91    host.run('touch /mnt/stateful_partition/.force_provision', )
92    logging.info('Pushing ARC userdebug image {} to {}.'.format(
93            arc_version, host.host_port))
94    run_func(
95            push_to_device_tool,
96            args=[
97                    '--use-prebuilt-file',
98                    image_file,
99                    '--sepolicy-artifacts-path',
100                    se_policy_file,
101                    '--force',
102                    host.host_port,
103            ],
104            ignore_status=False,
105            verbose=True,
106            nickname='Push userdebug image.',
107    )
108    return True
109