Beispiel #1
0
def _create_image(data, libvirt_xml):
    """ create libvirt.xml and copy files into instance path """
    def basepath(path=''):
        return os.path.abspath(os.path.join(data['basepath'], path))

    def imagepath(path=''):
        return os.path.join(FLAGS.images_path, path)

    def image_url(path):
        return "%s:%s/_images/%s" % (FLAGS.s3_host, FLAGS.s3_port, path)

    logging.info(basepath('disk'))
    try:
        os.makedirs(data['basepath'])
        os.chmod(data['basepath'], 0777)
    except OSError:
        # TODO: there is already an instance with this name, do something
        pass
    try:
        logging.info('Creating image for: %s', data['instance_id'])
        f = open(basepath('libvirt.xml'), 'w')
        f.write(libvirt_xml)
        f.close()
        if not FLAGS.fake_libvirt:
            if FLAGS.use_s3:
                if not os.path.exists(basepath('disk')):
                    utils.fetchfile(image_url("%s/image" % data['image_id']),
                                    basepath('disk-raw'))
                if not os.path.exists(basepath('kernel')):
                    utils.fetchfile(image_url("%s/image" % data['kernel_id']),
                                    basepath('kernel'))
                if not os.path.exists(basepath('ramdisk')):
                    utils.fetchfile(image_url("%s/image" % data['ramdisk_id']),
                                    basepath('ramdisk'))
            else:
                if not os.path.exists(basepath('disk')):
                    shutil.copyfile(imagepath("%s/image" % data['image_id']),
                                    basepath('disk-raw'))
                if not os.path.exists(basepath('kernel')):
                    shutil.copyfile(imagepath("%s/image" % data['kernel_id']),
                                    basepath('kernel'))
                if not os.path.exists(basepath('ramdisk')):
                    shutil.copyfile(imagepath("%s/image" % data['ramdisk_id']),
                                    basepath('ramdisk'))
            if data['key_data']:
                logging.info('Injecting key data into image %s' %
                             data['image_id'])
                disk.inject_key(data['key_data'], basepath('disk-raw'))
            if os.path.exists(basepath('disk')):
                os.remove(basepath('disk'))
            bytes = INSTANCE_TYPES[
                data['instance_type']]['local_gb'] * 1024 * 1024 * 1024
            disk.partition(basepath('disk-raw'), basepath('disk'), bytes)
        logging.info('Done create image for: %s', data['instance_id'])
    except Exception as ex:
        return {'exception': ex}
Beispiel #2
0
Datei: node.py Projekt: jxta/cc
def _create_image(data, libvirt_xml):
    """ create libvirt.xml and copy files into instance path """
    def basepath(path=''):
        return os.path.abspath(os.path.join(data['basepath'], path))

    def imagepath(path=''):
        return os.path.join(FLAGS.images_path, path)

    def image_url(path):
        return "%s:%s/_images/%s" % (FLAGS.s3_host, FLAGS.s3_port, path)
    logging.info(basepath('disk'))
    try:
        os.makedirs(data['basepath'])
        os.chmod(data['basepath'], 0777)
    except OSError:
        # TODO: there is already an instance with this name, do something
        pass
    try:
        logging.info('Creating image for: %s', data['instance_id'])
        f = open(basepath('libvirt.xml'), 'w')
        f.write(libvirt_xml)
        f.close()
        if not FLAGS.fake_libvirt:
            if FLAGS.use_s3:
                if not os.path.exists(basepath('disk')):
                    utils.fetchfile(image_url("%s/image" % data['image_id']),
                       basepath('disk-raw'))
                if not os.path.exists(basepath('kernel')):
                    utils.fetchfile(image_url("%s/image" % data['kernel_id']),
                                basepath('kernel'))
                if not os.path.exists(basepath('ramdisk')):
                    utils.fetchfile(image_url("%s/image" % data['ramdisk_id']),
                           basepath('ramdisk'))
            else:
                if not os.path.exists(basepath('disk')):
                    shutil.copyfile(imagepath("%s/image" % data['image_id']),
                        basepath('disk-raw'))
                if not os.path.exists(basepath('kernel')):
                    shutil.copyfile(imagepath("%s/image" % data['kernel_id']),
                        basepath('kernel'))
                if not os.path.exists(basepath('ramdisk')):
                    shutil.copyfile(imagepath("%s/image" %
                        data['ramdisk_id']),
                        basepath('ramdisk'))
            if data['key_data']:
                logging.info('Injecting key data into image %s' %
                        data['image_id'])
                disk.inject_key(data['key_data'], basepath('disk-raw'))
            if os.path.exists(basepath('disk')):
                os.remove(basepath('disk'))
            bytes = INSTANCE_TYPES[data['instance_type']]['local_gb'] * 1024 * 1024 * 1024
            disk.partition(basepath('disk-raw'), basepath('disk'), bytes)
        logging.info('Done create image for: %s', data['instance_id'])
    except Exception as ex:
        return {'exception': ex}
Beispiel #3
0
    def _create_image(self, libvirt_xml):
        # syntactic nicety
        data = self.datamodel
        basepath = self.basepath

        # ensure directories exist and are writable
        yield self._pool.simpleExecute('mkdir -p %s' % basepath())
        yield self._pool.simpleExecute('chmod 0777 %s' % basepath())


        # TODO(termie): these are blocking calls, it would be great
        #               if they weren't.
        logging.info('Creating image for: %s', data['instance_id'])
        f = open(basepath('libvirt.xml'), 'w')
        f.write(libvirt_xml)
        f.close()

        if FLAGS.fake_libvirt:
            logging.info('fake_libvirt, nothing to do for create_image')
            raise defer.returnValue(None);

        if FLAGS.use_s3:
            _fetch_file = self._fetch_s3_image
        else:
            _fetch_file = self._fetch_local_image

        if not os.path.exists(basepath('disk')):
           yield _fetch_file(data['image_id'], basepath('disk-raw'))
        if not os.path.exists(basepath('kernel')):
           yield _fetch_file(data['kernel_id'], basepath('kernel'))
        if not os.path.exists(basepath('ramdisk')):
           yield _fetch_file(data['ramdisk_id'], basepath('ramdisk'))

        execute = lambda cmd, input=None: self._pool.simpleExecute(cmd=cmd,
                                                                   input=input,
                                                                   error_ok=1)

        key = data['key_data']
        net = None
        if FLAGS.simple_network:
            with open(FLAGS.simple_network_template) as f:
                net = f.read() % {'address': data['private_dns_name'],
                                  'network': FLAGS.simple_network_network,
                                  'netmask': FLAGS.simple_network_netmask,
                                  'gateway': FLAGS.simple_network_gateway,
                                  'broadcast': FLAGS.simple_network_broadcast,
                                  'dns': FLAGS.simple_network_dns}
        if key or net:
            logging.info('Injecting data into image %s', data['image_id'])
            yield disk.inject_data(basepath('disk-raw'), key, net, execute=execute)

        if os.path.exists(basepath('disk')):
            yield self._pool.simpleExecute('rm -f %s' % basepath('disk'))

        bytes = (INSTANCE_TYPES[data['instance_type']]['local_gb']
                 * 1024 * 1024 * 1024)
        yield disk.partition(
                basepath('disk-raw'), basepath('disk'), bytes, execute=execute)
Beispiel #4
0
    def _create_image(self, libvirt_xml):
        # syntactic nicety
        data = self.datamodel
        basepath = self.basepath

        # ensure directories exist and are writable
        yield self._pool.simpleExecute('mkdir -p %s' % basepath())
        yield self._pool.simpleExecute('chmod 0777 %s' % basepath())

        # TODO(termie): these are blocking calls, it would be great
        #               if they weren't.
        logging.info('Creating image for: %s', data['instance_id'])
        f = open(basepath('libvirt.xml'), 'w')
        f.write(libvirt_xml)
        f.close()

        if FLAGS.fake_libvirt:
            logging.info('fake_libvirt, nothing to do for create_image')
            raise defer.returnValue(None)

        if FLAGS.use_s3:
            _fetch_file = self._fetch_s3_image
        else:
            _fetch_file = self._fetch_local_image

        if not os.path.exists(basepath('disk')):
            yield _fetch_file(data['image_id'], basepath('disk-raw'))
        if not os.path.exists(basepath('kernel')):
            yield _fetch_file(data['kernel_id'], basepath('kernel'))
        if not os.path.exists(basepath('ramdisk')):
            yield _fetch_file(data['ramdisk_id'], basepath('ramdisk'))

        execute = lambda cmd, input=None: self._pool.simpleExecute(
            cmd=cmd, input=input, error_ok=1)

        if data['key_data']:
            logging.info('Injecting key data into image %s', data['image_id'])
            yield disk.inject_key(data['key_data'],
                                  basepath('disk-raw'),
                                  execute=execute)

        if os.path.exists(basepath('disk')):
            yield self._pool.simpleExecute('rm -f %s' % basepath('disk'))

        bytes = (INSTANCE_TYPES[data['instance_type']]['local_gb'] * 1024 *
                 1024 * 1024)
        yield disk.partition(basepath('disk-raw'),
                             basepath('disk'),
                             bytes,
                             execute=execute)
Beispiel #5
0
Datei: node.py Projekt: sorenh/cc
    def _create_image(self, libvirt_xml):
        # syntactic nicety
        data = self.datamodel
        basepath = self.basepath

        # ensure directories exist and are writable
        yield self._pool.simpleExecute('mkdir -p %s' % basepath())
        yield self._pool.simpleExecute('chmod 0777 %s' % basepath())


        # TODO(termie): these are blocking calls, it would be great
        #               if they weren't.
        logging.info('Creating image for: %s', data['instance_id'])
        f = open(basepath('libvirt.xml'), 'w')
        f.write(libvirt_xml)
        f.close()

        if FLAGS.fake_libvirt:
            logging.info('fake_libvirt, nothing to do for create_image')
            raise defer.returnValue(None);

        if FLAGS.use_s3:
            _fetch_file = self._fetch_s3_image
        else:
            _fetch_file = self._fetch_local_image

        if not os.path.exists(basepath('disk')):
           yield _fetch_file(data['image_id'], basepath('disk-raw'))
        if not os.path.exists(basepath('kernel')):
           yield _fetch_file(data['kernel_id'], basepath('kernel'))
        if not os.path.exists(basepath('ramdisk')):
           yield _fetch_file(data['ramdisk_id'], basepath('ramdisk'))

        execute = lambda cmd, input=None: self._pool.simpleExecute(cmd=cmd, input=input, error_ok=1)

        if data['key_data']:
            logging.info('Injecting key data into image %s', data['image_id'])
            yield disk.inject_key(
                    data['key_data'], basepath('disk-raw'), execute=execute)

        if os.path.exists(basepath('disk')):
            yield self._pool.simpleExecute('rm -f %s' % basepath('disk'))

        bytes = (INSTANCE_TYPES[data['instance_type']]['local_gb']
                 * 1024 * 1024 * 1024)
        yield disk.partition(
                basepath('disk-raw'), basepath('disk'), bytes, execute=execute)