-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailman-migrate-fhs
executable file
·293 lines (244 loc) · 8.82 KB
/
mailman-migrate-fhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/python
import sys
import os
import re
import shutil
import getopt
from stat import *
#------------------------------------------------------------------------------
# Command Line Args
doit = True
verbose = False
quiet = False
warn = False
force = False
print_mapping = False
remove_files = False
remove_installation = False
# Scan Results
existing_files = {}
non_existing_files = {}
# Directory and File mappings
# This is the complete directory map, it includes both data files
# and run-time files
dir_map = {
'/var/mailman' : '/var/lib/mailman',
'/var/mailman/Mailman' : '/usr/lib/mailman/Mailman',
'/var/mailman/archives' : '/var/lib/mailman/archives',
'/var/mailman/bin' : '/usr/lib/mailman/bin',
'/var/mailman/cgi-bin' : '/usr/lib/mailman/cgi-bin',
'/var/mailman/cron' : '/usr/lib/mailman/cron',
'/var/mailman/data' : '/var/lib/mailman/data',
'/var/mailman/lists' : '/var/lib/mailman/lists',
'/var/mailman/locks' : '/var/lock/mailman',
'/var/mailman/logs' : '/var/log/mailman',
'/var/mailman/mail' : '/usr/lib/mailman/mail',
'/var/mailman/messages' : '/usr/lib/mailman/messages',
'/var/mailman/pythonlib' : '/usr/lib/mailman/pythonlib',
'/var/mailman/qfiles' : '/var/spool/mailman',
'/var/spool/mailman/qfiles' : '/var/spool/mailman',
'/var/mailman/scripts' : '/usr/lib/mailman/scripts',
'/var/mailman/spam' : '/var/lib/mailman/spam',
'/var/mailman/templates' : '/usr/lib/mailman/templates',
'/var/mailman/tests' : '/usr/lib/mailman/tests'
}
# These are directories that contain data files the user may
# want to preserve from an old installation and should be copied
# into the new directory location.
data_dir_map = {
'/var/mailman/archives' : '/var/lib/mailman/archives',
'/var/mailman/data' : '/var/lib/mailman/data',
'/var/mailman/lists' : '/var/lib/mailman/lists',
'/var/mailman/logs' : '/var/log/mailman',
'/var/mailman/qfiles' : '/var/spool/mailman',
'/var/spool/mailman/qfiles' : '/var/spool/mailman',
'/var/mailman/spam' : '/var/lib/mailman/spam',
}
# These are mappings for individual files. They represent files that
# cannot be mapped via their parent dirctories, they must be treated
# individually.
file_map = {
'/var/mailman/data/adm.pw' : '/etc/mailman/adm.pw',
'/var/mailman/data/creator.pw' : '/etc/mailman/creator.pw',
'/var/mailman/data/aliases' : '/etc/mailman/aliases',
'/var/mailman/data/virtual-mailman' : '/etc/mailman/virtual-mailman',
'/var/mailman/data/sitelist.cfg' : '/etc/mailman/sitelist.cfg',
'/var/mailman/data/master-qrunner.pid' : '/var/run/mailman/master-qrunner.pid'
}
#------------------------------------------------------------------------------
def DumpMapping():
'''Print out the directory and file mappings'''
print "Directory Mapping:"
for key in dir_map.keys():
print "%s --> %s" %(key, dir_map[key])
print "\nFile Mapping:"
for key in file_map.keys():
print "%s --> %s" %(key, file_map[key])
def RecordFile(src, dst):
'''If the src files (old) exists record this as a potential
file operation. File operations are grouped into two sets,
those where the dst (new) files exists and those where it does not
exist. This is done to prevent overwriting files'''
global existing_files, non_existing_files
if not os.path.exists(src):
return
if existing_files.has_key(src):
if warn:
print "WARNING: src file already seen (%s) and has dst match: (%s)" % (src, dst)
return
if non_existing_files.has_key(src):
if warn:
print "WARNING: src file already seen (%s) does not have dst match" % (src)
return
if os.path.exists(dst):
existing_files[src] = dst
else:
non_existing_files[src] = dst
def GetCopyFiles(old_root, new_root):
'''Recursively generate a list of src files (old) in the old_root
and pair each of them with their new dst path name'''
prefix_re = re.compile("^(%s)/*(.*)" % re.escape(old_root))
dst_files_existing = []
dst_files_non_existing = []
for root, dirs, files in os.walk(old_root):
match = prefix_re.match(root)
subdir = match.group(2)
for name in files:
oldpath = os.path.join(root, name)
newpath = os.path.join(new_root, subdir, name)
RecordFile(oldpath, newpath)
def CopyFile(src_path, dst_path):
'''Copy file, preserve its mode and ownership. If the dst directory
does not exist, create it preserving the mode and ownership of the
src direcotry'''
if not doit:
print "cp %s %s" % (src_path, dst_path)
return
src_dir = os.path.dirname(src_path)
dst_dir = os.path.dirname(dst_path)
if not os.path.isdir(dst_dir):
if os.path.exists(dst_dir):
print "ERROR: dst dir exists, but is not directory (%s)" % dst_dir
return
st = os.stat(src_dir)
os.makedirs(dst_dir, st[ST_MODE])
os.chown(dst_dir, st[ST_UID], st[ST_GID])
shutil.copy2(src_path, dst_path)
st = os.stat(src_path)
os.chown(dst_path, st[ST_UID], st[ST_GID])
def RemoveFile(path):
'''Remove the file'''
if not os.path.exists(path):
if warn:
print "WARNING: attempt to remove non-existent file (%s)" % path
return
if not os.path.isfile(path):
if warn:
print "WARNING: attempt to remove non-plain file (%s)" % path
return
if not doit:
print "rm %s" % (path)
return
os.unlink(path)
def RemoveDirs(top):
'''Delete everything reachable from the directory named in 'top',
assuming there are no symbolic links.
CAUTION: This is dangerous! For example, if top == '/', it
could delete all your disk files.'''
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
path = os.path.join(root, name)
if not doit:
print "rm %s" % (path)
else:
os.remove(path)
for name in dirs:
path = os.path.join(root, name)
if not doit:
print "rmdir %s" % (path)
else:
os.rmdir(path)
def Usage():
print """
This script will help you copy mailman data files from the old
directory structure to the new FHS directory structure.
Mailman should not be running when you perform this!
/sbin/service mailman stop
This script is conservative, by default it will not overwrite
any file in the new directory on the assumption it is most recent
and most correct. If you want to force overwrites use -f.
Files are copied to the new directories, if you want to remove the
old data files use -r. Hint: copy first and test, once everything is
working remove the old files with -r. If you want to remove the entire
old installation use -R
migrate [-f] [-n] [-q] [-v] [-w] [-m] [-r] [-R]
-n don't execute, but show what would be done
-f force destination overwrites
-m print mapping
-r remove old data files
-R remove entire old installation
-q be quiet
-v be verbose
-w print warnings
-h help
"""
#------------------------------------------------------------------------------
try:
opts, args = getopt.getopt(sys.argv[1:], "nfvmqwhrR")
for o, a in opts:
if o == "-n":
doit = False
elif o == "-f":
force = True
elif o == "-v":
verbose = True
elif o == "-m":
print_mapping = True
elif o == "-q":
quiet = True
elif o == "-w":
warn = True
elif o == "-r":
remove_files = True
elif o == "-R":
remove_installation = True
elif o == "-h":
Usage()
sys.exit(1)
except getopt.GetoptError, err:
print err
Usage()
sys.exit(1)
if print_mapping:
DumpMapping()
sys.exit(0)
# Generate file list
for src_dir in data_dir_map.keys():
GetCopyFiles(src_dir, dir_map[src_dir])
for src_file in file_map.keys():
RecordFile(src_file, file_map[src_file])
# Copy files
for src in non_existing_files:
dst = non_existing_files[src]
CopyFile(src, dst)
if force:
for src in existing_files:
dst = existing_files[src]
CopyFile(src, dst)
else:
if len(existing_files) > 0 and not quiet:
print "\nThe following files already exist in the destination, they will NOT be copied"
print "To force overwriting invoke with -f\n"
for src in existing_files:
dst = existing_files[src]
print "# cp %s %s" %(src, dst)
# Remove old files
if remove_files:
for src in existing_files:
RemoveFile(src)
for src in non_existing_files:
RemoveFile(src)
if remove_installation:
for old_dir in dir_map.keys():
RemoveDirs(old_dir)
sys.exit(0)