Chris@0: #!/usr/bin/env ruby Chris@0: Chris@1115: require 'optparse' Chris@0: require 'find' Chris@0: require 'etc' Chris@1115: require 'rubygems' Chris@0: Chris@1115: Version = "1.4" Chris@0: SUPPORTED_SCM = %w( Subversion Darcs Mercurial Bazaar Git Filesystem ) Chris@0: Chris@0: $verbose = 0 Chris@0: $quiet = false Chris@0: $redmine_host = '' Chris@0: $repos_base = '' Chris@0: $svn_owner = 'root' Chris@0: $svn_group = 'root' Chris@0: $use_groupid = true Chris@0: $svn_url = false Chris@0: $test = false Chris@0: $force = false Chris@0: $scm = 'Subversion' Chris@0: Chris@0: def log(text, options={}) Chris@0: level = options[:level] || 0 Chris@0: puts text unless $quiet or level > $verbose Chris@0: exit 1 if options[:exit] Chris@0: end Chris@0: Chris@0: def system_or_raise(command) Chris@0: raise "\"#{command}\" failed" unless system command Chris@0: end Chris@0: Chris@0: module SCM Chris@0: Chris@0: module Subversion Chris@0: def self.create(path) Chris@0: system_or_raise "svnadmin create #{path}" Chris@0: end Chris@0: end Chris@0: Chris@0: module Git Chris@0: def self.create(path) Chris@0: Dir.mkdir path Chris@0: Dir.chdir(path) do Chris@0: system_or_raise "git --bare init --shared" Chris@0: system_or_raise "git update-server-info" Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: end Chris@0: Chris@1115: def read_key_from_file(filename) Chris@1115: begin Chris@1115: $api_key = File.read(filename).strip Chris@1115: rescue Exception => e Chris@1115: $stderr.puts "Unable to read the key from #{filename}: #{e.message}" Chris@1115: exit 1 Chris@0: end Chris@0: end Chris@0: Chris@1115: def set_scm(scm) Chris@1115: $scm = scm.capitalize Chris@1115: unless SUPPORTED_SCM.include?($scm) Chris@1115: log("Invalid SCM: #{$scm}\nValid SCM are: #{SUPPORTED_SCM.join(', ')}", :exit => true) Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: optparse = OptionParser.new do |opts| Chris@1115: opts.banner = "Usage: reposman.rb [OPTIONS...] -s [DIR] -r [HOST] -k [KEY]" Chris@1115: opts.separator("") Chris@1115: opts.separator("Manages your repositories with Redmine.") Chris@1115: opts.separator("") Chris@1115: opts.separator("Required arguments:") Chris@1115: opts.on("-s", "--svn-dir DIR", "use DIR as base directory for svn repositories") {|v| $repos_base = v} Chris@1115: opts.on("-r", "--redmine-host HOST","assume Redmine is hosted on HOST. Examples:", Chris@1115: " -r redmine.example.net", Chris@1115: " -r http://redmine.example.net", Chris@1115: " -r https://redmine.example.net") {|v| $redmine_host = v} Chris@1115: opts.on("-k", "--key KEY", "use KEY as the Redmine API key", Chris@1115: "(you can use --key-file option as an alternative)") {|v| $api_key = v} Chris@1115: opts.separator("") Chris@1115: opts.separator("Options:") Chris@1115: opts.on("-o", "--owner OWNER", "owner of the repository. using the rails login", Chris@1115: "allows users to browse the repository within", Chris@1115: "Redmine even for private projects. If you want to", Chris@1115: "share repositories through Redmine.pm, you need", Chris@1115: "to use the apache owner.") {|v| $svn_owner = v; $use_groupid = false} Chris@1115: opts.on("-g", "--group GROUP", "group of the repository (default: root)") {|v| $svn_group = v; $use_groupid = false} Chris@1115: opts.on("-u", "--url URL", "the base url Redmine will use to access your", Chris@1115: "repositories. This option is used to register", Chris@1115: "the repositories in Redmine automatically. The", Chris@1115: "project identifier will be appended to this url.", Chris@1115: "Examples:", Chris@1115: " -u https://example.net/svn", Chris@1115: " -u file:///var/svn/", Chris@1115: "if this option isn't set, reposman won't register", Chris@1115: "the repositories in Redmine") {|v| $svn_url = v} Chris@1115: opts.on( "--scm SCM", "the kind of SCM repository you want to create", Chris@1115: "(and register) in Redmine (default: Subversion).", Chris@1115: "reposman is able to create Git and Subversion", Chris@1115: "repositories.", Chris@1115: "For all other kind, you must specify a --command", Chris@1115: "option") {|v| set_scm(v)} Chris@1115: opts.on("-c", "--command COMMAND", "use this command instead of `svnadmin create` to", Chris@1115: "create a repository. This option can be used to", Chris@1115: "create repositories other than subversion and git", Chris@1115: "kind.", Chris@1115: "This command override the default creation for", Chris@1115: "git and subversion.") {|v| $command = v} Chris@1115: opts.on( "--key-file FILE", "path to a file that contains the Redmine API key", Chris@1115: "(use this option instead of --key if you don't", Chris@1115: "want the key to appear in the command line)") {|v| read_key_from_file(v)} Chris@1115: opts.on("-t", "--test", "only show what should be done") {$test = true} Chris@1115: opts.on("-f", "--force", "force repository creation even if the project", "repository is already declared in Redmine") {$force = true} Chris@1115: opts.on("-v", "--verbose", "verbose") {$verbose += 1} Chris@1115: opts.on("-V", "--version", "show version and exit") {puts Version; exit} Chris@1115: opts.on("-h", "--help", "show help and exit") {puts opts; exit 1} Chris@1115: opts.on("-q", "--quiet", "no log") {$quiet = true} Chris@1115: opts.separator("") Chris@1115: opts.separator("Examples:") Chris@1115: opts.separator(" reposman.rb --svn-dir=/var/svn --redmine-host=redmine.host") Chris@1115: opts.separator(" reposman.rb -s /var/git -r redmine.host -u http://git.host --scm git") Chris@1115: opts.separator("") Chris@1115: opts.separator("You can find more information on the redmine's wiki:\nhttp://www.redmine.org/projects/redmine/wiki/HowTos") Chris@1115: Chris@1115: opts.summary_width = 25 Chris@1115: end Chris@1115: optparse.parse! Chris@1115: Chris@0: if $test Chris@0: log("running in test mode") Chris@0: end Chris@0: Chris@0: # Make sure command is overridden if SCM vendor is not handled internally (for the moment Subversion and Git) Chris@0: if $command.nil? Chris@0: begin Chris@0: scm_module = SCM.const_get($scm) Chris@0: rescue Chris@0: log("Please use --command option to specify how to create a #{$scm} repository.", :exit => true) Chris@0: end Chris@0: end Chris@0: Chris@0: $svn_url += "/" if $svn_url and not $svn_url.match(/\/$/) Chris@0: Chris@0: if ($redmine_host.empty? or $repos_base.empty?) Chris@1115: puts "Some arguments are missing. Use reposman.rb --help for getting help." Chris@1115: exit 1 Chris@0: end Chris@0: Chris@0: unless File.directory?($repos_base) Chris@0: log("directory '#{$repos_base}' doesn't exists", :exit => true) Chris@0: end Chris@0: Chris@0: begin Chris@0: require 'active_resource' Chris@0: rescue LoadError Chris@0: log("This script requires activeresource.\nRun 'gem install activeresource' to install it.", :exit => true) Chris@0: end Chris@0: chris@37: class Project < ActiveResource::Base chris@37: self.headers["User-agent"] = "Redmine repository manager/#{Version}" Chris@909: self.format = :xml chris@37: end Chris@0: Chris@0: log("querying Redmine for projects...", :level => 1); Chris@0: Chris@0: $redmine_host.gsub!(/^/, "http://") unless $redmine_host.match("^https?://") Chris@0: $redmine_host.gsub!(/\/$/, '') Chris@0: Chris@0: Project.site = "#{$redmine_host}/sys"; Chris@0: Chris@0: begin Chris@0: # Get all active projects that have the Repository module enabled Chris@0: projects = Project.find(:all, :params => {:key => $api_key}) Chris@909: rescue ActiveResource::ForbiddenAccess Chris@909: 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: rescue => e Chris@0: log("Unable to connect to #{Project.site}: #{e}", :exit => true) Chris@0: end Chris@0: Chris@0: if projects.nil? Chris@909: log('No project found, perhaps you forgot to "Enable WS for repository management"', :exit => true) Chris@0: end Chris@0: Chris@0: log("retrieved #{projects.size} projects", :level => 1) Chris@0: Chris@0: def set_owner_and_rights(project, repos_path, &block) Chris@441: if mswin? Chris@0: yield if block_given? Chris@0: else Chris@0: uid, gid = Etc.getpwnam($svn_owner).uid, ($use_groupid ? Etc.getgrnam(project.identifier).gid : Etc.getgrnam($svn_group).gid) Chris@0: right = project.is_public ? 0775 : 0770 Chris@0: yield if block_given? Chris@0: Find.find(repos_path) do |f| Chris@0: File.chmod right, f Chris@0: File.chown uid, gid, f Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: def other_read_right?(file) Chris@0: (File.stat(file).mode & 0007).zero? ? false : true Chris@0: end Chris@0: Chris@0: def owner_name(file) Chris@0: mswin? ? Chris@0: $svn_owner : Chris@441: Etc.getpwuid( File.stat(file).uid ).name Chris@0: end Chris@441: Chris@0: def mswin? Chris@0: (RUBY_PLATFORM =~ /(:?mswin|mingw)/) || (RUBY_PLATFORM == 'java' && (ENV['OS'] || ENV['os']) =~ /windows/i) Chris@0: end Chris@0: Chris@0: projects.each do |project| Chris@0: log("treating project #{project.name}", :level => 1) Chris@0: Chris@0: if project.identifier.empty? Chris@0: log("\tno identifier for project #{project.name}") Chris@0: next Chris@1115: elsif not project.identifier.match(/^[a-z0-9\-_]+$/) Chris@0: log("\tinvalid identifier for project #{project.name} : #{project.identifier}"); Chris@0: next; Chris@0: end Chris@0: Chris@0: repos_path = File.join($repos_base, project.identifier).gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR) Chris@0: Chris@0: if File.directory?(repos_path) Chris@0: # we must verify that repository has the good owner and the good Chris@0: # rights before leaving Chris@0: other_read = other_read_right?(repos_path) Chris@0: owner = owner_name(repos_path) Chris@0: next if project.is_public == other_read and owner == $svn_owner Chris@0: Chris@0: if $test Chris@0: log("\tchange mode on #{repos_path}") Chris@0: next Chris@0: end Chris@0: Chris@0: begin Chris@0: set_owner_and_rights(project, repos_path) Chris@0: rescue Errno::EPERM => e Chris@0: log("\tunable to change mode on #{repos_path} : #{e}\n") Chris@0: next Chris@0: end Chris@0: Chris@0: log("\tmode change on #{repos_path}"); Chris@0: Chris@0: else Chris@0: # if repository is already declared in redmine, we don't create Chris@0: # unless user use -f with reposman Chris@0: if $force == false and project.respond_to?(:repository) Chris@0: log("\trepository for project #{project.identifier} already exists in Redmine", :level => 1) Chris@0: next Chris@0: end Chris@0: Chris@0: project.is_public ? File.umask(0002) : File.umask(0007) Chris@0: Chris@0: if $test Chris@0: log("\tcreate repository #{repos_path}") Chris@0: log("\trepository #{repos_path} registered in Redmine with url #{$svn_url}#{project.identifier}") if $svn_url; Chris@0: next Chris@0: end Chris@0: Chris@0: begin Chris@0: set_owner_and_rights(project, repos_path) do Chris@0: if scm_module.nil? Chris@0: system_or_raise "#{$command} #{repos_path}" Chris@0: else Chris@0: scm_module.create(repos_path) Chris@0: end Chris@0: end Chris@0: rescue => e Chris@0: log("\tunable to create #{repos_path} : #{e}\n") Chris@0: next Chris@0: end Chris@0: Chris@0: if $svn_url Chris@0: begin Chris@0: project.post(:repository, :vendor => $scm, :repository => {:url => "#{$svn_url}#{project.identifier}"}, :key => $api_key) Chris@0: log("\trepository #{repos_path} registered in Redmine with url #{$svn_url}#{project.identifier}"); Chris@0: rescue => e Chris@0: log("\trepository #{repos_path} not registered in Redmine: #{e.message}"); Chris@0: end Chris@0: end Chris@0: log("\trepository #{repos_path} created"); Chris@0: end Chris@0: end