Class: Chef::Knife::BlueboxServerCreate

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/bluebox_server_create.rb

Constant Summary

Constants inherited from Chef::Knife

DEFAULT_SUBCOMMAND_FILES

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args

Instance Method Summary collapse

Methods inherited from Chef::Knife

#ask_question, #bulk_delete, category, #configure_chef, #confirm, #create_object, #delete_object, #edit_data, #edit_object, #file_exists_and_is_readable?, #format_for_display, #format_list_for_display, guess_category, inherited, #initialize, list_commands, load_commands, #load_from_file, #msg, msg, #output, #parse_options, #pretty_print, #rest, run, #show_usage, snake_case_name, #stdin, #stdout, subcommand_category, subcommand_class_from, subcommands, subcommands_by_category, unnamed?

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#hObject



57
58
59
# File 'lib/chef/knife/bluebox_server_create.rb', line 57

def h
  @highline ||= HighLine.new
end

#runObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
# File 'lib/chef/knife/bluebox_server_create.rb', line 61

def run
  require 'fog'
  require 'highline'
  require 'net/ssh/multi'
  require 'readline'
  require 'erb'

  bluebox = Fog::Bluebox::Compute.new(
    :bluebox_customer_id => Chef::Config[:knife][:bluebox_customer_id],
    :bluebox_api_key => Chef::Config[:knife][:bluebox_api_key]
  )

   flavors = bluebox.flavors.inject({}) { |h,f| h[f.id] = f.description; h }
  images  = bluebox.images.inject({}) { |h,i| h[i.id] = i.description; h }

  puts "#{h.color("Deploying a new Blue Box Block...", :green)}\n\n"
  server_args = {
    :flavor_id => config[:flavor],
    :image_id => config[:image],
    :user => config[:username],
    :password => config[:password]
    }
  server_args[:ssh_key] = Chef::Config[:knife][:ssh_key] if Chef::Config[:knife][:ssh_key]

  server = bluebox.servers.new(server_args)
  response = server.save
  $stdout.sync = true

  # Wait for the server to start
  begin

    # Make sure we could properly queue the server for creation on BBG.
    raise Fog::Bluebox::BlockInstantiationError if server.status != "queued"
    puts "#{h.color("Hostname", :cyan)}: #{server.hostname}"
    puts "#{h.color("Server Status", :cyan)}: #{server.status.capitalize}"
    puts "#{h.color("Flavor", :cyan)}: #{flavors[server.flavor_id]}"
    puts "#{h.color("Image", :cyan)}: #{images[server.image_id]}"
    puts "#{h.color("IP Address", :cyan)}: #{server.ips[0]['address']}"

    # The server was succesfully queued... Now wait for it to spin up...
    print "\n#{h.color("Requesting status of #{server.hostname}\n", :magenta)}"

    # Allow for 5 minutes to time out...
    # ready? will raise Fog::Bluebox::BlockInstantiationError if block creation fails.
    unless server.wait_for( 5 * 60 ){ print "."; STDOUT.flush; ready? }

      # The server wasn't started in 5 minutes... Send a destroy call to make sure it doesn't spin up on us later...
      server.destroy
      raise Fog::Bluebox::BlockInstantiationError, "BBG server not available after 5 minutes"

    else
      print "\n\n#{h.color("BBG Server startup succesful.  Accessible at #{server.hostname}\n", :green)}"

      # Make sure we should be bootstrapping.
      unless config[:bootstrap]
        puts "\n\n#{h.color("Boostrapping disabled per command line inputs.  Exiting here.}", :green)}"
        return true
      end

      # Bootstrap away!
      print "\n\n#{h.color("Starting bootstrapping process...", :green)}\n"

      # Connect via SSH and make this all happen.
      begin
        bootstrap = Chef::Knife::Bootstrap.new
        bootstrap.name_args = [ server.ips[0]['address'] ]
        bootstrap.config[:run_list] = @name_args
        unless Chef::Config[:knife][:ssh_key]
          bootstrap.config[:password] = password
        end
        bootstrap.config[:ssh_user] = config[:username]
        bootstrap.config[:identity_file] = config[:identity_file]
        bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.hostname
        bootstrap.config[:use_sudo] = true
        bootstrap.run
      rescue Errno::ECONNREFUSED
        puts h.color("Connection refused on SSH, retrying - CTRL-C to abort")
        sleep 1
        retry
      rescue Errno::ETIMEDOUT
        puts h.color("Connection timed out on SSH, retrying - CTRL-C to abort")
        sleep 1
        retry
      end

    end

  rescue Fog::Bluebox::BlockInstantiationError => e

    puts "\n\n#{h.color("Encountered error starting up BBG block. Auto destroy called.  Please try again.", :red)}"

  end

end