annotate extra/soundsoftware/reposman-soundsoftware.rb @ 1628:9c5f8e24dadc live tip

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