lib/gaptool_client/commands.rb in gaptool-client-0.8.0.pre.beta3 vs lib/gaptool_client/commands.rb in gaptool-client-0.8.0.pre.beta4

- old
+ new

@@ -252,51 +252,31 @@ default: nil) option ['-s', '--serial'], :flag, 'Run command serially. Order of execution is unknown.' option ['-W', '--whyrun'], :flag, 'Whyrun, like dry-run but different.' option ['-c', '--continue-on-errors'], :flag, 'Continue execution even if one or more hosts fail' option ['-B', '--batch-size'], 'SIZE', "How many hosts to run in parallel (defaults to #{Gaptool::SSH::BATCH_SIZE})", default: Gaptool::SSH::BATCH_SIZE + option ['-v', '--verbose'], :flag, 'More verbose output' def execute attrs = Gaptool::Helpers.split_attrs(attribute_list) nodes = Gaptool::API.query_nodes(hidden: hidden? ? true : nil, role: role, instance: instance, environment: environment) - - nodes = nodes.map do |x| - x['whyrun'] = whyrun? - x['attrs'] = { - 'chef_branch' => chef_branch - }.merge(attrs) - x - end - + nodes = nodes.map { |x| x.merge('attrs' => attrs) } pre_hook = proc do |node| - cl = Gaptool::API.new - json = cl.getnodeattrs(node['instance'], node['attrs']) - json['run_list'] ||= node['chef_runlist'] || ['role[base]'] - git = 'sudo -u admin git' - pull = "#{git} fetch --all; #{git} reset --hard origin/`#{git} rev-parse --abbrev-ref HEAD`" - wopts = node['whyrun'] ? ' -W ' : '' - - unless node['attrs']['chef_branch'].nil? - json['chefbranch'] = node['attrs']['chef_branch'] - pull = "#{git} checkout -fB #{node['attrs']['chef_branch']}; #{git} fetch --all; #{git} reset --hard origin/#{node['attrs']['chef_branch']}" - end - upload!(StringIO.new(json.to_json), '/tmp/chef.json') - script = <<-EOS -#!/bin/bash -set -e -cd /var/data/admin/ops -#{pull} -sudo chef-solo -c /var/data/admin/ops/cookbooks/solo.rb -j /tmp/chef.json -E #{node['environment']}#{wopts} -rm -f /tmp/chef.json -EOS - upload!(StringIO.new(script), '/tmp/chef.sh') + upload!(StringIO.new(node['attrs'].merge( + 'run_list' => node['chef_runlist'] || ['role[base]'] + ).to_json), '/tmp/chef.json') end + + command = 'sudo gtrunchef -j /tmp/chef.json' + command = "#{command} -W" if whyrun? + command = "#{command} -B #{chef_branch}" if chef_branch + command = "#{command} -v" if verbose? res = Gaptool::SSH.exec( - nodes, ['chmod +x /tmp/chef.sh', '/tmp/chef.sh', 'rm -f /tmp/chef.sh'], + nodes, [command], pre_hooks: [pre_hook], serial: serial?, continue_on_errors: continue_on_errors?, batch_size: batch_size ) exit res end @@ -306,10 +286,13 @@ option(['-a', '--app'], 'APP', 'Application(s) to deploy (can be set multiple times)', required: true, multivalued: true) option ['-m', '--migrate'], :flag, 'Toggle running migrations' option ['-e', '--environment'], 'ENVIRONMENT', 'Which environment, e.g. production', required: true + option(['-B', '--chef-branch'], 'BRANCH', + 'branch of the chef repository to use (defaults to last branch used during init/chefrun)', + default: nil) option ['-b', '--branch'], 'BRANCH', 'Git branch to deploy, default is master' option ['-r', '--rollback'], :flag, 'Toggle this to rollback last deploy' option ['-i', '--instance'], 'INSTANCE', 'Instance ID, e.g. i-12345678. If set, all applications MUST be hosted on this node.' option ['-A', '--attribute'], 'ATTRIBUTE', 'Pass one or more parameters to the deploy recipe in recipe.attr=value format', multivalued: true option ['-H', '--hidden'], :flag, 'Display hidden hosts' @@ -348,45 +331,35 @@ seen << x['instance'] res end nodes = nodes.map do |x| - x['verbose'] = verbose? - x['attrs'] = { - 'deploy_apps' => (Set.new(x['apps']) & app_set).to_a, - 'rollback' => rollback?, - 'branch' => branch, - 'migrate' => migrate? - }.merge(attrs) - x + x.merge( + 'attrs' => attrs, + 'deploy_apps' => (Set.new(x['apps']) & app_set).to_a + ) end - pre_hook = proc do |node| - client = Gaptool::API.new - json = client.getnodeattrs(node['instance'], node['attrs']) + 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(json['deploy_apps'].join(' ')).green + \ + Rainbow(node['deploy_apps'].join(' ')).green + \ "' #{Rainbow('on').cyan} " + \ Rainbow(host).green - puts "#{Rainbow(host).green}: #{JSON.pretty_generate(json)}" if node['verbose'] - upload!(StringIO.new(json.to_json), '/tmp/chef.json') - script = <<-EOS -#!/bin/bash -set -e -cd /var/data/admin/ops -sudo -u admin git pull -sudo chef-solo -c /var/data/admin/ops/cookbooks/solo.rb -j /tmp/chef.json -E #{node['environment']} -rm -f /tmp/chef.json -EOS - upload!(StringIO.new(script), '/tmp/deploy.sh') 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, - ['chmod +x /tmp/deploy.sh', '/tmp/deploy.sh', 'rm -f /tmp/deploy.sh'], + nodes, [command], pre_hooks: [pre_hook], serial: serial?, continue_on_errors: continue_on_errors?, batch_size: batch_size ) exit res end