Class: Chef::Knife::OvhPccVmClone
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Chef::Knife::OvhPccVmClone
- Includes:
- OvhBase
- Defined in:
- lib/chef/knife/ovh_pcc_vm_clone.rb
Instance Attribute Summary collapse
-
#initial_sleep_delay ⇒ Object
Returns the value of attribute initial_sleep_delay.
Instance Method Summary collapse
Methods included from OvhBase
#fatal_exit, #find_all_in_folders, #find_in_folders, #get_folders, #get_vim_connection, included
Instance Attribute Details
#initial_sleep_delay ⇒ Object
Returns the value of attribute initial_sleep_delay.
33 34 35 |
# File 'lib/chef/knife/ovh_pcc_vm_clone.rb', line 33 def initial_sleep_delay @initial_sleep_delay end |
Instance Method Details
#locate_config_value(key) ⇒ Object
98 99 100 101 |
# File 'lib/chef/knife/ovh_pcc_vm_clone.rb', line 98 def locate_config_value(key) key = key.to_sym Chef::Config[:knife][key] || config[key] end |
#run ⇒ Object
Run !
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 178 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/chef/knife/ovh_pcc_vm_clone.rb', line 132 def run $stdout.sync = true vmname = @name_args[0] if vmname.nil? show_usage fatal_exit("You must specify a virtual machine name") end template = @name_args[1] if template.nil? show_usage fatal_exit("You must specify a template name") end vim = get_vim_connection dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc] dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found" hosts = find_all_in_folders(dc.hostFolder, RbVmomi::VIM::ComputeResource) rp = hosts.first.resourcePool src_vm = find_in_folders(dc.vmFolder, RbVmomi::VIM::VirtualMachine, template) or abort "VM/Template not found" # kroknet - <[email protected]> # Fix to handle the vm creation with many parameters fixed_name = RbVmomi::VIM.CustomizationFixedName fixed_name.name = config[:customization_hostname] # Global settings vm_dns = RbVmomi::VIM.CustomizationGlobalIPSettings my_dns = config[:customization_dns].split(','); my_suffix = config[:customization_domain].split(','); vm_dns.dnsServerList = my_dns vm_dns.dnsSuffixList = my_suffix # Who am i ? identity_settings = RbVmomi::VIM.CustomizationLinuxPrep identity_settings.hostName = fixed_name identity_settings.domain = config[:customization_domain] identity_settings.hwClockUTC = false identity_settings.timeZone = 'Europe/Paris' cidr_ip = NetAddr::CIDR.create(config[:customization_ip]) # IP vm_ip = RbVmomi::VIM::CustomizationFixedIp(:ipAddress => cidr_ip.ip) # IPV4 Settings eth0 vm_ip_settings = RbVmomi::VIM.CustomizationIPSettings my_gw = config[:customization_gw].split(','); vm_ip_settings.ip = vm_ip vm_ip_settings.subnetMask = config[:customization_netmask] vm_ip_settings.dnsServerList = my_dns vm_ip_settings.gateway = my_gw vm_ip_settings.dnsDomain = config[:customization_domain] #adapter mapping adapter_mapping = RbVmomi::VIM.CustomizationAdapterMapping adapter_mapping.adapter = vm_ip_settings customization_spec = RbVmomi::VIM.CustomizationSpec multi_nic = [ adapter_mapping ] customization_spec.globalIPSettings = vm_dns customization_spec.identity = identity_settings customization_spec.nicSettingMap = multi_nic rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(:pool => rp) clone_spec = RbVmomi::VIM.VirtualMachineCloneSpec(:customization => customization_spec, :location => rspec, :powerOn => false, :template => false) task = src_vm.CloneVM_Task(:folder => src_vm.parent, :name => vmname, :spec => clone_spec) puts "Cloning template #{template} to new VM #{vmname}" task.wait_for_completion puts "Finished creating virtual machine #{vmname}" if config[:power] vm = find_in_folders(dc.vmFolder, RbVmomi::VIM::VirtualMachine, vmname) or fatal_exit("VM #{vmname} not found") vm.PowerOnVM_Task.wait_for_completion puts "Powered on virtual machine #{vmname}" # TODO: # Fix this crappy stuff - while loop is used # to ensure the hostname is well done updated by the the tools # should be nice to ask francois from ovh to figured out if one method exist to deal with this corner case # print "\n#{ui.color("Waiting for server", :magenta)}" while vm.guest.hostName != vmname print(".") sleep 2 end puts("\n") print "\n#{ui.color("VM #{vmname} - Ready - Starting chef bootstrap", :magenta)}" puts("\n") print "\n#{ui.color("As to bootstrap chef we need a ssh conn - lets check for it", :magenta)}" #or fatal_exit( "\n#{ui.color("No way to connect the remote server via SSH - IP : #{cidr_ip.ip} - If use used a private IP, ensure a vpn connection exist", :magenta)}" ) print "." until tcp_test_ssh(config[:customization_ip]) { bootstrap = Chef::Knife::Bootstrap.new bootstrap.name_args = [cidr_ip.ip] bootstrap.config[:run_list] = config[:run_list] bootstrap.config[:ssh_user] = config[:ssh_user] bootstrap.config[:chef_node_name] = config[:chef_node_name] bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version) bootstrap.config[:distro] = locate_config_value(:distro) bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root' bootstrap.config[:environment] = config[:environment] bootstrap.run puts("\n") puts("\n") print "\n#{ui.color("server is up and bootstraped with a chef-client", :green)}" puts("\n") } end end |
#tcp_test_ssh(hostname) ⇒ Object
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 |
# File 'lib/chef/knife/ovh_pcc_vm_clone.rb', line 103 def tcp_test_ssh(hostname) tcp_socket = TCPSocket.new(hostname, 22) readable = IO.select([tcp_socket], nil, nil, 5) if readable Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}") yield true else false end rescue SocketError sleep 2 false rescue Errno::ETIMEDOUT false rescue Errno::EPERM false rescue Errno::ECONNREFUSED sleep 2 false # This happens on OVH quite often rescue Errno::EHOSTUNREACH sleep 2 false ensure tcp_socket && tcp_socket.close end |