added support for changing upstream repo url
[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)[/^Last Changed Rev:\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)[/^Last Changed Rev:\s+(\d+)/m, 1]
84     end
85   end
86
87   def revision=(desired)
88     args = buildargs.push('switch', '-r', desired, @resource.value(:source))
89     at_path do
90       svn(*args)
91     end
92     update_owner
93   end
94
95   private
96
97   def checkout_repository(source, path, revision)
98     args = buildargs.push('checkout')
99     if revision
100       args.push('-r', revision)
101     end
102     args.push(source, path)
103     svn(*args)
104   end
105
106   def create_repository(path)
107     args = ['create']
108     if @resource.value(:fstype)
109       args.push('--fs-type', @resource.value(:fstype))
110     end
111     args << path
112     svnadmin(*args)
113   end
114
115   def update_owner
116     if @resource.value(:owner) or @resource.value(:group)
117       set_ownership
118     end
119   end
120 end