Class: Vcloud::Core::Vm

Inherits:
Object
  • Object
show all
Extended by:
ComputeMetadata
Defined in:
lib/vcloud/core/vm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ComputeMetadata

get_metadata

Constructor Details

#initialize(id, vapp) ⇒ Vm

Returns a new instance of Vm.



8
9
10
11
12
13
14
# File 'lib/vcloud/core/vm.rb', line 8

def initialize(id, vapp)
  unless id =~ /^#{self.class.id_prefix}-[-0-9a-f]+$/
    raise "#{self.class.id_prefix} id : #{id} is not in correct format"
  end
  @id = id
  @vapp = vapp
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/vcloud/core/vm.rb', line 6

def id
  @id
end

Instance Method Details

#add_extra_disks(extra_disks) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/vcloud/core/vm.rb', line 72

def add_extra_disks(extra_disks)
  vm = Vcloud::Fog::ModelInterface.new.get_vm_by_href(href)
  if extra_disks
    extra_disks.each do |extra_disk|
      Vcloud::Core.logger.debug("adding a disk of size #{extra_disk[:size]}MB into VM #{id}")
      vm.disks.create(extra_disk[:size])
    end
  end
end

#configure_guest_customization_section(name, bootstrap_config, extra_disks) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/vcloud/core/vm.rb', line 100

def configure_guest_customization_section(name, bootstrap_config, extra_disks)
  if bootstrap_config.nil? or bootstrap_config[:script_path].nil?
    interpolated_preamble = ''
  else
    preamble_vars = bootstrap_config[:vars] || {}
    preamble_vars.merge!(:extra_disks => extra_disks)
    interpolated_preamble = generate_preamble(
        bootstrap_config[:script_path],
        bootstrap_config[:script_post_processor],
        preamble_vars,
    )
  end
  Vcloud::Fog::ServiceInterface.new.put_guest_customization_section(id, name, interpolated_preamble)
end

#configure_network_interfaces(networks_config) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vcloud/core/vm.rb', line 82

def configure_network_interfaces(networks_config)
  return unless networks_config
  section = {PrimaryNetworkConnectionIndex: 0}
  section[:NetworkConnection] = networks_config.compact.each_with_index.map do |network, i|
    connection = {
        network: network[:name],
        needsCustomization: true,
        NetworkConnectionIndex: i,
        IsConnected: true
    }
    ip_address = network[:ip_address]
    connection[:IpAddress] = ip_address unless ip_address.nil?
    connection[:IpAddressAllocationMode] = ip_address ? 'MANUAL' : 'DHCP'
    connection
  end
  Vcloud::Fog::ServiceInterface.new.put_network_connection_system_section_vapp(id, section)
end

#cpuObject



33
34
35
36
# File 'lib/vcloud/core/vm.rb', line 33

def cpu
  cpu_item = virtual_hardware_section.detect { |i| i[:'rasd:ResourceType'] == '3' }
  cpu_item[:'rasd:VirtualQuantity']
end

#generate_preamble(script_path, script_post_processor, vars) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/vcloud/core/vm.rb', line 115

def generate_preamble(script_path, script_post_processor, vars)
  script = ERB.new(File.read(File.expand_path(script_path)), nil, '>-').result(binding)
  if script_post_processor
    script = Open3.capture2(File.expand_path(script_post_processor),
                            stdin_data: script).first
  end
  script
end

#hrefObject



42
43
44
# File 'lib/vcloud/core/vm.rb', line 42

def href
  vcloud_attributes[:href]
end

#memoryObject



28
29
30
31
# File 'lib/vcloud/core/vm.rb', line 28

def memory
  memory_item = virtual_hardware_section.detect { |i| i[:'rasd:ResourceType'] == '4' }
  memory_item[:'rasd:VirtualQuantity']
end

#nameObject



38
39
40
# File 'lib/vcloud/core/vm.rb', line 38

def name
  vcloud_attributes[:name]
end

#update_cpu_count(new_cpu_count) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/vcloud/core/vm.rb', line 55

def update_cpu_count(new_cpu_count)
  return if new_cpu_count.nil?
  return if new_cpu_count.to_i == 0
  unless cpu.to_i == new_cpu_count.to_i
    Vcloud::Fog::ServiceInterface.new.put_cpu(id, new_cpu_count)
  end
end

#update_memory_size_in_mb(new_memory) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/vcloud/core/vm.rb', line 20

def update_memory_size_in_mb(new_memory)
  return if new_memory.nil?
  return if new_memory.to_i < 64
  unless memory.to_i == new_memory.to_i
    Vcloud::Fog::ServiceInterface.new.put_memory(id, new_memory)
  end
end

#update_metadata(metadata) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/vcloud/core/vm.rb', line 63

def ()
  return if .nil?
  fsi = Vcloud::Fog::ServiceInterface.new
  .each do |k, v|
    fsi.(@vapp.id, k, v)
    fsi.(id, k, v)
  end
end

#update_name(new_name) ⇒ Object



46
47
48
49
# File 'lib/vcloud/core/vm.rb', line 46

def update_name(new_name)
  fsi = Vcloud::Fog::ServiceInterface.new
  fsi.put_vm(id, new_name) unless name == new_name
end

#update_storage_profile(storage_profile) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/vcloud/core/vm.rb', line 124

def update_storage_profile storage_profile
  storage_profile_href = get_storage_profile_href_by_name(storage_profile, @vapp.name)
  Vcloud::Fog::ServiceInterface.new.put_vm(id, name, {
    :StorageProfile => {
      name: storage_profile,
      href: storage_profile_href
    }
  })
end

#vapp_nameObject



51
52
53
# File 'lib/vcloud/core/vm.rb', line 51

def vapp_name
  @vapp.name
end

#vcloud_attributesObject



16
17
18
# File 'lib/vcloud/core/vm.rb', line 16

def vcloud_attributes
  Vcloud::Fog::ServiceInterface.new.get_vapp(id)
end