Merge pull request #273 from hunner/fix_trustcert
[puppet_vcsrepo.git] / lib / puppet / provider / vcsrepo / svn.rb
index 09fdd0e..fccfaa5 100644 (file)
@@ -4,9 +4,10 @@ Puppet::Type.type(:vcsrepo).provide(:svn, :parent => Puppet::Provider::Vcsrepo)
   desc "Supports Subversion repositories"
 
   commands :svn      => 'svn',
-           :svnadmin => 'svnadmin'
+           :svnadmin => 'svnadmin',
+           :svnlook  => 'svnlook'
 
-  defaultfor :svn => :exists
+  has_features :filesystem_types, :reference_tracking, :basic_auth, :configuration, :conflict, :depth
 
   def create
     if !@resource.value(:source)
@@ -14,37 +15,109 @@ Puppet::Type.type(:vcsrepo).provide(:svn, :parent => Puppet::Provider::Vcsrepo)
     else
       checkout_repository(@resource.value(:source),
                           @resource.value(:path),
-                          @resource.value(:revision))
+                          @resource.value(:revision),
+                          @resource.value(:depth))
     end
+    update_owner
+  end
+
+  def working_copy_exists?
+    if File.directory?(@resource.value(:path))
+      # :path is an svn checkout
+      return true if File.directory?(File.join(@resource.value(:path), '.svn'))
+      if File.file?(File.join(@resource.value(:path), 'format'))
+        # :path is an svn server
+        return true if svnlook('uuid', @resource.value(:path))
+      end
+    end
+    false
   end
 
   def exists?
-    File.directory?(@resource.value(:path))
+    working_copy_exists?
   end
 
   def destroy
     FileUtils.rm_rf(@resource.value(:path))
   end
-  
+
+  def latest?
+    at_path do
+      (self.revision >= self.latest) and (@resource.value(:source) == self.sourceurl)
+    end
+  end
+
+  def buildargs
+    args = ['--non-interactive']
+    if @resource.value(:basic_auth_username) && @resource.value(:basic_auth_password)
+      args.push('--username', @resource.value(:basic_auth_username))
+      args.push('--password', @resource.value(:basic_auth_password))
+      args.push('--no-auth-cache')
+    end
+
+    if @resource.value(:force)
+      args.push('--force')
+    end
+
+    if @resource.value(:configuration)
+      args.push('--config-dir', @resource.value(:configuration))
+    end
+
+    if @resource.value(:trust_server_cert) != :false
+      args.push('--trust-server-cert')
+    end
+
+    args
+  end
+
+  def latest
+    args = buildargs.push('info', '-r', 'HEAD')
+    at_path do
+      svn(*args)[/^Revision:\s+(\d+)/m, 1]
+    end
+  end
+
+  def sourceurl
+    args = buildargs.push('info')
+    at_path do
+      svn(*args)[/^URL:\s+(\S+)/m, 1]
+    end
+  end
+
   def revision
+    args = buildargs.push('info')
     at_path do
-      svn('info')[/^Revision:\s+(\d+)/m, 1]
+      svn(*args)[/^Revision:\s+(\d+)/m, 1]
     end
   end
 
   def revision=(desired)
+    args = if @resource.value(:source)
+             buildargs.push('switch', '-r', desired, @resource.value(:source))
+           else
+             buildargs.push('update', '-r', desired)
+           end
+
+    if @resource.value(:conflict)
+      args.push('--accept', @resource.value(:conflict))
+    end
+
     at_path do
-      svn('update', '-r', desired)
+      svn(*args)
     end
+    update_owner
   end
 
   private
 
-  def checkout_repository(source, path, revision = nil)
-    args = ['checkout']
+  def checkout_repository(source, path, revision, depth)
+    args = buildargs.push('checkout')
     if revision
       args.push('-r', revision)
     end
+    if depth
+      args.push('--depth', depth)
+    end
     args.push(source, path)
     svn(*args)
   end
@@ -58,4 +131,9 @@ Puppet::Type.type(:vcsrepo).provide(:svn, :parent => Puppet::Provider::Vcsrepo)
     svnadmin(*args)
   end
 
+  def update_owner
+    if @resource.value(:owner) or @resource.value(:group)
+      set_ownership
+    end
+  end
 end