• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 - 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
15"""RemoteInstanceDeviceFactory provides basic interface to create an FVP
16device factory."""
17
18import os
19
20from acloud.internal import constants
21from acloud.internal.lib import utils
22from acloud.internal.lib import ssh
23from acloud.public.actions import gce_device_factory
24
25class RemoteInstanceDeviceFactory(gce_device_factory.GCEDeviceFactory):
26    """A class that can produce a FVP device."""
27    def __init__(self, avd_spec):
28        super().__init__(avd_spec)
29
30    def CreateInstance(self):
31        """Start a GCE instance, copy the necessary artifacts to it and then
32        start FVP.
33
34        Returns:
35            The instance.
36        """
37        instance = self.CreateGceInstance()
38        if instance in self.GetFailures():
39            return instance
40
41        try:
42            self._UploadArtifacts()
43            self._StartFVP()
44        except Exception as e:
45            self._SetFailures(instance, e)
46
47        return instance
48
49    @utils.TimeExecute(function_description="Processing and uploading local images")
50    def _UploadArtifacts(self):
51        """Copy artifacts to the GCE instance: the local images, the model
52        itself and support files.
53        """
54        images_dir = self._avd_spec.local_image_dir
55        images_path = os.path.join(images_dir, "required_images")
56        with open(images_path, "r") as images:
57            artifact_files = images.read().splitlines()
58        ssh_cmd = self._ssh.GetBaseCmd(constants.SSH_BIN)
59
60        cmd = ("tar -cf - --lzop -S -C {images_dir} {artifact_files} | "
61               "{ssh_cmd} -- tar -xf - --lzop -S".format(
62                   images_dir=images_dir,
63                   artifact_files=" ".join(artifact_files),
64                   ssh_cmd=ssh_cmd))
65        ssh.ShellCmdWithRetry(cmd)
66
67        model_bin = utils.GetBuildEnvironmentVariable("MODEL_BIN")
68        cmd = ("tar -cf - --lzop -S -C {model_dir} . | "
69               "{ssh_cmd} -- tar -xf - --lzop -S".format(
70                   model_dir=os.path.dirname(model_bin),
71                   ssh_cmd=ssh_cmd))
72        ssh.ShellCmdWithRetry(cmd)
73
74        self._ssh.ScpPushFile(
75            src_file="device/generic/goldfish/fvpbase/run_model_only",
76            dst_file="run_model_only")
77
78        cmd = "{ssh_cmd} -- mkdir -p lib64".format(ssh_cmd=ssh_cmd)
79        ssh.ShellCmdWithRetry(cmd)
80        host_out = utils.GetBuildEnvironmentVariable("ANDROID_HOST_OUT")
81        self._ssh.ScpPushFile(
82            src_file="%s/lib64/bind_to_localhost.so" % host_out,
83            dst_file="lib64/bind_to_localhost.so")
84
85    @utils.TimeExecute(function_description="Starting FVP")
86    def _StartFVP(self):
87        """Start the model on the GCE instance."""
88        ssh_cmd = self._ssh.GetBaseCmd(constants.SSH_BIN)
89        model_bin = utils.GetBuildEnvironmentVariable("MODEL_BIN")
90
91        cmd = ("{ssh_cmd} -- sh -c \"'ANDROID_HOST_OUT=. "
92               "ANDROID_PRODUCT_OUT=. MODEL_BIN=./{model_basename} "
93               "./run_model_only > /dev/null 2> /dev/null &'\"".format(
94            ssh_cmd=ssh_cmd,
95            model_basename=os.path.basename(model_bin)))
96        ssh.ShellCmdWithRetry(cmd)
97