Esempio n. 1
0
 def test_capabilities_nobackend(self, has_pycdlib_mocked, has_isoinfo_mocked,
                                 has_isoread_mocked, can_mount_mocked):
     self.assertIsNone(iso9660.iso9660(self.iso_path, ['read']))
     self.assertTrue(has_pycdlib_mocked.called)
     self.assertTrue(has_isoinfo_mocked.called)
     self.assertTrue(has_isoread_mocked.called)
     self.assertTrue(can_mount_mocked.called)
Esempio n. 2
0
 def test_capabilities_nobackend(self, has_pycdlib_mocked, has_isoinfo_mocked,
                                 has_isoread_mocked, can_mount_mocked):
     self.assertIsNone(iso9660.iso9660(self.iso_path, ['read']))
     self.assertTrue(has_pycdlib_mocked.called)
     self.assertTrue(has_isoinfo_mocked.called)
     self.assertTrue(has_isoread_mocked.called)
     self.assertTrue(can_mount_mocked.called)
 def test_iso_no_phone_home(self):
     path = os.path.join(self.tmpdir, "cloudinit.iso")
     instance_id = b"INSTANCE_ID"
     username = b"AVOCADO_USER"
     password = b"AVOCADO_PASSWORD"
     cloudinit.iso(path, instance_id, username, password)
     iso = iso9660.iso9660(path)
     self.assertIn(instance_id, iso.read("/meta-data"))
     user_data = iso.read("/user-data")
     self.assertIn(username, user_data)
     self.assertIn(password, user_data)
Esempio n. 4
0
 def test_iso_no_phone_home(self):
     path = os.path.join(self.tmpdir, "cloudinit.iso")
     instance_id = b"INSTANCE_ID"
     username = b"AVOCADO_USER"
     password = b"AVOCADO_PASSWORD"
     cloudinit.iso(path, instance_id, username, password)
     iso = iso9660.iso9660(path)
     self.assertIn(instance_id, iso.read("/meta-data"))
     user_data = iso.read("/user-data")
     self.assertIn(username, user_data)
     self.assertIn(password, user_data)
def has_iso_create_write():
    return iso9660.iso9660(os.devnull, ["create", "write"]) is not None
Esempio n. 6
0
 def test_non_existing_capabilities(self):
     self.assertIsNone(
         iso9660.iso9660(self.iso_path, ['non-existing', 'capabilities']))
Esempio n. 7
0
 def test_capabilities_pycdlib(self, has_pycdlib_mocked):
     instance = iso9660.iso9660(self.iso_path, ['read', 'create', 'write'])
     self.assertIsInstance(instance, iso9660.ISO9660PyCDLib)
     self.assertTrue(has_pycdlib_mocked.called)
Esempio n. 8
0
 def test_non_existing_capabilities(self):
     self.assertIsNone(iso9660.iso9660(self.iso_path,
                                       ['non-existing', 'capabilities']))
Esempio n. 9
0
 def test_capabilities_pycdlib(self, has_pycdlib_mocked):
     instance = iso9660.iso9660(self.iso_path, ['read', 'create', 'write'])
     self.assertIsInstance(instance, iso9660.ISO9660PyCDLib)
     self.assertTrue(has_pycdlib_mocked.called)
Esempio n. 10
0
def has_iso_create_write():
    return iso9660.iso9660(os.devnull, ["create", "write"]) is not None
Esempio n. 11
0
    def setup_cdrom(self):
        """
        Mount cdrom and copy vmlinuz and initrd.img.
        """
        error_context.context("Copying vmlinuz and initrd.img from install cdrom %s" %
                              self.cdrom_cd1)
        if not os.path.isdir(self.image_path):
            os.makedirs(self.image_path)

        if (self.params.get('unattended_delivery_method') in
                ['integrated', 'url']):
            i = iso9660.Iso9660Mount(self.cdrom_cd1)
            self.cdrom_cd1_mount = i.mnt_dir
        else:
            i = iso9660.iso9660(self.cdrom_cd1)

        if i is None:
            raise exceptions.TestFail("Could not instantiate an iso9660 class")

        i.copy(os.path.join(self.boot_path, os.path.basename(self.kernel)),
               self.kernel)
        assert(os.path.getsize(self.kernel) > 0)
        i.copy(os.path.join(self.boot_path, os.path.basename(self.initrd)),
               self.initrd)
        assert(os.path.getsize(self.initrd) > 0)

        if self.unattended_file.endswith('.preseed'):
            self.preseed_initrd()

        if self.params.get("vm_type") == "libvirt":
            if self.vm.driver_type == 'qemu':
                # Virtinstall command needs files "vmlinuz" and "initrd.img"
                os.chdir(self.image_path)
                base_kernel = os.path.basename(self.kernel)
                base_initrd = os.path.basename(self.initrd)
                if base_kernel != 'vmlinuz':
                    process.run("mv %s vmlinuz" % base_kernel, verbose=DEBUG)
                if base_initrd != 'initrd.img':
                    process.run("mv %s initrd.img" %
                                base_initrd, verbose=DEBUG)
                if (self.params.get('unattended_delivery_method') !=
                        'integrated'):
                    i.close()
                    utils_disk.cleanup(self.cdrom_cd1_mount)
            elif ((self.vm.driver_type == 'xen') and
                  (self.params.get('hvm_or_pv') == 'pv')):
                logging.debug("starting unattended content web server")

                self.url_auto_content_port = utils_misc.find_free_port(8100,
                                                                       8199,
                                                                       self.url_auto_content_ip)

                start_auto_content_server_thread(self.url_auto_content_port,
                                                 self.cdrom_cd1_mount)

                self.medium = 'url'
                self.url = ('http://%s:%s' % (self.url_auto_content_ip,
                                              self.url_auto_content_port))

                pxe_path = os.path.join(
                    os.path.dirname(self.image_path), 'xen')
                if not os.path.isdir(pxe_path):
                    os.makedirs(pxe_path)

                pxe_kernel = os.path.join(pxe_path,
                                          os.path.basename(self.kernel))
                pxe_initrd = os.path.join(pxe_path,
                                          os.path.basename(self.initrd))
                process.run("cp %s %s" % (self.kernel, pxe_kernel))
                process.run("cp %s %s" % (self.initrd, pxe_initrd))

                if 'repo=cdrom' in self.kernel_params:
                    # Red Hat
                    self.kernel_params = re.sub('repo\=[\:\w\d\/]*',
                                                'repo=http://%s:%s' %
                                                (self.url_auto_content_ip,
                                                 self.url_auto_content_port),
                                                self.kernel_params)
Esempio n. 12
0
def iso(
    output_path,
    instance_id,
    username=None,
    password=None,
    phone_home_host=None,
    phone_home_port=None,
    authorized_key=None,
):
    """
    Generates an ISO image with cloudinit configuration

    The content always include the cloudinit metadata, and optionally
    the userdata content.  On the userdata file, it may contain a
    username/password section (if both parameters are given) and/or a
    phone home section (if both host and port are given).

    :param output_path: the location of the resulting (to be created) ISO
                        image containing the cloudinit configuration
    :param instance_id: the ID of the cloud instance, a form of identification
                        for the dynamically created executing instances
    :param username: the username to be used when logging interactively on the
                     instance
    :param password: the password to be used along with username when
                     authenticating with the login services on the instance
    :param phone_home_host: the address of the host the instance
                            should contact once it has finished
                            booting
    :param phone_home_port: the port acting as an HTTP phone home
                            server that the instance should contact
                            once it has finished booting
    :param authorized_key: a SSH public key to be added as an authorized key
                           for the default user, similar to "ssh-rsa ..."
    :type authorized_key: str
    :raises: RuntimeError if the system can not create ISO images.  On such
             a case, user is expected to install supporting packages, such as
             pycdlib.
    """
    # The only supported method to create/write in an ISO today is via pycdlib
    out = iso9660.iso9660(output_path, ["create", "write"])
    if out is None:
        msg = (
            "The system lacks support for creating ISO images. ",
            "Please install pycdlib dependency and run again.",
        )
        raise RuntimeError(msg)
    out.create(flags={
        "interchange_level": 3,
        "joliet": 3,
        "vol_ident": "cidata"
    })
    metadata = METADATA_TEMPLATE.format(instance_id,
                                        instance_id).encode(astring.ENCODING)
    out.write("/meta-data", metadata)
    userdata = USERDATA_HEADER
    if username:
        userdata += USERNAME_TEMPLATE.format(username)
        if username == "root":
            userdata += "\ndisable_root: False\n"
        if password:
            userdata += PASSWORD_TEMPLATE.format(password)
        if authorized_key:
            userdata += AUTHORIZED_KEY_TEMPLATE.format(authorized_key)
    if phone_home_host and phone_home_port:
        userdata += PHONE_HOME_TEMPLATE.format(phone_home_host,
                                               phone_home_port)
    out.write("/user-data", userdata.encode(astring.ENCODING))
    out.close()
Esempio n. 13
0
 def test_non_existing_capabilities(self):
     self.assertIsNone(
         iso9660.iso9660(self.iso_path, ["non-existing", "capabilities"]))
Esempio n. 14
0
    def setup_cdrom(self):
        """
        Mount cdrom and copy vmlinuz and initrd.img.
        """
        error_context.context(
            "Copying vmlinuz and initrd.img from install cdrom %s" %
            self.cdrom_cd1)
        if not os.path.isdir(self.image_path):
            os.makedirs(self.image_path)

        if (self.params.get('unattended_delivery_method')
                in ['integrated', 'url']):
            i = iso9660.Iso9660Mount(self.cdrom_cd1)
            self.cdrom_cd1_mount = i.mnt_dir
        else:
            i = iso9660.iso9660(self.cdrom_cd1)

        if i is None:
            raise exceptions.TestFail("Could not instantiate an iso9660 class")

        i.copy(os.path.join(self.boot_path, os.path.basename(self.kernel)),
               self.kernel)
        assert (os.path.getsize(self.kernel) > 0)
        i.copy(os.path.join(self.boot_path, os.path.basename(self.initrd)),
               self.initrd)
        assert (os.path.getsize(self.initrd) > 0)

        if self.unattended_file.endswith('.preseed'):
            self.preseed_initrd()

        if self.params.get("vm_type") == "libvirt":
            if self.vm.driver_type == 'qemu':
                # Virtinstall command needs files "vmlinuz" and "initrd.img"
                os.chdir(self.image_path)
                base_kernel = os.path.basename(self.kernel)
                base_initrd = os.path.basename(self.initrd)
                if base_kernel != 'vmlinuz':
                    process.run("mv %s vmlinuz" % base_kernel, verbose=DEBUG)
                if base_initrd != 'initrd.img':
                    process.run("mv %s initrd.img" % base_initrd,
                                verbose=DEBUG)
                if (self.params.get('unattended_delivery_method') !=
                        'integrated'):
                    i.close()
                    utils_disk.cleanup(self.cdrom_cd1_mount)
            elif ((self.vm.driver_type == 'xen')
                  and (self.params.get('hvm_or_pv') == 'pv')):
                logging.debug("starting unattended content web server")

                self.url_auto_content_port = utils_misc.find_free_port(
                    8100, 8199, self.url_auto_content_ip)

                start_auto_content_server_thread(self.url_auto_content_port,
                                                 self.cdrom_cd1_mount)

                self.medium = 'url'
                self.url = (
                    'http://%s:%s' %
                    (self.url_auto_content_ip, self.url_auto_content_port))

                pxe_path = os.path.join(os.path.dirname(self.image_path),
                                        'xen')
                if not os.path.isdir(pxe_path):
                    os.makedirs(pxe_path)

                pxe_kernel = os.path.join(pxe_path,
                                          os.path.basename(self.kernel))
                pxe_initrd = os.path.join(pxe_path,
                                          os.path.basename(self.initrd))
                process.run("cp %s %s" % (self.kernel, pxe_kernel))
                process.run("cp %s %s" % (self.initrd, pxe_initrd))

                if 'repo=cdrom' in self.kernel_params:
                    # Red Hat
                    self.kernel_params = re.sub(
                        'repo\=[\:\w\d\/]*', 'repo=http://%s:%s' %
                        (self.url_auto_content_ip, self.url_auto_content_port),
                        self.kernel_params)