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