Merge pull request #145 from hunner/fix_beaker_tests
[puppet_vcsrepo.git] / lib / puppet / provider / vcsrepo / bzr.rb
1 require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
2
3 Puppet::Type.type(:vcsrepo).provide(:bzr, :parent => Puppet::Provider::Vcsrepo) do
4   desc "Supports Bazaar repositories"
5
6   commands :bzr => 'bzr'
7   has_features :reference_tracking
8
9   def create
10     if !@resource.value(:source)
11       create_repository(@resource.value(:path))
12     else
13       clone_repository(@resource.value(:revision))
14     end
15   end
16
17   def working_copy_exists?
18     File.directory?(File.join(@resource.value(:path), '.bzr'))
19   end
20
21   def exists?
22     working_copy_exists?
23   end
24
25   def destroy
26     FileUtils.rm_rf(@resource.value(:path))
27   end
28
29   def revision
30     at_path do
31       current_revid = bzr('version-info')[/^revision-id:\s+(\S+)/, 1]
32       desired = @resource.value(:revision)
33       begin
34         desired_revid = bzr('revision-info', desired).strip.split(/\s+/).last
35       rescue Puppet::ExecutionFailure
36         # Possible revid available during update (but definitely not current)
37         desired_revid = nil
38       end
39       if current_revid == desired_revid
40         desired
41       else
42         current_revid
43       end
44     end
45   end
46
47   def revision=(desired)
48     at_path do
49       begin
50         bzr('update', '-r', desired)
51       rescue Puppet::ExecutionFailure
52         bzr('update', '-r', desired, ':parent')
53       end
54     end
55     update_owner
56   end
57
58   def latest
59     at_path do
60       bzr('version-info', ':parent')[/^revision-id:\s+(\S+)/, 1]
61     end
62   end
63
64   def latest?
65     at_path do
66       return self.revision == self.latest
67     end
68   end
69
70   private
71
72   def create_repository(path)
73     bzr('init', path)
74     update_owner
75   end
76
77   def clone_repository(revision)
78     args = ['branch']
79     if revision
80       args.push('-r', revision)
81     end
82     args.push(@resource.value(:source),
83               @resource.value(:path))
84     bzr(*args)
85     update_owner
86   end
87
88   def update_owner
89     if @resource.value(:owner) or @resource.value(:group)
90       set_ownership
91     end
92   end
93 end