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
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}..."
sr = connection.storage_repositories.find { |sr| sr.name == config[:storage_repository] }
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
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
connection.vbds.create :server => vm, :vdi => vdi
puts "\nDone."
end
|