From f08516929901730dc3b003f0942dbbd630663a80 Mon Sep 17 00:00:00 2001 From: Jhon Honce Date: Thu, 25 Sep 2014 17:25:27 -0700 Subject: Add cluster.sh --- cloud.rb | 83 ++++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 20 deletions(-) (limited to 'cloud.rb') diff --git a/cloud.rb b/cloud.rb index 8f53b9460..840742b8c 100755 --- a/cloud.rb +++ b/cloud.rb @@ -6,13 +6,14 @@ require 'yaml' require 'securerandom' require 'fileutils' require 'parseconfig' +require 'open3' SCRIPT_DIR = File.expand_path(File.dirname(__FILE__)) module OpenShift module Ops # WARNING: we do not currently support environments with hyphens in the name - SUPPORTED_ENVS = ['prod','stg','int','tint','kint','test'] + SUPPORTED_ENVS = %w(prod stg int tint kint test jint) class GceHelper def self.list_hosts() @@ -105,10 +106,7 @@ module OpenShift @inventory = 'inventory/hosts' if @inventory.nil? # This is used instead of passing in the json on the cli to avoid quoting problems - tmpfile = Tempfile.new('extra_vars') - tmpfile.write(@extra_vars.to_json) - tmpfile.sync() - tmpfile.close() + tmpfile = Tempfile.open('extra_vars') { |f| f.write(@extra_vars.to_json); f} cmds = [] @@ -121,18 +119,21 @@ module OpenShift # We need pipelining off so that we can do sudo to enable the root account cmds << %Q[export ANSIBLE_SSH_PIPELINING='#{@pipelining.to_s}'] - - ssh_key_arg = %q[--private-key=~/.ssh/mmcgrath_libra] if File.file?(ENV['HOME']+'/.ssh/mmcgrath_libra.pem') - - cmds << %Q[time -p ansible-playbook -i #{@inventory} #{@verbosity} #{playbook} #{ssh_key_arg} --extra-vars '@#{tmpfile.path}'] + cmds << %Q[time -p ansible-playbook -i #{@inventory} #{@verbosity} #{playbook} --extra-vars '@#{tmpfile.path}'] cmd = cmds.join(' ; ') - unless system(cmd) - puts %Q[Following command failed with exit code: #{$?.exitstatus}\n#{cmd}] - puts %Q[extra_vars: #{@extra_vars.to_json}] + stdout, stderr, status = Open3.capture3(cmd) + if 0 != status.exitstatus + raise %Q[Following command failed with exit code: #{status.exitstatus} +#{cmd} +extra_vars: #{@extra_vars.to_json} +stdout: #{stdout} +stderr: #{stderr} +] end - tmpfile.unlink + ensure + tmpfile.unlink if tmpfile end def merge_extra_vars_file(file) @@ -165,9 +166,17 @@ module OpenShift ah.inventory = 'inventory/gce/gce.py' return ah end + + def ignore_bug_6407 + puts + puts %q[ .---- Spurious warning "It is unnecessary to use '{{' in loops" (ansible bug 6407) ----.] + puts %q[ V V] + end + end class GceCommand < Thor + option :type, :required => true, :enum => LaunchHelper.get_gce_host_types, :desc => 'The host type of the new instances.' option :env, :required => true, :aliases => '-e', :enum => OpenShift::Ops::SUPPORTED_ENVS, @@ -199,14 +208,12 @@ module OpenShift puts puts 'Creating instance(s) in GCE...' - puts - puts %q[ .---- Spurious warning "It is unnecessary to use '{{' in loops" (ansible bug 6407) ----.] - puts %q[ V V] - + ah.ignore_bug_6407 ah.run_playbook("playbooks/gce/#{options[:type]}/launch.yml") end + option :name, :required => false, :type => :string, :desc => 'The name of the instance to configure.' option :env, :required => false, :aliases => '-e', :enum => OpenShift::Ops::SUPPORTED_ENVS, @@ -238,13 +245,49 @@ module OpenShift puts puts "Configuring #{options[:type]} instance(s) in GCE..." - puts - puts " .---- Disregard this (ansible bug 6407) ----." - puts " V V" + ah.ignore_bug_6407 ah.run_playbook("playbooks/gce/#{host_type}/config.yml") end + option :name, :required => false, :type => :string, + :desc => 'The name of the instance to terminate.' + option :env, :required => false, :aliases => '-e', :enum => OpenShift::Ops::SUPPORTED_ENVS, + :desc => 'The environment of the new instances.' + option :type, :required => false, :enum => LaunchHelper.get_gce_host_types, + :desc => 'The type of the instances to configure.' + option :confirm, :required => false, :type => :boolean, + :desc => 'Terminate without interactive confirmation' + desc "terminate", 'Terminate instances' + def terminate() + ah = AnsibleHelper.for_gce() + + abort 'Error: you can\'t specify both --name and --type' unless options[:type].nil? || options[:name].nil? + + abort 'Error: you can\'t specify both --name and --env' unless options[:env].nil? || options[:name].nil? + + host_type = nil + if options[:name] + details = GceHelper.get_host_details(options[:name]) + ah.extra_vars['oo_host_group_exp'] = options[:name] + ah.extra_vars['oo_env'] = details['env'] + host_type = details['host-type'] + elsif options[:type] && options[:env] + oo_env_host_type_tag = GceHelper.generate_env_host_type_tag_name(options[:env], options[:type]) + ah.extra_vars['oo_host_group_exp'] = "groups['#{oo_env_host_type_tag}']" + ah.extra_vars['oo_env'] = options[:env] + host_type = options[:type] + else + abort 'Error: you need to specify either --name or (--type and --env)' + end + + puts + puts "Terminating #{options[:type]} instance(s) in GCE..." + ah.ignore_bug_6407 + + ah.run_playbook("playbooks/gce/#{host_type}/terminate.yml") + end + desc "list", "Lists instances." def list() hosts = GceHelper.list_hosts() -- cgit v1.2.3