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



239
240
241
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 239

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

#bootstrap_for_node(ip_address) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 227

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
end

#ip_address_available(droplet_id) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 192

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



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
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 118

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}"

  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)


243
244
245
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 243

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

#tcp_test_ssh(hostname, port) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 203

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