Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Logic fixes, using open-uri instead of nestful

  • Loading branch information...
commit 78ae853e8891641dcf246ca8b324eb8262fa075c 1 parent 67c8bc3
Winton Welsh authored

Showing 3 changed files with 58 additions and 23 deletions. Show diff stats Hide diff stats

  1. +13 4 README.md
  2. +2 2 cap_reserve.gemspec
  3. +43 17 lib/cap_reserve.rb
17 README.md
Source Rendered
@@ -19,13 +19,13 @@ You must have a [maitre_d](https://github.com/winton/maitre_d) server running fi
19 19
20 20 require 'cap_reserve'
21 21
22   - task :setup_reserve do
  22 + task :reserve do
23 23 ENV['RESERVE_ENV'] = 'staging'
24 24 ENV['RESERVE_URL'] = 'http://localhost:3000'
25   - reserve
  25 + maitre_d
26 26 end
27 27
28   - before "deploy", "setup_reserve"
  28 + before "deploy", "reserve"
29 29
30 30 Use It
31 31 ------
@@ -38,15 +38,24 @@ Force the deploy even if reserved:
38 38
39 39 cap deploy FORCE=1
40 40
  41 +Destroy the reservation:
  42 +
  43 + cap deploy DESTROY=1
  44 +
  45 +You can also reserve without deploying:
  46 +
  47 + cap reserve RESERVE=10
  48 +
41 49 How it Works
42 50 ------------
43 51
44 52 The `reserve` cap task looks for the following `ENV` variables:
45 53
  54 + ENV['DESTROY'] # Destroy reservation
46 55 ENV['FORCE'] # Force deploy
47 56 ENV['RESERVE'] # Minutes to reserve environment
48 57 ENV['RESERVE_ENV'] # Name of deploy environment
49 58 ENV['RESERVE_URL'] # URL to your maitre_d server
50 59 ENV['USER'] # Name of user
51 60
52   - In the example above, we use the `setup_reserve` cap task to set up these variables.
  61 + In the example above, we use the `setup_reserve` cap task to set up the `RESERVE_ENV` and `RESERVE_URL` variables.
4 cap_reserve.gemspec
@@ -20,6 +20,6 @@ Gem::Specification.new do |s|
20 20 s.test_files = `cd #{root} && git ls-files -- {features,test,spec}/*`.split("\n")
21 21
22 22 s.add_development_dependency "rspec", "~> 1.0"
23   -
24   - s.add_dependency "nestful", "= 0.0.7"
  23 +
  24 + s.add_dependency "yajl-ruby", "= 1.0.0"
25 25 end
60 lib/cap_reserve.rb
... ... @@ -1,45 +1,71 @@
1   -gem "nestful", "= 0.0.7"
2   -require "nestful"
  1 +gem 'yajl-ruby', '= 1.0.0'
  2 +
  3 +require 'open-uri'
  4 +require 'uri'
  5 +require 'yajl'
3 6
4 7 $:.unshift File.dirname(__FILE__)
5 8
6 9 Capistrano::Configuration.instance(:must_exist).load do
7 10
8 11 desc "Reserve environment using RESERVE=minutes"
9   - task :reserve do
10   - env, user, time, force, url =
11   - ENV['RESERVE_ENV'], ENV['USER'], ENV['RESERVE'], ENV['FORCE'], ENV['RESERVE_URL']
  12 + task :maitre_d do
  13 + env, user, time, force, url, destroy =
  14 + ENV['RESERVE_ENV'], ENV['USER'], ENV['RESERVE'], ENV['FORCE'], ENV['RESERVE_URL'], ENV['DESTROY']
  15 +
  16 + expires_to_string = lambda do |expires|
  17 + left = Time.at(expires) - Time.now
  18 + if left < 60
  19 + "#{left} seconds"
  20 + elsif left / 60 < 60
  21 + "#{sprintf "%.1f", left / 60} minutes"
  22 + else
  23 + "#{sprintf "%.1f", left / 60 / 60} hours"
  24 + end
  25 + end
  26 +
  27 + get = lambda do |full_url, hash|
  28 + params = ''
  29 + hash.each do |k, v|
  30 + params << "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}&"
  31 + end
  32 + params.chop! # trailing &
  33 +
  34 + puts "#{full_url}?#{params}"
  35 + json = open("#{full_url}?#{params}").read
  36 + Yajl::Parser.parse(json)
  37 + end
12 38
13 39 create = lambda do |params|
14 40 if time
15   - Nestful.get("#{url}/reservations/create", :format => :json, :params => {
  41 + get.call("#{url}/reservations/create", {
16 42 :environment => env, :user => user, :seconds => time.to_i * 60
17 43 }.merge(params))
18 44 puts "Reservation created: #{user}@#{env} for #{time.to_i} minutes"
19   - elsif force
20   - res = Nestful.get("#{url}/reservations/destroy", :params => { :environment => env }, :format => :json)
  45 + elsif destroy
  46 + res = get.call("#{url}/reservations/destroy", :environment => env)
21 47 if res['status'] == 'reserved'
22   - puts "Reservation destroyed: #{res['user']}@#{env} (#{(Time.at(res['expires']) - Time.now) / 60} minutes left)"
  48 + puts "Reservation destroyed: #{res['user']}@#{env} (#{expires_to_string.call Time.at(res['expires'])} left)"
23 49 end
24 50 end
25 51 end
26 52
27 53 if env && user
28   - if force
  54 + if destroy
  55 + create.call({})
  56 + elsif force
29 57 create.call(:force => true)
30 58 else
31   - res = Nestful.get("#{url}/reservations/show", :params => { :environment => env }, :format => :json)
  59 + res = get.call("#{url}/reservations/show", :environment => env)
32 60 if res['status'] == 'reserved'
33   - if res['user'] == user
34   - create.call({})
35   - else
36   - puts "Reservation exists: #{res['user']}@#{env} for #{(Time.at(res['expires']) - Time.now) / 60} minutes"
37   - exit 0
38   - end
  61 + puts "Reservation exists: #{res['user']}@#{env} for #{expires_to_string.call Time.at(res['expires'])}"
  62 + exit 0
39 63 else
40 64 create.call({})
41 65 end
42 66 end
43 67 end
  68 +
  69 + exit
44 70 end
45 71 end

0 comments on commit 78ae853

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