Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 1 | # Copyright 2018 - The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | r"""Delete entry point. |
| 15 | |
| 16 | Delete will handle all the logic related to deleting a local/remote instance |
| 17 | of an Android Virtual Device. |
| 18 | """ |
| 19 | |
| 20 | from __future__ import print_function |
chojoyce | 6e30cc5 | 2019-11-04 16:18:09 +0800 | [diff] [blame] | 21 | |
Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 22 | import logging |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 23 | import re |
| 24 | import subprocess |
Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 25 | |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 26 | from acloud import errors |
| 27 | from acloud.internal import constants |
cylan | d315c6d | 2019-12-06 17:06:56 +0000 | [diff] [blame] | 28 | from acloud.internal.lib import auth |
cylan | d315c6d | 2019-12-06 17:06:56 +0000 | [diff] [blame] | 29 | from acloud.internal.lib import cvd_compute_client_multi_stage |
Hsin-Yi Chen | 7d59edb | 2021-08-20 15:19:14 +0800 | [diff] [blame] | 30 | from acloud.internal.lib import emulator_console |
| 31 | from acloud.internal.lib import goldfish_remote_host_client |
herbertxue | eadbba0 | 2021-07-15 14:39:26 +0800 | [diff] [blame] | 32 | from acloud.internal.lib import oxygen_client |
cylan | d315c6d | 2019-12-06 17:06:56 +0000 | [diff] [blame] | 33 | from acloud.internal.lib import ssh as ssh_object |
herbertxue | eadbba0 | 2021-07-15 14:39:26 +0800 | [diff] [blame] | 34 | from acloud.internal.lib import utils |
Sam Chiu | 99dfee3 | 2018-11-20 10:19:17 +0800 | [diff] [blame] | 35 | from acloud.list import list as list_instances |
Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 36 | from acloud.public import config |
| 37 | from acloud.public import device_driver |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 38 | from acloud.public import report |
Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 39 | |
herbertxue | 1512f8a | 2019-06-27 13:56:23 +0800 | [diff] [blame] | 40 | |
Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 41 | logger = logging.getLogger(__name__) |
| 42 | |
chojoyce | 5535cfb | 2019-10-09 18:28:03 +0800 | [diff] [blame] | 43 | _COMMAND_GET_PROCESS_ID = ["pgrep", "run_cvd"] |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 44 | _COMMAND_GET_PROCESS_COMMAND = ["ps", "-o", "command", "-p"] |
chojoyce | 5535cfb | 2019-10-09 18:28:03 +0800 | [diff] [blame] | 45 | _RE_RUN_CVD = re.compile(r"^(?P<run_cvd>.+run_cvd)") |
herbertxue | 2aa26d1 | 2019-12-05 11:03:05 +0800 | [diff] [blame] | 46 | _LOCAL_INSTANCE_PREFIX = "local-" |
Dan Shi | e8b7e43 | 2021-08-18 11:01:31 -0700 | [diff] [blame] | 47 | _RE_OXYGEN_RELEASE_ERROR = re.compile( |
| 48 | r".*Error received while trying to release device: (?P<error>.*)$", re.DOTALL) |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 49 | |
Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 50 | |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 51 | def DeleteInstances(cfg, instances_to_delete): |
| 52 | """Delete instances according to instances_to_delete. |
| 53 | |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 54 | Args: |
| 55 | cfg: AcloudConfig object. |
Sam Chiu | 99dfee3 | 2018-11-20 10:19:17 +0800 | [diff] [blame] | 56 | instances_to_delete: List of list.Instance() object. |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 57 | |
| 58 | Returns: |
Hsin-Yi Chen | 3947479 | 2020-10-21 18:16:44 +0800 | [diff] [blame] | 59 | Report object. |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 60 | """ |
Hsin-Yi Chen | 9037ee4 | 2019-11-01 16:33:44 +0800 | [diff] [blame] | 61 | delete_report = report.Report(command="delete") |
Sam Chiu | 99dfee3 | 2018-11-20 10:19:17 +0800 | [diff] [blame] | 62 | remote_instance_list = [] |
| 63 | for instance in instances_to_delete: |
| 64 | if instance.islocal: |
Hsin-Yi Chen | 9037ee4 | 2019-11-01 16:33:44 +0800 | [diff] [blame] | 65 | if instance.avd_type == constants.TYPE_GF: |
| 66 | DeleteLocalGoldfishInstance(instance, delete_report) |
| 67 | elif instance.avd_type == constants.TYPE_CF: |
| 68 | DeleteLocalCuttlefishInstance(instance, delete_report) |
| 69 | else: |
| 70 | delete_report.AddError("Deleting %s is not supported." % |
| 71 | instance.avd_type) |
| 72 | delete_report.SetStatus(report.Status.FAIL) |
Sam Chiu | 99dfee3 | 2018-11-20 10:19:17 +0800 | [diff] [blame] | 73 | else: |
| 74 | remote_instance_list.append(instance.name) |
herbertxue | 9e1e27a | 2018-12-12 16:25:27 +0800 | [diff] [blame] | 75 | # Delete ssvnc viewer |
Sam Chiu | dd6ce36 | 2020-01-23 09:35:52 +0800 | [diff] [blame] | 76 | if instance.vnc_port: |
| 77 | utils.CleanupSSVncviewer(instance.vnc_port) |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 78 | |
Sam Chiu | 99dfee3 | 2018-11-20 10:19:17 +0800 | [diff] [blame] | 79 | if remote_instance_list: |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 80 | # TODO(119283708): We should move DeleteAndroidVirtualDevices into |
| 81 | # delete.py after gce is deprecated. |
| 82 | # Stop remote instances. |
Sam Chiu | 99dfee3 | 2018-11-20 10:19:17 +0800 | [diff] [blame] | 83 | return DeleteRemoteInstances(cfg, remote_instance_list, delete_report) |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 84 | |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 85 | return delete_report |
| 86 | |
| 87 | |
Sam Chiu | 99dfee3 | 2018-11-20 10:19:17 +0800 | [diff] [blame] | 88 | @utils.TimeExecute(function_description="Deleting remote instances", |
| 89 | result_evaluator=utils.ReportEvaluator, |
| 90 | display_waiting_dots=False) |
| 91 | def DeleteRemoteInstances(cfg, instances_to_delete, delete_report=None): |
| 92 | """Delete remote instances. |
| 93 | |
| 94 | Args: |
| 95 | cfg: AcloudConfig object. |
| 96 | instances_to_delete: List of instance names(string). |
| 97 | delete_report: Report object. |
| 98 | |
| 99 | Returns: |
| 100 | Report instance if there are instances to delete, None otherwise. |
Sam Chiu | 43d81c5 | 2020-02-04 10:49:47 +0800 | [diff] [blame] | 101 | |
| 102 | Raises: |
| 103 | error.ConfigError: when config doesn't support remote instances. |
Sam Chiu | 99dfee3 | 2018-11-20 10:19:17 +0800 | [diff] [blame] | 104 | """ |
Sam Chiu | 43d81c5 | 2020-02-04 10:49:47 +0800 | [diff] [blame] | 105 | if not cfg.SupportRemoteInstance(): |
| 106 | raise errors.ConfigError("No gcp project info found in config! " |
| 107 | "The execution of deleting remote instances " |
| 108 | "has been aborted.") |
Sam Chiu | 99dfee3 | 2018-11-20 10:19:17 +0800 | [diff] [blame] | 109 | utils.PrintColorString("") |
| 110 | for instance in instances_to_delete: |
| 111 | utils.PrintColorString(" - %s" % instance, utils.TextColors.WARNING) |
| 112 | utils.PrintColorString("") |
| 113 | utils.PrintColorString("status: waiting...", end="") |
| 114 | |
| 115 | # TODO(119283708): We should move DeleteAndroidVirtualDevices into |
| 116 | # delete.py after gce is deprecated. |
| 117 | # Stop remote instances. |
| 118 | delete_report = device_driver.DeleteAndroidVirtualDevices( |
| 119 | cfg, instances_to_delete, delete_report) |
| 120 | |
| 121 | return delete_report |
| 122 | |
| 123 | |
Hsin-Yi Chen | 9037ee4 | 2019-11-01 16:33:44 +0800 | [diff] [blame] | 124 | @utils.TimeExecute(function_description="Deleting local cuttlefish instances", |
Sam Chiu | 99dfee3 | 2018-11-20 10:19:17 +0800 | [diff] [blame] | 125 | result_evaluator=utils.ReportEvaluator) |
Hsin-Yi Chen | 9037ee4 | 2019-11-01 16:33:44 +0800 | [diff] [blame] | 126 | def DeleteLocalCuttlefishInstance(instance, delete_report): |
| 127 | """Delete a local cuttlefish instance. |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 128 | |
Sam Chiu | dd6ce36 | 2020-01-23 09:35:52 +0800 | [diff] [blame] | 129 | Delete local instance and write delete instance |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 130 | information to report. |
| 131 | |
Sam Chiu | 34e752e | 2019-08-16 18:15:36 +0800 | [diff] [blame] | 132 | Args: |
| 133 | instance: instance.LocalInstance object. |
| 134 | delete_report: Report object. |
| 135 | |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 136 | Returns: |
Hsin-Yi Chen | 9037ee4 | 2019-11-01 16:33:44 +0800 | [diff] [blame] | 137 | delete_report. |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 138 | """ |
Hsin-Yi Chen | f3663ca | 2020-07-17 14:10:27 +0800 | [diff] [blame] | 139 | ins_lock = instance.GetLock() |
| 140 | if not ins_lock.Lock(): |
Hsin-Yi Chen | caec52f | 2020-07-16 14:59:26 +0800 | [diff] [blame] | 141 | delete_report.AddError("%s is locked by another process." % |
| 142 | instance.name) |
| 143 | delete_report.SetStatus(report.Status.FAIL) |
| 144 | return delete_report |
| 145 | |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 146 | try: |
Hsin-Yi Chen | 3947479 | 2020-10-21 18:16:44 +0800 | [diff] [blame] | 147 | ins_lock.SetInUse(False) |
Sam Chiu | dd6ce36 | 2020-01-23 09:35:52 +0800 | [diff] [blame] | 148 | instance.Delete() |
| 149 | delete_report.SetStatus(report.Status.SUCCESS) |
| 150 | device_driver.AddDeletionResultToReport( |
| 151 | delete_report, [instance.name], failed=[], |
| 152 | error_msgs=[], |
| 153 | resource_name="instance") |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 154 | except subprocess.CalledProcessError as e: |
| 155 | delete_report.AddError(str(e)) |
| 156 | delete_report.SetStatus(report.Status.FAIL) |
Hsin-Yi Chen | caec52f | 2020-07-16 14:59:26 +0800 | [diff] [blame] | 157 | finally: |
Hsin-Yi Chen | f3663ca | 2020-07-17 14:10:27 +0800 | [diff] [blame] | 158 | ins_lock.Unlock() |
Sam Chiu | 34e752e | 2019-08-16 18:15:36 +0800 | [diff] [blame] | 159 | |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 160 | return delete_report |
Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 161 | |
| 162 | |
Hsin-Yi Chen | 9037ee4 | 2019-11-01 16:33:44 +0800 | [diff] [blame] | 163 | @utils.TimeExecute(function_description="Deleting local goldfish instances", |
| 164 | result_evaluator=utils.ReportEvaluator) |
| 165 | def DeleteLocalGoldfishInstance(instance, delete_report): |
| 166 | """Delete a local goldfish instance. |
| 167 | |
| 168 | Args: |
| 169 | instance: LocalGoldfishInstance object. |
| 170 | delete_report: Report object. |
| 171 | |
| 172 | Returns: |
| 173 | delete_report. |
| 174 | """ |
Hsin-Yi Chen | f3663ca | 2020-07-17 14:10:27 +0800 | [diff] [blame] | 175 | lock = instance.GetLock() |
| 176 | if not lock.Lock(): |
| 177 | delete_report.AddError("%s is locked by another process." % |
| 178 | instance.name) |
Hsin-Yi Chen | 9037ee4 | 2019-11-01 16:33:44 +0800 | [diff] [blame] | 179 | delete_report.SetStatus(report.Status.FAIL) |
Hsin-Yi Chen | f3663ca | 2020-07-17 14:10:27 +0800 | [diff] [blame] | 180 | return delete_report |
| 181 | |
| 182 | try: |
Hsin-Yi Chen | 3947479 | 2020-10-21 18:16:44 +0800 | [diff] [blame] | 183 | lock.SetInUse(False) |
Hsin-Yi Chen | f3663ca | 2020-07-17 14:10:27 +0800 | [diff] [blame] | 184 | if instance.adb.EmuCommand("kill") == 0: |
| 185 | delete_report.SetStatus(report.Status.SUCCESS) |
| 186 | device_driver.AddDeletionResultToReport( |
| 187 | delete_report, [instance.name], failed=[], |
| 188 | error_msgs=[], |
| 189 | resource_name="instance") |
| 190 | else: |
| 191 | delete_report.AddError("Cannot kill %s." % instance.device_serial) |
| 192 | delete_report.SetStatus(report.Status.FAIL) |
| 193 | finally: |
Hsin-Yi Chen | f3663ca | 2020-07-17 14:10:27 +0800 | [diff] [blame] | 194 | lock.Unlock() |
Hsin-Yi Chen | 9037ee4 | 2019-11-01 16:33:44 +0800 | [diff] [blame] | 195 | |
Hsin-Yi Chen | 9037ee4 | 2019-11-01 16:33:44 +0800 | [diff] [blame] | 196 | return delete_report |
| 197 | |
| 198 | |
Hsin-Yi Chen | 3947479 | 2020-10-21 18:16:44 +0800 | [diff] [blame] | 199 | def ResetLocalInstanceLockByName(name, delete_report): |
| 200 | """Set the lock state of a local instance to be not in use. |
| 201 | |
| 202 | Args: |
| 203 | name: The instance name. |
| 204 | delete_report: Report object. |
| 205 | """ |
| 206 | ins_lock = list_instances.GetLocalInstanceLockByName(name) |
| 207 | if not ins_lock: |
| 208 | delete_report.AddError("%s is not a valid local instance name." % name) |
| 209 | delete_report.SetStatus(report.Status.FAIL) |
| 210 | return |
| 211 | |
| 212 | if not ins_lock.Lock(): |
| 213 | delete_report.AddError("%s is locked by another process." % name) |
| 214 | delete_report.SetStatus(report.Status.FAIL) |
| 215 | return |
| 216 | |
| 217 | try: |
| 218 | ins_lock.SetInUse(False) |
| 219 | delete_report.SetStatus(report.Status.SUCCESS) |
| 220 | device_driver.AddDeletionResultToReport( |
| 221 | delete_report, [name], failed=[], error_msgs=[], |
| 222 | resource_name="instance") |
| 223 | finally: |
| 224 | ins_lock.Unlock() |
| 225 | |
| 226 | |
Hsin-Yi Chen | 7d59edb | 2021-08-20 15:19:14 +0800 | [diff] [blame] | 227 | @utils.TimeExecute(function_description=("Deleting remote host goldfish " |
| 228 | "instance"), |
| 229 | result_evaluator=utils.ReportEvaluator) |
| 230 | def DeleteHostGoldfishInstance(cfg, name, ssh_user, |
| 231 | ssh_private_key_path, delete_report): |
| 232 | """Delete a goldfish instance on a remote host by console command. |
| 233 | |
| 234 | Args: |
| 235 | cfg: An AcloudConfig object. |
| 236 | name: String, the instance name. |
| 237 | remote_host : String, the IP address of the host. |
| 238 | ssh_user: String or None, the ssh user for the host. |
| 239 | ssh_private_key_path: String or None, the ssh private key for the host. |
| 240 | delete_report: A Report object. |
| 241 | |
| 242 | Returns: |
| 243 | delete_report. |
| 244 | """ |
| 245 | ip_addr, port = goldfish_remote_host_client.ParseEmulatorConsoleAddress( |
| 246 | name) |
| 247 | try: |
| 248 | with emulator_console.RemoteEmulatorConsole( |
| 249 | ip_addr, port, |
| 250 | (ssh_user or constants.GCE_USER), |
| 251 | (ssh_private_key_path or cfg.ssh_private_key_path), |
| 252 | cfg.extra_args_ssh_tunnel) as console: |
| 253 | console.Kill() |
| 254 | delete_report.SetStatus(report.Status.SUCCESS) |
| 255 | device_driver.AddDeletionResultToReport( |
| 256 | delete_report, [name], failed=[], error_msgs=[], |
| 257 | resource_name="instance") |
| 258 | except errors.DeviceConnectionError as e: |
| 259 | delete_report.AddError("%s is not deleted: %s" % (name, str(e))) |
| 260 | delete_report.SetStatus(report.Status.FAIL) |
| 261 | return delete_report |
| 262 | |
| 263 | |
cylan | 0f61af6 | 2019-12-30 22:31:10 +0800 | [diff] [blame] | 264 | def CleanUpRemoteHost(cfg, remote_host, host_user, |
cylan | d315c6d | 2019-12-06 17:06:56 +0000 | [diff] [blame] | 265 | host_ssh_private_key_path=None): |
| 266 | """Clean up the remote host. |
| 267 | |
| 268 | Args: |
| 269 | cfg: An AcloudConfig instance. |
| 270 | remote_host : String, ip address or host name of the remote host. |
| 271 | host_user: String of user login into the instance. |
| 272 | host_ssh_private_key_path: String of host key for logging in to the |
| 273 | host. |
| 274 | |
| 275 | Returns: |
| 276 | A Report instance. |
| 277 | """ |
| 278 | delete_report = report.Report(command="delete") |
| 279 | credentials = auth.CreateCredentials(cfg) |
| 280 | compute_client = cvd_compute_client_multi_stage.CvdComputeClient( |
| 281 | acloud_config=cfg, |
| 282 | oauth2_credentials=credentials) |
| 283 | ssh = ssh_object.Ssh( |
| 284 | ip=ssh_object.IP(ip=remote_host), |
cylan | 0f61af6 | 2019-12-30 22:31:10 +0800 | [diff] [blame] | 285 | user=host_user, |
cylan | d315c6d | 2019-12-06 17:06:56 +0000 | [diff] [blame] | 286 | ssh_private_key_path=( |
| 287 | host_ssh_private_key_path or cfg.ssh_private_key_path)) |
| 288 | try: |
cylan | 0f61af6 | 2019-12-30 22:31:10 +0800 | [diff] [blame] | 289 | compute_client.InitRemoteHost(ssh, remote_host, host_user) |
cylan | d315c6d | 2019-12-06 17:06:56 +0000 | [diff] [blame] | 290 | delete_report.SetStatus(report.Status.SUCCESS) |
| 291 | device_driver.AddDeletionResultToReport( |
| 292 | delete_report, [remote_host], failed=[], |
| 293 | error_msgs=[], |
| 294 | resource_name="remote host") |
| 295 | except subprocess.CalledProcessError as e: |
| 296 | delete_report.AddError(str(e)) |
| 297 | delete_report.SetStatus(report.Status.FAIL) |
| 298 | |
| 299 | return delete_report |
| 300 | |
herbertxue | 2aa26d1 | 2019-12-05 11:03:05 +0800 | [diff] [blame] | 301 | |
Hsin-Yi Chen | 7d59edb | 2021-08-20 15:19:14 +0800 | [diff] [blame] | 302 | def DeleteInstanceByNames(cfg, instances, host_user, |
| 303 | host_ssh_private_key_path): |
| 304 | """Delete instances by the given instance names. |
| 305 | |
| 306 | This method can identify the following types of instance names: |
| 307 | local cuttlefish instance: local-instance-<id> |
| 308 | local goldfish instance: local-goldfish-instance-<id> |
Hsin-Yi Chen | df97204 | 2021-09-13 18:21:30 +0800 | [diff] [blame] | 309 | remote host goldfish instance: host-goldfish-<ip_addr>-<port>-<build_info> |
Hsin-Yi Chen | 7d59edb | 2021-08-20 15:19:14 +0800 | [diff] [blame] | 310 | remote instance: ins-<uuid>-<build_info> |
herbertxue | 2aa26d1 | 2019-12-05 11:03:05 +0800 | [diff] [blame] | 311 | |
| 312 | Args: |
| 313 | cfg: AcloudConfig object. |
| 314 | instances: List of instance name. |
Hsin-Yi Chen | 7d59edb | 2021-08-20 15:19:14 +0800 | [diff] [blame] | 315 | host_user: String or None, the ssh user for remote hosts. |
| 316 | host_ssh_private_key_path: String or None, the ssh private key for |
| 317 | remote hosts. |
herbertxue | 2aa26d1 | 2019-12-05 11:03:05 +0800 | [diff] [blame] | 318 | |
| 319 | Returns: |
| 320 | A Report instance. |
| 321 | """ |
| 322 | delete_report = report.Report(command="delete") |
Hsin-Yi Chen | 3947479 | 2020-10-21 18:16:44 +0800 | [diff] [blame] | 323 | local_names = set(name for name in instances if |
| 324 | name.startswith(_LOCAL_INSTANCE_PREFIX)) |
Hsin-Yi Chen | 7d59edb | 2021-08-20 15:19:14 +0800 | [diff] [blame] | 325 | remote_host_gf_names = set( |
| 326 | name for name in instances if |
| 327 | goldfish_remote_host_client.ParseEmulatorConsoleAddress(name)) |
| 328 | remote_names = list(set(instances) - local_names - remote_host_gf_names) |
| 329 | |
Hsin-Yi Chen | 3947479 | 2020-10-21 18:16:44 +0800 | [diff] [blame] | 330 | if local_names: |
| 331 | active_instances = list_instances.GetLocalInstancesByNames(local_names) |
| 332 | inactive_names = local_names.difference(ins.name for ins in |
| 333 | active_instances) |
| 334 | if active_instances: |
| 335 | utils.PrintColorString("Deleting local instances") |
| 336 | delete_report = DeleteInstances(cfg, active_instances) |
| 337 | if inactive_names: |
| 338 | utils.PrintColorString("Unlocking local instances") |
| 339 | for name in inactive_names: |
| 340 | ResetLocalInstanceLockByName(name, delete_report) |
Hsin-Yi Chen | 7d59edb | 2021-08-20 15:19:14 +0800 | [diff] [blame] | 341 | |
| 342 | if remote_host_gf_names: |
| 343 | for name in remote_host_gf_names: |
| 344 | DeleteHostGoldfishInstance( |
| 345 | cfg, name, host_user, host_ssh_private_key_path, delete_report) |
| 346 | |
Hsin-Yi Chen | 3947479 | 2020-10-21 18:16:44 +0800 | [diff] [blame] | 347 | if remote_names: |
| 348 | delete_report = DeleteRemoteInstances(cfg, remote_names, delete_report) |
herbertxue | 2aa26d1 | 2019-12-05 11:03:05 +0800 | [diff] [blame] | 349 | return delete_report |
| 350 | |
| 351 | |
herbertxue | eadbba0 | 2021-07-15 14:39:26 +0800 | [diff] [blame] | 352 | def _ReleaseOxygenDevice(cfg, instances, ip): |
| 353 | """ Release one Oxygen device. |
| 354 | |
| 355 | Args: |
| 356 | cfg: AcloudConfig object. |
| 357 | instances: List of instance name. |
| 358 | ip: String of device ip. |
| 359 | |
| 360 | Returns: |
| 361 | A Report instance. |
| 362 | """ |
| 363 | if len(instances) != 1: |
| 364 | raise errors.CommandArgError( |
| 365 | "The release device function doesn't support multiple instances. " |
| 366 | "Please check the specified instance names: %s" % instances) |
| 367 | instance_name = instances[0] |
| 368 | delete_report = report.Report(command="delete") |
| 369 | try: |
| 370 | oxygen_client.OxygenClient.ReleaseDevice(instance_name, ip, |
| 371 | cfg.oxygen_client) |
| 372 | delete_report.SetStatus(report.Status.SUCCESS) |
| 373 | device_driver.AddDeletionResultToReport( |
| 374 | delete_report, [instance_name], failed=[], |
| 375 | error_msgs=[], |
| 376 | resource_name="instance") |
| 377 | except subprocess.CalledProcessError as e: |
Dan Shi | e8b7e43 | 2021-08-18 11:01:31 -0700 | [diff] [blame] | 378 | logger.error("Failed to release device from Oxygen, error: %s", |
| 379 | e.output) |
| 380 | error = str(e) |
| 381 | match = _RE_OXYGEN_RELEASE_ERROR.match(e.output) |
| 382 | if match: |
| 383 | error = match.group("error").strip() |
| 384 | delete_report.AddError(error) |
| 385 | delete_report.SetErrorType(constants.ACLOUD_OXYGEN_RELEASE_ERROR) |
herbertxue | eadbba0 | 2021-07-15 14:39:26 +0800 | [diff] [blame] | 386 | delete_report.SetStatus(report.Status.FAIL) |
| 387 | return delete_report |
| 388 | |
| 389 | |
Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 390 | def Run(args): |
| 391 | """Run delete. |
| 392 | |
herbertxue | 7bd20a2 | 2019-02-22 14:08:54 +0800 | [diff] [blame] | 393 | After delete command executed, tool will return one Report instance. |
| 394 | If there is no instance to delete, just reutrn empty Report. |
| 395 | |
Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 396 | Args: |
| 397 | args: Namespace object from argparse.parse_args. |
Sam Chiu | 99dfee3 | 2018-11-20 10:19:17 +0800 | [diff] [blame] | 398 | |
| 399 | Returns: |
| 400 | A Report instance. |
Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 401 | """ |
herbertxue | 2aa26d1 | 2019-12-05 11:03:05 +0800 | [diff] [blame] | 402 | # Prioritize delete instances by names without query all instance info from |
| 403 | # GCP project. |
Sam Chiu | 43d81c5 | 2020-02-04 10:49:47 +0800 | [diff] [blame] | 404 | cfg = config.GetAcloudConfig(args) |
herbertxue | eadbba0 | 2021-07-15 14:39:26 +0800 | [diff] [blame] | 405 | if args.oxygen: |
| 406 | return _ReleaseOxygenDevice(cfg, args.instance_names, args.ip) |
herbertxue | 2aa26d1 | 2019-12-05 11:03:05 +0800 | [diff] [blame] | 407 | if args.instance_names: |
Sam Chiu | 43d81c5 | 2020-02-04 10:49:47 +0800 | [diff] [blame] | 408 | return DeleteInstanceByNames(cfg, |
Hsin-Yi Chen | 7d59edb | 2021-08-20 15:19:14 +0800 | [diff] [blame] | 409 | args.instance_names, |
| 410 | args.host_user, |
| 411 | args.host_ssh_private_key_path) |
cylan | d315c6d | 2019-12-06 17:06:56 +0000 | [diff] [blame] | 412 | if args.remote_host: |
cylan | d315c6d | 2019-12-06 17:06:56 +0000 | [diff] [blame] | 413 | return CleanUpRemoteHost(cfg, args.remote_host, args.host_user, |
| 414 | args.host_ssh_private_key_path) |
herbertxue | 2aa26d1 | 2019-12-05 11:03:05 +0800 | [diff] [blame] | 415 | |
Hsin-Yi Chen | 9037ee4 | 2019-11-01 16:33:44 +0800 | [diff] [blame] | 416 | instances = list_instances.GetLocalInstances() |
Sam Chiu | 43d81c5 | 2020-02-04 10:49:47 +0800 | [diff] [blame] | 417 | if not args.local_only and cfg.SupportRemoteInstance(): |
Hsin-Yi Chen | 9037ee4 | 2019-11-01 16:33:44 +0800 | [diff] [blame] | 418 | instances.extend(list_instances.GetRemoteInstances(cfg)) |
Sam Chiu | 99dfee3 | 2018-11-20 10:19:17 +0800 | [diff] [blame] | 419 | |
herbertxue | 2aa26d1 | 2019-12-05 11:03:05 +0800 | [diff] [blame] | 420 | if args.adb_port: |
Hsin-Yi Chen | 9037ee4 | 2019-11-01 16:33:44 +0800 | [diff] [blame] | 421 | instances = list_instances.FilterInstancesByAdbPort(instances, |
| 422 | args.adb_port) |
| 423 | elif not args.all: |
| 424 | # Provide instances list to user and let user choose what to delete if |
| 425 | # user didn't specify instances in args. |
| 426 | instances = list_instances.ChooseInstancesFromList(instances) |
herbertxue | 7bd20a2 | 2019-02-22 14:08:54 +0800 | [diff] [blame] | 427 | |
Hsin-Yi Chen | 3947479 | 2020-10-21 18:16:44 +0800 | [diff] [blame] | 428 | if not instances: |
| 429 | utils.PrintColorString("No instances to delete") |
Hsin-Yi Chen | 9037ee4 | 2019-11-01 16:33:44 +0800 | [diff] [blame] | 430 | return DeleteInstances(cfg, instances) |