Fix svn provider to look for '^Revision:'.
[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   optional_commands :svn      => 'svn',
7                     :svnadmin => 'svnadmin',
8                     :svnlook  => 'svnlook'
9
10   has_features :filesystem_types, :reference_tracking, :basic_auth, :configuration
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       # :path is an svn server
28       return true if svnlook('uuid', @resource.value(:path))
29     end
30     false
31   end
32
33   def exists?
34     working_copy_exists?
35   end
36
37   def destroy
38     FileUtils.rm_rf(@resource.value(:path))
39   end
40
41   def latest?
42     at_path do
43       (self.revision >= self.latest) and (@resource.value(:source) == self.sourceurl)
44     end
45   end
46
47   def buildargs
48     args = ['--non-interactive']
49     if @resource.value(:basic_auth_username) && @resource.value(:basic_auth_password)
50       args.push('--username', @resource.value(:basic_auth_username))
51       args.push('--password', @resource.value(:basic_auth_password))
52       args.push('--no-auth-cache')
53     end
54
55     if @resource.value(:force)
56       args.push('--force')
57     end
58
59     if @resource.value(:configuration)
60       args.push('--config-dir', @resource.value(:configuration))
61     end
62
63     args
64   end
65
66   def latest
67     args = buildargs.push('info', '-r', 'HEAD')
68     at_path do
69       svn(*args)[/^Revision:\s+(\d+)/m, 1]
70     end
71   end
72
73   def sourceurl
74     args = buildargs.push('info')
75     at_path do
76       svn(*args)[/^URL:\s+(\S+)/m, 1]
77     end
78   end
79
80   def revision
81     args = buildargs.push('info')
82     at_path do
83       svn(*args)[/^Revision:\s+(\d+)/m, 1]
84     end
85   end
86
87   def revision=(desired)
88     args = if @resource.value(:source)
89              buildargs.push('switch', '-r', desired, @resource.value(:source))
90            else
91              buildargs.push('update', '-r', desired)
92            end
93     at_path do
94       svn(*args)
95     end
96     update_owner
97   end
98
99   private
100
101   def checkout_repository(source, path, revision)
102     args = buildargs.push('checkout')
103     if revision
104       args.push('-r', revision)
105     end
106     args.push(source, path)
107     svn(*args)
108   end
109
110   def create_repository(path)
111     args = ['create']
112     if @resource.value(:fstype)
113       args.push('--fs-type', @resource.value(:fstype))
114     end
115     args << path
116     svnadmin(*args)
117   end
118
119   def update_owner
120     if @resource.value(:owner) or @resource.value(:group)
121       set_ownership
122     end
123   end
124 end