Skip to content
Browse files

Adding maitre_d:available

  • Loading branch information...
1 parent 269879f commit bdbdbb7bd9a4c511354d4bf7ad5327756962a2dd @winton committed Jan 20, 2012
Showing with 91 additions and 54 deletions.
  1. +1 −1 README.md
  2. +90 −53 lib/cap_reserve.rb
View
2 README.md
@@ -22,7 +22,7 @@ You must have a [maitre_d](https://github.com/winton/maitre_d) server running fi
task :reserve do
ENV['RESERVE_ENV'] = 'staging'
ENV['RESERVE_URL'] = 'http://localhost:3000'
- maitre_d
+ maitre_d.default
end
before "deploy", "reserve"
View
143 lib/cap_reserve.rb
@@ -6,73 +6,110 @@
$:.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
+
+ def yellow
+ "\e[33m#{self}\e[0m"
+ end
+end
- json = open("#{full_url}?#{params}").read
- Yajl::Parser.parse(json)
+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

0 comments on commit bdbdbb7

Please sign in to comment.
Something went wrong with that request. Please try again.