annotate .svn/pristine/8e/8ea72f11aca3633b56300020cda571d3f13b20ac.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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