blob: d0bdf3550b5bbb4af57e7cd6e69b0258430f9f01 [file] [log] [blame]
Keun Soo Yimb293fdb2016-09-21 16:03:44 -07001#!/usr/bin/env python
2#
3# Copyright 2016 - 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"""Public Device Driver APIs.
18
19This module provides public device driver APIs that can be called
20as a Python library.
21
22TODO(fdeng): The following APIs have not been implemented
23 - RebootAVD(ip):
24 - RegisterSshPubKey(username, key):
25 - UnregisterSshPubKey(username, key):
26 - CleanupStaleImages():
27 - CleanupStaleDevices():
28"""
29
30import datetime
31import logging
32import os
33
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070034import dateutil.parser
35import dateutil.tz
36
37from acloud.public import avd
38from acloud.public import errors
39from acloud.public import report
40from acloud.internal import constants
41from acloud.internal.lib import auth
42from acloud.internal.lib import android_build_client
43from acloud.internal.lib import android_compute_client
44from acloud.internal.lib import gstorage_client
45from acloud.internal.lib import utils
46
47logger = logging.getLogger(__name__)
48
49ALL_SCOPES = " ".join([android_build_client.AndroidBuildClient.SCOPE,
50 gstorage_client.StorageClient.SCOPE,
51 android_compute_client.AndroidComputeClient.SCOPE])
52
53MAX_BATCH_CLEANUP_COUNT = 100
54
55
56class AndroidVirtualDevicePool(object):
57 """A class that manages a pool of devices."""
58
59 def __init__(self, cfg, devices=None):
60 self._devices = devices or []
61 self._cfg = cfg
62 credentials = auth.CreateCredentials(cfg, ALL_SCOPES)
63 self._build_client = android_build_client.AndroidBuildClient(
64 credentials)
65 self._storage_client = gstorage_client.StorageClient(credentials)
66 self._compute_client = android_compute_client.AndroidComputeClient(
67 cfg, credentials)
68
69 def _CreateGceImageWithBuildInfo(self, build_target, build_id):
70 """Creates a Gce image using build from Launch Control.
71
72 Clone avd-system.tar.gz of a build to a cache storage bucket
73 using launch control api. And then create a Gce image.
74
75 Args:
76 build_target: Target name, e.g. "gce_x86-userdebug"
77 build_id: Build id, a string, e.g. "2263051", "P2804227"
78
79 Returns:
80 String, name of the Gce image that has been created.
81 """
82 logger.info("Creating a new gce image using build: build_id %s, "
83 "build_target %s", build_id, build_target)
84 disk_image_id = utils.GenerateUniqueName(
85 suffix=self._cfg.disk_image_name)
86 self._build_client.CopyTo(
87 build_target,
88 build_id,
89 artifact_name=self._cfg.disk_image_name,
90 destination_bucket=self._cfg.storage_bucket_name,
91 destination_path=disk_image_id)
92 disk_image_url = self._storage_client.GetUrl(
93 self._cfg.storage_bucket_name, disk_image_id)
94 try:
95 image_name = self._compute_client.GenerateImageName(build_target,
96 build_id)
97 self._compute_client.CreateImage(image_name=image_name,
98 source_uri=disk_image_url)
99 finally:
100 self._storage_client.Delete(self._cfg.storage_bucket_name,
101 disk_image_id)
102 return image_name
103
104 def _CreateGceImageWithLocalFile(self, local_disk_image):
105 """Create a Gce image with a local image file.
106
107 The local disk image can be either a tar.gz file or a
108 raw vmlinux image.
109 e.g. /tmp/avd-system.tar.gz or /tmp/android_system_disk_syslinux.img
110 If a raw vmlinux image is provided, it will be archived into a tar.gz file.
111
112 The final tar.gz file will be uploaded to a cache bucket in storage.
113
114 Args:
115 local_disk_image: string, path to a local disk image,
116
117 Returns:
118 String, name of the Gce image that has been created.
119
120 Raises:
121 DriverError: if a file with an unexpected extension is given.
122 """
123 logger.info("Creating a new gce image from a local file %s",
124 local_disk_image)
125 with utils.TempDir() as tempdir:
126 if local_disk_image.endswith(self._cfg.disk_raw_image_extension):
127 dest_tar_file = os.path.join(tempdir,
128 self._cfg.disk_image_name)
129 utils.MakeTarFile(
130 src_dict={local_disk_image: self._cfg.disk_raw_image_name},
131 dest=dest_tar_file)
132 local_disk_image = dest_tar_file
133 elif not local_disk_image.endswith(self._cfg.disk_image_extension):
134 raise errors.DriverError(
135 "Wrong local_disk_image type, must be a *%s file or *%s file"
136 % (self._cfg.disk_raw_image_extension,
137 self._cfg.disk_image_extension))
138
139 disk_image_id = utils.GenerateUniqueName(
140 suffix=self._cfg.disk_image_name)
141 self._storage_client.Upload(
142 local_src=local_disk_image,
143 bucket_name=self._cfg.storage_bucket_name,
144 object_name=disk_image_id,
145 mime_type=self._cfg.disk_image_mime_type)
146 disk_image_url = self._storage_client.GetUrl(
147 self._cfg.storage_bucket_name, disk_image_id)
148 try:
149 image_name = self._compute_client.GenerateImageName()
150 self._compute_client.CreateImage(image_name=image_name,
151 source_uri=disk_image_url)
152 finally:
153 self._storage_client.Delete(self._cfg.storage_bucket_name,
154 disk_image_id)
155 return image_name
156
157 def CreateDevices(self,
158 num,
159 build_target=None,
160 build_id=None,
161 gce_image=None,
162 local_disk_image=None,
163 cleanup=True,
164 extra_data_disk_size_gb=None,
165 precreated_data_image=None):
166 """Creates |num| devices for given build_target and build_id.
167
168 - If gce_image is provided, will use it to create an instance.
169 - If local_disk_image is provided, will upload it to a temporary
170 caching storage bucket which is defined by user as |storage_bucket_name|
171 And then create an gce image with it; and then create an instance.
172 - If build_target and build_id are provided, will clone the disk image
173 via launch control to the temporary caching storage bucket.
174 And then create an gce image with it; and then create an instance.
175
176 Args:
177 num: Number of devices to create.
178 build_target: Target name, e.g. "gce_x86-userdebug"
179 build_id: Build id, a string, e.g. "2263051", "P2804227"
180 gce_image: string, if given, will use this image
181 instead of creating a new one.
182 implies cleanup=False.
183 local_disk_image: string, path to a local disk image, e.g.
184 /tmp/avd-system.tar.gz
185 cleanup: boolean, if True clean up compute engine image after creating
186 the instance.
187 extra_data_disk_size_gb: Integer, size of extra disk, or None.
188 precreated_data_image: A string, the image to use for the extra disk.
189
190 Raises:
191 errors.DriverError: If no source is specified for image creation.
192 """
193 if gce_image:
194 # GCE image is provided, we can directly move to instance creation.
195 logger.info("Using existing gce image %s", gce_image)
196 image_name = gce_image
197 cleanup = False
198 elif local_disk_image:
199 image_name = self._CreateGceImageWithLocalFile(local_disk_image)
200 elif build_target and build_id:
201 image_name = self._CreateGceImageWithBuildInfo(build_target,
202 build_id)
203 else:
204 raise errors.DriverError(
205 "Invalid image source, must specify one of the following: gce_image, "
206 "local_disk_image, or build_target and build id.")
207
208 # Create GCE instances.
209 try:
210 for _ in range(num):
211 instance = self._compute_client.GenerateInstanceName(
212 build_target, build_id)
213 extra_disk_name = None
214 if extra_data_disk_size_gb > 0:
215 extra_disk_name = self._compute_client.GetDataDiskName(
216 instance)
217 self._compute_client.CreateDisk(extra_disk_name,
218 precreated_data_image,
219 extra_data_disk_size_gb)
220 self._compute_client.CreateInstance(instance, image_name,
221 extra_disk_name)
222 ip = self._compute_client.GetInstanceIP(instance)
223 self.devices.append(avd.AndroidVirtualDevice(
224 ip=ip, instance_name=instance))
225 finally:
226 if cleanup:
227 self._compute_client.DeleteImage(image_name)
228
229 def DeleteDevices(self):
230 """Deletes devices.
231
232 Returns:
233 A tuple, (deleted, failed, error_msgs)
234 deleted: A list of names of instances that have been deleted.
235 faild: A list of names of instances that we fail to delete.
236 error_msgs: A list of failure messages.
237 """
238 instance_names = [device.instance_name for device in self._devices]
239 return self._compute_client.DeleteInstances(instance_names,
240 self._cfg.zone)
241
242 def WaitForBoot(self):
243 """Waits for all devices to boot up.
244
245 Returns:
246 A dictionary that contains all the failures.
247 The key is the name of the instance that fails to boot,
248 the value is an errors.DeviceBootTimeoutError object.
249 """
250 failures = {}
251 for device in self._devices:
252 try:
253 self._compute_client.WaitForBoot(device.instance_name)
254 except errors.DeviceBootTimeoutError as e:
255 failures[device.instance_name] = e
256 return failures
257
258 @property
259 def devices(self):
260 """Returns a list of devices in the pool.
261
262 Returns:
263 A list of devices in the pool.
264 """
265 return self._devices
266
267
268def _AddDeletionResultToReport(report_obj, deleted, failed, error_msgs,
269 resource_name):
270 """Adds deletion result to a Report object.
271
272 This function will add the following to report.data.
273 "deleted": [
274 {"name": "resource_name", "type": "resource_name"},
275 ],
276 "failed": [
277 {"name": "resource_name", "type": "resource_name"},
278 ],
279 This function will append error_msgs to report.errors.
280
281 Args:
282 report_obj: A Report object.
283 deleted: A list of names of the resources that have been deleted.
284 failed: A list of names of the resources that we fail to delete.
285 error_msgs: A list of error message strings to be added to the report.
286 resource_name: A string, representing the name of the resource.
287 """
288 for name in deleted:
289 report_obj.AddData(key="deleted",
290 value={"name": name,
291 "type": resource_name})
292 for name in failed:
293 report_obj.AddData(key="failed",
294 value={"name": name,
295 "type": resource_name})
296 report_obj.AddErrors(error_msgs)
297 if failed or error_msgs:
298 report_obj.SetStatus(report.Status.FAIL)
299
300
301def _FetchSerialLogsFromDevices(compute_client, instance_names, output_file,
302 port):
303 """Fetch serial logs from a port for a list of devices to a local file.
304
305 Args:
306 compute_client: An object of android_compute_client.AndroidComputeClient
307 instance_names: A list of instance names.
308 output_file: A path to a file ending with "tar.gz"
309 port: The number of serial port to read from, 0 for serial output, 1 for
310 logcat.
311 """
312 with utils.TempDir() as tempdir:
313 src_dict = {}
314 for instance_name in instance_names:
315 serial_log = compute_client.GetSerialPortOutput(
316 instance=instance_name, port=port)
317 file_name = "%s.log" % instance_name
318 file_path = os.path.join(tempdir, file_name)
319 src_dict[file_path] = file_name
320 with open(file_path, "w") as f:
321 f.write(serial_log.encode("utf-8"))
322 utils.MakeTarFile(src_dict, output_file)
323
324
325def CreateAndroidVirtualDevices(cfg,
326 build_target=None,
327 build_id=None,
328 num=1,
329 gce_image=None,
330 local_disk_image=None,
331 cleanup=True,
332 serial_log_file=None,
333 logcat_file=None):
334 """Creates one or multiple android devices.
335
336 Args:
337 cfg: An AcloudConfig instance.
338 build_target: Target name, e.g. "gce_x86-userdebug"
339 build_id: Build id, a string, e.g. "2263051", "P2804227"
340 num: Number of devices to create.
341 gce_image: string, if given, will use this gce image
342 instead of creating a new one.
343 implies cleanup=False.
344 local_disk_image: string, path to a local disk image, e.g.
345 /tmp/avd-system.tar.gz
346 cleanup: boolean, if True clean up compute engine image and
347 disk image in storage after creating the instance.
348 serial_log_file: A path to a file where serial output should
Fang Dengfbef7c92017-02-08 14:09:34 -0800349 be saved to.
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700350 logcat_file: A path to a file where logcat logs should be saved.
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700351
352 Returns:
353 A Report instance.
354 """
355 r = report.Report(command="create")
356 credentials = auth.CreateCredentials(cfg, ALL_SCOPES)
357 compute_client = android_compute_client.AndroidComputeClient(cfg,
358 credentials)
359 try:
360 device_pool = AndroidVirtualDevicePool(cfg)
361 device_pool.CreateDevices(
362 num,
363 build_target,
364 build_id,
365 gce_image,
366 local_disk_image,
367 cleanup,
368 extra_data_disk_size_gb=cfg.extra_data_disk_size_gb,
369 precreated_data_image=cfg.precreated_data_image_map.get(
370 cfg.extra_data_disk_size_gb))
371 failures = device_pool.WaitForBoot()
372 # Write result to report.
373 for device in device_pool.devices:
374 device_dict = {"ip": device.ip,
375 "instance_name": device.instance_name}
376 if device.instance_name in failures:
377 r.AddData(key="devices_failing_boot", value=device_dict)
378 r.AddError(str(failures[device.instance_name]))
379 else:
380 r.AddData(key="devices", value=device_dict)
381 if failures:
382 r.SetStatus(report.Status.BOOT_FAIL)
383 else:
384 r.SetStatus(report.Status.SUCCESS)
385
386 # Dump serial and logcat logs.
387 if serial_log_file:
Fang Dengfbef7c92017-02-08 14:09:34 -0800388 _FetchSerialLogsFromDevices(
389 compute_client,
390 instance_names=[d.instance_name for d in device_pool.devices],
391 port=constants.DEFAULT_SERIAL_PORT,
392 output_file=serial_log_file)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700393 if logcat_file:
Fang Dengfbef7c92017-02-08 14:09:34 -0800394 _FetchSerialLogsFromDevices(
395 compute_client,
396 instance_names=[d.instance_name for d in device_pool.devices],
397 port=constants.LOGCAT_SERIAL_PORT,
398 output_file=logcat_file)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700399 except errors.DriverError as e:
400 r.AddError(str(e))
401 r.SetStatus(report.Status.FAIL)
402 return r
403
404
405def DeleteAndroidVirtualDevices(cfg, instance_names):
406 """Deletes android devices.
407
408 Args:
409 cfg: An AcloudConfig instance.
410 instance_names: A list of names of the instances to delete.
411
412 Returns:
413 A Report instance.
414 """
415 r = report.Report(command="delete")
416 credentials = auth.CreateCredentials(cfg, ALL_SCOPES)
417 compute_client = android_compute_client.AndroidComputeClient(cfg,
418 credentials)
419 try:
420 deleted, failed, error_msgs = compute_client.DeleteInstances(
421 instance_names, cfg.zone)
422 _AddDeletionResultToReport(
423 r, deleted,
424 failed, error_msgs,
425 resource_name="instance")
426 if r.status == report.Status.UNKNOWN:
427 r.SetStatus(report.Status.SUCCESS)
428 except errors.DriverError as e:
429 r.AddError(str(e))
430 r.SetStatus(report.Status.FAIL)
431 return r
432
433
434def _FindOldItems(items, cut_time, time_key):
435 """Finds items from |items| whose timestamp is earlier than |cut_time|.
436
437 Args:
438 items: A list of items. Each item is a dictionary represent
439 the properties of the item. It should has a key as noted
440 by time_key.
441 cut_time: A datetime.datatime object.
442 time_key: String, key for the timestamp.
443
444 Returns:
445 A list of those from |items| whose timestamp is earlier than cut_time.
446 """
447 cleanup_list = []
448 for item in items:
449 t = dateutil.parser.parse(item[time_key])
450 if t < cut_time:
451 cleanup_list.append(item)
452 return cleanup_list
453
454
455def Cleanup(cfg, expiration_mins):
456 """Cleans up stale gce images, gce instances, and disk images in storage.
457
458 Args:
459 cfg: An AcloudConfig instance.
460 expiration_mins: Integer, resources older than |expiration_mins| will
461 be cleaned up.
462
463 Returns:
464 A Report instance.
465 """
466 r = report.Report(command="cleanup")
467 try:
468 cut_time = (datetime.datetime.now(dateutil.tz.tzlocal()) -
469 datetime.timedelta(minutes=expiration_mins))
470 logger.info(
471 "Cleaning up any gce images/instances and cached build artifacts."
472 "in google storage that are older than %s", cut_time)
473 credentials = auth.CreateCredentials(cfg, ALL_SCOPES)
474 compute_client = android_compute_client.AndroidComputeClient(
475 cfg, credentials)
476 storage_client = gstorage_client.StorageClient(credentials)
477
478 # Cleanup expired instances
479 items = compute_client.ListInstances(zone=cfg.zone)
480 cleanup_list = [
481 item["name"]
482 for item in _FindOldItems(items, cut_time, "creationTimestamp")
483 ]
484 logger.info("Found expired instances: %s", cleanup_list)
485 for i in range(0, len(cleanup_list), MAX_BATCH_CLEANUP_COUNT):
486 result = compute_client.DeleteInstances(
487 instances=cleanup_list[i:i + MAX_BATCH_CLEANUP_COUNT],
488 zone=cfg.zone)
489 _AddDeletionResultToReport(r, *result, resource_name="instance")
490
491 # Cleanup expired images
492 items = compute_client.ListImages()
493 skip_list = cfg.precreated_data_image_map.viewvalues()
494 cleanup_list = [
495 item["name"]
496 for item in _FindOldItems(items, cut_time, "creationTimestamp")
497 if item["name"] not in skip_list
498 ]
499 logger.info("Found expired images: %s", cleanup_list)
500 for i in range(0, len(cleanup_list), MAX_BATCH_CLEANUP_COUNT):
501 result = compute_client.DeleteImages(
502 image_names=cleanup_list[i:i + MAX_BATCH_CLEANUP_COUNT])
503 _AddDeletionResultToReport(r, *result, resource_name="image")
504
505 # Cleanup expired disks
506 # Disks should have been attached to instances with autoDelete=True.
507 # However, sometimes disks may not be auto deleted successfully.
508 items = compute_client.ListDisks(zone=cfg.zone)
509 cleanup_list = [
510 item["name"]
511 for item in _FindOldItems(items, cut_time, "creationTimestamp")
512 if not item.get("users")
513 ]
514 logger.info("Found expired disks: %s", cleanup_list)
515 for i in range(0, len(cleanup_list), MAX_BATCH_CLEANUP_COUNT):
516 result = compute_client.DeleteDisks(
517 disk_names=cleanup_list[i:i + MAX_BATCH_CLEANUP_COUNT],
518 zone=cfg.zone)
519 _AddDeletionResultToReport(r, *result, resource_name="disk")
520
521 # Cleanup expired google storage
522 items = storage_client.List(bucket_name=cfg.storage_bucket_name)
523 cleanup_list = [
524 item["name"]
525 for item in _FindOldItems(items, cut_time, "timeCreated")
526 ]
527 logger.info("Found expired cached artifacts: %s", cleanup_list)
528 for i in range(0, len(cleanup_list), MAX_BATCH_CLEANUP_COUNT):
529 result = storage_client.DeleteFiles(
530 bucket_name=cfg.storage_bucket_name,
531 object_names=cleanup_list[i:i + MAX_BATCH_CLEANUP_COUNT])
532 _AddDeletionResultToReport(
533 r, *result, resource_name="cached_build_artifact")
534
535 # Everything succeeded, write status to report.
536 if r.status == report.Status.UNKNOWN:
537 r.SetStatus(report.Status.SUCCESS)
538 except errors.DriverError as e:
539 r.AddError(str(e))
540 r.SetStatus(report.Status.FAIL)
541 return r
542
543
544def AddSshRsa(cfg, user, ssh_rsa_path):
545 """Add public ssh rsa key to the project.
546
547 Args:
548 cfg: An AcloudConfig instance.
549 user: the name of the user which the key belongs to.
550 ssh_rsa_path: The absolute path to public rsa key.
551
552 Returns:
553 A Report instance.
554 """
555 r = report.Report(command="sshkey")
556 try:
557 credentials = auth.CreateCredentials(cfg, ALL_SCOPES)
558 compute_client = android_compute_client.AndroidComputeClient(
559 cfg, credentials)
560 compute_client.AddSshRsa(user, ssh_rsa_path)
561 r.SetStatus(report.Status.SUCCESS)
562 except errors.DriverError as e:
563 r.AddError(str(e))
564 r.SetStatus(report.Status.FAIL)
565 return r