Permalink
Browse files

Logic fixes, using open-uri instead of nestful

  • Loading branch information...
1 parent 67c8bc3 commit 78ae853e8891641dcf246ca8b324eb8262fa075c @winton committed Oct 13, 2011
Showing with 58 additions and 23 deletions.
  1. +13 −4 README.md
  2. +2 −2 cap_reserve.gemspec
  3. +43 −17 lib/cap_reserve.rb
View
@@ -19,13 +19,13 @@ You must have a [maitre_d](https://github.com/winton/maitre_d) server running fi
require 'cap_reserve'
- task :setup_reserve do
+ task :reserve do
ENV['RESERVE_ENV'] = 'staging'
ENV['RESERVE_URL'] = 'http://localhost:3000'
- reserve
+ maitre_d
end
- before "deploy", "setup_reserve"
+ before "deploy", "reserve"
Use It
------
@@ -38,15 +38,24 @@ Force the deploy even if reserved:
cap deploy FORCE=1
+Destroy the reservation:
+
+ cap deploy DESTROY=1
+
+You can also reserve without deploying:
+
+ cap reserve RESERVE=10
+
How it Works
------------
The `reserve` cap task looks for the following `ENV` variables:
+ ENV['DESTROY'] # Destroy reservation
ENV['FORCE'] # Force deploy
ENV['RESERVE'] # Minutes to reserve environment
ENV['RESERVE_ENV'] # Name of deploy environment
ENV['RESERVE_URL'] # URL to your maitre_d server
ENV['USER'] # Name of user
- In the example above, we use the `setup_reserve` cap task to set up these variables.
+ In the example above, we use the `setup_reserve` cap task to set up the `RESERVE_ENV` and `RESERVE_URL` variables.
View
@@ -20,6 +20,6 @@ Gem::Specification.new do |s|
s.test_files = `cd #{root} && git ls-files -- {features,test,spec}/*`.split("\n")
s.add_development_dependency "rspec", "~> 1.0"
-
- s.add_dependency "nestful", "= 0.0.7"
+
+ s.add_dependency "yajl-ruby", "= 1.0.0"
end
View
@@ -1,45 +1,71 @@
-gem "nestful", "= 0.0.7"
-require "nestful"
+gem 'yajl-ruby', '= 1.0.0'
+
+require 'open-uri'
+require 'uri'
+require 'yajl'
$:.unshift File.dirname(__FILE__)
Capistrano::Configuration.instance(:must_exist).load do
desc "Reserve environment using RESERVE=minutes"
- task :reserve do
- env, user, time, force, url =
- ENV['RESERVE_ENV'], ENV['USER'], ENV['RESERVE'], ENV['FORCE'], ENV['RESERVE_URL']
+ task :maitre_d do
+ env, user, time, force, url, destroy =
+ ENV['RESERVE_ENV'], ENV['USER'], ENV['RESERVE'], ENV['FORCE'], ENV['RESERVE_URL'], ENV['DESTROY']
+
+ 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
+
+ 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 &
+
+ puts "#{full_url}?#{params}"
+ json = open("#{full_url}?#{params}").read
+ Yajl::Parser.parse(json)
+ end
create = lambda do |params|
if time
- Nestful.get("#{url}/reservations/create", :format => :json, :params => {
+ get.call("#{url}/reservations/create", {
:environment => env, :user => user, :seconds => time.to_i * 60
}.merge(params))
puts "Reservation created: #{user}@#{env} for #{time.to_i} minutes"
- elsif force
- res = Nestful.get("#{url}/reservations/destroy", :params => { :environment => env }, :format => :json)
+ elsif destroy
+ res = get.call("#{url}/reservations/destroy", :environment => env)
if res['status'] == 'reserved'
- puts "Reservation destroyed: #{res['user']}@#{env} (#{(Time.at(res['expires']) - Time.now) / 60} minutes left)"
+ puts "Reservation destroyed: #{res['user']}@#{env} (#{expires_to_string.call Time.at(res['expires'])} left)"
end
end
end
if env && user
- if force
+ if destroy
+ create.call({})
+ elsif force
create.call(:force => true)
else
- res = Nestful.get("#{url}/reservations/show", :params => { :environment => env }, :format => :json)
+ res = get.call("#{url}/reservations/show", :environment => env)
if res['status'] == 'reserved'
- if res['user'] == user
- create.call({})
- else
- puts "Reservation exists: #{res['user']}@#{env} for #{(Time.at(res['expires']) - Time.now) / 60} minutes"
- exit 0
- end
+ puts "Reservation exists: #{res['user']}@#{env} for #{expires_to_string.call Time.at(res['expires'])}"
+ exit 0
else
create.call({})
end
end
end
+
+ exit
end
end

0 comments on commit 78ae853

Please sign in to comment.