Merge pull request #83 from sodabrew/issue_8331_svnlook
[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
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)[/^Last Changed Rev:\s+(\d+)/m, 1]
70     end
71   end
72
73   def revision
74     args = buildargs.push('info')
75     at_path do
76       svn(*args)[/^Last Changed Rev:\s+(\d+)/m, 1]
77     end
78   end
79
80   def revision=(desired)
81     args = buildargs.push('update', '-r', desired)
82     at_path do
83       svn(*args)
84     end
85     update_owner
86   end
87
88   private
89
90   def checkout_repository(source, path, revision)
91     args = buildargs.push('checkout')
92     if revision
93       args.push('-r', revision)
94     end
95     args.push(source, path)
96     svn(*args)
97   end
98
99   def create_repository(path)
100     args = ['create']
101     if @resource.value(:fstype)
102       args.push('--fs-type', @resource.value(:fstype))
103     end
104     args << path
105     svnadmin(*args)
106   end
107
108   def update_owner
109     if @resource.value(:owner) or @resource.value(:group)
110       set_ownership
111     end
112   end
113 end