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, port) ⇒ Object
Methods included from DigitalOceanBase
#client, #h, included, load_deps, #locate_config_value, #validate!
Instance Method Details
#bootstrap_class ⇒ Object
270 271 272 |
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 270 def bootstrap_class solo_bootstrap? ? Chef::Knife::SoloBootstrap : Chef::Knife::Bootstrap end |
#bootstrap_for_node(ip_address) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 255 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.config[:secret_file] = locate_config_value(:secret_file) || {} bootstrap end |
#ip_address_available(droplet_id) ⇒ Object
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 220 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 |
#run ⇒ Object
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 |
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 141 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(','), :private_networking => locate_config_value(:private_networking)) 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
274 275 276 |
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 274 def solo_bootstrap? config[:solo] || (config[:solo].nil? && Chef::Config[:knife][:solo]) end |
#tcp_test_ssh(hostname, port) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/chef/knife/digital_ocean_droplet_create.rb', line 231 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 |