Merge "Use c-style string interpolation for log messages"

This commit is contained in:
Jenkins 2015-12-22 01:32:27 +00:00 committed by Gerrit Code Review
commit 635ef667a5

View File

@ -42,7 +42,7 @@ def validate_config(config):
for data in config.get('config_files', list()): for data in config.get('config_files', list()):
# Verify required keys exist. # Verify required keys exist.
if not data.viewkeys() >= required_keys: if not data.viewkeys() >= required_keys:
LOG.error('Config is missing required keys: {}'.format(data)) LOG.error("Config is missing required keys: %s", data)
sys.exit(1) sys.exit(1)
@ -57,10 +57,10 @@ def validate_source(data):
if not exists: if not exists:
if data.get('optional'): if data.get('optional'):
LOG.warn('{} does not exist, but is not required'.format(source)) LOG.warn("%s does not exist, but is not required", source)
return False return False
else: else:
LOG.error('The source to copy does not exist: {}'.format(source)) LOG.error("The source to copy does not exist: %s", source)
sys.exit(1) sys.exit(1)
return True return True
@ -105,11 +105,10 @@ def zk_copy_tree(zk, src, dest):
if data: if data:
dest_path = os.path.dirname(dest) dest_path = os.path.dirname(dest)
if not os.path.exists(dest_path): if not os.path.exists(dest_path):
LOG.info('Creating dest parent directory: {}'.format( LOG.info("Creating dest parent directory: %s", dest_path)
dest_path))
os.makedirs(dest_path) os.makedirs(dest_path)
LOG.info('Copying {} to {}'.format(src, dest)) LOG.info("Copying %s to %s", src, dest)
with open(dest, 'w') as df: with open(dest, 'w') as df:
df.write(data.decode("utf-8")) df.write(data.decode("utf-8"))
@ -127,7 +126,7 @@ def copy_files(data):
source = data.get('source') source = data.get('source')
if os.path.exists(dest): if os.path.exists(dest):
LOG.info('Removing existing destination: {}'.format(dest)) LOG.info("Removing existing destination: %s", dest)
if os.path.isdir(dest): if os.path.isdir(dest):
shutil.rmtree(dest) shutil.rmtree(dest)
else: else:
@ -146,18 +145,18 @@ def copy_files(data):
dest_path = os.path.dirname(dest) dest_path = os.path.dirname(dest)
if not os.path.exists(dest_path): if not os.path.exists(dest_path):
LOG.info('Creating dest parent directory: {}'.format(dest_path)) LOG.info("Creating dest parent directory: %s", dest_path)
os.makedirs(dest_path) os.makedirs(dest_path)
if source != source_path: if source != source_path:
# Source is file # Source is file
LOG.info('Copying {} to {}'.format(source, dest)) LOG.info("Copying %s to %s", source, dest)
shutil.copy(source, dest) shutil.copy(source, dest)
else: else:
# Source is a directory # Source is a directory
for src in os.listdir(source_path): for src in os.listdir(source_path):
LOG.info('Copying {} to {}'.format( LOG.info("Copying %s to %s",
os.path.join(source_path, src), dest_path)) os.path.join(source_path, src), dest_path)
if os.path.isdir(src): if os.path.isdir(src):
shutil.copytree(os.path.join(source_path, src), dest_path) shutil.copytree(os.path.join(source_path, src), dest_path)
@ -167,14 +166,13 @@ def copy_files(data):
def set_permissions(data): def set_permissions(data):
def set_perms(file_, uid, guid, perm): def set_perms(file_, uid, guid, perm):
LOG.info('Setting permissions for {}'.format(file_)) LOG.info("Setting permissions for %s", file_)
# Give config file proper perms. # Give config file proper perms.
try: try:
os.chown(file_, uid, gid) os.chown(file_, uid, gid)
os.chmod(file_, perm) os.chmod(file_, perm)
except OSError as e: except OSError as e:
LOG.error('Error while setting permissions for {}: {}'.format( LOG.error("Error while setting permissions for %s: %r", file_, e)
file_, e))
sys.exit(1) sys.exit(1)
dest = data.get('dest') dest = data.get('dest')
@ -185,7 +183,7 @@ def set_permissions(data):
try: try:
user = getpwnam(owner) user = getpwnam(owner)
except KeyError: except KeyError:
LOG.error('The specified user does not exist: {}'.format(owner)) LOG.error("The specified user does not exist: %s", owner)
sys.exit(1) sys.exit(1)
uid = user.pw_uid uid = user.pw_uid
@ -217,18 +215,17 @@ def load_config():
def load_from_file(): def load_from_file():
config_file = '/var/lib/kolla/config_files/config.json' config_file = '/var/lib/kolla/config_files/config.json'
LOG.info('Loading config file at {}'.format(config_file)) LOG.info("Loading config file at %s", config_file)
# Attempt to read config file # Attempt to read config file
with open(config_file) as f: with open(config_file) as f:
try: try:
return json.load(f) return json.load(f)
except ValueError: except ValueError:
LOG.error('Invalid json file found at {}'.format(config_file)) LOG.error("Invalid json file found at %s", config_file)
sys.exit(1) sys.exit(1)
except IOError as e: except IOError as e:
LOG.error('Could not read file {}. Failed with error {}' LOG.error("Could not read file %s: %r", config_file, e)
.format(config_file, e))
sys.exit(1) sys.exit(1)
config = load_from_env() config = load_from_env()
@ -248,7 +245,7 @@ def load_config():
LOG.debug('No files to copy found in config') LOG.debug('No files to copy found in config')
LOG.info('Writing out command to execute') LOG.info('Writing out command to execute')
LOG.debug('Command is: {}'.format(config['command'])) LOG.debug("Command is: %s", config['command'])
# The value from the 'command' key will be written to '/run_command' # The value from the 'command' key will be written to '/run_command'
with open('/run_command', 'w+') as f: with open('/run_command', 'w+') as f:
f.write(config['command']) f.write(config['command'])
@ -256,7 +253,7 @@ def load_config():
def execute_config_strategy(): def execute_config_strategy():
config_strategy = os.environ.get("KOLLA_CONFIG_STRATEGY") config_strategy = os.environ.get("KOLLA_CONFIG_STRATEGY")
LOG.info('Kolla config strategy set to: {}'.format(config_strategy)) LOG.info("Kolla config strategy set to: %s", config_strategy)
if config_strategy == "COPY_ALWAYS": if config_strategy == "COPY_ALWAYS":
load_config() load_config()