PageRenderTime 49ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/extra/svn/reposman.rb

https://github.com/ginkel/redmine
Ruby | 312 lines | 225 code | 25 blank | 62 comment | 13 complexity | a0d169b0fc20459335e531e4f3921ede MD5 | raw file
  1. #!/usr/bin/env ruby
  2. # == Synopsis
  3. #
  4. # reposman: manages your repositories with Redmine
  5. #
  6. # == Usage
  7. #
  8. # reposman [OPTIONS...] -s [DIR] -r [HOST]
  9. #
  10. # Examples:
  11. # reposman --svn-dir=/var/svn --redmine-host=redmine.example.net --scm subversion
  12. # reposman -s /var/git -r redmine.example.net -u http://svn.example.net --scm git
  13. #
  14. # == Arguments (mandatory)
  15. #
  16. # -s, --svn-dir=DIR use DIR as base directory for svn repositories
  17. # -r, --redmine-host=HOST assume Redmine is hosted on HOST. Examples:
  18. # -r redmine.example.net
  19. # -r http://redmine.example.net
  20. # -r https://example.net/redmine
  21. # -k, --key=KEY use KEY as the Redmine API key
  22. #
  23. # == Options
  24. #
  25. # -o, --owner=OWNER owner of the repository. using the rails login
  26. # allow user to browse the repository within
  27. # Redmine even for private project. If you want to
  28. # share repositories through Redmine.pm, you need
  29. # to use the apache owner.
  30. # -g, --group=GROUP group of the repository. (default: root)
  31. # --scm=SCM the kind of SCM repository you want to create (and
  32. # register) in Redmine (default: Subversion).
  33. # reposman is able to create Git and Subversion
  34. # repositories. For all other kind, you must specify
  35. # a --command option
  36. # -u, --url=URL the base url Redmine will use to access your
  37. # repositories. This option is used to automatically
  38. # register the repositories in Redmine. The project
  39. # identifier will be appended to this url. Examples:
  40. # -u https://example.net/svn
  41. # -u file:///var/svn/
  42. # if this option isn't set, reposman won't register
  43. # the repositories in Redmine
  44. # -c, --command=COMMAND use this command instead of "svnadmin create" to
  45. # create a repository. This option can be used to
  46. # create repositories other than subversion and git
  47. # kind.
  48. # This command override the default creation for git
  49. # and subversion.
  50. # -f, --force force repository creation even if the project
  51. # repository is already declared in Redmine
  52. # -t, --test only show what should be done
  53. # -h, --help show help and exit
  54. # -v, --verbose verbose
  55. # -V, --version print version and exit
  56. # -q, --quiet no log
  57. #
  58. # == References
  59. #
  60. # You can find more information on the redmine's wiki : http://www.redmine.org/wiki/redmine/HowTos
  61. require 'getoptlong'
  62. require 'rdoc/usage'
  63. require 'find'
  64. require 'etc'
  65. Version = "1.3"
  66. SUPPORTED_SCM = %w( Subversion Darcs Mercurial Bazaar Git Filesystem )
  67. opts = GetoptLong.new(
  68. ['--svn-dir', '-s', GetoptLong::REQUIRED_ARGUMENT],
  69. ['--redmine-host', '-r', GetoptLong::REQUIRED_ARGUMENT],
  70. ['--key', '-k', GetoptLong::REQUIRED_ARGUMENT],
  71. ['--owner', '-o', GetoptLong::REQUIRED_ARGUMENT],
  72. ['--group', '-g', GetoptLong::REQUIRED_ARGUMENT],
  73. ['--url', '-u', GetoptLong::REQUIRED_ARGUMENT],
  74. ['--command' , '-c', GetoptLong::REQUIRED_ARGUMENT],
  75. ['--scm', GetoptLong::REQUIRED_ARGUMENT],
  76. ['--test', '-t', GetoptLong::NO_ARGUMENT],
  77. ['--force', '-f', GetoptLong::NO_ARGUMENT],
  78. ['--verbose', '-v', GetoptLong::NO_ARGUMENT],
  79. ['--version', '-V', GetoptLong::NO_ARGUMENT],
  80. ['--help' , '-h', GetoptLong::NO_ARGUMENT],
  81. ['--quiet' , '-q', GetoptLong::NO_ARGUMENT]
  82. )
  83. $verbose = 0
  84. $quiet = false
  85. $redmine_host = ''
  86. $repos_base = ''
  87. $svn_owner = 'root'
  88. $svn_group = 'root'
  89. $use_groupid = true
  90. $svn_url = false
  91. $test = false
  92. $force = false
  93. $scm = 'Subversion'
  94. def log(text, options={})
  95. level = options[:level] || 0
  96. puts text unless $quiet or level > $verbose
  97. exit 1 if options[:exit]
  98. end
  99. def system_or_raise(command)
  100. raise "\"#{command}\" failed" unless system command
  101. end
  102. module SCM
  103. module Subversion
  104. def self.create(path)
  105. system_or_raise "svnadmin create #{path}"
  106. end
  107. end
  108. module Git
  109. def self.create(path)
  110. Dir.mkdir path
  111. Dir.chdir(path) do
  112. system_or_raise "git --bare init --shared"
  113. system_or_raise "git update-server-info"
  114. end
  115. end
  116. end
  117. end
  118. begin
  119. opts.each do |opt, arg|
  120. case opt
  121. when '--svn-dir'; $repos_base = arg.dup
  122. when '--redmine-host'; $redmine_host = arg.dup
  123. when '--key'; $api_key = arg.dup
  124. when '--owner'; $svn_owner = arg.dup; $use_groupid = false;
  125. when '--group'; $svn_group = arg.dup; $use_groupid = false;
  126. when '--url'; $svn_url = arg.dup
  127. when '--scm'; $scm = arg.dup.capitalize; log("Invalid SCM: #{$scm}", :exit => true) unless SUPPORTED_SCM.include?($scm)
  128. when '--command'; $command = arg.dup
  129. when '--verbose'; $verbose += 1
  130. when '--test'; $test = true
  131. when '--force'; $force = true
  132. when '--version'; puts Version; exit
  133. when '--help'; RDoc::usage
  134. when '--quiet'; $quiet = true
  135. end
  136. end
  137. rescue
  138. exit 1
  139. end
  140. if $test
  141. log("running in test mode")
  142. end
  143. # Make sure command is overridden if SCM vendor is not handled internally (for the moment Subversion and Git)
  144. if $command.nil?
  145. begin
  146. scm_module = SCM.const_get($scm)
  147. rescue
  148. log("Please use --command option to specify how to create a #{$scm} repository.", :exit => true)
  149. end
  150. end
  151. $svn_url += "/" if $svn_url and not $svn_url.match(/\/$/)
  152. if ($redmine_host.empty? or $repos_base.empty?)
  153. RDoc::usage
  154. end
  155. unless File.directory?($repos_base)
  156. log("directory '#{$repos_base}' doesn't exists", :exit => true)
  157. end
  158. begin
  159. require 'active_resource'
  160. rescue LoadError
  161. log("This script requires activeresource.\nRun 'gem install activeresource' to install it.", :exit => true)
  162. end
  163. class Project < ActiveResource::Base
  164. self.headers["User-agent"] = "Redmine repository manager/#{Version}"
  165. end
  166. log("querying Redmine for projects...", :level => 1);
  167. $redmine_host.gsub!(/^/, "http://") unless $redmine_host.match("^https?://")
  168. $redmine_host.gsub!(/\/$/, '')
  169. Project.site = "#{$redmine_host}/sys";
  170. begin
  171. # Get all active projects that have the Repository module enabled
  172. projects = Project.find(:all, :params => {:key => $api_key})
  173. rescue => e
  174. log("Unable to connect to #{Project.site}: #{e}", :exit => true)
  175. end
  176. if projects.nil?
  177. log('no project found, perhaps you forgot to "Enable WS for repository management"', :exit => true)
  178. end
  179. log("retrieved #{projects.size} projects", :level => 1)
  180. def set_owner_and_rights(project, repos_path, &block)
  181. if RUBY_PLATFORM =~ /mswin/
  182. yield if block_given?
  183. else
  184. uid, gid = Etc.getpwnam($svn_owner).uid, ($use_groupid ? Etc.getgrnam(project.identifier).gid : Etc.getgrnam($svn_group).gid)
  185. right = project.is_public ? 0775 : 0770
  186. yield if block_given?
  187. Find.find(repos_path) do |f|
  188. File.chmod right, f
  189. File.chown uid, gid, f
  190. end
  191. end
  192. end
  193. def other_read_right?(file)
  194. (File.stat(file).mode & 0007).zero? ? false : true
  195. end
  196. def owner_name(file)
  197. mswin? ?
  198. $svn_owner :
  199. Etc.getpwuid( File.stat(file).uid ).name
  200. end
  201. def mswin?
  202. (RUBY_PLATFORM =~ /(:?mswin|mingw)/) || (RUBY_PLATFORM == 'java' && (ENV['OS'] || ENV['os']) =~ /windows/i)
  203. end
  204. projects.each do |project|
  205. log("treating project #{project.name}", :level => 1)
  206. if project.identifier.empty?
  207. log("\tno identifier for project #{project.name}")
  208. next
  209. elsif not project.identifier.match(/^[a-z0-9\-]+$/)
  210. log("\tinvalid identifier for project #{project.name} : #{project.identifier}");
  211. next;
  212. end
  213. repos_path = File.join($repos_base, project.identifier).gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR)
  214. if File.directory?(repos_path)
  215. # we must verify that repository has the good owner and the good
  216. # rights before leaving
  217. other_read = other_read_right?(repos_path)
  218. owner = owner_name(repos_path)
  219. next if project.is_public == other_read and owner == $svn_owner
  220. if $test
  221. log("\tchange mode on #{repos_path}")
  222. next
  223. end
  224. begin
  225. set_owner_and_rights(project, repos_path)
  226. rescue Errno::EPERM => e
  227. log("\tunable to change mode on #{repos_path} : #{e}\n")
  228. next
  229. end
  230. log("\tmode change on #{repos_path}");
  231. else
  232. # if repository is already declared in redmine, we don't create
  233. # unless user use -f with reposman
  234. if $force == false and project.respond_to?(:repository)
  235. log("\trepository for project #{project.identifier} already exists in Redmine", :level => 1)
  236. next
  237. end
  238. project.is_public ? File.umask(0002) : File.umask(0007)
  239. if $test
  240. log("\tcreate repository #{repos_path}")
  241. log("\trepository #{repos_path} registered in Redmine with url #{$svn_url}#{project.identifier}") if $svn_url;
  242. next
  243. end
  244. begin
  245. set_owner_and_rights(project, repos_path) do
  246. if scm_module.nil?
  247. system_or_raise "#{$command} #{repos_path}"
  248. else
  249. scm_module.create(repos_path)
  250. end
  251. end
  252. rescue => e
  253. log("\tunable to create #{repos_path} : #{e}\n")
  254. next
  255. end
  256. if $svn_url
  257. begin
  258. project.post(:repository, :vendor => $scm, :repository => {:url => "#{$svn_url}#{project.identifier}"}, :key => $api_key)
  259. log("\trepository #{repos_path} registered in Redmine with url #{$svn_url}#{project.identifier}");
  260. rescue => e
  261. log("\trepository #{repos_path} not registered in Redmine: #{e.message}");
  262. end
  263. end
  264. log("\trepository #{repos_path} created");
  265. end
  266. end