#!/usr/bin/ruby require 'rubygems' require 'optparse' require 'poolparty' require "fileutils" # Set defaults commandables = %w(ssh cmd scp restart start stop install start_maintain stop_maintain) options = PoolParty.options(:optsparse => { :banner => <<-EOU Usage: instance [OPTIONS] { #{commandables.join(" | ")} } ----------------------------------------------------------------- EOU }) PoolParty.load_plugins master = PoolParty::Master.new list = PoolParty::Optioner.parse(ARGV.dup, %w(-v --verbose)) num = list.reject {|a| commandables.include?(a) }.pop instance = master.get_node(num) unless instance puts "Cloud is not running" exit end list.each do |cmd| case cmd when "ssh" PoolParty.message "Ssh'ing into #{instance.ip}" instance.ssh when "cmd" PoolParty.message "Executing #{instance_options[:cmd]} on #{instance.ip}" instance.ssh list.shift when "scp" list.shift src, dest = list.shift, (list.shift || "~") PoolParty.message "Scp'ing #{src} to #{dest}" instance.scp src, (dest || "~") when "restart" PoolParty.message "Restarting services" instance.restart_with_monit when "start" PoolParty.message "Starting services" instance.start_with_monit when "stop" PoolParty.message "Stopping services" instance.stop_with_monit when "install" PoolParty.message "Installing services" instance.install when "start_maintain" PoolParty.message "Running heartbeat failover service" pid = Master.run_thread_loop(:daemonize => true) do instance.become_master if instance.is_not_master_and_master_is_not_running? end File.open(Application.maintain_pid_path) {|f| f.write(pid)} when "stop_maintain" PoolParty.message "Stopping heartbeat failover service" pid = open(Application.maintain_pid_path).read `kill -9 #{pid}` FileUtils.rm Application.maintain_pid_path # Check this else puts master.list end end