Merge pull request #224 from puppet-by-examples/single-branch
[puppet_vcsrepo.git] / lib / puppet / provider / vcsrepo / svn.rb
1 require File.join(File.dirname(__FILE__), '..', 'vcsrepo')
2
3 Puppet::Type.type(:vcsrepo).provide(:svn, :parent => Puppet::Provider::Vcsrepo) do
4   desc "Supports Subversion repositories"
5
6   commands :svn      => 'svn',
7            :svnadmin => 'svnadmin',
8            :svnlook  => 'svnlook'
9
10   has_features :filesystem_types, :reference_tracking, :basic_auth, :configuration, :conflict
11
12   def create
13     if !@resource.value(:source)
14       create_repository(@resource.value(:path))
15     else
16       checkout_repository(@resource.value(:source),
17                           @resource.value(:path),
18                           @resource.value(:revision))
19     end
20     update_owner
21   end
22
23   def working_copy_exists?
24     if File.directory?(@resource.value(:path))
25       # :path is an svn checkout
26       return true if File.directory?(File.join(@resource.value(:path), '.svn'))
27       if File.file?(File.join(@resource.value(:path), 'format'))
28         # :path is an svn server
29         return true if svnlook('uuid', @resource.value(:path))
30       end
31     end
32     false
33   end
34
35   def exists?
36     working_copy_exists?
37   end
38
39   def destroy
40     FileUtils.rm_rf(@resource.value(:path))
41   end
42
43   def latest?
44     at_path do
45       (self.revision >= self.latest) and (@resource.value(:source) == self.sourceurl)
46     end
47   end
48
49   def buildargs
50     args = ['--non-interactive']
51     if @resource.value(:basic_auth_username) && @resource.value(:basic_auth_password)
52       args.push('--username', @resource.value(:basic_auth_username))
53       args.push('--password', @resource.value(:basic_auth_password))
54       args.push('--no-auth-cache')
55     end
56
57     if @resource.value(:force)
58       args.push('--force')
59     end
60
61     if @resource.value(:configuration)
62       args.push('--config-dir', @resource.value(:configuration))
63     end
64
65     args
66   end
67
68   def latest
69     args = buildargs.push('info', '-r', 'HEAD')
70     at_path do
71       svn(*args)[/^Revision:\s+(\d+)/m, 1]
72     end
73   end
74
75   def sourceurl
76     args = buildargs.push('info')
77     at_path do
78       svn(*args)[/^URL:\s+(\S+)/m, 1]
79     end
80   end
81
82   def revision
83     args = buildargs.push('info')
84     at_path do
85       svn(*args)[/^Revision:\s+(\d+)/m, 1]
86     end
87   end
88
89   def revision=(desired)
90     args = if @resource.value(:source)
91              buildargs.push('switch', '-r', desired, @resource.value(:source))
92            else
93              buildargs.push('update', '-r', desired)
94            end
95
96     if @resource.value(:conflict)
97       args.push('--accept', @resource.value(:conflict))
98     end
99
100     at_path do
101       svn(*args)
102     end
103     update_owner
104   end
105
106   private
107
108   def checkout_repository(source, path, revision)
109     args = buildargs.push('checkout')
110     if revision
111       args.push('-r', revision)
112     end
113     args.push(source, path)
114     svn(*args)
115   end
116
117   def create_repository(path)
118     args = ['create']
119     if @resource.value(:fstype)
120       args.push('--fs-type', @resource.value(:fstype))
121     end
122     args << path
123     svnadmin(*args)
124   end
125
126   def update_owner
127     if @resource.value(:owner) or @resource.value(:group)
128       set_ownership
129     end
130   end
131 end