コード例 #1
0
 def test_make_targz(self, mock_uuid, mock_exec, mock_makedirs):
     self.assertEqual(bu.make_targz('/test/path'), 'fake_uuid.tar.gz')
     mock_exec.assert_called_with('tar',
                                  '-czf',
                                  'fake_uuid.tar.gz',
                                  '--directory',
                                  '/test/path',
                                  '.',
                                  logged=True)
コード例 #2
0
 def test_make_targz_with_name(self, mock_exec, mock_makedirs):
     self.assertEqual(bu.make_targz('/test/path', 'testname'), 'testname')
     mock_exec.assert_called_with('tar',
                                  '-czf',
                                  'testname',
                                  '--directory',
                                  '/test/path',
                                  '.',
                                  logged=True)
コード例 #3
0
 def test_make_targz_with_name(self, mock_exec, mock_makedirs):
     self.assertEqual(bu.make_targz('/test/path', 'testname'), 'testname')
     mock_exec.assert_called_with('tar', '-czf', 'testname', '--directory',
                                  '/test/path', '.', logged=True)
コード例 #4
0
 def test_make_targz(self, mock_uuid, mock_exec, mock_makedirs):
     self.assertEqual(bu.make_targz('/test/path'), 'fake_uuid.tar.gz')
     mock_exec.assert_called_with('tar', '-czf', 'fake_uuid.tar.gz',
                                  '--directory', '/test/path', '.',
                                  logged=True)
コード例 #5
0
ファイル: manager.py プロジェクト: tinyxiao/fuel-agent
    def do_mkbootstrap(self):
        """Building bootstrap image

        Currently supports only Ubuntu-Trusty
        Includes the following steps
        1) Allocate and configure debootstrap.
        2) Install packages
        3) Run user-post script(is defined)
        4) populate squashfs\init\vmlinuz files
        5) create metadata.yaml and pack thats all into tar.gz
        """

        LOG.info('--- Building bootstrap image (do_mkbootstrap) ---')
        # c_dir = output container directory, where all builded files will
        # be stored, before packaging into archive
        LOG.debug('Creating bootstrap container folder')
        c_dir = bu.mkdtemp_smart(CONF.image_build_dir,
                                 CONF.image_build_suffix + '_container')
        try:
            chroot = bu.mkdtemp_smart(
                CONF.image_build_dir, CONF.image_build_suffix)
            self.install_base_os(chroot)
            bs_scheme = self.driver.bootstrap_scheme
            # init modules, needed for bootstrap. Currently
            #  we support only one scheme initrd + rootfs + kernel
            initrd = filter(lambda x: x.name == 'initrd',
                            bs_scheme.modules)[0]
            rootfs = filter(lambda x: x.name == 'rootfs',
                            bs_scheme.modules)[0]
            metadata = {}
            metadata['os'] = self.driver.operating_system.to_dict()
            packages = self.driver.operating_system.packages
            metadata['packages'] = packages
            self._set_apt_repos(chroot, self.driver.operating_system.repos)
            self._update_metadata_with_repos(
                metadata, self.driver.operating_system.repos)
            LOG.debug('Installing packages using apt-get: %s',
                      ' '.join(packages))
            # disable hosts/resolv files
            bu.propagate_host_resolv_conf(chroot)
            bu.run_apt_get(chroot, packages=packages,
                           attempts=CONF.fetch_packages_attempts)
            LOG.debug('Post-install OS configuration')
            if hasattr(bs_scheme, 'extra_files') and bs_scheme.extra_files:
                for extra in bs_scheme.extra_files:
                        bu.rsync_inject(extra, chroot)
            if (hasattr(bs_scheme, 'root_ssh_authorized_file') and
                    bs_scheme.root_ssh_authorized_file):
                LOG.debug('Put ssh auth file %s',
                          bs_scheme.root_ssh_authorized_file)
                auth_file = os.path.join(chroot, 'root/.ssh/authorized_keys')
                utils.makedirs_if_not_exists(os.path.dirname(
                    auth_file), mode=0o700)
                shutil.copy(
                    bs_scheme.root_ssh_authorized_file,
                    auth_file)
                os.chmod(auth_file, 0o700)
            # Allow user to drop and run script inside chroot:
            if (hasattr(bs_scheme, 'post_script_file') and
                    bs_scheme.post_script_file):
                bu.run_script_in_chroot(
                    chroot, bs_scheme.post_script_file)
            # Save runtime_uuid into bootstrap
            bu.dump_runtime_uuid(bs_scheme.uuid,
                                 os.path.join(chroot,
                                              'etc/nailgun-agent/config.yaml'))
            bu.do_post_inst(chroot,
                            allow_unsigned_file=CONF.allow_unsigned_file,
                            force_ipv4_file=CONF.force_ipv4_file)
            # restore disabled hosts/resolv files
            bu.restore_resolv_conf(chroot)
            metadata['all_packages'] = bu.get_installed_packages(chroot)
            # We need to recompress initramfs with new compression:
            bu.recompress_initramfs(
                chroot,
                compress=initrd.compress_format)
            # Bootstrap nodes load the kernel and initramfs via the network,
            # therefore remove the kernel and initramfs located in root
            # filesystem to make the image smaller (and save the network
            # bandwidth and the boot time)
            bu.copy_kernel_initramfs(chroot, c_dir, clean=True)
            LOG.debug('Making sure there are no running processes '
                      'inside chroot before trying to umount chroot')
            if not bu.stop_chrooted_processes(chroot, signal=signal.SIGTERM):
                if not bu.stop_chrooted_processes(
                        chroot, signal=signal.SIGKILL):
                    raise errors.UnexpectedProcessError(
                        'Stopping chrooted processes failed. '
                        'There are some processes running in chroot %s',
                        chroot)
            bu.run_mksquashfs(
                chroot, os.path.join(c_dir, os.path.basename(rootfs.uri)),
                rootfs.compress_format)
            self.dump_mkbootstrap_meta(metadata, c_dir, bs_scheme)
            arch_file = bu.make_targz(c_dir, self.driver.data['output'])
            LOG.debug('Output archive file : {0}'.format(arch_file))
            LOG.info('--- Building bootstrap image END (do_mkbootstrap) ---')
            return arch_file
        except Exception as exc:
            LOG.error('Failed to bootstrap image: %s', exc)
            raise
        finally:
            LOG.info('Cleanup chroot')
            self.destroy_chroot(chroot)
            try:
                shutil.rmtree(c_dir)
            except OSError:
                LOG.debug('Finally: directory %s seems does not exist '
                          'or can not be removed', c_dir)