cap_reserve 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,7 +22,7 @@ You must have a [maitre_d](https://github.com/winton/maitre_d) server running fi
22
22
  task :reserve do
23
23
  ENV['RESERVE_ENV'] = 'staging'
24
24
  ENV['RESERVE_URL'] = 'http://localhost:3000'
25
- maitre_d
25
+ maitre_d.default
26
26
  end
27
27
 
28
28
  before "deploy", "reserve"
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.5'
9
+ s.version = '0.1.6'
10
10
  s.platform = Gem::Platform::RUBY
11
11
  s.authors = [ "Winton Welsh" ]
12
12
  s.email = [ "mail@wintoni.us" ]
data/lib/cap_reserve.rb CHANGED
@@ -6,73 +6,110 @@ require 'yajl'
6
6
 
7
7
  $:.unshift File.dirname(__FILE__)
8
8
 
9
- Capistrano::Configuration.instance(:must_exist).load do
9
+ class String
10
10
 
11
- desc "Reserve environment using RESERVE=minutes"
12
- task :maitre_d do
13
- begin
14
- env, user, time, force, url, destroy, branch =
15
- ENV['RESERVE_ENV'], ENV['USER'], ENV['RESERVE'], ENV['FORCE'], ENV['RESERVE_URL'], ENV['DESTROY'], ENV['BRANCH']
11
+ # Colors
16
12
 
17
- help = <<-HELP
18
- FORCE=1 to deploy anyway
19
- DESTROY=1 to deploy and destroy reservation
20
- HELP
13
+ def blue
14
+ "\e[34m#{self}\e[0m"
15
+ end
21
16
 
22
- expires_to_string = lambda do |expires|
23
- left = Time.at(expires) - Time.now
24
- if left < 60
25
- "#{left} seconds"
26
- elsif left / 60 < 60
27
- "#{sprintf "%.1f", left / 60} minutes"
28
- else
29
- "#{sprintf "%.1f", left / 60 / 60} hours"
30
- end
31
- end
17
+ def green
18
+ "\e[32m#{self}\e[0m"
19
+ end
32
20
 
33
- get = lambda do |full_url, hash|
34
- params = ''
35
- hash.each do |k, v|
36
- params << "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}&"
37
- end
38
- params.chop! # trailing &
21
+ def red
22
+ "\e[31m#{self}\e[0m"
23
+ end
24
+
25
+ def yellow
26
+ "\e[33m#{self}\e[0m"
27
+ end
28
+ end
39
29
 
40
- json = open("#{full_url}?#{params}").read
41
- Yajl::Parser.parse(json)
30
+ Capistrano::Configuration.instance(:must_exist).load do
31
+ namespace :maitre_d do
32
+
33
+ expires_to_string = lambda do |expires|
34
+ left = Time.at(expires) - Time.now
35
+ if left < 60
36
+ "#{left} seconds"
37
+ elsif left / 60 < 60
38
+ "#{sprintf "%.1f", left / 60} minutes"
39
+ else
40
+ "#{sprintf "%.1f", left / 60 / 60} hours"
42
41
  end
42
+ end
43
43
 
44
- create = lambda do |params|
45
- if time
46
- get.call("#{url}/reservations/create", {
47
- :environment => env, :user => user, :seconds => time.to_i * 60, :branch => branch
48
- }.merge(params))
49
- puts "\n\e[32mReservation created\e[0m: \e[33m#{user}@#{env}\e[0m for \e[33m#{time.to_i} minutes\e[0m\n\n"
50
- elsif destroy
51
- res = get.call("#{url}/reservations/destroy", :environment => env)
52
- if res['status'] == 'reserved'
53
- 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"
44
+ get = lambda do |full_url, hash|
45
+ params = ''
46
+ hash.each do |k, v|
47
+ params << "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}&"
48
+ end
49
+ params.chop! # trailing &
50
+
51
+ json = open("#{full_url}?#{params}").read
52
+ Yajl::Parser.parse(json)
53
+ end
54
+
55
+ desc "Reserve environment using RESERVE=minutes"
56
+ task :default do
57
+ begin
58
+ env, user, time, force, url, destroy, branch =
59
+ ENV['RESERVE_ENV'], ENV['USER'], ENV['RESERVE'], ENV['FORCE'], ENV['RESERVE_URL'], ENV['DESTROY'], ENV['BRANCH']
60
+
61
+ help = <<-HELP
62
+ FORCE=1 to deploy anyway
63
+ DESTROY=1 to deploy and destroy reservation
64
+ HELP
65
+
66
+ create = lambda do |params|
67
+ if time
68
+ get.call("#{url}/reservations/create", {
69
+ :environment => env, :user => user, :seconds => time.to_i * 60, :branch => branch
70
+ }.merge(params))
71
+ puts "\n#{"Reservation created".green}: #{"#{user}@#{env}".yellow} for #{"#{time.to_i} minutes".yellow}\n\n"
72
+ elsif destroy
73
+ res = get.call("#{url}/reservations/destroy", :environment => env)
74
+ if res['status'] == 'reserved'
75
+ puts "\n#{"Reservation destroyed".green}: #{"#{res['user']}@#{env}".yellow} (#{expires_to_string.call(Time.at(res['expires'])).yellow} left)\n\n"
76
+ end
54
77
  end
55
78
  end
56
- end
57
79
 
58
- if env && user
59
- if destroy
60
- create.call({})
61
- elsif force
62
- create.call(:force => true)
63
- else
64
- res = get.call("#{url}/reservations/show", :environment => env)
65
- if res['status'] == 'reserved'
66
- 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"
67
- exit 0
68
- else
80
+ if env && user
81
+ if destroy
69
82
  create.call({})
83
+ elsif force
84
+ create.call(:force => true)
85
+ else
86
+ res = get.call("#{url}/reservations/show", :environment => env)
87
+ if res['status'] == 'reserved'
88
+ puts "\n#{"Reservation exists".red}: #{"#{res['user']}@#{env}".yellow} (#{expires_to_string.call(Time.at(res['expires'])).yellow})\n#{help}\n"
89
+ exit 0
90
+ else
91
+ create.call({})
92
+ end
70
93
  end
71
94
  end
95
+ rescue Exception => e
96
+ if e.inspect.include?('SystemExit')
97
+ exit 0
98
+ end
72
99
  end
73
- rescue Exception => e
74
- if e.inspect.include?('SystemExit')
75
- exit 0
100
+ end
101
+
102
+ desc "Show environment reservation status"
103
+ task :available do
104
+ puts "\n"
105
+ url, envs = ENV['RESERVE_URL'], ENV['RESERVE_ENVS']
106
+ envs.split(/\s/).each do |env|
107
+ res = get.call("#{url}/reservations/show", :environment => env)
108
+ puts "#{env.yellow} is #{res['status'] == 'available' ? "available".green : "reserved".red}"
109
+ if res['status'] == 'reserved'
110
+ puts " #{"#{res['user']}@#{env}".yellow} for #{expires_to_string.call(Time.at(res['expires'])).yellow}"
111
+ end
112
+ puts "\n"
76
113
  end
77
114
  end
78
115
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cap_reserve
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 5
10
- version: 0.1.5
9
+ - 6
10
+ version: 0.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Winton Welsh
@@ -15,7 +15,8 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-12-06 00:00:00 Z
18
+ date: 2012-01-20 00:00:00 -08:00
19
+ default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: rspec
@@ -67,6 +68,7 @@ files:
67
68
  - lib/cap_reserve.rb
68
69
  - spec/cap_reserve_spec.rb
69
70
  - spec/spec_helper.rb
71
+ has_rdoc: true
70
72
  homepage: http://guthub.com/winton/cap_reserve
71
73
  licenses: []
72
74
 
@@ -96,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
98
  requirements: []
97
99
 
98
100
  rubyforge_project:
99
- rubygems_version: 1.8.10
101
+ rubygems_version: 1.5.0
100
102
  signing_key:
101
103
  specification_version: 3
102
104
  summary: Uses a maitre_d server to reserve time on deploy environments