Skip to content

Commit 3a2b52b

Browse files
committed
feat: more mixins
1 parent c61a7a9 commit 3a2b52b

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

lib/vsc/utils/script_tools.py

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,39 @@ def _script_name(full_name):
8585
}
8686

8787
CLI_BASE_OPTIONS = {
88-
'disable-locking': ('do NOT protect this script by a file-based lock', None, 'store_true', False),
8988
'dry-run': ('do not make any updates whatsoever', None, 'store_true', False),
9089
'ha': ('high-availability master IP address', None, 'store', None),
9190
}
9291

92+
HA_MIXIN_OPTIONS = {
93+
'ha': ('high-availability master IP address', None, 'store', None),
94+
}
95+
96+
LOCK_MIXIN_OPTIONS = {
97+
'disable-locking': ('do NOT protect this script by a file-based lock', None, 'store_true', False),
98+
'locking-filename':
99+
( 'file that will serve as a lock', None, 'store',
100+
os.path.join(
101+
LOCKFILE_DIR,
102+
LOCKFILE_FILENAME_TEMPLATE % (_script_name(sys.argv[0]),)
103+
)
104+
),
105+
}
106+
93107
TIMESTAMP_MIXIN_OPTIONS = {
94108
"start_timestamp": ("The timestamp form which to start, otherwise use the cached value", None, "store", None),
95-
TIMESTAMP_FILE_OPTION: ("Location to cache the start timestamp", None, "store", None),
109+
"timestamnp_file": ("Location to cache the start timestamp", None, "store", None),
96110
}
97111

98112
NAGIOS_MIXIN_OPTIONS = {
99113
'nagios-report': ('print out nagios information', None, 'store_true', False, 'n'),
100-
'nagios-check-filename': ('filename of where the nagios check data is stored', 'string', 'store',
101-
os.path.join(NAGIOS_CACHE_DIR,
102-
NAGIOS_CACHE_FILENAME_TEMPLATE % (_script_name(sys.argv[0]),))),
114+
'nagios-check-filename':
115+
('filename of where the nagios check data is stored', 'string', 'store',
116+
os.path.join(
117+
NAGIOS_CACHE_DIR,
118+
NAGIOS_CACHE_FILENAME_TEMPLATE % (_script_name(sys.argv[0]),)
119+
)
120+
),
103121
'nagios-check-interval-threshold': ('threshold of nagios checks timing out', 'int', 'store', 0),
104122
'nagios-user': ('user nagios runs as', 'string', 'store', 'nrpe'),
105123
'nagios-world-readable-check': ('make the nagios check data file world readable', None, 'store_true', False),
@@ -161,6 +179,17 @@ def populate_config_parser(parser, options):
161179

162180
return parser
163181

182+
class HAMixin:
183+
"""
184+
A mixin class providing methods for high-availability check.
185+
"""
186+
pass
187+
188+
class LockMixin:
189+
"""
190+
A mixin class providing methods for file locking.
191+
"""
192+
pass
164193

165194
class TimestampMixin:
166195
"""
@@ -223,6 +252,16 @@ def unknown(self, msg):
223252

224253
class CLIBase:
225254

255+
CLI_OPTIONS = {}
256+
257+
def get_options(self):
258+
# Gather options from the current class and its hierarchy
259+
options = {}
260+
for cls in reversed(self.__class__.mro()):
261+
if hasattr(cls, "CLI_OPTIONS"):
262+
options.update(cls.OPTIONS)
263+
return options
264+
226265
def do(self, dryrun=False):
227266
"""
228267
Method to add actual work to do.
@@ -239,18 +278,22 @@ def main(self):
239278
"""
240279
errors = []
241280

242-
argparser = ConfigArgParse()
281+
argparser = ArgParser()
243282
argparser = populate_config_parser(argparser, CLI_BASE_OPTIONS)
244283

284+
if isinstance(self, HAMixin):
285+
argparser = populate_config_parser(argparser, HA_MIXIN_OPTIONS)
286+
245287
if isinstance(self, TimestampMixin):
246-
argperser = populate_config_parser(argparser, TIMESTAMP_MIXIN_OPTIONS)
288+
argparser = populate_config_parser(argparser, TIMESTAMP_MIXIN_OPTIONS)
247289

248290
if isinstance(self, LockMixin):
249291
argparser = populate_config_parser(argparser, LOCK_MIXIN_OPTIONS)
250292

251293
if isinstance(self, NagiosStatusMixin):
252294
argparser = populate_config_parser(argparser, NAGIOS_MIXIN_OPTIONS)
253295

296+
argparser = populate_config_parser(argparser, self.get_options())
254297

255298
self.options = argparser.parse_args()
256299

@@ -321,7 +364,7 @@ def __init__(self, options, config_files=None):
321364
self.options = self.parser.parse_args()
322365

323366

324-
class ExtendedSimpleOption(ConfigOption):
367+
class ExtendedSimpleOption(SimpleOption):
325368
"""
326369
Extends the SimpleOption class to allow other checks to occur at script prologue and epilogue.
327370

0 commit comments

Comments
 (0)