Skip to content

Commit ae0a270

Browse files
committed
Don't mix deploy actions and site update/create actions
1 parent d11b8f7 commit ae0a270

File tree

7 files changed

+134
-86
lines changed

7 files changed

+134
-86
lines changed

README.md

+24-14
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,13 @@ Creating a site from a zip file:
132132
site = bitballoon.sites.create(:zip => "/tmp/my-site.zip")
133133
```
134134

135-
Both methods will create the site and upload the files. The site will then be processing.
135+
Both methods will create the site and upload the files to a new deploy.
136136

137-
```ruby
138-
site.state == "processing"
139-
site.processing? == true
140-
```
141-
142-
Refresh a site to update the state:
137+
Creating a site with a dir or a zip is actually a shortcut for something like this:
143138

144139
```ruby
145-
site.refresh
140+
site = bitballoon.sites.create(:name => "unique-site-subdomain", :custom_domain => "www.example.com")
141+
deploy = site.deploys.create(:dir => "path/to/my-site")
146142
```
147143

148144
Use `wait_for_ready` to wait until a site has finished processing.
@@ -153,20 +149,29 @@ site.wait_for_ready
153149
site.state == "ready"
154150
```
155151

152+
This also works on a specific deploy, and you can pass in a block to execute after each polling action:
153+
154+
```ruby
155+
deploy = site.deploys.create(:dir => "/tmp/my-site")
156+
deploy.wait_for_ready do |deploy|
157+
puts "Current state: #{deploy.state}"
158+
end
159+
```
160+
156161
Redeploy a site from a dir:
157162

158163
```ruby
159164
site = bitballoon.sites.get(site_id)
160-
site.update(:dir => "/tmp/my-site")
161-
site.wait_for_ready
165+
deploy = site.deploys.create(:dir => "/tmp/my-site")
166+
deploy.wait_for_ready
162167
```
163168

164169
Redeploy a site from a zip file:
165170

166171
```ruby
167172
site = bitballoon.sites.get(site_id)
168-
site.update(:zip => "/tmp/my-site.zip")
169-
site.wait_for_ready
173+
deploy = site.deploys.create(:zip => "/tmp/my-site.zip")
174+
deploy.wait_for_ready
170175
```
171176

172177
Update the name of the site (its subdomain), the custom domain and the notification email for form submissions:
@@ -195,7 +200,7 @@ Access a specific deploy
195200

196201
```ruby
197202
site = bitballoon.sites.get(site_id)
198-
site.deploys.get(id)
203+
deploy = site.deploys.get(id)
199204
```
200205

201206
Restore a deploy (makes it the current live version of the site)
@@ -204,6 +209,12 @@ Restore a deploy (makes it the current live version of the site)
204209
site.deploys.get(id).restore
205210
```
206211

212+
Create a new deploy
213+
214+
```ruby
215+
deploy = site.deploys.create(:dir => "/tmp/my-site")
216+
```
217+
207218
Users
208219
=====
209220

@@ -427,4 +438,3 @@ Revoke access token:
427438
```ruby
428439
bitballoon.access_tokens.get("token-string").destroy
429440
```
430-

bin/bitballoon

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ opts = Slop.parse do
6565
puts "Site deployed: #{site.url}"
6666
end
6767
end
68-
end
68+
end

lib/bitballoon/collection_proxy.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ def path
4545
[prefix, self.class.path].compact.join("/")
4646
end
4747
end
48-
end
48+
end

lib/bitballoon/deploy.rb

+45-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,54 @@ class Deploy < Model
44
:admin_url, :deploy_url, :screenshot_url, :created_at, :updated_at,
55
:user_id, :required
66

7+
def upload_dir(dir)
8+
return unless state == "uploading"
9+
10+
shas = Hash.new { [] }
11+
glob = ::File.join(dir, "**", "*")
12+
13+
Dir.glob(glob) do |file|
14+
next unless ::File.file?(file)
15+
pathname = ::File.join("/", file[dir.length..-1])
16+
next if pathname.match(/(^\/?__MACOSX\/|\/\.)/)
17+
sha = Digest::SHA1.hexdigest(::File.read(file))
18+
shas[sha] = shas[sha] + [pathname]
19+
end
20+
21+
(required || []).each do |sha|
22+
shas[sha].each do |pathname|
23+
client.request(:put, ::File.join(path, "files", URI.encode(pathname)), :body => ::File.read(::File.join(dir, pathname)), :headers => {"Content-Type" => "application/octet-stream"})
24+
end
25+
end
26+
27+
refresh
28+
end
29+
30+
def wait_for_ready(timeout = 900)
31+
start = Time.now
32+
while !(ready?)
33+
sleep 5
34+
refresh
35+
puts "Got state: #{state}"
36+
raise "Error processing site: #{error_message}" if error?
37+
yield(self) if block_given?
38+
raise "Timeout while waiting for ready" if Time.now - start > timeout
39+
end
40+
self
41+
end
42+
43+
def ready?
44+
state == "ready"
45+
end
46+
47+
def error?
48+
state == "error"
49+
end
50+
751
def restore
852
response = client.request(:post, ::File.join(path, "restore"))
953
process(response.parsed)
1054
self
1155
end
1256
end
13-
end
57+
end

lib/bitballoon/deploys.rb

+47-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,51 @@
33
module BitBalloon
44
class Deploys < CollectionProxy
55
path "/deploys"
6+
7+
def create(attributes)
8+
response = nil
9+
if attributes[:dir]
10+
response = client.request(:post, path, :body => {:files => inventory(attributes[:dir])})
11+
elsif attributes[:zip]
12+
response = client.request(:post, path, :body => ::File.read(attributes[:zip]), :headers => {"Content-Type" => "application/zip"})
13+
else
14+
raise "Need dir or zip to create a deploy"
15+
end
16+
Deploy.new(client, response.parsed)
17+
18+
#
19+
# site_id = attributes.delete(:id)
20+
# path = site_id ? "/sites/#{site_id}" : "/sites"
21+
# method = site_id ? :put : :post
22+
# if attributes[:dir]
23+
# dir = attributes.delete(:dir)
24+
# response = client.request(method, path, :body => JSON.generate(attributes.merge(:files => inventory(dir))), :headers => {"Content-Type" => "application/json"})
25+
# Site.new(client, response.parsed).tap do |site|
26+
# deploy = Deploy.new(client, :id => site.deploy_id, :required => site.required, :state => site.state)
27+
# puts "Uploading dir"
28+
# deploy.upload_dir(dir)
29+
# site.refresh
30+
# end
31+
# elsif attributes[:zip]
32+
# zip = attributes.delete(:zip)
33+
# ::File.open(zip) do |file|
34+
# response = client.request(method, path, :body => attributes.merge(
35+
# :zip => Faraday::UploadIO.new(file, 'application/zip')
36+
# ))
37+
# Site.new(client, response.parsed)
38+
# end
39+
# end
40+
end
41+
42+
private
43+
def inventory(dir)
44+
files = {}
45+
Dir[::File.join(dir, "**", "*")].each do |file|
46+
next unless ::File.file?(file)
47+
path = ::File.join("/", file[dir.length..-1])
48+
files[path] = Digest::SHA1.hexdigest(::File.read(file))
49+
end
50+
files
51+
end
652
end
7-
end
53+
end

lib/bitballoon/site.rb

+10-40
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,8 @@
44
module BitBalloon
55
class Site < Model
66
fields :id, :state, :premium, :claimed, :name, :custom_domain, :url,
7-
:admin_url, :deploy_url, :screenshot_url, :created_at, :updated_at,
8-
:password, :notification_email, :user_id, :required, :error_message
9-
10-
def upload_dir(dir)
11-
return unless state == "uploading"
12-
13-
shas = Hash.new { [] }
14-
glob = ::File.join(dir, "**", "*")
15-
16-
Dir.glob(glob) do |file|
17-
next unless ::File.file?(file)
18-
pathname = ::File.join("/", file[dir.length..-1])
19-
next if pathname.match(/(^\/?__MACOSX\/|\/\.)/)
20-
sha = Digest::SHA1.hexdigest(::File.read(file))
21-
shas[sha] = shas[sha] + [pathname]
22-
end
23-
24-
25-
(required || []).each do |sha|
26-
shas[sha].each do |pathname|
27-
client.request(:put, ::File.join(path, "files", URI.encode(pathname)), :body => ::File.read(::File.join(dir, pathname)), :headers => {"Content-Type" => "application/octet-stream"})
28-
end
29-
end
30-
31-
refresh
32-
end
7+
:admin_url, :deploy_id, :deploy_url, :screenshot_url, :created_at, :updated_at,
8+
:password, :notification_email, :user_id, :error_message, :required
339

3410
def ready?
3511
state == "current"
@@ -39,25 +15,19 @@ def error?
3915
state == "error"
4016
end
4117

42-
def wait_for_ready(timeout = 900)
43-
start = Time.now
44-
while !(ready?)
45-
sleep 5
46-
refresh
47-
raise "Error processing site: #{error_message}" if error?
48-
yield(self) if block_given?
49-
raise "Timeout while waiting for ready" if Time.now - start > timeout
50-
end
18+
def wait_for_ready(timeout = 900, &block)
19+
deploy = deploys.get(deploy_id)
20+
raise "Error fetching deploy #{deploy_id}" unless deploy
21+
deploy.wait_for_ready(timeout, &block)
5122
self
5223
end
5324

5425
def update(attributes)
26+
response = client.request(:put, path, :body => mutable_attributes(attributes))
27+
process(response.parsed)
5528
if attributes[:zip] || attributes[:dir]
56-
site = collection.new(client).create(attributes.merge(:id => id))
57-
process(site.attributes)
58-
else
59-
response = client.request(:put, path, :body => mutable_attributes(attributes))
60-
process(response.parsed)
29+
deploy = deploys.create(attributes)
30+
self.deploy_id = deploy.id
6131
end
6232
self
6333
end

lib/bitballoon/sites.rb

+6-28
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,13 @@ class Sites < CollectionProxy
66
path "/sites"
77

88
def create(attributes = {})
9-
site_id = attributes.delete(:id)
10-
path = site_id ? "/sites/#{site_id}" : "/sites"
11-
method = site_id ? :put : :post
12-
if attributes[:dir]
13-
dir = attributes.delete(:dir)
14-
response = client.request(method, path, :body => JSON.generate(attributes.merge(:files => inventory(dir))), :headers => {"Content-Type" => "application/json"})
15-
Site.new(client, response.parsed).tap do |site|
16-
site.upload_dir(dir)
9+
response = client.request(:post, path, :body => Site.new(client, {}).send(:mutable_attributes, attributes))
10+
Site.new(client, response.parsed).tap do |site|
11+
if attributes[:zip] || attributes[:dir]
12+
deploy = site.deploys.create(attributes)
13+
site.deploy_id = deploy.id
1714
end
18-
elsif attributes[:zip]
19-
zip = attributes.delete(:zip)
20-
::File.open(zip) do |file|
21-
response = client.request(method, path, :body => attributes.merge(
22-
:zip => Faraday::UploadIO.new(file, 'application/zip')
23-
))
24-
Site.new(client, response.parsed)
25-
end
26-
end
27-
end
28-
29-
private
30-
def inventory(dir)
31-
files = {}
32-
Dir[::File.join(dir, "**", "*")].each do |file|
33-
next unless ::File.file?(file)
34-
path = ::File.join("/", file[dir.length..-1])
35-
files[path] = Digest::SHA1.hexdigest(::File.read(file))
3615
end
37-
files
3816
end
3917
end
40-
end
18+
end

0 commit comments

Comments
 (0)