/usr/share/pyshared/CedarBackup2/actions/validate.py is in cedar-backup2 2.27.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | # -*- coding: iso-8859-1 -*-
# vim: set ft=python ts=3 sw=3 expandtab:
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# C E D A R
# S O L U T I O N S "Software done right."
# S O F T W A R E
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Copyright (c) 2004-2007,2010 Kenneth J. Pronovici.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# Version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Copies of the GNU General Public License are available from
# the Free Software Foundation website, http://www.gnu.org/.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Author : Kenneth J. Pronovici <pronovic@ieee.org>
# Language : Python 2 (>= 2.7)
# Project : Cedar Backup, release 2
# Purpose : Implements the standard 'validate' action.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
########################################################################
# Module documentation
########################################################################
"""
Implements the standard 'validate' action.
@sort: executeValidate
@author: Kenneth J. Pronovici <pronovic@ieee.org>
"""
########################################################################
# Imported modules
########################################################################
# System modules
import os
import logging
# Cedar Backup modules
from CedarBackup2.util import getUidGid, getFunctionReference
from CedarBackup2.actions.util import createWriter
########################################################################
# Module-wide constants and variables
########################################################################
logger = logging.getLogger("CedarBackup2.log.actions.validate")
########################################################################
# Public functions
########################################################################
#############################
# executeValidate() function
#############################
# pylint: disable=W0613
def executeValidate(configPath, options, config):
"""
Executes the validate action.
This action validates each of the individual sections in the config file.
This is a "runtime" validation. The config file itself is already valid in
a structural sense, so what we check here that is that we can actually use
the configuration without any problems.
There's a separate validation function for each of the configuration
sections. Each validation function returns a true/false indication for
whether configuration was valid, and then logs any configuration problems it
finds. This way, one pass over configuration indicates most or all of the
obvious problems, rather than finding just one problem at a time.
Any reported problems will be logged at the ERROR level normally, or at the
INFO level if the quiet flag is enabled.
@param configPath: Path to configuration file on disk.
@type configPath: String representing a path on disk.
@param options: Program command-line options.
@type options: Options object.
@param config: Program configuration.
@type config: Config object.
@raise ValueError: If some configuration value is invalid.
"""
logger.debug("Executing the 'validate' action.")
if options.quiet:
logfunc = logger.info # info so it goes to the log
else:
logfunc = logger.error # error so it goes to the screen
valid = True
valid &= _validateReference(config, logfunc)
valid &= _validateOptions(config, logfunc)
valid &= _validateCollect(config, logfunc)
valid &= _validateStage(config, logfunc)
valid &= _validateStore(config, logfunc)
valid &= _validatePurge(config, logfunc)
valid &= _validateExtensions(config, logfunc)
if valid:
logfunc("Configuration is valid.")
else:
logfunc("Configuration is not valid.")
########################################################################
# Private utility functions
########################################################################
#######################
# _checkDir() function
#######################
def _checkDir(path, writable, logfunc, prefix):
"""
Checks that the indicated directory is OK.
The path must exist, must be a directory, must be readable and executable,
and must optionally be writable.
@param path: Path to check.
@param writable: Check that path is writable.
@param logfunc: Function to use for logging errors.
@param prefix: Prefix to use on logged errors.
@return: True if the directory is OK, False otherwise.
"""
if not os.path.exists(path):
logfunc("%s [%s] does not exist." % (prefix, path))
return False
if not os.path.isdir(path):
logfunc("%s [%s] is not a directory." % (prefix, path))
return False
if not os.access(path, os.R_OK):
logfunc("%s [%s] is not readable." % (prefix, path))
return False
if not os.access(path, os.X_OK):
logfunc("%s [%s] is not executable." % (prefix, path))
return False
if writable and not os.access(path, os.W_OK):
logfunc("%s [%s] is not writable." % (prefix, path))
return False
return True
################################
# _validateReference() function
################################
def _validateReference(config, logfunc):
"""
Execute runtime validations on reference configuration.
We only validate that reference configuration exists at all.
@param config: Program configuration.
@param logfunc: Function to use for logging errors
@return: True if configuration is valid, false otherwise.
"""
valid = True
if config.reference is None:
logfunc("Required reference configuration does not exist.")
valid = False
return valid
##############################
# _validateOptions() function
##############################
def _validateOptions(config, logfunc):
"""
Execute runtime validations on options configuration.
The following validations are enforced:
- The options section must exist
- The working directory must exist and must be writable
- The backup user and backup group must exist
@param config: Program configuration.
@param logfunc: Function to use for logging errors
@return: True if configuration is valid, false otherwise.
"""
valid = True
if config.options is None:
logfunc("Required options configuration does not exist.")
valid = False
else:
valid &= _checkDir(config.options.workingDir, True, logfunc, "Working directory")
try:
getUidGid(config.options.backupUser, config.options.backupGroup)
except ValueError:
logfunc("Backup user:group [%s:%s] invalid." % (config.options.backupUser, config.options.backupGroup))
valid = False
return valid
##############################
# _validateCollect() function
##############################
def _validateCollect(config, logfunc):
"""
Execute runtime validations on collect configuration.
The following validations are enforced:
- The target directory must exist and must be writable
- Each of the individual collect directories must exist and must be readable
@param config: Program configuration.
@param logfunc: Function to use for logging errors
@return: True if configuration is valid, false otherwise.
"""
valid = True
if config.collect is not None:
valid &= _checkDir(config.collect.targetDir, True, logfunc, "Collect target directory")
if config.collect.collectDirs is not None:
for collectDir in config.collect.collectDirs:
valid &= _checkDir(collectDir.absolutePath, False, logfunc, "Collect directory")
return valid
############################
# _validateStage() function
############################
def _validateStage(config, logfunc):
"""
Execute runtime validations on stage configuration.
The following validations are enforced:
- The target directory must exist and must be writable
- Each local peer's collect directory must exist and must be readable
@note: We currently do not validate anything having to do with remote peers,
since we don't have a straightforward way of doing it. It would require
adding an rsh command rather than just an rcp command to configuration, and
that just doesn't seem worth it right now.
@param config: Program configuration.
@param logfunc: Function to use for logging errors
@return: True if configuration is valid, False otherwise.
"""
valid = True
if config.stage is not None:
valid &= _checkDir(config.stage.targetDir, True, logfunc, "Stage target dir ")
if config.stage.localPeers is not None:
for peer in config.stage.localPeers:
valid &= _checkDir(peer.collectDir, False, logfunc, "Local peer collect dir ")
return valid
############################
# _validateStore() function
############################
def _validateStore(config, logfunc):
"""
Execute runtime validations on store configuration.
The following validations are enforced:
- The source directory must exist and must be readable
- The backup device (path and SCSI device) must be valid
@param config: Program configuration.
@param logfunc: Function to use for logging errors
@return: True if configuration is valid, False otherwise.
"""
valid = True
if config.store is not None:
valid &= _checkDir(config.store.sourceDir, False, logfunc, "Store source directory")
try:
createWriter(config)
except ValueError:
logfunc("Backup device [%s] [%s] is not valid." % (config.store.devicePath, config.store.deviceScsiId))
valid = False
return valid
############################
# _validatePurge() function
############################
def _validatePurge(config, logfunc):
"""
Execute runtime validations on purge configuration.
The following validations are enforced:
- Each purge directory must exist and must be writable
@param config: Program configuration.
@param logfunc: Function to use for logging errors
@return: True if configuration is valid, False otherwise.
"""
valid = True
if config.purge is not None:
if config.purge.purgeDirs is not None:
for purgeDir in config.purge.purgeDirs:
valid &= _checkDir(purgeDir.absolutePath, True, logfunc, "Purge directory")
return valid
#################################
# _validateExtensions() function
#################################
def _validateExtensions(config, logfunc):
"""
Execute runtime validations on extensions configuration.
The following validations are enforced:
- Each indicated extension function must exist.
@param config: Program configuration.
@param logfunc: Function to use for logging errors
@return: True if configuration is valid, False otherwise.
"""
valid = True
if config.extensions is not None:
if config.extensions.actions is not None:
for action in config.extensions.actions:
try:
getFunctionReference(action.module, action.function)
except ImportError:
logfunc("Unable to find function [%s.%s]." % (action.module, action.function))
valid = False
except ValueError:
logfunc("Function [%s.%s] is not callable." % (action.module, action.function))
valid = False
return valid
|