Class: Gaptool::DeployCommand

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/gaptool_client/commands.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/gaptool_client/commands.rb', line 304

def execute
  attrs = Gaptool::Helpers.split_attrs(attribute_list)
  if instance
    n = Gaptool::API.client.getonenode(instance)
    if n['environment'] != environment
      Gaptool::Helpers.error "Instance #{instance} is not in environment #{environment}"
    else
      app_list.each do |app|
        Gaptool::Helpers.error "Instance #{instance} does not host #{app} in env #{environment}" \
          unless n['apps'].include?(app)
      end
    end
    nodes = [n]

  else
    params = hidden? ? { hidden: true } : {}
    nodes = []
    app_list.each do |app|
      nodes.concat(Gaptool::API.client.getappnodes(app, environment, params))
    end
  end

  # dedup nodes
  seen = Set.new
  app_set = Set.new(app_list)
  nodes = nodes.select do |x|
    res = !seen.include?(x['instance'])
    seen << x['instance']
    res
  end

  nodes = nodes.map do |x|
    x.merge(
      'attrs' => attrs,
      'deploy_apps' => (Set.new(x['apps']) & app_set).to_a
    )
  end
  pre_hook = proc do |node|
    json = node['attrs']
    json['run_list'] = node['chef_runlist'] || ['recipe[deploy]']
    json['deploy_apps'] = node['deploy_apps']
    host = "#{node['role']}:#{node['environment']}:#{node['instance']}"
    puts "#{Rainbow('Deploying apps').cyan} '" + \
         Rainbow(node['deploy_apps'].join(' ')).green + \
         "' #{Rainbow('on').cyan} " + \
         Rainbow(host).green
    upload!(StringIO.new(json.to_json), '/tmp/chef.json')
  end

  command = 'sudo gtrunchef -j /tmp/chef.json'
  command = "#{command} -v" if verbose?
  command = "#{command} -b #{branch}" if branch
  command = "#{command} -M" if migrate?
  command = "#{command} -R" if rollback?
  command = "#{commnad} -B #{chef_branch}" if chef_branch

  res = Gaptool::SSH.exec(
    nodes, [command],
    pre_hooks: [pre_hook], serial: serial?, continue_on_errors: continue_on_errors?,
    batch_size: batch_size
  )
  exit res
end