def create_image(self, location): img = RwcalYang.ImageInfoItem() img.name = basename(location) img.location = location img.disk_format = "qcow2" img.container_format = "bare" logger.info("Uploading image : %s" % img.name) rc, img_id = self._cal.create_image(self._acct, img) assert rc == RwStatus.SUCCESS rs = None rc = None image = None for i in range(100): rc, rs = self._cal.get_image(self._acct, img_id) assert rc == RwStatus.SUCCESS logger.info("Image (image_id: %s) reached status : %s" % (img_id, rs.state)) if rs.state == 'active': image = rs break else: time.sleep(2) # Sleep for a second if image is None: logger.error("Failed to upload openstack image: %s", img) sys.exit(1) self._image_id = img_id logger.info("Uploading image.......[Done]")
def _fill_image_info(img_info): """Create a GI object from image info dictionary Converts image information dictionary object returned by AWS driver into Protobuf Gi Object Arguments: account - a cloud account img_info - image information dictionary object from AWS Returns: The ImageInfoItem """ img = RwcalYang.ImageInfoItem() img.name = img_info.name img.id = img_info.id tag_fields = ['checksum'] # Look for any properties if img_info.tags: for tag in img_info.tags: if tag['Key'] == 'checksum': setattr(img, tag['Key'], tag['Value']) img.disk_format = 'ami' if img_info.state == 'available': img.state = 'active' else: img.state = 'inactive' return img
def create_image(cls): image = RwcalYang.ImageInfoItem() image.name = "rift-lxc-image" image.location = "/net/sharedfiles/home1/common/vm/R0.4/rift-mano-devel-latest.qcow2" image.disk_format = "qcow2" image.id = cls.cal.do_create_image(cls.account, image, no_rwstatus=True) cls.image = image
def create_image(self, location): """Creates and returns a CAL image""" image = RwcalYang.ImageInfoItem() image.name = "rift-lxc-image" image.location = location image.disk_format = "qcow2" rc, image.id = self.cal.create_image(self.account, image) return image
def _get_image_info_request(self): """ Returns request object of type RwcalYang.ImageInfoItem() """ img = RwcalYang.ImageInfoItem() img.name = "rift.cal.unittest.image" img.location = '/net/sharedfiles/home1/common/vm/rift-root-latest.qcow2' img.disk_format = "qcow2" img.container_format = "bare" img.checksum = OpenStackTest.IMG_Checksum return img
def test_create_image_name_and_checksum_exists(self): image_name = "test.qcow2" with self.create_image_hdl(image_name) as hdl: image_checksum = get_image_checksum(hdl) image_entry = RwcalYang.ImageInfoItem(id="asdf", name=image_name, checksum=image_checksum) self._create_image_mock.add_existing_image(image_entry) self._image_uploader.upload_images({image_name: hdl}) # No image should have been uploaded, since the name and checksum self.assertEqual(self._create_image_mock.image_checksum, None)
def do_get_image_list(self, account): """ Return a list of the names of all available images. """ boxed_image_list = RwcalYang.VimResources() for image in self.resources[account.name].images.values(): image_entry = RwcalYang.ImageInfoItem() image_entry.id = image.id image_entry.name = image.name if image.has_field('checksum'): image_entry.checksum = image.checksum boxed_image_list.imageinfo_list.append(image_entry) return boxed_image_list
def image_subcommand(driver, account, cmdargs): """Process the image subcommand""" if cmdargs.which == 'list': rc, images = driver.get_image_list(account) assert rc == RwStatus.SUCCESS for i in images.imageinfo_list: print(i) elif cmdargs.which == 'delete': image_delete_subcommand(driver, account, cmdargs) elif cmdargs.which == 'create': account.openstack.key = 'admin' rc, images = driver.get_image_list(account) assert rc == RwStatus.SUCCESS for i in images.imageinfo_list: if i.name == cmdargs.image_name: print("FATAL: image \"%s\" already exists" % cmdargs.image_name) return 1 print("creating image \"%s\" using %s ..." % \ (cmdargs.image_name, cmdargs.file_name)) img = RwcalYang.ImageInfoItem() img.name = cmdargs.image_name img.location = cmdargs.file_name img.disk_format = "qcow2" img.container_format = "bare" rc, img_id = driver.create_image(account, img) print("... done. image_id is %s" % img_id) return img_id elif cmdargs.which == 'getid': rc, images = driver.get_image_list(account) assert rc == RwStatus.SUCCESS found = 0 for i in images.imageinfo_list: if i.name == cmdargs.image_name: print(i.id) found += 1 if found != 1: sys.exit(1)
def create_default_resources(self, account): """ Create default resources """ link_list = [] ### Add virtual links #for i in range(1): # vlink = RwcalYang.VirtualLinkReqParams() # vlink.name = 'link-'+str(i) # vlink.subnet = '10.0.0.0/24' # rs, vlink_id = self.do_create_virtual_link(account, vlink) # assert vlink_id != '' # logger.debug("Creating static virtual-link with name: %s", vlink.name) # link_list.append(vlink_id) #### Add VDUs #for i in range(8): # vdu = RwcalYang.VDUInitParams() # vdu.name = 'vdu-'+str(i) # vdu.node_id = str(i) # vdu.image_id = self.get_uuid('image-'+str(i)) # vdu.flavor_id = self.get_uuid('flavor'+str(i)) # vdu.vm_flavor.vcpu_count = 4 # vdu.vm_flavor.memory_mb = 4096*2 # vdu.vm_flavor.storage_gb = 40 # for j in range(2): # c = vdu.connection_points.add() # c.name = vdu.name+'-port-'+str(j) # c.virtual_link_id = link_list[j] # rs, vdu_id = self.do_create_vdu(account, vdu) # assert vdu_id != '' # logger.debug("Creating static VDU with name: %s", vdu.name) for i in range(2): flavor = RwcalYang.FlavorInfoItem() flavor.name = 'flavor-' + str(i) flavor.vm_flavor.vcpu_count = 4 flavor.vm_flavor.memory_mb = 4096 * 2 flavor.vm_flavor.storage_gb = 40 rc, flavor_id = self.do_create_flavor(account, flavor) for i in range(2): image = RwcalYang.ImageInfoItem() image.name = "rwimage" image.id = self.get_uuid('image-' + str(i)) image.checksum = self.get_uuid('rwimage' + str(i)) image.location = "/dev/null" rc, image_id = self.do_create_image(account, image) image = RwcalYang.ImageInfoItem() image.name = "Fedora-x86_64-20-20131211.1-sda.qcow2" image.id = self.get_uuid(image.name) image.checksum = self.get_uuid(image.name) image.location = "/dev/null" rc, image_id = self.do_create_image(account, image) image = RwcalYang.ImageInfoItem() image.name = "Fedora-x86_64-20-20131211.1-sda-ping.qcow2" image.id = self.get_uuid(image.name) image.checksum = self.get_uuid(image.name) image.location = "/dev/null" rc, image_id = self.do_create_image(account, image) image = RwcalYang.ImageInfoItem() image.name = "Fedora-x86_64-20-20131211.1-sda-pong.qcow2" image.id = self.get_uuid(image.name) image.checksum = self.get_uuid(image.name) image.location = "/dev/null" rc, image_id = self.do_create_image(account, image)
def main(argv=sys.argv[1:]): """ Assuming that an LVM backing-store has been created with a volume group called 'rift', the following creates an lxc 'image' and a pair of 'vms'. In the LXC based container CAL, an 'image' is container and a 'vm' is a snapshot of the original container. In addition to the LVM backing store, it is assumed that there is a network bridge called 'virbr0'. """ logging.basicConfig(level=logging.DEBUG) parser = argparse.ArgumentParser() parser.add_argument('--rootfs', '-r') parser.add_argument('--num-vms', '-n', type=int, default=2) parser.add_argument('--terminate', '-t', action='store_true') args = parser.parse_args(argv) # Acquire the plugin from peas plugin = rw_peas.PeasPlugin('rwcal-plugin', 'RwCal-1.0') engine, info, extension = plugin() # Get the RwLogger context rwloggerctx = rwlogger.RwLog.Ctx.new("Cal-Log") cal = plugin.get_interface("Cloud") cal.init(rwloggerctx) # The account object is not currently used, but it is required by the CAL # interface, so we create an empty object here to represent it. account = RwcalYang.CloudAccount() account.account_type = "lxc" # Make sure that any containers that were previously created have been # stopped and destroyed. containers = lxc.containers() for container in containers: lxc.stop(container) for container in containers: lxc.destroy(container) template = os.path.join( os.environ['RIFT_INSTALL'], 'etc/lxc-fedora-rift.lxctemplate', ) logger.info(template) logger.info(args.rootfs) # Create an image that can be used to create VMs image = RwcalYang.ImageInfoItem() image.name = 'rift-master' image.lxc.size = '2.5G' image.lxc.template_path = template image.lxc.tarfile = args.rootfs cal.create_image(account, image) # Create a VM vms = [] for index in range(args.num_vms): vm = RwcalYang.VMInfoItem() vm.vm_name = 'rift-s{}'.format(index + 1) vm.image_id = image.id cal.create_vm(account, vm) vms.append(vm) # Create the default and data networks network = RwcalYang.NetworkInfoItem(network_name='virbr0') cal.create_network(account, network) os.system('brctl show') # Create pairs of ports to connect the networks for index, vm in enumerate(vms): port = RwcalYang.PortInfoItem() port.port_name = "eth0" port.network_id = network.network_id port.vm_id = vm.vm_id port.ip_address = "192.168.122.{}".format(index + 101) port.lxc.veth_name = "rws{}".format(index) cal.create_port(account, port) # Swap out the current instance of the plugin to test that the data is # shared among different instances cal = plugin.get_interface("Cloud") cal.init() # Start the VMs for vm in vms: cal.start_vm(account, vm.vm_id) lxc.ls() # Exit if the containers are not supposed to be terminated if not args.terminate: return time.sleep(3) # Stop the VMs for vm in vms: cal.stop_vm(account, vm.vm_id) lxc.ls() # Delete the VMs for vm in vms: cal.delete_vm(account, vm.vm_id) # Delete the image cal.delete_image(account, image.id)
def upload_images(self, image_name_hdl_map): """ Upload images to all CloudAccounts Arguments: image_name_hdl_map - A map of image name to an opened image handle Raises: ImageUploadError - A image failed to be uploaded to a cloud account """ if len(image_name_hdl_map) == 0: return self._log.debug("uploading %s images to cloud accounts: %s", len(image_name_hdl_map), self._accounts) account_images = self._get_account_images() for image_name, image_hdl in image_name_hdl_map.items(): self._log.debug('uploading image: {}'.format(image_name)) image_checksum = rift.package.checksums.checksum(image_hdl) image_hdl.seek(0) image = rwcal.ImageInfoItem() image.name = image_name try: image.fileno = image_hdl.fileno() image.location = image_hdl.name except Exception as e: msg = "Image handle must a fileno() and name attribute" self._log.error(msg) raise ImageUploadError(msg) for account in self._accounts: # Find images on the cloud account which have the same name matching_images = [ i for i in account_images[account] if i.name == image.name ] matching_checksum = [ i for i in matching_images if i.checksum == image_checksum ] if len(matching_checksum) > 0: self._log.debug( "found matching image with checksum, not uploading to {}" .format(account.name)) continue self._log.debug("uploading to account {}: {}".format( account.name, image)) try: # If we are using file descriptor to upload the image, make sure we # set the position to the beginning of the file for each upload. if image.has_field("fileno"): os.lseek(image.fileno, 0, os.SEEK_SET) image.id = account.create_image(image) except rift.mano.cloud.CloudAccountCalError as e: msg = "error when uploading image {} to cloud account: {}".format( image_name, str(e)) self._log.error(msg) raise ImageUploadError(msg) from e self._log.debug('uploaded image to account{}: {}'.format( account.name, image_name))