src/vsphere_cpi/lib/cloud/vsphere/stemcell.rb
# Replicating a stemcell allows the creation of linked clones which can share files with a snapshot.
# For details see https://www.vmware.com/support/ws55/doc/ws_clone_overview.html.
#
# @param [Resources::Datacenter] datacenter
# @param [Resources::Cluster] cluster
# @param [Resources::Datastore] to_datastore
# @param [Resources::StoragePod] datastore_cluster
Replicating a stemcell allows the creation of linked clones which can share files with a snapshot.
Replicating a stemcell allows the creation of linked clones which can share files with a snapshot .
def replicate(datacenter, cluster, to_datastore, datastore_cluster = nil)
      client = datacenter.client
      vm = find_replica_in(datacenter.mob, client)
      raise "Could not find VM for stemcell '#{id}'" unless vm
      to_datastore_mob = if datastore_cluster
        srm = client.service_instance.content.storage_resource_manager
        get_recommended_datastore(srm, vm, datacenter.template_folder.mob, cluster.resource_pool.mob, datastore_cluster)
      else
        to_datastore.mob
      end
      datastore_name = to_datastore_mob.info.name
      logger.info("Searching for stemcell #{id} on datastore #{datastore_name}")
      replica_vm = find_replica_in(datacenter.mob,  client, datastore_name)
      return replica_vm if replica_vm
      replica_name = "#{id} %2f #{to_datastore_mob.__mo_id__}"
      logger.info("No stemcell #{id} on datastore #{datastore_name}, replicating as #{replica_name}")
      begin
        clone_spec = VimSdk::Vim::Vm::CloneSpec.new
        clone_spec.location = VimSdk::Vim::Vm::RelocateSpec.new(
          datastore: to_datastore_mob,
          pool: cluster.resource_pool.mob
        )
        clone_spec.template = false
        if client.service_content.extension_manager.find_extension(
          VCPIExtension::DEFAULT_VSPHERE_CPI_EXTENSION_KEY) then
          managed_by_info = VimSdk::Vim::Ext::ManagedByInfo.new
          managed_by_info.extension_key = VCPIExtension::DEFAULT_VSPHERE_CPI_EXTENSION_KEY
          managed_by_info.type =  VCPIExtension::DEFAULT_VSPHERE_MANAGED_BY_INFO_RESOURCE
          clone_spec.config = VimSdk::Vim::Vm::ConfigSpec.new
          clone_spec.config.managed_by = managed_by_info
        end
        replica_vm = client.wait_for_task do
          vm.clone(datacenter.template_folder.mob, replica_name, clone_spec)
        end
        disable_sdrs(srm, datastore_cluster.mob, replica_vm) if datastore_cluster
        logger.info("Creating initial snapshot for linked clones on #{replica_vm}")
        client.wait_for_task do
          replica_vm.create_snapshot('initial', nil, false, false)
        end
        logger.info("Created initial snapshot for linked clones on #{replica_vm}")
      rescue VSphereCloud::VCenterClient::DuplicateName
        logger.info("Stemcell is already being replicated, waiting for #{replica_name} to be ready")
        path_array = [datacenter.name, 'vm', datacenter.template_folder.path_components, replica_name]
        replica_vm = client.find_by_inventory_path(path_array.flatten)
        client.cloud_searcher.get_properties(replica_vm, VimSdk::Vim::VirtualMachine, ['snapshot'], ensure_all: true)
        logger.info("Stemcell #{replica_name} has been replicated.")
      end
      replica_vm
    end
def replicate ( datacenter , cluster , to_datastore , datastore_cluster = nil ) client = datacenter . client vm = find_replica_in ( datacenter . mob , client ) raise "Could not find VM for stemcell '#{id}'" unless vm to_datastore_mob = if datastore_cluster srm = client . service_instance . content . storage_resource_manager get_recommended_datastore ( srm , vm , datacenter . template_folder . mob , cluster . resource_pool . mob , datastore_cluster ) else to_datastore . mob end datastore_name = to_datastore_mob . info . name logger . info ( "Searching for stemcell #{id} on datastore #{datastore_name}" ) replica_vm = find_replica_in ( datacenter . mob , client , datastore_name ) return replica_vm if replica_vm replica_name = "#{id} %2f #{to_datastore_mob.__mo_id__}" logger . info ( "No stemcell #{id} on datastore #{datastore_name}, replicating as #{replica_name}" ) begin clone_spec = VimSdk :: Vim :: Vm :: CloneSpec . new clone_spec . location = VimSdk :: Vim :: Vm :: RelocateSpec . new ( datastore : to_datastore_mob , pool : cluster . resource_pool . mob ) clone_spec . template = false if client . service_content . extension_manager . find_extension ( VCPIExtension :: DEFAULT_VSPHERE_CPI_EXTENSION_KEY ) then managed_by_info = VimSdk :: Vim :: Ext :: ManagedByInfo . new managed_by_info . extension_key = VCPIExtension :: DEFAULT_VSPHERE_CPI_EXTENSION_KEY managed_by_info . type = VCPIExtension :: DEFAULT_VSPHERE_MANAGED_BY_INFO_RESOURCE clone_spec . config = VimSdk :: Vim :: Vm :: ConfigSpec . new clone_spec . config . managed_by = managed_by_info end replica_vm = client . wait_for_task do vm . clone ( datacenter . template_folder . mob , replica_name , clone_spec ) end disable_sdrs ( srm , datastore_cluster . mob , replica_vm ) if datastore_cluster logger . info ( "Creating initial snapshot for linked clones on #{replica_vm}" ) client . wait_for_task do replica_vm . create_snapshot ( 'initial' , nil , false , false ) end logger . info ( "Created initial snapshot for linked clones on #{replica_vm}" ) rescue VSphereCloud :: VCenterClient :: DuplicateName logger . info ( "Stemcell is already being replicated, waiting for #{replica_name} to be ready" ) path_array = [ datacenter . name , 'vm' , datacenter . template_folder . path_components , replica_name ] replica_vm = client . find_by_inventory_path ( path_array . flatten ) client . cloud_searcher . get_properties ( replica_vm , VimSdk :: Vim :: VirtualMachine , [ 'snapshot' ] , ensure_all : true ) logger . info ( "Stemcell #{replica_name} has been replicated." ) end replica_vm end
Replicating a stemcell allows the creation of linked clones which can share files with a snapshot.
Replicating a stemcell allows the creation of linked clones which can share files with a snapshot .
# Find a replica of this stemcell to act as the source of the replication. # This lets stemcell replication work even if the original VM is deleted by a user. # if datastore cluster, then find the recommended datastore and then proceed as usual # Check if original stemcell lives on same datastore. # add extension managed by info to clone spec only if extension exists # cloud_searcher#get_properties will ensure the existence of a snapshot # by retrying. This forces us to wait for a valid snapshot before # returning with the replicated stemcell vm. If a snapshot is not found # then an exception is thrown.
datacenter
cluster
to_datastore
datastore_cluster
None