#!/usr/bin/env ruby require "bundler/setup" require "EndlessWaffleCLI" require 'json' require 'getoptlong' require 'console_table' require 'colorize' require 'date' require 'pp' if ENV['EndlessWaffleURL'].nil? puts "EndlessWaffleCLI requires the 'EndlessWaffleURL' environment variable to be set." exit 1 else EndlessWaffleCLI.setServer ENV['EndlessWaffleURL'] end if ENV['EndlessWaffleToken'].nil? puts "EndlessWaffleCLI requires the 'EndlessWaffleToken' environment variable to be set." exit 1 else EndlessWaffleCLI.setToken ENV['EndlessWaffleToken'] end def showHelp puts puts "Endless Waffle Terminate" puts "Usage example: #{$0} -n sandbox1.dev1" puts puts "Options:" puts "-n or --name".ljust(30) +"-> name of ec2 instance you wish to terminate" puts "-h or --help".ljust(30) +"-> Show this help message." puts end def colorizeState(state) if state == "running" state.colorize(:green) else state.colorize(:red) end end def showTable(result) table_config = [ {:key=>:instanceId, :size=>13, :title=>"Instance Id"}, {:key=>:name, :size=>13, :title=>"Name"}, {:key=>:role, :size=>8, :title=>"Role"}, {:key=>:environment, :size=>14, :title=>"Env"}, {:key=>:instanceType, :size=>10, :title=>"Type"}, {:key=>:availabilityZone, :size=>12, :title=>"Zone"}, {:key=>:instanceState, :size=>12, :title=>"State"}, {:key=>:dnsName, :size=>"*", :title=>"DNS Name"}, ] ConsoleTable.define(table_config) do |table| result.each do |r| table << [ r["instanceId"].colorize(:yellow), r["nodemap"]["name"], r["nodemap"]["role"], r["nodemap"]["environment"], r["instanceType"], r["placement"]["availabilityZone"], colorizeState(r["instanceState"]["name"]), r["dnsName"]] end end end parser = GetoptLong.new parser.set_options(["-n", "--name", GetoptLong::NO_ARGUMENT], ["-h", "--help", GetoptLong::NO_ARGUMENT] ) name = "" begin begin opt,arg = parser.get_option break if not opt case opt when "-h" || "--help" showHelp exit when "-n" || "--name" name = ARGV[0] end rescue => err puts "#{err.class()}: #{err.message}" exit 1 end end while 1 if name.chomp == "" || name.chomp.empty? showHelp puts "Hint: you must specify a name of a instance to terminate, in the form of name.enviroment, for example sanbox24.dev1" exit 1 end puts "" puts "Searching for possable matches, Note: the first match is allways selected if any found" query = {:name => name.split('.')[0], :environment => name.split('.')[1]} result = EndlessWaffleCLI.queryEc2 query if result.length <1 puts "No results Found, Exiting now...".colorize(:red) exit 1 end showTable(result) selected_instance = result.first puts "\n\n" puts "Selected Instance".colorize(:light_cyan) puts "========================================" puts "instanceId: ".ljust(15) + selected_instance["instanceId"].colorize(:yellow) puts "Role: ".ljust(15) + selected_instance["nodemap"]["role"] puts "Name: ".ljust(15) + selected_instance["nodemap"]["name"] puts "Environment: ".ljust(15) + selected_instance["nodemap"]["environment"] puts "Launch Time: ".ljust(15) + DateTime.parse(selected_instance["launchTime"]).strftime('%b %e, %Y %H:%M:%S %z') puts "\n\n" print "Are you sure you want to " + "TERMINATE".colorize(:red) +" this? (Y/N): " user_choice=STDIN.gets user_choice=user_choice.to_s.chomp if ( (user_choice=="y") || (user_choice=="Y") ) puts "Proceeding with the TERMINATE:" output = EndlessWaffleCLI.ec2terminate(selected_instance["instanceId"]) puts output["log"].join else puts "ABORTING NOW!" end