cap_reserve 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +13 -4
  2. data/cap_reserve.gemspec +3 -3
  3. data/lib/cap_reserve.rb +43 -17
  4. metadata +63 -36
data/README.md CHANGED
@@ -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.
data/cap_reserve.gemspec CHANGED
@@ -6,7 +6,7 @@ $:.unshift lib unless $:.include?(lib)
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "cap_reserve"
9
- s.version = '0.1.0'
9
+ s.version = '0.1.1'
10
10
  s.platform = Gem::Platform::RUBY
11
11
  s.authors = [ "Winton Welsh" ]
12
12
  s.email = [ "mail@wintoni.us" ]
@@ -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
data/lib/cap_reserve.rb CHANGED
@@ -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
metadata CHANGED
@@ -1,45 +1,63 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: cap_reserve
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Winton Welsh
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2011-10-13 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2011-10-13 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: rspec
16
- requirement: &70146890925900 !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
17
24
  none: false
18
- requirements:
25
+ requirements:
19
26
  - - ~>
20
- - !ruby/object:Gem::Version
21
- version: '1.0'
27
+ - !ruby/object:Gem::Version
28
+ hash: 15
29
+ segments:
30
+ - 1
31
+ - 0
32
+ version: "1.0"
22
33
  type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: yajl-ruby
23
37
  prerelease: false
24
- version_requirements: *70146890925900
25
- - !ruby/object:Gem::Dependency
26
- name: nestful
27
- requirement: &70146890925420 !ruby/object:Gem::Requirement
38
+ requirement: &id002 !ruby/object:Gem::Requirement
28
39
  none: false
29
- requirements:
30
- - - =
31
- - !ruby/object:Gem::Version
32
- version: 0.0.7
40
+ requirements:
41
+ - - "="
42
+ - !ruby/object:Gem::Version
43
+ hash: 23
44
+ segments:
45
+ - 1
46
+ - 0
47
+ - 0
48
+ version: 1.0.0
33
49
  type: :runtime
34
- prerelease: false
35
- version_requirements: *70146890925420
50
+ version_requirements: *id002
36
51
  description: Uses a maitre_d server to reserve time on deploy environments.
37
- email:
52
+ email:
38
53
  - mail@wintoni.us
39
54
  executables: []
55
+
40
56
  extensions: []
57
+
41
58
  extra_rdoc_files: []
42
- files:
59
+
60
+ files:
43
61
  - .gitignore
44
62
  - Gemfile
45
63
  - LICENSE
@@ -51,28 +69,37 @@ files:
51
69
  - spec/spec_helper.rb
52
70
  homepage: http://guthub.com/winton/cap_reserve
53
71
  licenses: []
72
+
54
73
  post_install_message:
55
74
  rdoc_options: []
56
- require_paths:
75
+
76
+ require_paths:
57
77
  - lib
58
- required_ruby_version: !ruby/object:Gem::Requirement
78
+ required_ruby_version: !ruby/object:Gem::Requirement
59
79
  none: false
60
- requirements:
61
- - - ! '>='
62
- - !ruby/object:Gem::Version
63
- version: '0'
64
- required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
88
  none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
70
96
  requirements: []
97
+
71
98
  rubyforge_project:
72
- rubygems_version: 1.8.6
99
+ rubygems_version: 1.8.10
73
100
  signing_key:
74
101
  specification_version: 3
75
102
  summary: Uses a maitre_d server to reserve time on deploy environments
76
- test_files:
103
+ test_files:
77
104
  - spec/cap_reserve_spec.rb
78
105
  - spec/spec_helper.rb