| 1 |
# vim: set ft=python ts=4 sw=4 expandtab: |
| 2 |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
| 3 |
# |
| 4 |
# C E D A R |
| 5 |
# S O L U T I O N S "Software done right." |
| 6 |
# S O F T W A R E |
| 7 |
# |
| 8 |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
| 9 |
# |
| 10 |
# Copyright (c) 2004-2007,2010,2015 Kenneth J. Pronovici. |
| 11 |
# All rights reserved. |
| 12 |
# |
| 13 |
# This program is free software; you can redistribute it and/or |
| 14 |
# modify it under the terms of the GNU General Public License, |
| 15 |
# Version 2, as published by the Free Software Foundation. |
| 16 |
# |
| 17 |
# This program is distributed in the hope that it will be useful, |
| 18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 20 |
# |
| 21 |
# Copies of the GNU General Public License are available from |
| 22 |
# the Free Software Foundation website, http://www.gnu.org/. |
| 23 |
# |
| 24 |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
| 25 |
# |
| 26 |
# Author : Kenneth J. Pronovici <[email protected]> |
| 27 |
# Language : Python 3 |
| 28 |
# Project : Cedar Backup, release 3 |
| 29 |
# Purpose : Implements the standard 'validate' action. |
| 30 |
# |
| 31 |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
| 32 |
|
| 33 |
######################################################################## |
| 34 |
# Module documentation |
| 35 |
######################################################################## |
| 36 |
|
| 37 |
""" |
| 38 |
Implements the standard 'validate' action. |
| 39 |
:author: Kenneth J. Pronovici <[email protected]> |
| 40 |
""" |
| 41 |
|
| 42 |
|
| 43 |
######################################################################## |
| 44 |
# Imported modules |
| 45 |
######################################################################## |
| 46 |
|
| 47 |
import logging |
| 48 |
import os |
| 49 |
|
| 50 |
from CedarBackup3.actions.util import createWriter |
| 51 |
from CedarBackup3.util import getFunctionReference, getUidGid |
| 52 |
|
| 53 |
######################################################################## |
| 54 |
# Module-wide constants and variables |
| 55 |
######################################################################## |
| 56 |
|
| 57 |
logger = logging.getLogger("CedarBackup3.log.actions.validate") |
| 58 |
|
| 59 |
|
| 60 |
######################################################################## |
| 61 |
# Public functions |
| 62 |
######################################################################## |
| 63 |
|
| 64 |
############################# |
| 65 |
# executeValidate() function |
| 66 |
############################# |
| 67 |
|
| 68 |
|
| 69 |
def executeValidate(configPath, options, config): # ruff: ignore[unused-function-argument] |
| 70 |
""" |
| 71 |
Executes the validate action. |
| 72 |
|
| 73 |
This action validates each of the individual sections in the config file. |
| 74 |
This is a "runtime" validation. The config file itself is already valid in |
| 75 |
a structural sense, so what we check here that is that we can actually use |
| 76 |
the configuration without any problems. |
| 77 |
|
| 78 |
There's a separate validation function for each of the configuration |
| 79 |
sections. Each validation function returns a true/false indication for |
| 80 |
whether configuration was valid, and then logs any configuration problems it |
| 81 |
finds. This way, one pass over configuration indicates most or all of the |
| 82 |
obvious problems, rather than finding just one problem at a time. |
| 83 |
|
| 84 |
Any reported problems will be logged at the ERROR level normally, or at the |
| 85 |
INFO level if the quiet flag is enabled. |
| 86 |
|
| 87 |
Args: |
| 88 |
configPath (String representing a path on disk): Path to configuration file on disk |
| 89 |
options (Options object): Program command-line options |
| 90 |
config (Config object): Program configuration |
| 91 |
Raises: |
| 92 |
ValueError: If some configuration value is invalid |
| 93 |
""" |
| 94 |
logger.debug("Executing the 'validate' action.") |
| 95 |
if options.quiet: |
| 96 |
logfunc = logger.info # info so it goes to the log |
| 97 |
else: |
| 98 |
logfunc = logger.error # error so it goes to the screen |
| 99 |
valid = True |
| 100 |
valid &= _validateReference(config, logfunc) |
| 101 |
valid &= _validateOptions(config, logfunc) |
| 102 |
valid &= _validateCollect(config, logfunc) |
| 103 |
valid &= _validateStage(config, logfunc) |
| 104 |
valid &= _validateStore(config, logfunc) |
| 105 |
valid &= _validatePurge(config, logfunc) |
| 106 |
valid &= _validateExtensions(config, logfunc) |
| 107 |
if valid: |
| 108 |
logfunc("Configuration is valid.") |
| 109 |
else: |
| 110 |
logfunc("Configuration is not valid.") |
| 111 |
|
| 112 |
|
| 113 |
######################################################################## |
| 114 |
# Private utility functions |
| 115 |
######################################################################## |
| 116 |
|
| 117 |
####################### |
| 118 |
# _checkDir() function |
| 119 |
####################### |
| 120 |
|
| 121 |
|
| 122 |
def _checkDir(path, writable, logfunc, prefix): |
| 123 |
""" |
| 124 |
Checks that the indicated directory is OK. |
| 125 |
|
| 126 |
The path must exist, must be a directory, must be readable and executable, |
| 127 |
and must optionally be writable. |
| 128 |
|
| 129 |
Args: |
| 130 |
path: Path to check |
| 131 |
writable: Check that path is writable |
| 132 |
logfunc: Function to use for logging errors |
| 133 |
prefix: Prefix to use on logged errors |
| 134 |
|
| 135 |
Returns: |
| 136 |
True if the directory is OK, False otherwise |
| 137 |
""" |
| 138 |
if not os.path.exists(path): |
| 139 |
logfunc("%s [%s] does not exist." % (prefix, path)) |
| 140 |
return False |
| 141 |
if not os.path.isdir(path): |
| 142 |
logfunc("%s [%s] is not a directory." % (prefix, path)) |
| 143 |
return False |
| 144 |
if not os.access(path, os.R_OK): |
| 145 |
logfunc("%s [%s] is not readable." % (prefix, path)) |
| 146 |
return False |
| 147 |
if not os.access(path, os.X_OK): |
| 148 |
logfunc("%s [%s] is not executable." % (prefix, path)) |
| 149 |
return False |
| 150 |
if writable and not os.access(path, os.W_OK): |
| 151 |
logfunc("%s [%s] is not writable." % (prefix, path)) |
| 152 |
return False |
| 153 |
return True |
| 154 |
|
| 155 |
|
| 156 |
################################ |
| 157 |
# _validateReference() function |
| 158 |
################################ |
| 159 |
|
| 160 |
|
| 161 |
def _validateReference(config, logfunc): |
| 162 |
""" |
| 163 |
Execute runtime validations on reference configuration. |
| 164 |
|
| 165 |
We only validate that reference configuration exists at all. |
| 166 |
|
| 167 |
Args: |
| 168 |
config: Program configuration |
| 169 |
logfunc: Function to use for logging errors |
| 170 |
|
| 171 |
Returns: |
| 172 |
True if configuration is valid, false otherwise |
| 173 |
""" |
| 174 |
valid = True |
| 175 |
if config.reference is None: |
| 176 |
logfunc("Required reference configuration does not exist.") |
| 177 |
valid = False |
| 178 |
return valid |
| 179 |
|
| 180 |
|
| 181 |
############################## |
| 182 |
# _validateOptions() function |
| 183 |
############################## |
| 184 |
|
| 185 |
|
| 186 |
def _validateOptions(config, logfunc): |
| 187 |
""" |
| 188 |
Execute runtime validations on options configuration. |
| 189 |
|
| 190 |
The following validations are enforced: |
| 191 |
|
| 192 |
- The options section must exist |
| 193 |
- The working directory must exist and must be writable |
| 194 |
- The backup user and backup group must exist |
| 195 |
|
| 196 |
Args: |
| 197 |
config: Program configuration |
| 198 |
logfunc: Function to use for logging errors |
| 199 |
|
| 200 |
Returns: |
| 201 |
True if configuration is valid, false otherwise |
| 202 |
""" |
| 203 |
valid = True |
| 204 |
if config.options is None: |
| 205 |
logfunc("Required options configuration does not exist.") |
| 206 |
valid = False |
| 207 |
else: |
| 208 |
valid &= _checkDir(config.options.workingDir, True, logfunc, "Working directory") |
| 209 |
try: |
| 210 |
getUidGid(config.options.backupUser, config.options.backupGroup) |
| 211 |
except ValueError: |
| 212 |
logfunc("Backup user:group [%s:%s] invalid." % (config.options.backupUser, config.options.backupGroup)) |
| 213 |
valid = False |
| 214 |
return valid |
| 215 |
|
| 216 |
|
| 217 |
############################## |
| 218 |
# _validateCollect() function |
| 219 |
############################## |
| 220 |
|
| 221 |
|
| 222 |
def _validateCollect(config, logfunc): |
| 223 |
""" |
| 224 |
Execute runtime validations on collect configuration. |
| 225 |
|
| 226 |
The following validations are enforced: |
| 227 |
|
| 228 |
- The target directory must exist and must be writable |
| 229 |
- Each of the individual collect directories must exist and must be readable |
| 230 |
|
| 231 |
Args: |
| 232 |
config: Program configuration |
| 233 |
logfunc: Function to use for logging errors |
| 234 |
|
| 235 |
Returns: |
| 236 |
True if configuration is valid, false otherwise |
| 237 |
""" |
| 238 |
valid = True |
| 239 |
if config.collect is not None: |
| 240 |
valid &= _checkDir(config.collect.targetDir, True, logfunc, "Collect target directory") |
| 241 |
if config.collect.collectDirs is not None: |
| 242 |
for collectDir in config.collect.collectDirs: |
| 243 |
valid &= _checkDir(collectDir.absolutePath, False, logfunc, "Collect directory") |
| 244 |
return valid |
| 245 |
|
| 246 |
|
| 247 |
############################ |
| 248 |
# _validateStage() function |
| 249 |
############################ |
| 250 |
|
| 251 |
|
| 252 |
def _validateStage(config, logfunc): |
| 253 |
""" |
| 254 |
Execute runtime validations on stage configuration. |
| 255 |
|
| 256 |
The following validations are enforced: |
| 257 |
|
| 258 |
- The target directory must exist and must be writable |
| 259 |
- Each local peer's collect directory must exist and must be readable |
| 260 |
|
| 261 |
*Note:* We currently do not validate anything having to do with remote peers, |
| 262 |
since we don't have a straightforward way of doing it. It would require |
| 263 |
adding an rsh command rather than just an rcp command to configuration, and |
| 264 |
that just doesn't seem worth it right now. |
| 265 |
|
| 266 |
Args: |
| 267 |
config: Program configuration |
| 268 |
logfunc: Function to use for logging errors |
| 269 |
|
| 270 |
Returns: |
| 271 |
True if configuration is valid, False otherwise |
| 272 |
""" |
| 273 |
valid = True |
| 274 |
if config.stage is not None: |
| 275 |
valid &= _checkDir(config.stage.targetDir, True, logfunc, "Stage target dir ") |
| 276 |
if config.stage.localPeers is not None: |
| 277 |
for peer in config.stage.localPeers: |
| 278 |
valid &= _checkDir(peer.collectDir, False, logfunc, "Local peer collect dir ") |
| 279 |
return valid |
| 280 |
|
| 281 |
|
| 282 |
############################ |
| 283 |
# _validateStore() function |
| 284 |
############################ |
| 285 |
|
| 286 |
|
| 287 |
def _validateStore(config, logfunc): |
| 288 |
""" |
| 289 |
Execute runtime validations on store configuration. |
| 290 |
|
| 291 |
The following validations are enforced: |
| 292 |
|
| 293 |
- The source directory must exist and must be readable |
| 294 |
- The backup device (path and SCSI device) must be valid |
| 295 |
|
| 296 |
Args: |
| 297 |
config: Program configuration |
| 298 |
logfunc: Function to use for logging errors |
| 299 |
|
| 300 |
Returns: |
| 301 |
True if configuration is valid, False otherwise |
| 302 |
""" |
| 303 |
valid = True |
| 304 |
if config.store is not None: |
| 305 |
valid &= _checkDir(config.store.sourceDir, False, logfunc, "Store source directory") |
| 306 |
try: |
| 307 |
createWriter(config) |
| 308 |
except ValueError: |
| 309 |
logfunc("Backup device [%s] [%s] is not valid." % (config.store.devicePath, config.store.deviceScsiId)) |
| 310 |
valid = False |
| 311 |
return valid |
| 312 |
|
| 313 |
|
| 314 |
############################ |
| 315 |
# _validatePurge() function |
| 316 |
############################ |
| 317 |
|
| 318 |
|
| 319 |
def _validatePurge(config, logfunc): |
| 320 |
""" |
| 321 |
Execute runtime validations on purge configuration. |
| 322 |
|
| 323 |
The following validations are enforced: |
| 324 |
|
| 325 |
- Each purge directory must exist and must be writable |
| 326 |
|
| 327 |
Args: |
| 328 |
config: Program configuration |
| 329 |
logfunc: Function to use for logging errors |
| 330 |
|
| 331 |
Returns: |
| 332 |
True if configuration is valid, False otherwise |
| 333 |
""" |
| 334 |
valid = True |
| 335 |
if config.purge is not None: |
| 336 |
if config.purge.purgeDirs is not None: |
| 337 |
for purgeDir in config.purge.purgeDirs: |
| 338 |
valid &= _checkDir(purgeDir.absolutePath, True, logfunc, "Purge directory") |
| 339 |
return valid |
| 340 |
|
| 341 |
|
| 342 |
################################# |
| 343 |
# _validateExtensions() function |
| 344 |
################################# |
| 345 |
|
| 346 |
|
| 347 |
def _validateExtensions(config, logfunc): |
| 348 |
""" |
| 349 |
Execute runtime validations on extensions configuration. |
| 350 |
|
| 351 |
The following validations are enforced: |
| 352 |
|
| 353 |
- Each indicated extension function must exist. |
| 354 |
|
| 355 |
Args: |
| 356 |
config: Program configuration |
| 357 |
logfunc: Function to use for logging errors |
| 358 |
|
| 359 |
Returns: |
| 360 |
True if configuration is valid, False otherwise |
| 361 |
""" |
| 362 |
valid = True |
| 363 |
if config.extensions is not None: |
| 364 |
if config.extensions.actions is not None: |
| 365 |
for action in config.extensions.actions: |
| 366 |
try: |
| 367 |
getFunctionReference(action.module, action.function) |
| 368 |
except ImportError: |
| 369 |
logfunc("Unable to find function [%s.%s]." % (action.module, action.function)) |
| 370 |
valid = False |
| 371 |
except ValueError: |
| 372 |
logfunc("Function [%s.%s] is not callable." % (action.module, action.function)) |
| 373 |
valid = False |
| 374 |
return valid |