示例#1
0
class LibraryPublishSubscribe(SampleBase):
    """
    Demonstrates the basic sync workflow to publish and subscribe content libraries.
    Note: the workflow needs an existing VC datastore with available storage.
    """
    VCSP_USERNAME = '******'
    DEMO_PASSWORD = '******'
    SYNC_TIMEOUT_SEC = 60
    DEMO_FILENAME = 'test.iso'

    def __init__(self):
        SampleBase.__init__(self, self.__doc__)
        self.servicemanager = None
        self.client = None
        self.helper = None
        self.datastore_name = None
        self.pub_lib_name = "demo-publib"
        self.sub_lib_name = "demo-sublib"
        self.pub_lib_id = None
        self.sub_lib_id = None

    def _options(self):
        self.argparser.add_argument('-datastorename',
                                    '--datastorename',
                                    help='The name of the datastore.')

    def _setup(self):
        self.datastore_name = self.args.datastorename
        assert self.datastore_name is not None

        self.servicemanager = self.get_service_manager()
        self.client = ClsApiClient(self.servicemanager)
        self.helper = ClsApiHelper(self.client, self.skip_verification)

    def _execute(self):
        storage_backings = self.helper.create_storage_backings(
            self.servicemanager, self.datastore_name)

        # Create a published library backed the VC datastore using vAPIs
        self.pub_lib_id = self.create_published_library(storage_backings)
        assert self.pub_lib_id is not None
        print('Published library created: ID: {0}'.format(self.pub_lib_id))
        pub_lib = self.client.local_library_service.get(self.pub_lib_id)
        pub_lib_url = pub_lib.publish_info.publish_url
        assert pub_lib_url is not None
        print('Publish URL : {0}'.format(pub_lib_url))

        # Create a library item in the published library
        pub_lib_item_id = self.helper.create_iso_library_item(
            self.pub_lib_id, 'item_1', self.DEMO_FILENAME)
        assert self.client.library_item_service.get(
            pub_lib_item_id) is not None

        # Create the subscribed library
        sub_lib, sub_spec = self.create_subcribed_library(
            storage_backings, pub_lib_url)
        assert self.sub_lib_id is not None
        print('Subscribed library created: ID: {0}'.format(self.sub_lib_id))

        # It is not mandatory to verify sync, it is just for demonstrating the sample workflow.
        assert (ClsSyncHelper(self.client,
                              self.SYNC_TIMEOUT_SEC).verify_library_sync(
                                  self.pub_lib_id, sub_lib))
        sub_lib = self.client.subscribed_library_service.get(self.sub_lib_id)
        print('Subscribed library synced : {0}'.format(sub_lib.last_sync_time))

        sub_item_ids = self.client.library_item_service.list(self.sub_lib_id)
        assert len(sub_item_ids) == 1, 'Subscribed library must have one item'

        # Add another item to the published library
        self.helper.create_iso_library_item(self.pub_lib_id, 'item_2',
                                            self.DEMO_FILENAME)

        # Manually synchronize the subscribed library to get the latest changes immediately.
        self.client.subscribed_library_service.sync(self.sub_lib_id)
        # It is not mandatory to verify sync, it is just for demonstrating the sample workflow.
        assert (ClsSyncHelper(self.client,
                              self.SYNC_TIMEOUT_SEC).verify_library_sync(
                                  self.pub_lib_id, sub_lib))
        sub_lib = self.client.subscribed_library_service.get(self.sub_lib_id)
        print('Subscribed library synced : {0}'.format(sub_lib.last_sync_time))

        # List the subscribed items.
        sub_item_ids = self.client.library_item_service.list(self.sub_lib_id)
        assert len(sub_item_ids) == 2, 'Subscribed library must have two items'
        for sub_item_id in sub_item_ids:
            sub_item = self.client.library_item_service.get(sub_item_id)
            print('Subscribed item : {0}'.format(sub_item.name))

        # Change the subscribed library to be on-demand
        sub_spec.subscription_info.on_demand = True
        self.client.subscribed_library_service.update(self.sub_lib_id,
                                                      sub_spec)

        # Evict the cached content of the first subscribed library item
        self.client.subscribed_item_service.evict(sub_item_id)
        sub_item = self.client.library_item_service.get(sub_item_id)
        print('Subscribed item evicted : {0}'.format(sub_item.name))
        assert not sub_item.cached, 'Subscribed item must not be cached'

        # Force synchronize the subscribed library item to fetch and cache the content
        self.client.subscribed_item_service.sync(sub_item_id, True)
        # It is not mandatory to verify sync, it is just for demonstrating the sample workflow.
        assert (ClsSyncHelper(
            self.client, self.SYNC_TIMEOUT_SEC).verify_item_sync(sub_item_id))
        sub_item = self.client.library_item_service.get(sub_item_id)
        print('Subscribed item force sync : {0}'.format(sub_item.name))
        assert sub_item.cached, 'Subscribed item must be cached'

    def create_published_library(self, storage_backings):
        # Build the authenticated publish info.
        # Note: The username will be 'vcsp'.
        pub_info = PublishInfo()
        pub_info.published = True
        pub_info.authentication_method = PublishInfo.AuthenticationMethod.BASIC
        pub_info.password = self.DEMO_PASSWORD

        # Build the specification for the published library to be created
        pub_spec = LibraryModel()
        pub_spec.name = self.pub_lib_name
        pub_spec.description = "Published library backed by VC datastore"
        pub_spec.publish_info = pub_info
        pub_spec.type = pub_spec.LibraryType.LOCAL
        pub_spec.storage_backings = storage_backings

        pub_lib_id = self.client.local_library_service.create(
            create_spec=pub_spec, client_token=generate_random_uuid())

        return pub_lib_id

    def create_subcribed_library(self, storage_backings, pub_lib_url):
        # Build the subscription information using the publish URL of the published
        # library. The username must be 'vcsp'.
        sub_info = SubscriptionInfo()
        sub_info.authentication_method = SubscriptionInfo.AuthenticationMethod.BASIC
        sub_info.user_name = self.VCSP_USERNAME
        sub_info.password = self.DEMO_PASSWORD
        sub_info.on_demand = False
        sub_info.automatic_sync_enabled = True
        sub_info.subscription_url = pub_lib_url

        # Build the specification for the subscribed library
        sub_spec = LibraryModel()
        sub_spec.name = self.sub_lib_name
        sub_spec.type = sub_spec.LibraryType.SUBSCRIBED
        sub_spec.subscription_info = sub_info
        sub_spec.storage_backings = storage_backings

        self.sub_lib_id = self.client.subscribed_library_service.create(
            create_spec=sub_spec, client_token=generate_random_uuid())
        sub_lib = self.client.subscribed_library_service.get(self.sub_lib_id)
        return sub_lib, sub_spec

    def _cleanup(self):
        if self.sub_lib_id:
            self.client.subscribed_library_service.delete(self.sub_lib_id)
            print('Deleted subscribed library Id: {0}'.format(self.sub_lib_id))

        if self.pub_lib_id:
            self.client.local_library_service.delete(self.pub_lib_id)
            print('Deleted published library Id : {0}'.format(self.pub_lib_id))
示例#2
0
class IsoMount(SampleBase):
    """
    Demonstrates the content library ISO item mount and
    unmount workflow via the mount and unmount APIs from the
    ISO service.
    """

    ISO_FILENAME = 'test.iso'

    def __init__(self):
        SampleBase.__init__(self, self.__doc__)
        self.servicemanager = None
        self.client = None
        self.datastore_name = None
        self.lib_name = "iso-demo-lib"
        self.local_library = None
        self.iso_item_name = "iso-demo-lib-item"
        self.vm_name = None

    def _options(self):
        self.argparser.add_argument(
            '-datastorename',
            '--datastorename',
            required=True,
            help='The name of a datastore (of type vmfs) that is'
            ' accessible to the vm specified with --vmname.')
        self.argparser.add_argument(
            '-vmname',
            '--vmname',
            required=True,
            help='The name of the vm where iso will be mounted. '
            'The vm needs to be already created on the vCenter')

    def _setup(self):
        self.datastore_name = self.args.datastorename
        assert self.datastore_name is not None

        self.vm_name = self.args.vmname
        assert self.vm_name is not None

        self.servicemanager = self.get_service_manager()

        self.client = ClsApiClient(self.servicemanager)
        self.helper = ClsApiHelper(self.client, self.skip_verification)

        session = get_unverified_session() if self.skip_verification else None
        self.vsphere_client = create_vsphere_client(server=self.server,
                                                    username=self.username,
                                                    password=self.password,
                                                    session=session)

    def _execute(self):
        storage_backings = self.helper.create_storage_backings(
            self.servicemanager, self.datastore_name)

        library_id = self.helper.create_local_library(storage_backings,
                                                      self.lib_name)
        self.local_library = self.client.local_library_service.get(library_id)

        library_item_id = self.helper.create_iso_library_item(
            library_id, self.iso_item_name, self.ISO_FILENAME)

        vm_id = get_vm(self.vsphere_client, self.vm_name)
        assert vm_id is not None

        # Mount the iso item as a CDROM device
        device_id = self.client.iso_service.mount(library_item_id, vm_id)
        assert device_id is not None
        print('Mounted library item {0} on vm {1} at device {2}'.format(
            self.iso_item_name, self.vm_name, device_id))
        # Unmount the CDROM
        self.client.iso_service.unmount(vm_id, device_id)
        print('Unmounted library item {0} from vm {1} mounted at device {2}'.
              format(self.iso_item_name, self.vm_name, device_id))

    def _cleanup(self):
        if self.local_library:
            self.client.local_library_service.delete(
                library_id=self.local_library.id)
            print('Deleted Library Id: {0}'.format(self.local_library.id))