def install_ssp(self, ssp_url): """Downloads and installs the given server package. @param ssp_url: The URL of the ssp to download and install. """ # The host dir is mounted directly on /usr/local/autotest within the # container. The SSP structure assumes it gets untarred into the # /usr/local directory of the container's rootfs. In order to unpack # with the correct directory structure, create a tmpdir, mount the # container's host dir as ./autotest, and unpack the SSP. if not self.is_running(): super(Zygote, self).install_ssp(ssp_url) return usr_local_path = os.path.join(self.host_path, 'usr', 'local') os.makedirs(usr_local_path) with lxc_utils.TempDir(dir=usr_local_path) as tmpdir: download_tmp = os.path.join(tmpdir, 'autotest_server_package.tar.bz2') lxc.download_extract(ssp_url, download_tmp, usr_local_path) container_ssp_path = os.path.join( constants.CONTAINER_HOST_DIR, constants.CONTAINER_AUTOTEST_DIR.lstrip(os.path.sep)) self.attach_run('mkdir -p %s && mount --bind %s %s' % (constants.CONTAINER_AUTOTEST_DIR, container_ssp_path, constants.CONTAINER_AUTOTEST_DIR))
def install_ssp(self, ssp_url): """Downloads and installs the given server package. @param ssp_url: The URL of the ssp to download and install. """ # The host dir is mounted directly on /usr/local/autotest within the # container. The SSP structure assumes it gets untarred into the # /usr/local directory of the container's rootfs. In order to unpack # with the correct directory structure, create a tmpdir, mount the # container's host dir as ./autotest, and unpack the SSP. tmpdir = None autotest_tmp = None try: tmpdir = tempfile.mkdtemp(dir=self.container_path, prefix='%s.' % self.name, suffix='.tmp') autotest_tmp = os.path.join(tmpdir, 'autotest') os.mkdir(autotest_tmp) utils.run('sudo mount --bind %s %s' % (self.host_path, autotest_tmp)) download_tmp = os.path.join(tmpdir, 'autotest_server_package.tar.bz2') lxc.download_extract(ssp_url, download_tmp, tmpdir) finally: if autotest_tmp is not None: try: utils.run('sudo umount %s' % autotest_tmp) except error.CmdError: logging.exception('Failure while cleaning up SSP tmpdir.') if tmpdir is not None: utils.run('sudo rm -rf %s' % tmpdir)
def _download_and_install_base_container(self): """Downloads the base image, untars and configures it.""" base_path = os.path.join(self.container_path, self.base_name) tar_path = os.path.join(self.container_path, '%s.tar.xz' % self.base_name) # Force cleanup of any previously downloaded/installed base containers. # This ensures a clean setup of the new base container. # # TODO(kenobi): Add a check to ensure that the base container doesn't # get deleted while snapshot clones exist (otherwise running tests might # get disrupted). path_to_cleanup = [tar_path, base_path] for path in path_to_cleanup: if os.path.exists(path): utils.run('sudo rm -rf "%s"' % path) container_url = constants.CONTAINER_BASE_URL_FMT % self.base_name lxc.download_extract(container_url, tar_path, self.container_path) # Remove the downloaded container tar file. utils.run('sudo rm "%s"' % tar_path) # Update container config with container_path from global config. config_path = os.path.join(base_path, 'config') rootfs_path = os.path.join(base_path, 'rootfs') utils.run(('sudo sed ' '-i "s|\(lxc\.rootfs[[:space:]]*=\).*$|\\1 {rootfs}|" ' '"{config}"').format(rootfs=rootfs_path, config=config_path))
def install_ssp(self, ssp_url): """Downloads and installs the given server package. @param ssp_url: The URL of the ssp to download and install. """ usr_local_path = os.path.join(self.rootfs, 'usr', 'local') autotest_pkg_path = os.path.join(usr_local_path, 'autotest_server_package.tar.bz2') # Changes within the container rootfs require sudo. utils.run('sudo mkdir -p %s' % usr_local_path) lxc.download_extract(ssp_url, autotest_pkg_path, usr_local_path)
def install_ssp(self, ssp_url): """Downloads and installs the given server package. @param ssp_url: The URL of the ssp to download and install. """ usr_local_path = os.path.join(self.rootfs, 'usr', 'local') autotest_pkg_path = os.path.join(usr_local_path, 'autotest_server_package.tar.bz2') # sudo is required so os.makedirs may not work. utils.run('sudo mkdir -p %s'% usr_local_path) lxc.download_extract(ssp_url, autotest_pkg_path, usr_local_path)
def setup_base(self, name=constants.BASE, force_delete=False): """Setup base container. @param name: Name of the base container, default to base. @param force_delete: True to force to delete existing base container. This action will destroy all running test containers. Default is set to False. """ if not self.container_path: raise error.ContainerError( 'You must set a valid directory to store containers in ' 'global config "AUTOSERV/ container_path".') if not os.path.exists(self.container_path): os.makedirs(self.container_path) base_path = os.path.join(self.container_path, name) if self.exist(name) and not force_delete: logging.error( 'Base container already exists. Set force_delete to True ' 'to force to re-stage base container. Note that this ' 'action will destroy all running test containers') # Set proper file permission. base container in moblab may have # owner of not being root. Force to update the folder's owner. # TODO(dshi): Change root to current user when test container can be # unprivileged container. utils.run('sudo chown -R root "%s"' % base_path) utils.run('sudo chgrp -R root "%s"' % base_path) return # Destroy existing base container if exists. if self.exist(name): # TODO: We may need to destroy all snapshots created from this base # container, not all container. self.destroy_all() # Download and untar the base container. tar_path = os.path.join(self.container_path, '%s.tar.xz' % name) path_to_cleanup = [tar_path, base_path] for path in path_to_cleanup: if os.path.exists(path): utils.run('sudo rm -rf "%s"' % path) container_url = constants.CONTAINER_BASE_URL_FMT % name lxc.download_extract(container_url, tar_path, self.container_path) # Remove the downloaded container tar file. utils.run('sudo rm "%s"' % tar_path) # Set proper file permission. # TODO(dshi): Change root to current user when test container can be # unprivileged container. utils.run('sudo chown -R root "%s"' % base_path) utils.run('sudo chgrp -R root "%s"' % base_path) # Update container config with container_path from global config. config_path = os.path.join(base_path, 'config') rootfs_path = os.path.join(base_path, 'rootfs') utils.run(('sudo sed ' '-i "s|\(lxc\.rootfs[[:space:]]*=\).*$|\\1 {rootfs}|" ' '"{config}"').format(rootfs=rootfs_path, config=config_path)) self.base_container = Container.createFromExistingDir( self.container_path, name) self._setup_shared_host_path()