1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tradefed.util.avd; 18 19 import com.android.tradefed.util.MultiMap; 20 21 import com.google.common.collect.Lists; 22 23 import java.io.File; 24 import java.util.List; 25 import java.util.Map; 26 27 /** A utility for Acloud related operations */ 28 public class AcloudUtil { 29 30 /** 31 * Build and return the command to launch GCE. Exposed for testing. 32 * 33 * @param binary Full path to the binary to run 34 * @param createCmd command to create the instance, e.g., create, create_gf 35 * @param reportFile Full path to the report file to save the output to 36 * @param buildTarget name of the device build target 37 * @param buildBranch name of the device build branch 38 * @param buildID ID of the device build 39 * @param ipDevice IP address of the remote host 40 * @param hostUser Name of the user to use for the remote host, must be set if ipDevice is set 41 * @param sshKeyPath path to the private ssh key file 42 * @param extraArgs a {@link List<String>} of extra command line args 43 * @param serviceAccountKeyPath path to the service account json key file 44 * @param offset integer of the device offset 45 * @param gceAccount account for the GCP project 46 * @param gceDriverParams a {@link List<String>} of parameters from gce-driver-param option 47 * @param gceDriverFileParams a {@link MultiMap<String, File>} of parameters from 48 * gce-driver-file-param option 49 * @param extraFiles a {@link MultiMap<File, String>} of extra files fromg gce-extra-files 50 * option, which contains the files to upload GCE instance during Acloud create. Key is 51 * local file, value is GCE destination path. 52 * @return a {@link List<String>} of command line args 53 */ buildGceCmd( String binary, String createCmd, File reportFile, String buildTarget, String buildBranch, String buildId, String ipDevice, String hostUser, String sshKeyPath, List<String> extraArgs, String serviceAccountKeyPath, Integer offset, String gceAccount, List<String> gceDriverParams, MultiMap<String, File> gceDriverFileParams, MultiMap<File, String> extraFiles)54 public static List<String> buildGceCmd( 55 String binary, 56 String createCmd, 57 File reportFile, 58 String buildTarget, 59 String buildBranch, 60 String buildId, 61 String ipDevice, 62 String hostUser, 63 String sshKeyPath, 64 List<String> extraArgs, 65 String serviceAccountKeyPath, 66 Integer offset, 67 String gceAccount, 68 List<String> gceDriverParams, 69 MultiMap<String, File> gceDriverFileParams, 70 MultiMap<File, String> extraFiles) { 71 List<String> gceArgs = Lists.newArrayList(binary); 72 gceArgs.add(createCmd); 73 gceArgs.addAll(extraArgs); 74 75 /* If args passed by gce-driver-param contain build-target or build_target, or 76 test device options include local-image and cvd-host-package to side load prebuilt virtual 77 device images, there is no need to pass the build info from device BuildInfo to gce 78 arguments. Otherwise, generate gce args from device BuildInfo. Please refer to acloud 79 arguments for the supported format: 80 https://android.googlesource.com/platform/tools/acloud/+/refs/heads/master/create/create_args.py */ 81 if (!gceDriverParams.contains("--build-target") 82 && !gceDriverParams.contains("--build_target") 83 && !(gceDriverFileParams.containsKey("local-image") 84 && gceDriverFileParams.containsKey("cvd-host-package"))) { 85 gceArgs.add("--build-target"); 86 gceArgs.add(buildTarget); 87 gceArgs.add("--branch"); 88 gceArgs.add(buildBranch); 89 gceArgs.add("--build-id"); 90 gceArgs.add(buildId); 91 } 92 93 for (Map.Entry<String, File> entry : gceDriverFileParams.entries()) { 94 gceArgs.add("--" + entry.getKey()); 95 gceArgs.add(entry.getValue().getAbsolutePath()); 96 } 97 98 if (!extraFiles.isEmpty()) { 99 gceArgs.add("--extra-files"); 100 for (File local : extraFiles.keySet()) { 101 for (String remoteDestination : extraFiles.get(local)) { 102 gceArgs.add(local.getAbsolutePath() + "," + remoteDestination); 103 } 104 } 105 } 106 107 // Add additional args passed by gce-driver-param. 108 gceArgs.addAll(gceDriverParams); 109 if (serviceAccountKeyPath != null) { 110 gceArgs.add("--service-account-json-private-key-path"); 111 gceArgs.add(serviceAccountKeyPath); 112 } 113 114 if (ipDevice != null) { 115 gceArgs.add("--host"); 116 gceArgs.add(ipDevice); 117 gceArgs.add("--host-user"); 118 gceArgs.add(hostUser); 119 gceArgs.add("--host-ssh-private-key-path"); 120 gceArgs.add(sshKeyPath); 121 } 122 gceArgs.add("--report_file"); 123 gceArgs.add(reportFile.getAbsolutePath()); 124 125 // Add base-instance-num args with offset, and override the remote adb port. 126 // When offset is 1, base-instance-num=2 and virtual device adb forward port is 6521. 127 if (offset != null) { 128 gceArgs.add("--base-instance-num"); 129 gceArgs.add(String.valueOf(offset + 1)); 130 } 131 132 if (gceAccount != null) { 133 gceArgs.add("--email"); 134 gceArgs.add(gceAccount); 135 } 136 // Do not pass flags --logcat_file and --serial_log_file to collect logcat and serial logs. 137 138 return gceArgs; 139 } 140 } 141