capistrano-alice 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,12 +2,14 @@ class Capistrano::Alice::Release
2
2
  attr_accessor :servers
3
3
  attr_accessor :processes
4
4
  attr_accessor :path_rules
5
+ attr_accessor :environment
5
6
 
6
7
  attr_accessor :id
7
8
  attr_accessor :number
8
9
 
9
10
  def initialize(config)
10
- @config = config
11
+ @config = config
12
+ @environment = {}
11
13
  end
12
14
 
13
15
  def create!
@@ -15,7 +17,8 @@ class Capistrano::Alice::Release
15
17
  "application" => @config.application,
16
18
  "machines" => @servers,
17
19
  "processes" => @processes,
18
- "path_rules" => (@path_rules || {}).to_a
20
+ "path_rules" => (@path_rules || {}).to_a,
21
+ "environment" => @environment
19
22
  }
20
23
 
21
24
  Net::HTTP.start(@config.alice_host, @config.alice_port) do |http|
@@ -26,8 +29,9 @@ class Capistrano::Alice::Release
26
29
  response = http.request(request)
27
30
  if Net::HTTPSuccess === response
28
31
  response = Yajl::Parser.parse(response.body)
29
- @id = response['release']['id']
30
- @number = response['release']['number']
32
+ @id = response['release']['id']
33
+ @number = response['release']['number']
34
+ @environment = response['release']['environment']
31
35
  else
32
36
  raise "Failed to create release!"
33
37
  end
@@ -67,10 +71,27 @@ Capistrano::Configuration.instance(:must_exist).load do
67
71
  find_and_execute_task("alice:release:_create:collect_servers")
68
72
  find_and_execute_task("alice:release:_create:collect_processes")
69
73
  find_and_execute_task("alice:release:_create:collect_path_rules")
74
+ find_and_execute_task("alice:release:_create:collect_environment_variables")
75
+ find_and_execute_task("alice:release:_create:detect_ruby_version")
76
+ find_and_execute_task("alice:release:_create:detect_node_version")
70
77
 
71
78
  on_rollback { find_and_execute_task("alice:release:destroy") }
72
79
 
73
- alice_release.create!
80
+ begin
81
+ alice_release.create!
82
+ rescue RuntimeError => e
83
+ abort e.message
84
+ end
85
+
86
+ default_environment.merge! alice_release.environment
87
+
88
+ if alice_release.environment['RUBY_VERSION']
89
+ set :rvm_ruby_string, alice_release.environment['RUBY_VERSION']
90
+ end
91
+
92
+ if alice_release.environment['RAILS_ENV']
93
+ set :rails_env, alice_release.environment['RAILS_ENV']
94
+ end
74
95
  end
75
96
 
76
97
  task :destroy, :except => { :no_release => true } do
@@ -141,12 +162,12 @@ Capistrano::Configuration.instance(:must_exist).load do
141
162
  Dir.entries('public').each do |path|
142
163
  next if path[0,1] == '.'
143
164
 
144
- if File.directory?(path)
165
+ if File.directory?(File.join('public',path))
145
166
  path_rules[File.join('', path, '*')] = [
146
167
  ["cache-control", "public,max-age=600"],
147
168
  ["forward", "static"]
148
169
  ]
149
- elsif File.file?(path)
170
+ elsif File.file?(File.join('public',path))
150
171
  path_rules[File.join('', path)] = [
151
172
  ["cache-control", "public,max-age=600"],
152
173
  ["forward", "static"]
@@ -160,6 +181,63 @@ Capistrano::Configuration.instance(:must_exist).load do
160
181
  alice_release.path_rules = path_rules
161
182
  end
162
183
 
184
+ task :collect_environment_variables, :except => { :no_release => true } do
185
+ env = {}
186
+
187
+ if File.file?('.envrc')
188
+ File.read('.envrc').split("\n").each do |line|
189
+ line = line.split('#', 2).first.strip
190
+ case line
191
+ when /^export\s+([a-zA-Z0-9_]+)[=](.+)$/
192
+ env[$1] = $2
193
+
194
+ when /^unset\s+([a-zA-Z0-9_]+)$/
195
+ env[$1] = nil
196
+
197
+ end
198
+ end
199
+ end
200
+
201
+ env.merge! fetch(:alice_environment, {})
202
+
203
+ alice_release.environment.merge! env
204
+ end
205
+
206
+ task :detect_ruby_version, :except => { :no_release => true } do
207
+ ruby_version = fetch(:ruby_version, nil)
208
+
209
+ if !ruby_version and File.file?('.rvmrc')
210
+ File.read('.rvmrc').split("\n").each do |line|
211
+ line = line.split('#', 2).first.strip
212
+ next unless /^rvm\s+(.+)$/ =~ line
213
+ line = $1
214
+ if /^use\s+(.+)$/ =~ line then line = $1 end
215
+ line = line.split('@', 2).first
216
+ next unless /^[a-z0-9_.-]+$/ =~ line
217
+ ruby_version = line
218
+ end
219
+ end
220
+
221
+ alice_release.environment['RUBY_VERSION'] = ruby_version
222
+ end
223
+
224
+ task :detect_node_version, :except => { :no_release => true } do
225
+ node_version = fetch(:node_version, nil)
226
+
227
+ if !node_version and File.file?('.nvmrc')
228
+ File.read('.nvmrc').split("\n").each do |line|
229
+ line = line.split('#', 2).first.strip
230
+ next unless /^nvm\s+(.+)$/ =~ line
231
+ line = $1
232
+ if /^use\s+(.+)$/ =~ line then line = $1 end
233
+ next unless /^v?([a-z0-9_.-]+)$/ =~ line
234
+ node_version = line
235
+ end
236
+ end
237
+
238
+ alice_release.environment['NODE_VERSION'] = node_version
239
+ end
240
+
163
241
  end
164
242
  end
165
243
  end
@@ -1,5 +1,5 @@
1
1
  module Capistrano
2
2
  module Alice
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-alice
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Simon Menke
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-13 00:00:00 Z
18
+ date: 2012-01-14 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: yajl-ruby