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