4
4
# --- BEGIN_HEADER ---
5
5
#
6
6
# checkconf - check MiGserver.conf file
7
- # Copyright (C) 2003-2021 The MiG Project lead by Brian Vinter
7
+ # Copyright (C) 2003-2024 The MiG Project lead by Brian Vinter
8
8
#
9
9
# This file is part of MiG.
10
10
#
25
25
# -- END_HEADER ---
26
26
#
27
27
28
- """Check MiGserver.conf or specified MiG server conf file:
29
- Currently only checks that files and dirs exist, but could be
30
- extended to include other variable checks.
28
+ """Check MiGserver.conf either in specified path, pointed to in MIG_CONF or in
29
+ default mig/server/ location.
30
+ Currently mainly checks that required files and dirs exist, but may be extended
31
+ to include other variable checks.
31
32
"""
32
33
33
34
from __future__ import print_function
34
35
from __future__ import absolute_import
35
36
37
+ from builtins import input
38
+ from past .builtins import basestring
36
39
import os
37
40
import re
38
41
import sys
@@ -56,7 +59,7 @@ def usage():
56
59
def ask_reply (question ):
57
60
"""Read user input"""
58
61
59
- return raw_input (question )
62
+ return input (question )
60
63
61
64
62
65
def ask_confirm (question , allow_always = False ):
@@ -79,135 +82,163 @@ def touch_file(path):
79
82
open (path , 'w' ).close ()
80
83
81
84
82
- argc = len (sys .argv )
83
- if argc == 1 :
84
-
85
- # Change CWD to ../mig/server/ to allow relative paths
86
-
87
- server_dir = os .path .dirname (os .path .abspath (sys .argv [0 ]))
88
- conf_file = server_dir + os .sep + 'MiGserver.conf'
89
- elif argc == 2 :
90
- conf_file = sys .argv [1 ]
91
- else :
92
- print (usage ())
93
- sys .exit (2 )
94
-
95
- conf = None
96
- if not os .path .isfile (conf_file ):
97
- if YES == ask_confirm ('Configuration file %s does not exist! %s'
98
- % (conf_file , 'create it? [Y/n] ' )):
99
- try :
100
- touch_file (conf_file )
101
- print ('created empty configuration file: %s' % conf_file )
102
- except Exception as err :
103
- print ('could not create %s: %s' % (conf_file , err ))
85
+ def check_conf (conf_file ):
86
+ """Verify conf_file to be complete and have all referenced paths"""
87
+ conf = None
88
+ if not os .path .isfile (conf_file ):
89
+ if YES == ask_confirm ('Configuration file %s does not exist! %s' %
90
+ (conf_file , 'create it? [Y/n] ' )):
91
+ try :
92
+ touch_file (conf_file )
93
+ print ('created empty configuration file: %s' % conf_file )
94
+ except Exception as err :
95
+ print ('could not create %s: %s' % (conf_file , err ))
96
+ else :
97
+ return 2
104
98
else :
105
- sys .exit (1 )
106
- else :
107
- try :
108
- conf = Configuration (conf_file )
109
- except Exception as err :
110
- print ('configuration file %s is incomplete! %s' % (conf_file ,
111
- err ))
112
-
113
- if not conf :
114
- if YES == ask_confirm ('Add missing configuration options? [Y/n] ' ):
115
- fix_missing (conf_file )
116
99
try :
117
100
conf = Configuration (conf_file )
118
101
except Exception as err :
119
- print ('configuration file %s is still incomplete! %s'
120
- % (conf_file , err ))
121
- sys .exit (1 )
102
+ print ('configuration file %s is incomplete! %s' % (conf_file , err ))
103
+
104
+ if not conf :
105
+ if YES == ask_confirm ('Add missing configuration options? [Y/n] ' ):
106
+ fix_missing (conf_file )
107
+ try :
108
+ conf = Configuration (conf_file )
109
+ except Exception as err :
110
+ print ('configuration file %s is still incomplete! %s' %
111
+ (conf_file , err ))
112
+ return 1
113
+ else :
114
+ return 2
115
+
116
+ # Search object attributes for possible paths
117
+
118
+ attrs = []
119
+ ignore_attrs = ['__class__' , '__doc__' , '__module__' ]
120
+ path_re = re .compile ('(' + os .sep + "[\w\._-]*)+$" )
121
+
122
+ _logger = conf .logger
123
+ _logger .info ('Checking configuration paths in %s ...' , conf_file )
124
+ print ('Checking configuration paths in %s ...' % conf_file )
125
+ warnings = 0
126
+ missing_paths = []
127
+ conf_parser = ConfigParser ()
128
+ conf_parser .read ([conf_file ])
129
+ for (name , val ) in conf_parser .items ('GLOBAL' ):
130
+ if not isinstance (val , basestring ):
131
+
132
+ # ignore non-string values
133
+
134
+ continue
135
+ elif not val :
136
+
137
+ # ignore empty string values
138
+
139
+ continue
140
+ elif name .endswith ('_url' ) or val .find ('://' ) != - 1 :
141
+
142
+ # ignore url values
143
+
144
+ continue
145
+ elif val .find ('/emailAddress=' ) != - 1 :
146
+
147
+ # ignore x509 DN values
148
+
149
+ continue
150
+ elif path_re .match (val ):
151
+ path = os .path .normpath (val )
152
+ if os .path .isdir (path ):
153
+ _logger .info ('%s OK: %s is a dir' , name , val )
154
+ elif os .path .isfile (path ):
155
+ _logger .info ('%s OK: %s is a file' , name , val )
156
+ elif os .path .exists (path ):
157
+ _logger .info ('%s OK: %s exists' , name , val )
158
+ else :
159
+ _logger .warning ('%s: %s does not exist!' , name , val )
160
+ print ('* WARNING *: %s: %s does not exist!' % (name , val ))
161
+ if not path in missing_paths :
162
+ missing_paths .append (path )
163
+ warnings += 1
164
+
165
+ _logger .info ('Found %d configuration problem(s)' , warnings )
166
+ print ('Found %d configuration problem(s)' % warnings )
167
+
168
+ if warnings > 0 :
169
+ answer = ask_confirm ('Add missing path(s)? [Y/n/a]: ' ,
170
+ allow_always = True )
171
+ if answer != NO :
172
+
173
+ # Sort missing paths to avoid overlaps resulting in errors
174
+
175
+ missing_paths .sort ()
176
+
177
+ for path in missing_paths :
178
+ if ALWAYS == answer :
179
+
180
+ # 'always' answer reults in default type for all missing entries
181
+
182
+ path_type = None
183
+ elif YES == answer :
184
+ path_type = \
185
+ ask_reply ('Create %s as a (d)irectory, (f)ile or (p)ipe? [D/f/p] '
186
+ % path )
187
+ if not path_type or 'D' == path_type .upper ():
188
+ try :
189
+ os .makedirs (path )
190
+ print ('created directory %s' % path )
191
+ except Exception as err :
192
+ print ('could not create directory %s: %s' %
193
+ (path , err ))
194
+ elif 'F' == path_type .upper ():
195
+ try :
196
+ dirname = os .path .dirname (path )
197
+ if dirname and not os .path .exists (dirname ):
198
+ os .makedirs (dirname )
199
+ print ('created directory %s' % dirname )
200
+ touch_file (path )
201
+ print ('created file %s' % path )
202
+ except Exception as err :
203
+ print ('could not create file %s: %s' % (path , err ))
204
+ elif 'P' == path_type .upper ():
205
+ try :
206
+ dirname = os .path .dirname (path )
207
+ if dirname and not os .path .exists (dirname ):
208
+ os .makedirs (dirname )
209
+ print ('created directory %s' % dirname )
210
+ os .mkfifo (path )
211
+ print ('created pipe %s' % path )
212
+ except Exception as err :
213
+ print ('could not create file %s: %s' % (path , err ))
214
+ print ('completed check of %s: %d warning(s)' % (conf_file , warnings ))
215
+ return warnings
216
+
217
+
218
+ if __name__ == "__main__" :
219
+ if sys .argv [1 :]:
220
+ file_list = sys .argv [1 :]
122
221
else :
123
- sys .exit (1 )
124
-
125
- # Search object attributes for possible paths
126
-
127
- attrs = []
128
- ignore_attrs = ['__class__' , '__doc__' , '__module__' ]
129
- path_re = re .compile ('(' + os .sep + "\w+)+" )
130
-
131
- conf .logger .info ('Checking configuration paths in %s ...' , conf_file )
132
- print ('Checking configuration paths in %s ...' % conf_file )
133
- warnings = 0
134
- missing_paths = []
135
- conf_parser = ConfigParser ()
136
- conf_parser .read ([conf_file ])
137
- for (name , val ) in conf_parser .items ('GLOBAL' ):
138
- if not isinstance (val , basestring ):
139
-
140
- # ignore non-string values
141
-
142
- continue
143
- elif not val :
144
-
145
- # ignore empty string values
146
-
147
- continue
148
- elif path_re .match (val ):
149
- path = os .path .normpath (val )
150
- if os .path .isdir (path ):
151
- conf .logger .info ('%s OK: %s is a dir' , name , val )
152
- elif os .path .isfile (path ):
153
- conf .logger .info ('%s OK: %s is a file' , name , val )
154
- elif os .path .exists (path ):
155
- conf .logger .info ('%s OK: %s exists' , name , val )
222
+ env_conf = os .environ .get ('MIG_CONF' , '' )
223
+ if env_conf :
224
+ conf_file = env_conf
156
225
else :
157
- conf .logger .warning ('%s: %s does not exist!' , name , val )
158
- print ('* WARNING *: %s: %s does not exist!' % (name , val ))
159
- if not path in missing_paths :
160
- missing_paths .append (path )
161
- warnings += 1
162
-
163
- conf .logger .info ('Found %d configuration problem(s)' , warnings )
164
- print ('Found %d configuration problem(s)' % warnings )
165
-
166
- if warnings > 0 :
167
- answer = ask_confirm ('Add missing path(s)? [Y/n/a]: ' ,
168
- allow_always = True )
169
- if answer != NO :
170
-
171
- # Sort missing paths to avoid overlaps resulting in errors
172
-
173
- missing_paths .sort ()
174
-
175
- for path in missing_paths :
176
- if ALWAYS == answer :
177
-
178
- # 'always' answer reults in default type for all missing entries
179
-
180
- path_type = None
181
- elif YES == answer :
182
- path_type = \
183
- ask_reply ('Create %s as a (d)irectory, (f)ile or (p)ipe? [D/f/p] '
184
- % path )
185
- if not path_type or 'D' == path_type .upper ():
186
- try :
187
- os .makedirs (path )
188
- print ('created directory %s' % path )
189
- except Exception as err :
190
- print ('could not create directory %s: %s' % (path ,
191
- err ))
192
- elif 'F' == path_type .upper ():
193
- try :
194
- dirname = os .path .dirname (path )
195
- if dirname and not os .path .exists (dirname ):
196
- os .makedirs (dirname )
197
- print ('created directory %s' % dirname )
198
- touch_file (path )
199
- print ('created file %s' % path )
200
- except Exception as err :
201
- print ('could not create file %s: %s' % (path , err ))
202
- elif 'P' == path_type .upper ():
203
- try :
204
- dirname = os .path .dirname (path )
205
- if dirname and not os .path .exists (dirname ):
206
- os .makedirs (dirname )
207
- print ('created directory %s' % dirname )
208
- os .mkfifo (path )
209
- print ('created pipe %s' % path )
210
- except Exception as err :
211
- print ('could not create file %s: %s' % (path , err ))
212
-
213
- sys .exit (0 )
226
+ # We may be called from MIG_ROOT/mig/server or from MIG_ROOT/bin
227
+ current_dir = os .path .dirname (os .path .abspath (sys .argv [0 ]))
228
+ parent_dir = os .path .dirname (current_dir )
229
+ if os .path .basename (current_dir ) == 'bin' :
230
+ base_dir = parent_dir
231
+ else :
232
+ base_dir = os .path .dirname (parent_dir )
233
+
234
+ conf_file = os .path .join (base_dir , "mig" , "server" ,
235
+ 'MiGserver.conf' )
236
+ if not os .path .isfile (conf_file ):
237
+ print (usage ())
238
+ sys .exit (2 )
239
+ file_list = [conf_file ]
240
+
241
+ retval = 0
242
+ for conf_file in file_list :
243
+ retval += check_conf (conf_file )
244
+ sys .exit (retval )
0 commit comments