Method: Qops::Instance#run_command

Defined in:
lib/qops/deployment/instances.rb

#run_commandObject



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
260
261
262
263
264
265
266
267
268
269
# File 'lib/qops/deployment/instances.rb', line 191

def run_command
  initialize_run
  instances = retrieve_instances

  command = options[:command_for_run_command] || ask('Which command you want to execute?', limited_to: %w[setup configure install_dependencies update_dependencies execute_recipes])

  option = options[:options_for_run_command] || ask('Which command you want to execute?', limited_to: %w[current all_in_once one_by_one])

  puts "Preparing to run command to all servers (#{instances.map(&:hostname).join(', ')})" if option != 'current'

  recipes = options[:recipes] || ask('Recipes list?') if command == 'execute_recipes'

  base_deployment_params = {
    stack_id: config.stack_id,
    command: {
      name: command.to_s
    }
  }

  base_deployment_params[:command][:args] = { recipes: recipes.split(',') } if recipes

  puts "#{base_deployment_params}"

  manifest = { environment: config.deploy_type }

  case option
  when 'current'

    instance = retrieve_instance if config.deploy_type == 'staging'

    if instance.nil?
      puts 'No instance available to execute_recipes'
      exit(0)
    else
      instance_id = instance.instance_id
    end

    print "Run command #{command} on instance #{instance_id}"
    deployment_params = base_deployment_params.deep_dup
    run_opsworks_command(base_deployment_params, [instance_id])
  when 'all_in_once'
    print "Run command #{command} on all instances at once ..."
    deployment_params = base_deployment_params.deep_dup
    run_opsworks_command(deployment_params)
    ping_slack(
      'Quandl::Slack::Release',
      "Run command: `#{command}` on all instances",
      'success',
      manifest.merge(
        app_name: config.app_name,
        command: 'deploy',
        migrate: false,
        completed: Time.now,
        hostname: instances.map(&:hostname),
        instance_id: instances.map(&:instance_id)
      )
    )
  else
    instances.each do |instance|
      print "Run command #{command} on instance #{instance.ec2_instance_id}"

      run_opsworks_command(base_deployment_params, [instance.instance_id])

      ping_slack('Quandl::Slack::InstanceDown', "Run command: `#{command}` on existing instance", 'success',
                 manifest.merge(
                   completed: Time.now,
                   hostname: instance.hostname,
                   instance_id: instance.instance_id,
                   private_ip: instance.private_ip,
                   public_ip: instance.public_ip
                 ))
      puts 'Success'
      break if instance.instance_id == instances.last.instance_id
      delay = config.wait_deploy
      puts "wait for #{delay / 60.0} mintues"
      sleep delay
    end
  end
end