Class: Chef::Knife::XenserverTemplateCreate

Inherits:
Chef::Knife
  • Object
show all
Includes:
XenserverBase
Defined in:
lib/chef/knife/xenserver_template_create.rb

Instance Method Summary collapse

Methods included from XenserverBase

#bytes_to_megabytes, #connection, included, #locate_config_value

Instance Method Details

#create_nics(networks, macs, vm) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/chef/knife/xenserver_template_create.rb', line 179

def create_nics(networks, macs, vm)
  net_arr = networks.split(/,/).map { |x| { :network => x } }
  nics = []
  if macs
    mac_arr = macs.split(/,/)
    nics = net_arr.each_index { |x| net_arr[x][:mac_address] = mac_arr[x] if mac_arr[x] and !mac_arr[x].empty? }
  else
    nics = net_arr
  end
  networks = connection.networks
  highest_device = 0
  vm.vifs.each { |vif| highest_device = vif.device.to_i if vif.device.to_i > highest_device }
  nic_count = 0
  nics.each do |n|
    net = networks.find { |net| net.name == n[:network] }
    if net.nil?
      ui.error "Network #{n[:network]} not found"
      exit 1
    end
    nic_count += 1
    c = {
     'MAC_autogenerated' => n[:mac_address].nil? ? 'True':'False',
     'VM' => vm.reference,
     'network' => net.reference,
     'MAC' => n[:mac_address] || '',
     'device' => (highest_device + nic_count).to_s,
     'MTU' => '0',
     'other_config' => {},
     'qos_algorithm_type' => 'ratelimit',
     'qos_algorithm_params' => {}
    }
    connection.create_vif_custom c
  end
end

#runObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/chef/knife/xenserver_template_create.rb', line 74

def run
  source = config[:vm_disk]
  vm_name = config[:vm_name]
  host = config[:xenserver_host] || Chef::Config[:knife][:xenserver_host]
  user = config[:xenserver_username] || Chef::Config[:knife][:xenserver_username]
  password = config[:xenserver_password] || Chef::Config[:knife][:xenserver_password]
  if host.nil?
    ui.error "Invalid Xen host. Use #{'--xenserver-host'.red.bold} argument."
    exit 1
  end
  if user.nil?
    ui.error "Invalid Xen username. Use #{'--xenserver-username'.red.bold} argument."
    exit 1
  end
  if password.nil?
    ui.error "Invalid Xen password. Use #{'--xenserver-password'.red.bold} argument."
    exit 1
  end
  
  if vm_name.nil?
    ui.error "Invalid name for the template. Use #{'--vm-name'.red.bold} argument."
    exit 1
  end

  if source.nil? or not File.exist?(source)
    ui.error "Invalid source disk #{(source || '').bold}."
    ui.error "#{'--vm-disk'.red.bold} argument is mandatory" \
      if source.nil?
    exit 1
  end
  if source !~ /\.vhd$/
    ui.error "Invalid source disk #{source.red.bold}. I need a VHD file."
    exit 1
  end
  
  # Create the VM but do not start/provision it
  if config[:hvm]
    ui.info "HVM".yellow + " template selected"
    pv_bootloader = 'eliloader'
    hvm_boot_policy = 'BIOS order'
    pv_args = ''
  else
    ui.info "PV".yellow + " template selected"
    pv_bootloader = 'pygrub'
    hvm_boot_policy = ''
    pv_args = '-- console=hvc0'
  end

  ui.info "Creating VM #{vm_name.yellow} on #{host.yellow}..."
  
  # We will create the VDI in this SR
  sr = connection.storage_repositories.find { |sr| sr.name == config[:storage_repository] }
  # Upload and replace the VDI with our template
  uuid = UUIDTools::UUID.random_create.to_s
  dest = "/var/run/sr-mount/#{sr.uuid}/#{uuid}.vhd"
  Net::SSH.start(host, user, :password => password) do |ssh|
    puts "Uploading file #{File.basename(source).yellow}..." 
    puts "Destination SR #{sr.name.yellow}"
    ssh.scp.upload!(source, dest) do |ch, name, sent, total|
      p = (sent.to_f * 100 / total.to_f).to_i.to_s
      print "\rProgress: #{p.yellow.bold}% completed"
    end
  end
  
  sr.scan
  # Create a ~8GB VDI in storage repository 'Local Storage'
  #vdi = connection.vdis.create :name => "#{vm_name}-disk1", 
  #                       :storage_repository => sr,
  #                       :description => "#{vm_name}-disk1",
  #                       :virtual_size => '8589934592' # ~8GB in bytes

  vdi = nil
  sr.vdis.each do |v| 
    if v.uuid == uuid
      v.set_attribute 'name_label', "#{vm_name}-template"
      vdi = v
      break
    end
  end
  
  mem = (config[:vm_memory].to_i * 1024 * 1024).to_s
  vm = connection.servers.new :name => "#{vm_name}",
                        :affinity => connection.hosts.first,
                        :other_config => {},
                        :pv_bootloader => pv_bootloader,
                        :hvm_boot_policy => hvm_boot_policy,
                        :pv_args => pv_args,
                        :memory_static_max => mem,
                        :memory_static_min => mem,
                        :memory_dynamic_max => mem,
                        :memory_dynamic_min => mem
  vm.save
  if config[:vm_tags]
    vm.set_attribute 'tags', config[:vm_tags].split(',')
  end

  if config[:vm_networks]
    create_nics(config[:vm_networks], config[:mac_addresses], vm)
  end
  # Add the required VBD to the VM 
  connection.vbds.create :server => vm, :vdi => vdi
  puts "\nDone."
  
end