lib/gaptool_client/commands.rb in gaptool-client-0.8.0.pre.alpha9 vs lib/gaptool_client/commands.rb in gaptool-client-0.8.0.pre.alpha10
- old
+ new
@@ -1,8 +1,7 @@
# coding: utf-8
# rubocop:disable Metrics/LineLength
-#
require 'rainbow'
require 'json'
require 'clamp'
require 'set'
require 'gaptool_client/api'
@@ -21,15 +20,25 @@
option ['-b', '--chef-branch'], 'BRANCH', 'branch of the chef repository to use'
option(['-R', '--chef_runlist'], 'RECIPE|ROLE',
'override chef run_list. recipe[cb::recipe] or role[myrole]. Can be specified multiple times',
multivalued: true, attribute_name: 'chef_runlist')
option ['--no-terminate'], :flag, 'Add terminate protection'
+
def execute
no_terminate = no_terminate? ? true : nil
- Gaptool::API.client.addnode(zone, type, role, environment, nil, security_group,
- ami, chef_repo, chef_branch, chef_runlist,
- no_terminate)
+ res = Gaptool::API.client.addnode(zone, type, role, environment, nil, security_group,
+ ami, chef_repo, chef_branch, chef_runlist,
+ no_terminate)
+ if res['result'] == 'error'
+ puts Rainbow(res['message']).red
+ exit 1
+ end
+
+ puts "#{Rainbow('Successfully init instance').green} #{Rainbow(res['instance']).blue}:"
+ res.select { |k, _| k != 'instance' && k != 'secret' }.sort.each do |k, v|
+ puts "- #{k}: #{Rainbow(v).blue}"
+ end
end
end
class TerminateCommand < Clamp::Command
option ['-i', '--instance'], 'INSTANCE', 'Instance ID, e.g. i-12345678', required: true
@@ -212,42 +221,32 @@
instance: instance,
environment: environment)
nodes = nodes.map do |x|
x['whyrun'] = whyrun?
- x['chef_branch'] = chef_branch
- x['attrs'] = attrs
+ x['attrs'] = {
+ 'chef_branch' => chef_branch
+ }.merge(attrs)
x
end
pre_hook = proc do |node|
- if node['chef_runlist'].nil?
- runlist = ['recipe[main]']
- elsif node['chef_runlist'].is_a? Array
- runlist = node['chef_runlist']
- end
- json = {
- 'role' => node['role'],
- 'environment' => node['environment'],
- 'run_list' => runlist,
- 'apps' => node['apps'] || [],
- 'gaptool' => {
- 'user' => ENV['GT_USER'],
- 'key' => ENV['GT_KEY'],
- 'url' => ENV['GT_URL']
- }
- }.merge(node['attrs'])
+ cl = Gaptool::API.new
+ json = cl.getnodeattrs(node['instance'], node['attrs'])
+ json['run_list'] ||= node['chef_runlist'] || ['recipe[main]']
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['chef_branch'].nil?
json['chefbranch'] = node['chef_branch']
pull = "#{git} checkout -f #{node['chef_branch']}; #{git} fetch --all; #{git} reset --hard origin/#{node['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
@@ -274,12 +273,13 @@
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'
option ['-s', '--serial'], :flag, 'Run command serially. Order of execution is unknown.'
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 # rubocop:disable Metrics/MethodLength
+ 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}"
@@ -305,48 +305,36 @@
nodes = nodes.select do |x|
res = !seen.include?(x['instance'])
seen << x['instance']
res
end
+
nodes = nodes.map do |x|
- x['apps'] = x['apps']
- x['apps_to_deploy'] = (Set.new(x['apps']) & app_set).to_a
- x['rollback'] = rollback?
- x['branch'] = branch || 'master'
- x['migrate'] = migrate?
- x['attrs'] = attrs
+ x['verbose'] = verbose?
+ x['attrs'] = {
+ 'deploy_apps' => (Set.new(x['apps']) & app_set).to_a,
+ 'rollback' => rollback?,
+ 'branch' => branch,
+ 'migrate' => migrate?
+ }.merge(attrs)
x
end
pre_hook = proc do |node|
+ client = Gaptool::API.new
+ json = client.getnodeattrs(node['instance'], node['attrs'])
+ json['run_list'] = node['chef_runlist'] || ['recipe[deploy]']
host = "#{node['role']}:#{node['environment']}:#{node['instance']}"
puts "#{Rainbow('Deploying apps').cyan} '" + \
- Rainbow(node['apps_to_deploy'].join(' ')).green + \
+ Rainbow(json['deploy_apps'].join(' ')).green + \
"' #{Rainbow('on').cyan} " + \
Rainbow(host).green
+ puts "#{Rainbow(host).green}: #{JSON.pretty_generate(json)}" if node['verbose']
- if node['chef_runlist'].nil?
- runlist = ['recipe[deploy]']
- elsif node['chef_runlist'].is_a? Array
- runlist = node['chef_runlist']
- end
- json = {
- 'role' => node['role'],
- 'environment' => node['environment'],
- 'run_list' => runlist,
- 'apps' => node['apps'],
- 'deploy_apps' => node['apps_to_deploy'],
- 'rollback' => node['rollback'],
- 'branch' => node['branch'],
- 'migrate' => node['migrate'],
- 'gaptool' => {
- 'user' => ENV['GT_USER'],
- 'key' => ENV['GT_KEY'],
- 'url' => ENV['GT_URL']
- }
- }.merge(node['attrs']).to_json
- upload!(StringIO.new(json), '/tmp/chef.json')
+ 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