Ejemplo n.º 1
0
    def _run(self):
        if os.path.isfile(self._result()):
            logging.info(("{0} is already there. "
                          "Delete it to regenerate it."
                          ).format(self._result()))
            return self._result()

        self._require_sudo()

        bootstrap_cmd = Bootstrap()

        # This command is based upon the output of the bootstrap command
        bootstrap_result = bootstrap_cmd.run(self.config.get_base_config_file())

        workdir = get_workdir()

        print("Going to upgrade the bootstrap image to a lxc image.")

        with tempfile.TemporaryDirectory(dir=workdir) as tempdir:
            chown_to_user(tempdir)
            lxcimagedir = os.path.join(tempdir, "lxcimage")
            self._unpack_image(bootstrap_result, lxcimagedir)
            self._write_container_metadata(lxcimagedir)
            archive = self._pack_image(tempdir, lxcimagedir)
            chown_to_user(archive)
            create_artifact_dir()
            shutil.move(archive, self._result())

        print_success("Created lxc image {}.".format(self._result()))
        return self._result()
Ejemplo n.º 2
0
def test_bootstrap(config_files, monkeypatch):
    with open(config_files, "r") as main_file:
        def fakegetuid():
            return 0

        monkeypatch.setattr(os, 'getuid', fakegetuid)

        def fakechown(*_):
            pass

        monkeypatch.setattr(shutil, 'chown', fakechown)

        def fakerun(*popenargs, **kwargs):
            if get_command(popenargs) == "chroot":
                rootfs_path = get_command_parameter(popenargs, "chroot")
                if not os.path.exists(rootfs_path):
                    os.mkdir(rootfs_path)
            elif get_command(popenargs) == "debootstrap":
                rootfs_path = popenargs[0][-2]
                apt_dir = os.path.join(rootfs_path, 'etc', 'apt')
                os.makedirs(apt_dir)
                pass
            elif get_command(popenargs) == "tar":
                archive = get_command_parameter(popenargs, '-acf')
                with open(archive, mode="w") as fakearchive:
                    fakearchive.write("fake archive")
            elif popenargs[0][-2] == "dpkg" and popenargs[0][-1] == "--print-architecture":
                return subprocess.CompletedProcess("fakerun", 0, 'amd64')
            elif get_command(popenargs) == "lxd" and get_sub_command(popenargs) == "--version":
                return subprocess.CompletedProcess("fakerun", 0, '2.18')
            elif get_command(popenargs) == "printenv":
                if get_sub_command(popenargs) == "HOME":
                    return subprocess.CompletedProcess("fakerun", 0, '/no/such/directory')
                else:
                    return subprocess.CompletedProcess("fakerun", 0, '')
            else:
                print('Passthrough: {}'.format(get_command(popenargs)))
                return subprocess.run(*popenargs, **kwargs)

            return subprocess.CompletedProcess("fakerun", 0, '')

        monkeypatch.setattr(mockablerun, 'run_mockable', fakerun)

        monkeypatch.chdir(os.path.dirname(config_files))

        bootstrap_cmd = Bootstrap()
        with requests_mock.Mocker() as m:
            m.get('https://ftp-master.debian.org/keys/archive-key-8.asc', text='key file mockup')
            bootstrap_cmd.run(main_file)

        expected_result = bootstrap_cmd._result()
        assert os.path.exists(expected_result)

        previous_result_text = "previous result"
        with open(expected_result, mode="w") as previous_result:
            previous_result.write(previous_result_text)
        bootstrap_cmd2 = Bootstrap()
        bootstrap_cmd2.run(main_file)
        with open(expected_result, mode="r") as same_result:
            assert same_result.read() == previous_result_text
Ejemplo n.º 3
0
    def _clean(self):
        if os.path.isfile(self._result()):
            logging.info("Removing '{}'.".format(self._result()))
            os.remove(self._result())
            print_success("Removed lxc image {}.".format(self._result()))

        if self.clean_depth > 0:
            Bootstrap().clean_recursive(self.config.get_base_config_file(), self.clean_depth - 1)
Ejemplo n.º 4
0
 def _dry_run(self):
     plugins = {}
     plugins.update(Bootstrap().dry_run(self.config.get_base_config_file()))
     plugins.update(self._get_plugin_report())
     return plugins