@@ -1,233 +1,234 | |||
|
1 | 1 | #!/usr/bin/ruby |
|
2 | 2 | |
|
3 | 3 | # == Synopsis |
|
4 | 4 | # |
|
5 | 5 | # reposman: manages your svn repositories with Redmine |
|
6 | 6 | # |
|
7 | 7 | # == Usage |
|
8 | 8 | # |
|
9 | 9 | # reposman [ -h | --help ] [ -v | --verbose ] [ -V | --version ] [ -q | --quiet ] -s /var/svn -r redmine.host.org |
|
10 | 10 | # example: reposman --svn-dir=/var/svn --redmine-host=redmine.mydomain.foo |
|
11 | 11 | # reposman -s /var/svn -r redmine.mydomain.foo |
|
12 | 12 | # |
|
13 | 13 | # == Arguments (mandatory) |
|
14 | 14 | # |
|
15 | 15 | # -s, --svn-dir=DIR |
|
16 | 16 | # use DIR as base directory for svn repositories |
|
17 | 17 | # |
|
18 | 18 | # -r, --redmine-host=HOST |
|
19 | 19 | # assume Redmine is hosted on HOST. |
|
20 | 20 | # you can use : |
|
21 | 21 | # * -r redmine.mydomain.foo (will add http://) |
|
22 | 22 | # * -r http://redmine.mydomain.foo |
|
23 | 23 | # * -r https://mydomain.foo/redmine |
|
24 | 24 | # |
|
25 | 25 | # == Options |
|
26 | 26 | # |
|
27 | 27 | # -o, --owner=OWNER |
|
28 | 28 | # owner of the repository. using the rails login allow user to browse |
|
29 | 29 | # the repository in Redmine even for private project |
|
30 | 30 | # |
|
31 | 31 | # -u, --url=URL |
|
32 | 32 | # the base url Redmine will use to access your repositories. This |
|
33 | 33 | # will be used to register the repository in Redmine so that user |
|
34 | 34 | # doesn't need to do anything. reposman will add the identifier to this url : |
|
35 | 35 | # |
|
36 | 36 | # -u https://my.svn.server/my/reposity/root # if the repository can be access by http |
|
37 | 37 | # -u file:///var/svn/ # if the repository is local |
|
38 | 38 | # if this option isn't set, reposman won't register the repository |
|
39 | 39 | # |
|
40 | 40 | # -t, --test |
|
41 | 41 | # only show what should be done |
|
42 | 42 | # |
|
43 | 43 | # -h, --help: |
|
44 | 44 | # show help and exit |
|
45 | 45 | # |
|
46 | 46 | # -v, --verbose |
|
47 | 47 | # verbose |
|
48 | 48 | # |
|
49 | 49 | # -V, --version |
|
50 | 50 | # print version and exit |
|
51 | 51 | # |
|
52 | 52 | # -q, --quiet |
|
53 | 53 | # no log |
|
54 | 54 | # |
|
55 | 55 | |
|
56 | 56 | require 'getoptlong' |
|
57 | 57 | require 'rdoc/usage' |
|
58 | 58 | require 'soap/wsdlDriver' |
|
59 | 59 | require 'find' |
|
60 | 60 | require 'etc' |
|
61 | 61 | |
|
62 | 62 | Version = "1.0" |
|
63 | 63 | |
|
64 | 64 | opts = GetoptLong.new( |
|
65 | 65 | ['--svn-dir', '-s', GetoptLong::REQUIRED_ARGUMENT], |
|
66 | 66 | ['--redmine-host', '-r', GetoptLong::REQUIRED_ARGUMENT], |
|
67 | 67 | ['--owner', '-o', GetoptLong::REQUIRED_ARGUMENT], |
|
68 | 68 | ['--url', '-u', GetoptLong::REQUIRED_ARGUMENT], |
|
69 | 69 | ['--test', '-t', GetoptLong::NO_ARGUMENT], |
|
70 | 70 | ['--verbose', '-v', GetoptLong::NO_ARGUMENT], |
|
71 | 71 | ['--version', '-V', GetoptLong::NO_ARGUMENT], |
|
72 | 72 | ['--help' , '-h', GetoptLong::NO_ARGUMENT], |
|
73 | 73 | ['--quiet' , '-q', GetoptLong::NO_ARGUMENT] |
|
74 | 74 | ) |
|
75 | 75 | |
|
76 | 76 | $verbose = 0 |
|
77 | 77 | $quiet = false |
|
78 | 78 | $redmine_host = '' |
|
79 | 79 | $repos_base = '' |
|
80 | 80 | $svn_owner = 'root' |
|
81 | $use_groupid = true | |
|
81 | 82 | $svn_url = false |
|
82 | 83 | $test = false |
|
83 | 84 | |
|
84 | 85 | def log(text,level=0, exit=false) |
|
85 | 86 | return if $quiet or level > $verbose |
|
86 | 87 | puts text |
|
87 | 88 | exit 1 if exit |
|
88 | 89 | end |
|
89 | 90 | |
|
90 | 91 | begin |
|
91 | 92 | opts.each do |opt, arg| |
|
92 | 93 | case opt |
|
93 | 94 | when '--svn-dir'; $repos_base = arg.dup |
|
94 | 95 | when '--redmine-host'; $redmine_host = arg.dup |
|
95 | when '--owner'; $svn_owner = arg.dup | |
|
96 | when '--owner'; $svn_owner = arg.dup; $use_groupid = false; | |
|
96 | 97 | when '--url'; $svn_url = arg.dup |
|
97 | 98 | when '--verbose'; $verbose += 1 |
|
98 | 99 | when '--test'; $test = true |
|
99 | 100 | when '--version'; puts Version; exit |
|
100 | 101 | when '--help'; RDoc::usage |
|
101 | 102 | when '--quiet'; $quiet = true |
|
102 | 103 | end |
|
103 | 104 | end |
|
104 | 105 | rescue |
|
105 | 106 | exit 1 |
|
106 | 107 | end |
|
107 | 108 | |
|
108 | 109 | if $test |
|
109 | 110 | log("running in test mode") |
|
110 | 111 | end |
|
111 | 112 | |
|
112 | 113 | $svn_url += "/" if $svn_url and not $svn_url.match(/\/$/) |
|
113 | 114 | |
|
114 | 115 | if ($redmine_host.empty? or $repos_base.empty?) |
|
115 | 116 | RDoc::usage |
|
116 | 117 | end |
|
117 | 118 | |
|
118 | 119 | unless File.directory?($repos_base) |
|
119 | 120 | log("directory '#{$repos_base}' doesn't exists", 0, true) |
|
120 | 121 | end |
|
121 | 122 | |
|
122 | 123 | log("querying Redmine for projects...", 1); |
|
123 | 124 | |
|
124 | 125 | $redmine_host.gsub!(/^/, "http://") unless $redmine_host.match("^https?://") |
|
125 | 126 | $redmine_host.gsub!(/\/$/, '') |
|
126 | 127 | |
|
127 | 128 | wsdl_url = "#{$redmine_host}/sys/service.wsdl"; |
|
128 | 129 | |
|
129 | 130 | begin |
|
130 | 131 | soap = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver |
|
131 | 132 | rescue => e |
|
132 | 133 | log("Unable to connect to #{wsdl_url} : #{e}", 0, true) |
|
133 | 134 | end |
|
134 | 135 | |
|
135 | 136 | projects = soap.Projects |
|
136 | 137 | |
|
137 | 138 | if projects.nil? |
|
138 | 139 | log('no project found, perhaps you forgot to "Enable WS for repository management"', 0, true) |
|
139 | 140 | end |
|
140 | 141 | |
|
141 | 142 | log("retrieved #{projects.size} projects", 1) |
|
142 | 143 | |
|
143 | 144 | def set_owner_and_rights(project, repos_path, &block) |
|
144 | 145 | if RUBY_PLATFORM =~ /mswin/ |
|
145 | 146 | yield if block_given? |
|
146 | 147 | else |
|
147 | uid, gid = Etc.getpwnam($svn_owner).uid, Etc.getgrnam(project.identifier).gid | |
|
148 | uid, gid = Etc.getpwnam($svn_owner).uid, ($use_groupid ? Etc.getgrnam(project.identifier).gid : 0) | |
|
148 | 149 | right = project.is_public ? 0775 : 0770 |
|
149 | 150 | yield if block_given? |
|
150 | 151 | Find.find(repos_path) do |f| |
|
151 | 152 | File.chmod right, f |
|
152 | 153 | File.chown uid, gid, f |
|
153 | 154 | end |
|
154 | 155 | end |
|
155 | 156 | end |
|
156 | 157 | |
|
157 | 158 | def other_read_right?(file) |
|
158 | 159 | (File.stat(file).mode & 0007).zero? ? false : true |
|
159 | 160 | end |
|
160 | 161 | |
|
161 | 162 | def owner_name(file) |
|
162 | 163 | RUBY_PLATFORM =~ /mswin/ ? |
|
163 | 164 | $svn_owner : |
|
164 | 165 | Etc.getpwuid( File.stat(file).uid ).name |
|
165 | 166 | end |
|
166 | 167 | |
|
167 | 168 | projects.each do |project| |
|
168 | 169 | log("treating project #{project.name}", 1) |
|
169 | 170 | |
|
170 | 171 | if project.identifier.empty? |
|
171 | 172 | log("\tno identifier for project #{project.name}") |
|
172 | 173 | next |
|
173 | 174 | elsif not project.identifier.match(/^[a-z0-9\-]+$/) |
|
174 | 175 | log("\tinvalid identifier for project #{project.name} : #{project.identifier}"); |
|
175 | 176 | next; |
|
176 | 177 | end |
|
177 | 178 | |
|
178 | 179 | repos_path = $repos_base + "/" + project.identifier |
|
179 | 180 | |
|
180 | 181 | if File.directory?(repos_path) |
|
181 | 182 | |
|
182 | 183 | # we must verify that repository has the good owner and the good |
|
183 | 184 | # rights before leaving |
|
184 | 185 | other_read = other_read_right?(repos_path) |
|
185 | 186 | owner = owner_name(repos_path) |
|
186 | 187 | next if project.is_public == other_read and owner == $svn_owner |
|
187 | 188 | |
|
188 | 189 | if $test |
|
189 | 190 | log("\tchange mode on #{repos_path}") |
|
190 | 191 | next |
|
191 | 192 | end |
|
192 | 193 | |
|
193 | 194 | begin |
|
194 | 195 | set_owner_and_rights(project, repos_path) |
|
195 | 196 | rescue Errno::EPERM => e |
|
196 | 197 | log("\tunable to change mode on #{repos_path} : #{e}\n") |
|
197 | 198 | next |
|
198 | 199 | end |
|
199 | 200 | |
|
200 | 201 | log("\tmode change on #{repos_path}"); |
|
201 | 202 | |
|
202 | 203 | else |
|
203 | 204 | project.is_public ? File.umask(0002) : File.umask(0007) |
|
204 | 205 | |
|
205 | 206 | if $test |
|
206 | 207 | log("\tcreate repository #{repos_path}") |
|
207 | 208 | log("\trepository #{repos_path} registered in Redmine with url #{$svn_url}#{project.identifier}") if $svn_url; |
|
208 | 209 | next |
|
209 | 210 | end |
|
210 | 211 | |
|
211 | 212 | begin |
|
212 | 213 | set_owner_and_rights(project, repos_path) do |
|
213 | 214 | raise "svnadmin create #{repos_path} failed" unless system("svnadmin", "create", repos_path) |
|
214 | 215 | end |
|
215 | 216 | rescue => e |
|
216 | 217 | log("\tunable to create #{repos_path} : #{e}\n") |
|
217 | 218 | next |
|
218 | 219 | end |
|
219 | 220 | |
|
220 | 221 | if $svn_url |
|
221 | 222 | ret = soap.RepositoryCreated project.identifier, "#{$svn_url}#{project.identifier}" |
|
222 | 223 | if ret > 0 |
|
223 | 224 | log("\trepository #{repos_path} registered in Redmine with url #{$svn_url}#{project.identifier}"); |
|
224 | 225 | else |
|
225 | 226 | log("\trepository #{repos_path} not registered in Redmine. Look in your log to find why."); |
|
226 | 227 | end |
|
227 | 228 | end |
|
228 | 229 | |
|
229 | 230 | log("\trepository #{repos_path} created"); |
|
230 | 231 | end |
|
231 | 232 | |
|
232 | 233 | end |
|
233 | 234 |
General Comments 0
You need to be logged in to leave comments.
Login now