Class: Chef::Knife::DigitalOceanDropletCreate

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

Instance Method Summary collapse

Methods included from DigitalOceanBase

#client, #h, included, load_deps, #locate_config_value, #validate!

Instance Method Details

#bootstrap_classObject



258
259
260
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 258

def bootstrap_class
  solo_bootstrap? ? Chef::Knife::SoloBootstrap : Chef::Knife::Bootstrap
end

#bootstrap_for_node(ip_address) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 244

def bootstrap_for_node(ip_address)
  bootstrap = bootstrap_class.new
  bootstrap.name_args = [ ip_address ]
  bootstrap.config.merge! config
  bootstrap.config[:chef_node_name] = locate_config_value(:server_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[:template_file] = locate_config_value(:template_file)
  bootstrap.config[:environment] = locate_config_value(:environment)
  bootstrap.config[:first_boot_attributes] = locate_config_value(:json_attributes) || {}
  bootstrap
end

#ip_address_available(droplet_id) ⇒ Object



209
210
211
212
213
214
215
216
217
218
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 209

def ip_address_available(droplet_id)
  response = client.droplets.show(droplet_id)
  if response.droplet.ip_address
    yield
    response.droplet.ip_address
  else
    sleep @initial_sleep_delay ||= 10
    false
  end
end

#runObject



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

def run
  $stdout.sync = true

  validate!

  unless locate_config_value(:server_name)
    ui.error('Server Name cannot be empty: -N <servername>')
    exit 1
  end

  unless locate_config_value(:image)
    ui.error('Image cannot be empty: -I <image>')
    exit 1
  end

  unless locate_config_value(:size)
    ui.error('Size cannot be empty: -S <size>')
    exit 1
  end

  unless locate_config_value(:location)
    ui.error('Location cannot be empty: -L <region>')
    exit 1
  end

  unless locate_config_value(:ssh_key_ids)
    ui.error('One or more DigitalOcean SSH key ids missing: -K <KEY1>, <KEY2> ...')
    exit 1
  end

  if solo_bootstrap? && !defined?(Chef::Knife::SoloBootstrap)
    ui.error [
      'Knife plugin knife-solo was not found.',
      'Please add the knife-solo gem to your Gemfile or',
      'install it manually with `gem install knife-solo`.'
    ].join(" ")
    exit 1
  end

  response = client.droplets.create(:name        => locate_config_value(:server_name),
                                    :size_id     => locate_config_value(:size),
                                    :image_id    => locate_config_value(:image),
                                    :region_id   => locate_config_value(:location),
                                    :ssh_key_ids => locate_config_value(:ssh_key_ids).join(','))

  if response.status != 'OK'
    ui.error("Droplet could not be started #{response.inspect}")
    exit 1
  end

  puts "Droplet creation for #{locate_config_value(:server_name)} started. Droplet-ID is #{response.droplet.id}"

  unless !config.has_key?(:json_attributes) || config[:json_attributes].empty?
    puts ui.color("JSON Attributes: #{config[:json_attributes]}", :magenta)
  end

  print ui.color("Waiting for IPv4-Address", :magenta)
  print(".") until ip_address = ip_address_available(response.droplet.id) {
    puts 'done'
  }

  puts ui.color("IPv4 address is: #{ip_address}", :green)

  print ui.color('Waiting for sshd:', :magenta)
  print('.') until tcp_test_ssh(ip_address, 22) {
    sleep 2
    puts 'done'
  }

  if locate_config_value(:bootstrap) || solo_bootstrap?
    bootstrap_for_node(ip_address).run
  else
    puts ip_address
    exit 0
  end

end

#solo_bootstrap?Boolean

Returns:

  • (Boolean)


262
263
264
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 262

def solo_bootstrap?
  config[:solo] || (config[:solo].nil? && Chef::Config[:knife][:solo])
end

#tcp_test_ssh(hostname, port) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 220

def tcp_test_ssh(hostname, port)
  tcp_socket = TCPSocket.new(hostname, port)
  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