Class: Chef::Knife::DigitalOceanDropletCreate
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Chef::Knife::DigitalOceanDropletCreate
- Includes:
- DigitalOceanBase
- Defined in:
- lib/chef/knife/digital_ocean_droplet_create.rb
Instance Method Summary collapse
- #bootstrap_class ⇒ Object
- #bootstrap_for_node(ip_address) ⇒ Object
- #ip_address_available(droplet_id) ⇒ Object
- #run ⇒ Object
- #solo_bootstrap? ⇒ Boolean
- #tcp_test_ssh(hostname) ⇒ Object
- #zero_bootstrap? ⇒ Boolean
Methods included from DigitalOceanBase
#client, included, load_deps, #locate_config_value, #validate!, #wait_for_status
Instance Method Details
#bootstrap_class ⇒ Object
313 314 315 316 317 318 319 320 321 |
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 313 def bootstrap_class if solo_bootstrap? Chef::Knife::SoloBootstrap elsif zero_bootstrap? Chef::Knife::ZeroBootstrap else Chef::Knife::Bootstrap end end |
#bootstrap_for_node(ip_address) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 297 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[:ssh_port] = Chef::Config[:knife][:ssh_port] || config[:ssh_port] 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.config[:secret_file] = locate_config_value(:secret_file) || {} bootstrap end |
#ip_address_available(droplet_id) ⇒ Object
261 262 263 264 265 266 267 268 269 270 |
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 261 def ip_address_available(droplet_id) server = client.droplets.find(id: droplet_id) if server.public_ip yield server.public_ip else sleep @initial_sleep_delay ||= 10 false end end |
#run ⇒ Object
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 |
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 169 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 if zero_bootstrap? && !defined?(Chef::Knife::ZeroBootstrap) ui.error [ 'Knife plugin knife-zero was not found.', 'Please add the knife-zero gem to your Gemfile or', 'install it manually with `gem install knife-zero`.' ].join(' ') exit 1 end droplet = DropletKit::Droplet.new(name: locate_config_value(:server_name), size: locate_config_value(:size), image: locate_config_value(:image), region: locate_config_value(:location), ssh_keys: locate_config_value(:ssh_key_ids), private_networking: locate_config_value(:private_networking), backups: locate_config_value(:backups), ipv6: locate_config_value(:ipv6) ) server = client.droplets.create(droplet) if client.droplets.find(id: server.id).status != 'new' ui.error("Droplet could not be started #{server.inspect}") exit 1 end puts "Droplet creation for #{locate_config_value(:server_name)} started. Droplet-ID is #{server.id}" unless !config.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(server.id) do puts 'done' end puts ui.color("IPv4 address is: #{ip_address}", :green) print ui.color('Waiting for sshd:', :magenta) print('.') until tcp_test_ssh(ip_address) do sleep 2 puts 'done' end if locate_config_value(:bootstrap) || solo_bootstrap? || zero_bootstrap? bootstrap_for_node(ip_address).run else puts ip_address exit 0 end end |
#solo_bootstrap? ⇒ Boolean
323 324 325 |
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 323 def solo_bootstrap? config[:solo] || (config[:solo].nil? && Chef::Config[:knife][:solo]) end |
#tcp_test_ssh(hostname) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 272 def tcp_test_ssh(hostname) port = Chef::Config[:knife][:ssh_port] || config[:ssh_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 |
#zero_bootstrap? ⇒ Boolean
327 328 329 |
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 327 def zero_bootstrap? config[:zero] || (config[:zero].nil? && Chef::Config[:knife][:zero]) end |