Class: Chef::Knife::BlueboxServerCreate

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

Instance Method Summary collapse

Instance Method Details

#hObject



102
103
104
# File 'lib/chef/knife/bluebox_server_create.rb', line 102

def h
  @highline ||= HighLine.new
end

#runObject



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
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
219
220
221
# File 'lib/chef/knife/bluebox_server_create.rb', line 106

def run
  $stdout.sync = true

  public_key = config[:public_identity_file] || Chef::Config[:knife][:public_identity_file]
  identity_file = config[:identity_file] || Chef::Config[:knife][:identity_file]

  if config[:password].nil?

    if !public_key
      ui.error('Since no password was provided a public_identity_file needs to be set.')
      exit 1
    end

    if !identity_file
      ui.error('Since no password was provided an identity_file needs to be set.')
      exit 1
    end
  end

  bluebox = Fog::Compute::Bluebox.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"

  image_id = Chef::Config[:knife][:image] || config[:image]
  location_id = Chef::Config[:knife][:location] || config[:location]
  
  server = bluebox.servers.new(
    :flavor_id => Chef::Config[:knife][:flavor] || config[:flavor],
    :image_id => image_id,
    :location_id => location_id,
    :hostname => config[:chef_node_name],
    :username => Chef::Config[:knife][:username] || config[:username],
    :password => config[:password],
    :public_key => public_key.nil? ? nil : File.read(public_key),
    :lb_applications => Chef::Config[:knife][:load_balancer] || config[:load_balancer]
  )

  begin
    response = server.save
  rescue Fog::Compute::Bluebox::NotFound
    puts "#{h.color("Could not locate an image with uuid #{image_id}.\n", :red)}"
    exit 1
  end

  # Wait for the server to start
  begin

    # Make sure we could properly queue the server for creation on BBG.
    raise Fog::Compute::Bluebox::BlockInstantiationError if server.state != "queued"
    puts "#{h.color("Hostname", :cyan)}: #{server.hostname}"
    puts "#{h.color("Server Status", :cyan)}: #{server.state.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']}"
    puts "#{h.color("Load Balanced Applications", :cyan)}: #{server.lb_applications.collect { |x| x['lb_application_name'] }.join(", ")}" unless server.lb_applications.empty?

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

    # Define a timeout and ensure the block starts up in the specified amount of time:
    # ready? will raise Fog::Bluebox::Compute::BlockInstantiationError if block creation fails.
    unless server.wait_for(config[:block_startup_timeout]){ print "."; STDOUT.flush; ready? }

      # The server wasn't started in specified timeout ... Send a destroy call to make sure it doesn't spin up on us later.
      server.destroy
      raise Fog::Compute::Bluebox::BlockInstantiationError, "BBG server not available after #{config[:block_startup_timeout]} seconds."

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

      # Make sure we should be bootstrapping.
      if config[:disable_bootstrap]
        puts "\n\n#{h.color("Boostrapping disabled per command line inputs.  Exiting here.", :green)}"
        exit 0
      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] = run_list
        bootstrap.config[:ssh_password] = config[:password]
        bootstrap.config[:ssh_user] = config[:username]
        bootstrap.config[:identity_file] = identity_file
        bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.hostname
        bootstrap.config[:use_sudo] = true
        bootstrap.config[:distro] = config[:distro]
        bootstrap.run
        print "\n#{h.color("Finished bootstrapping #{server.hostname}\n\n", :green)}"
      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::Compute::Bluebox::BlockInstantiationError => e

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

  end
end

#run_listObject



223
224
225
226
227
# File 'lib/chef/knife/bluebox_server_create.rb', line 223

def run_list
  candidate = @name_args.first
  return candidate if /[\s,]+/ !~ candidate
  candidate.split(/[\s,]+/)
end