@@ -0,0 +1,157 | |||||
|
1 | #!/usr/bin/ruby | |||
|
2 | ||||
|
3 | # == Synopsis | |||
|
4 | # | |||
|
5 | # reposman: manages your svn repositories with redMine | |||
|
6 | # | |||
|
7 | # == Usage | |||
|
8 | # | |||
|
9 | # reposman [ -h | --help ] [ -v | --verbose ] [ -V | --version ] [ -q | --quiet ] -s /var/svn -r redmine.host.org | |||
|
10 | # example: reposman --svn-dir=/var/svn --redmine-host=redmine.mydomain.foo | |||
|
11 | # reposman -s /var/svn -r redmine.mydomain.foo | |||
|
12 | # | |||
|
13 | # == Arguments (mandatory) | |||
|
14 | # | |||
|
15 | # -s, --svn-dir=DIR | |||
|
16 | # use DIR as base directory for svn repositories | |||
|
17 | # | |||
|
18 | # -r, --redmine-host=HOST | |||
|
19 | # assume redMine is hosted on HOST. | |||
|
20 | # you can use : | |||
|
21 | # * -r redmine.mydomain.foo (will add http://) | |||
|
22 | # * -r http://redmine.mydomain.foo | |||
|
23 | # * -r https://mydomain.foo/redmine | |||
|
24 | # | |||
|
25 | # == Options | |||
|
26 | # | |||
|
27 | # -h, --help: | |||
|
28 | # show help and exit | |||
|
29 | # | |||
|
30 | # -v, --verbose | |||
|
31 | # verbose | |||
|
32 | # | |||
|
33 | # -V, --version | |||
|
34 | # print version and exit | |||
|
35 | # | |||
|
36 | # -q, --quiet | |||
|
37 | # no log | |||
|
38 | # | |||
|
39 | ||||
|
40 | require 'getoptlong' | |||
|
41 | require 'rdoc/usage' | |||
|
42 | require 'soap/wsdlDriver' | |||
|
43 | require 'find' | |||
|
44 | require 'etc' | |||
|
45 | ||||
|
46 | Version = "1.0" | |||
|
47 | ||||
|
48 | opts = GetoptLong.new( | |||
|
49 | ['--svn-dir', '-s', GetoptLong::REQUIRED_ARGUMENT], | |||
|
50 | ['--redmine-host', '-r', GetoptLong::REQUIRED_ARGUMENT], | |||
|
51 | ['--verbose', '-v', GetoptLong::NO_ARGUMENT], | |||
|
52 | ['--version', '-V', GetoptLong::NO_ARGUMENT], | |||
|
53 | ['--help' , '-h', GetoptLong::NO_ARGUMENT], | |||
|
54 | ['--quiet' , '-q', GetoptLong::NO_ARGUMENT] | |||
|
55 | ) | |||
|
56 | ||||
|
57 | $verbose = 0 | |||
|
58 | $quiet = false | |||
|
59 | $redmine_host = '' | |||
|
60 | $repos_base = '' | |||
|
61 | ||||
|
62 | def log(text,level=0, exit=false) | |||
|
63 | return if $quiet or level > $verbose | |||
|
64 | puts text | |||
|
65 | exit 1 if exit | |||
|
66 | end | |||
|
67 | ||||
|
68 | begin | |||
|
69 | opts.each do |opt, arg| | |||
|
70 | case opt | |||
|
71 | when '--svn-dir'; $repos_base = arg.dup | |||
|
72 | when '--redmine-host'; $redmine_host = arg.dup | |||
|
73 | when '--verbose'; $verbose += 1 | |||
|
74 | when '--version'; puts Version; exit | |||
|
75 | when '--help'; RDoc::usage | |||
|
76 | when '--quiet'; $quiet = true | |||
|
77 | end | |||
|
78 | end | |||
|
79 | rescue | |||
|
80 | exit 1 | |||
|
81 | end | |||
|
82 | ||||
|
83 | if ($redmine_host.empty? or $repos_base.empty?) | |||
|
84 | RDoc::usage | |||
|
85 | end | |||
|
86 | ||||
|
87 | unless File.directory?($repos_base) | |||
|
88 | log("directory '#{$repos_base}' doesn't exists", 0, true) | |||
|
89 | end | |||
|
90 | ||||
|
91 | log("querying redMine for projects...", 1); | |||
|
92 | ||||
|
93 | $redmine_host.gsub!(/^/, "http://") unless $redmine_host.match("^https?://") | |||
|
94 | $redmine_host.gsub!(/\/$/, '') | |||
|
95 | ||||
|
96 | wsdl_url = "#{$redmine_host}/sys/service.wsdl"; | |||
|
97 | ||||
|
98 | begin | |||
|
99 | soap = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver | |||
|
100 | rescue => e | |||
|
101 | log("Unable to connect to #{wsdl_url} : #{e}", 0, true) | |||
|
102 | end | |||
|
103 | ||||
|
104 | projects = soap.Projects | |||
|
105 | ||||
|
106 | if projects.nil? | |||
|
107 | log('no project found, perhaps you forgot to "Enable WS for repository management"', 0, true) | |||
|
108 | end | |||
|
109 | ||||
|
110 | log("retrieved #{projects.size} projects", 1) | |||
|
111 | ||||
|
112 | projects.each do |p| | |||
|
113 | log("treating project #{p.name}", 1) | |||
|
114 | ||||
|
115 | if p.identifier.empty? | |||
|
116 | log("\tno identifier for project #{p.name}") | |||
|
117 | next | |||
|
118 | elsif not p.identifier.match(/^[a-z0-9\-]+$/) | |||
|
119 | log("\tinvalid identifier for project #{p.name} : #{p.identifier}"); | |||
|
120 | next; | |||
|
121 | end | |||
|
122 | ||||
|
123 | repos_path = $repos_base + "/" + p.identifier | |||
|
124 | ||||
|
125 | if File.directory?(repos_path) | |||
|
126 | ||||
|
127 | other_read = (File.stat(repos_path).mode & 0007).zero? ? false : true | |||
|
128 | next if p.is_public == other_read | |||
|
129 | ||||
|
130 | right = p.is_public ? 0775 : 0770 | |||
|
131 | ||||
|
132 | begin | |||
|
133 | Find.find(repos_path) { |f| File.chmod right, f } | |||
|
134 | rescue Errno::EPERM => e | |||
|
135 | log("\tunable to change mode on #{repos_path} : #{e}\n") | |||
|
136 | next | |||
|
137 | end | |||
|
138 | ||||
|
139 | log("\tmode change on #{repos_path}"); | |||
|
140 | ||||
|
141 | else | |||
|
142 | p.is_public ? File.umask(0002) : File.umask(0007) | |||
|
143 | ||||
|
144 | begin | |||
|
145 | uid, gid = Etc.getpwnam("root").uid, Etc.getgrnam(p.identifier).gid | |||
|
146 | raise "svnadmin create #{repos_path} failed" unless system("svnadmin", "create", repos_path) | |||
|
147 | Find.find(repos_path) { |f| File.chown uid, gid, f } | |||
|
148 | rescue => e | |||
|
149 | log("\tunable to create #{repos_path} : #{e}\n") | |||
|
150 | next | |||
|
151 | end | |||
|
152 | ||||
|
153 | log("\trepository #{repos_path} created"); | |||
|
154 | end | |||
|
155 | ||||
|
156 | end | |||
|
157 |
General Comments 0
You need to be logged in to leave comments.
Login now