lib/cap_reserve.rb in cap_reserve-0.1.5 vs lib/cap_reserve.rb in cap_reserve-0.1.6

- old
+ new

@@ -4,76 +4,113 @@ require 'uri' require 'yajl' $:.unshift File.dirname(__FILE__) -Capistrano::Configuration.instance(:must_exist).load do +class String - desc "Reserve environment using RESERVE=minutes" - task :maitre_d do - begin - env, user, time, force, url, destroy, branch = - ENV['RESERVE_ENV'], ENV['USER'], ENV['RESERVE'], ENV['FORCE'], ENV['RESERVE_URL'], ENV['DESTROY'], ENV['BRANCH'] + # Colors - help = <<-HELP - FORCE=1 to deploy anyway - DESTROY=1 to deploy and destroy reservation -HELP + def blue + "\e[34m#{self}\e[0m" + end - expires_to_string = lambda do |expires| - left = Time.at(expires) - Time.now - if left < 60 - "#{left} seconds" - elsif left / 60 < 60 - "#{sprintf "%.1f", left / 60} minutes" - else - "#{sprintf "%.1f", left / 60 / 60} hours" - end - end + def green + "\e[32m#{self}\e[0m" + end - get = lambda do |full_url, hash| - params = '' - hash.each do |k, v| - params << "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}&" - end - params.chop! # trailing & + def red + "\e[31m#{self}\e[0m" + end - json = open("#{full_url}?#{params}").read - Yajl::Parser.parse(json) + def yellow + "\e[33m#{self}\e[0m" + end +end + +Capistrano::Configuration.instance(:must_exist).load do + namespace :maitre_d do + + expires_to_string = lambda do |expires| + left = Time.at(expires) - Time.now + if left < 60 + "#{left} seconds" + elsif left / 60 < 60 + "#{sprintf "%.1f", left / 60} minutes" + else + "#{sprintf "%.1f", left / 60 / 60} hours" end + end - create = lambda do |params| - if time - get.call("#{url}/reservations/create", { - :environment => env, :user => user, :seconds => time.to_i * 60, :branch => branch - }.merge(params)) - puts "\n\e[32mReservation created\e[0m: \e[33m#{user}@#{env}\e[0m for \e[33m#{time.to_i} minutes\e[0m\n\n" - elsif destroy - res = get.call("#{url}/reservations/destroy", :environment => env) - if res['status'] == 'reserved' - puts "\n\e[32mReservation destroyed\e[0m: \e[33m#{res['user']}@#{env}\e[0m (\e[33m#{expires_to_string.call Time.at(res['expires'])}\e[0m left)\n\n" + get = lambda do |full_url, hash| + params = '' + hash.each do |k, v| + params << "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}&" + end + params.chop! # trailing & + + json = open("#{full_url}?#{params}").read + Yajl::Parser.parse(json) + end + + desc "Reserve environment using RESERVE=minutes" + task :default do + begin + env, user, time, force, url, destroy, branch = + ENV['RESERVE_ENV'], ENV['USER'], ENV['RESERVE'], ENV['FORCE'], ENV['RESERVE_URL'], ENV['DESTROY'], ENV['BRANCH'] + + help = <<-HELP +FORCE=1 to deploy anyway +DESTROY=1 to deploy and destroy reservation +HELP + + create = lambda do |params| + if time + get.call("#{url}/reservations/create", { + :environment => env, :user => user, :seconds => time.to_i * 60, :branch => branch + }.merge(params)) + puts "\n#{"Reservation created".green}: #{"#{user}@#{env}".yellow} for #{"#{time.to_i} minutes".yellow}\n\n" + elsif destroy + res = get.call("#{url}/reservations/destroy", :environment => env) + if res['status'] == 'reserved' + puts "\n#{"Reservation destroyed".green}: #{"#{res['user']}@#{env}".yellow} (#{expires_to_string.call(Time.at(res['expires'])).yellow} left)\n\n" + end end end - end - if env && user - if destroy - create.call({}) - elsif force - create.call(:force => true) - else - res = get.call("#{url}/reservations/show", :environment => env) - if res['status'] == 'reserved' - puts "\n\e[31mReservation exists\e[0m: \e[33m#{res['user']}@#{env}\e[0m for \e[33m#{expires_to_string.call Time.at(res['expires'])}\e[0m\n#{help}\n" - exit 0 - else + if env && user + if destroy create.call({}) + elsif force + create.call(:force => true) + else + res = get.call("#{url}/reservations/show", :environment => env) + if res['status'] == 'reserved' + puts "\n#{"Reservation exists".red}: #{"#{res['user']}@#{env}".yellow} (#{expires_to_string.call(Time.at(res['expires'])).yellow})\n#{help}\n" + exit 0 + else + create.call({}) + end end end + rescue Exception => e + if e.inspect.include?('SystemExit') + exit 0 + end end - rescue Exception => e - if e.inspect.include?('SystemExit') - exit 0 + end + + desc "Show environment reservation status" + task :available do + puts "\n" + url, envs = ENV['RESERVE_URL'], ENV['RESERVE_ENVS'] + envs.split(/\s/).each do |env| + res = get.call("#{url}/reservations/show", :environment => env) + puts "#{env.yellow} is #{res['status'] == 'available' ? "available".green : "reserved".red}" + if res['status'] == 'reserved' + puts " #{"#{res['user']}@#{env}".yellow} for #{expires_to_string.call(Time.at(res['expires'])).yellow}" + end + puts "\n" end end end end \ No newline at end of file