Skip to content

Commit 178102f

Browse files
committed
revision-deploy provider
1 parent add1371 commit 178102f

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#
2+
# Author:: Daniel DeLeo (<dan@kallistec.com>)
3+
# Copyright:: Copyright (c) 2009 Daniel DeLeo
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
class Chef
20+
class Provider
21+
class Deploy
22+
class Revision < Chef::Provider::Deploy
23+
24+
def all_releases
25+
sorted_releases
26+
end
27+
28+
protected
29+
30+
def release_created(release)
31+
sorted_releases { |r| r << release }
32+
end
33+
34+
def release_deleted(release)
35+
sorted_releases { |r| r.delete(release)}
36+
end
37+
38+
def release_slug
39+
scm_provider.revision_slug
40+
end
41+
42+
private
43+
44+
def sorted_releases
45+
cache = load_cache
46+
if block_given?
47+
yield cache
48+
save_cache(cache)
49+
end
50+
cache
51+
end
52+
53+
def load_cache
54+
begin
55+
JSON.parse(Chef::FileCache.load("revision-deploys/#{new_resource.name}"))
56+
rescue
57+
Chef::Exceptions::FileNotFound
58+
save_cache([])
59+
end
60+
end
61+
62+
def save_cache(cache)
63+
Chef::FileCache.store("revision-deploys/#{new_resource.name}", cache.to_json)
64+
cache
65+
end
66+
67+
end
68+
end
69+
end
70+
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#
2+
# Author:: Daniel DeLeo (<dan@kallistec.com>)
3+
# Copyright:: Copyright (c) 2008 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require File.expand_path(File.join(File.dirname(__FILE__),"..", "..", "..", "spec_helper"))
20+
21+
describe Chef::Provider::Deploy::Revision do
22+
23+
before do
24+
Chef::Config[:file_cache_path] = '/tmp/foo'
25+
@resource = Chef::Resource::Deploy.new("/my/deploy/dir")
26+
@resource.revision("8a3195bf3efa246f743c5dfa83683201880f935c")
27+
@node = Chef::Node.new
28+
@provider = Chef::Provider::Deploy::Revision.new(@node, @resource)
29+
@provider.load_current_resource
30+
@runner = mock("runnah", :null_object => true)
31+
Chef::Runner.stub!(:new).and_return(@runner)
32+
@expected_release_dir = "/my/deploy/dir/releases/8a3195bf3efa246f743c5dfa83683201880f935c"
33+
end
34+
35+
after do
36+
# Make sure we don't keep any state in our tests
37+
FileUtils.rm_rf Chef::FileCache.create_cache_path('revision-deploys',false)
38+
end
39+
40+
41+
it "uses the resolved revision from the SCM as the release slug" do
42+
@provider.scm_provider.stub!(:revision_slug).and_return("uglySlugly")
43+
@provider.send(:release_slug).should == "uglySlugly"
44+
end
45+
46+
it "deploys to a dir named after the revision" do
47+
@provider.release_path.should == @expected_release_dir
48+
end
49+
50+
it "stores the release dir in the file cache when copying the cached repo" do
51+
FileUtils.stub!(:mkdir_p)
52+
FileUtils.stub!(:cp_r)
53+
@provider.copy_cached_repo
54+
@provider.stub!(:release_slug).and_return("73219b87e977d9c7ba1aa57e9ad1d88fa91a0ec2")
55+
@provider.load_current_resource
56+
@provider.copy_cached_repo
57+
second_release = "/my/deploy/dir/releases/73219b87e977d9c7ba1aa57e9ad1d88fa91a0ec2"
58+
@provider.all_releases.should == [@expected_release_dir,second_release]
59+
end
60+
61+
it "removes a release from the file cache when it's deleted by :cleanup!" do
62+
%w{first second third fourth fifth latest}.each do |release_name|
63+
@provider.send(:release_created, release_name)
64+
end
65+
@provider.all_releases.should == %w{first second third fourth fifth latest}
66+
67+
FileUtils.stub(:rm_rf)
68+
@provider.cleanup!
69+
@provider.all_releases.should == %w{second third fourth fifth latest}
70+
end
71+
72+
end

0 commit comments

Comments
 (0)