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 +1 -1
- data/cap_reserve.gemspec +1 -1
- data/lib/cap_reserve.rb +90 -53
- metadata +7 -5
data/README.md
CHANGED
data/cap_reserve.gemspec
CHANGED
data/lib/cap_reserve.rb
CHANGED
@@ -6,73 +6,110 @@ require 'yajl'
|
|
6
6
|
|
7
7
|
$:.unshift File.dirname(__FILE__)
|
8
8
|
|
9
|
-
|
9
|
+
class String
|
10
10
|
|
11
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
HELP
|
13
|
+
def blue
|
14
|
+
"\e[34m#{self}\e[0m"
|
15
|
+
end
|
21
16
|
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
41
|
-
|
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
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
59
|
-
|
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
|
-
|
74
|
-
|
75
|
-
|
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:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
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:
|
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.
|
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
|