Class: Chef::Knife::OpennebulaServerCreate

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

Instance Method Summary collapse

Methods included from OpennebulaBase

#connection, included, #locate_config_value, #msg_pair, #opennebula_endpoint, #opennebula_password, #opennebula_username, #validate!

Instance Method Details

#bootstrap(ip) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/chef/knife/opennebula_server_create.rb', line 169

def bootstrap(ip)
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = ip
  bootstrap.config[:run_list] = locate_config_value(:run_list)
  bootstrap.config[:ssh_user] = locate_config_value(:ssh_user)
  bootstrap.config[:ssh_port] = locate_config_value(:ssh_port)
  bootstrap.config[:identity_file] = locate_config_value(:identity_file)
  bootstrap.config[:distro] = locate_config_value(:distro)
  bootstrap.config[:host_key_verify] = config[:host_key_verify]
  bootstrap.config[:first_boot_attributes] = locate_config_value(:json_attributes) || {}
  bootstrap.config[:template_file] = locate_config_value(:template_file)
  bootstrap.config[:chef_node_name] = locate_config_value(:chef_node_name)
  bootstrap.config[:bootstrap_install_command] = locate_config_value(:bootstrap_install_command)
  bootstrap.config[:use_sudo] = true unless bootstrap.config[:ssh_user] == 'root'
  bootstrap.run
end

#flavorObject



210
211
212
213
# File 'lib/chef/knife/opennebula_server_create.rb', line 210

def flavor
  @flavor ||= connection.flavors.get_by_name(locate_config_value(:opennebula_template))
	@flavor[0]
end

#hObject



115
116
117
# File 'lib/chef/knife/opennebula_server_create.rb', line 115

def h
  @highline ||= HighLine.new
end

#runObject



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
# File 'lib/chef/knife/opennebula_server_create.rb', line 119

def run
  validate!
	validate_flavor!

	newvm = connection.servers.new

newvm.flavor = connection.flavors.get(flavor.id)

# set the name of the vm
newvm.name = locate_config_value(:chef_node_name)

newvm.flavor.vcpu = 1

vm = newvm.save
	
ser = server(vm.id)
  puts ui.color("\nServer:", :green)
  msg_pair("VM Name", ser.name)
  msg_pair("VM ID", ser.id)
  msg_pair("IP", ser.ip)
	msg_pair("Template", flavor.name)

  print "\n#{ui.color("Waiting for server", :magenta)}"

  # wait for it to be ready to do stuff
  ser.wait_for { print "."; ready? }

  puts("\n")
  # hack to ensure the nodes have had time to spin up
  print(".")
  sleep 30
  print(".")

  print(".") until tcp_test_ssh(ser.ip) {
    sleep @initial_sleep_delay ||= 10
    puts("done")
  }

#Bootstrap VM
  bootstrap(ser.ip)

  puts ui.color("Server:", :green)
  msg_pair("Name", ser.name)
  msg_pair("IP", ser.ip)
  msg_pair("Environment", config[:environment] || '_default')
  msg_pair("Run List", config[:run_list].join(', '))
  msg_pair("JSON Attributes",config[:json_attributes]) unless !config[:json_attributes] || config[:json_attributes].empty?
end

#server(id) ⇒ Object



215
216
217
# File 'lib/chef/knife/opennebula_server_create.rb', line 215

def server(id)
  @server ||= connection.servers.get(id)
end

#tcp_test_ssh(hostname) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/chef/knife/opennebula_server_create.rb', line 186

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 Errno::ETIMEDOUT
  false
rescue Errno::EPERM
  false
rescue Errno::ECONNREFUSED
  sleep 2
  false
rescue Errno::EHOSTUNREACH
  sleep 2
  false
ensure
  tcp_socket && tcp_socket.close
end

#validate_flavor!Object



219
220
221
222
223
224
# File 'lib/chef/knife/opennebula_server_create.rb', line 219

def validate_flavor!
  if flavor.nil?
    ui.error("You have not provided a valid Template NAme. Please note the options for this value are -t or --template-name.")
    exit 1
  end
end