1#!/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"""Kernel Swapper.
17
18This class manages swapping kernel images for a Cloud Android instance.
19"""
20import subprocess
21
22from acloud import errors
23from acloud.public import report
24from acloud.internal.lib import android_compute_client
25from acloud.internal.lib import auth
26from acloud.internal.lib import utils
27
28
29# ssh flags used to communicate with the Cloud Android instance.
30SSH_FLAGS = [
31    '-q', '-o UserKnownHostsFile=/dev/null', '-o "StrictHostKeyChecking no"',
32    '-o ServerAliveInterval=10'
33]
34
35# Shell commands run on target.
36MOUNT_CMD = ('if mountpoint -q /boot ; then umount /boot ; fi ; '
37             'mount -t ext4 /dev/block/sda1 /boot')
38REBOOT_CMD = 'nohup reboot > /dev/null 2>&1 &'
39
40
41class KernelSwapper(object):
42    """A class that manages swapping a kernel image on a Cloud Android instance.
43
44    Attributes:
45        _compute_client: AndroidCopmuteClient object, manages AVD.
46        _instance_name: tring, name of Cloud Android Instance.
47        _target_ip: string, IP address of Cloud Android instance.
48        _ssh_flags: string list, flags to be used with ssh and scp.
49    """
50
51    def __init__(self, cfg, instance_name):
52        """Initialize.
53
54        Args:
55            cfg: AcloudConfig object, used to create credentials.
56            instance_name: string, instance name.
57        """
58        credentials = auth.CreateCredentials(cfg)
59        self._compute_client = android_compute_client.AndroidComputeClient(
60            cfg, credentials)
61        # Name of the Cloud Android instance.
62        self._instance_name = instance_name
63        # IP of the Cloud Android instance.
64        self._target_ip = self._compute_client.GetInstanceIP(instance_name)
65
66    def SwapKernel(self, local_kernel_image):
67        """Swaps the kernel image on target AVD with given kernel.
68
69        Mounts boot image containing the kernel image to the filesystem, then
70        overwrites that kernel image with a new kernel image, then reboots the
71        Cloud Android instance.
72
73        Args:
74            local_kernel_image: string, local path to a kernel image.
75
76        Returns:
77            A Report instance.
78        """
79        reboot_image = report.Report(command='swap_kernel')
80        try:
81            self._ShellCmdOnTarget(MOUNT_CMD)
82            self.PushFile(local_kernel_image, '/boot')
83            self.RebootTarget()
84        except subprocess.CalledProcessError as e:
85            reboot_image.AddError(str(e))
86            reboot_image.SetStatus(report.Status.FAIL)
87            return reboot_image
88        except errors.DeviceBootError as e:
89            reboot_image.AddError(str(e))
90            reboot_image.SetStatus(report.Status.BOOT_FAIL)
91            return reboot_image
92
93        reboot_image.SetStatus(report.Status.SUCCESS)
94        return reboot_image
95
96    def PushFile(self, src_path, dest_path):
97        """Pushes local file to target Cloud Android instance.
98
99        Args:
100            src_path: string, local path to file to be pushed.
101            dest_path: string, path on target where to push the file to.
102
103        Raises:
104            subprocess.CalledProcessError: see _ShellCmd.
105        """
106        cmd = 'scp %s %s root@%s:%s' % (' '.join(SSH_FLAGS), src_path,
107                                        self._target_ip, dest_path)
108        self._ShellCmd(cmd)
109
110    def RebootTarget(self):
111        """Reboots the target Cloud Android instance and waits for boot.
112
113        Raises:
114            subprocess.CalledProcessError: see _ShellCmd.
115            errors.DeviceBootError: if target fails to boot.
116        """
117        self._ShellCmdOnTarget(REBOOT_CMD)
118        self._compute_client.WaitForBoot(self._instance_name)
119
120    def _ShellCmdOnTarget(self, target_cmd):
121        """Runs a shell command on target Cloud Android instance.
122
123        Args:
124            target_cmd: string, shell command to be run on target.
125
126        Raises:
127            subprocess.CalledProcessError: see _ShellCmd.
128        """
129        ssh_cmd = 'ssh %s root@%s' % (' '.join(SSH_FLAGS), self._target_ip)
130        host_cmd = ' '.join([ssh_cmd, '"%s"' % target_cmd])
131        self._ShellCmd(host_cmd)
132
133    @staticmethod
134    def _ShellCmd(host_cmd):
135        """Runs a shell command on host device.
136
137        Args:
138            host_cmd: string, shell command to be run on host.
139
140        Raises:
141            subprocess.CalledProcessError: For any non-zero return code of
142                                           host_cmd.
143        """
144        utils.Retry(
145            retry_checker=lambda e: isinstance(e, subprocess.CalledProcessError),
146            max_retries=2,
147            functor=lambda cmd: subprocess.check_call(cmd, shell=True),
148            sleep_multiplier=0,
149            retry_backoff_factor=1,
150            cmd=host_cmd)
151