Pass all logging messages as strings

For unification and easier migration to unicode in the future, all messages
  are passed as str object.

Change-Id: I687df762ea450b33aeaca37983694281ffa6f0d4
Closes-bug: #1394170
This commit is contained in:
Ivan Kliuk 2015-06-14 15:52:53 +03:00
parent 5937d8c8c5
commit 1b63f59601
5 changed files with 52 additions and 52 deletions

View File

@ -122,33 +122,33 @@ class SupervisorClient(object):
def start_all_services(self): def start_all_services(self):
"""Stops all processes """Stops all processes
""" """
logger.info(u'Start all services') logger.info('Start all services')
self.supervisor.startAllProcesses() self.supervisor.startAllProcesses()
def stop_all_services(self): def stop_all_services(self):
"""Stops all processes """Stops all processes
""" """
logger.info(u'Stop all services') logger.info('Stop all services')
self.supervisor.stopAllProcesses() self.supervisor.stopAllProcesses()
def restart_and_wait(self): def restart_and_wait(self):
"""Restart supervisor and wait untill it will be available """Restart supervisor and wait untill it will be available
""" """
logger.info(u'Restart supervisor') logger.info('Restart supervisor')
self.supervisor.restart() self.supervisor.restart()
all_processes = utils.wait_for_true( all_processes = utils.wait_for_true(
lambda: self.get_all_processes_safely() is not None, lambda: self.get_all_processes_safely() is not None,
timeout=self.config.supervisor['restart_timeout']) timeout=self.config.supervisor['restart_timeout'])
logger.debug(u'List of supervisor processes %s', all_processes) logger.debug('List of supervisor processes %s', all_processes)
def start(self, service_name): def start(self, service_name):
"""Start the process under supervisor """Start the process under supervisor
:param str service_name: name of supervisor's process :param str service_name: name of supervisor's process
""" """
logger.debug(u'Start supervisor process %s', service_name) logger.debug('Start supervisor process %s', service_name)
self.supervisor.startProcess(service_name) self.supervisor.startProcess(service_name)
def get_all_processes_safely(self): def get_all_processes_safely(self):
@ -174,7 +174,7 @@ class SupervisorClient(object):
`autostart` - run the service on supervisor start `autostart` - run the service on supervisor start
""" """
logger.info( logger.info(
u'Generate supervisor configs for services %s', services) 'Generate supervisor configs for services %s', services)
for service in services: for service in services:
self.generate_config( self.generate_config(

View File

@ -119,7 +119,7 @@ class DockerUpgrader(UpgradeEngine):
def save_db(self): def save_db(self):
"""Saves postgresql database into the file """Saves postgresql database into the file
""" """
logger.debug(u'Backup database') logger.debug('Backup database')
pg_dump_path = os.path.join(self.working_directory, 'pg_dump_all.sql') pg_dump_path = os.path.join(self.working_directory, 'pg_dump_all.sql')
pg_dump_files = utils.VersionedFile(pg_dump_path) pg_dump_files = utils.VersionedFile(pg_dump_path)
pg_dump_tmp_path = pg_dump_files.next_file_name() pg_dump_tmp_path = pg_dump_files.next_file_name()
@ -137,7 +137,7 @@ class DockerUpgrader(UpgradeEngine):
valid_dumps[self.config.keep_db_backups_count:]) valid_dumps[self.config.keep_db_backups_count:])
else: else:
raise errors.DatabaseDumpError( raise errors.DatabaseDumpError(
u'Failed to make database dump, there ' 'Failed to make database dump, there '
'are no valid database backup ' 'are no valid database backup '
'files, {0}'.format(pg_dump_path)) 'files, {0}'.format(pg_dump_path))
@ -159,7 +159,7 @@ class DockerUpgrader(UpgradeEngine):
self.exec_cmd_in_container( self.exec_cmd_in_container(
container_name, container_name,
u"su postgres -c 'pg_dumpall --clean' > {0}".format( "su postgres -c 'pg_dumpall --clean' > {0}".format(
pg_dump_tmp_path)) pg_dump_tmp_path))
except (errors.ExecutedErrorNonZeroExitCode, except (errors.ExecutedErrorNonZeroExitCode,
errors.CannotFindContainerError) as exc: errors.CannotFindContainerError) as exc:
@ -169,7 +169,7 @@ class DockerUpgrader(UpgradeEngine):
return False return False
logger.debug( logger.debug(
u'Failed to make database dump, ' 'Failed to make database dump, '
'will be used dump from previous run: %s', exc) 'will be used dump from previous run: %s', exc)
return True return True
@ -203,41 +203,41 @@ class DockerUpgrader(UpgradeEngine):
# contain at least one file (default.json) # contain at least one file (default.json)
if len(configs) < 1: if len(configs) < 1:
raise errors.WrongCobblerConfigsError( raise errors.WrongCobblerConfigsError(
u'Cannot find json files in directory {0}'.format( 'Cannot find json files in directory {0}'.format(
self.cobbler_config_path)) self.cobbler_config_path))
for config in configs: for config in configs:
if not utils.check_file_is_valid_json(config): if not utils.check_file_is_valid_json(config):
raise errors.WrongCobblerConfigsError( raise errors.WrongCobblerConfigsError(
u'Invalid json config {0}'.format(config)) 'Invalid json config {0}'.format(config))
def upload_images(self): def upload_images(self):
"""Uploads images to docker """Uploads images to docker
""" """
logger.info(u'Start image uploading') logger.info('Start image uploading')
if not os.path.exists(self.config.images): if not os.path.exists(self.config.images):
logger.warn(u'Cannot find docker images "%s"', self.config.images) logger.warn('Cannot find docker images "%s"', self.config.images)
return return
# NOTE(eli): docker-py binding # NOTE(eli): docker-py binding
# doesn't have equal call for # doesn't have equal call for
# image importing which equals to # image importing which equals to
# `docker load` # `docker load`
utils.exec_cmd(u'docker load -i "{0}"'.format(self.config.images)) utils.exec_cmd('docker load -i "{0}"'.format(self.config.images))
def create_and_start_new_containers(self): def create_and_start_new_containers(self):
"""Create containers in the right order """Create containers in the right order
""" """
logger.info(u'Started containers creation') logger.info('Started containers creation')
graph = self.build_dependencies_graph(self.new_release_containers) graph = self.build_dependencies_graph(self.new_release_containers)
logger.debug(u'Built dependencies graph %s', graph) logger.debug('Built dependencies graph %s', graph)
containers_to_creation = utils.topological_sorting(graph) containers_to_creation = utils.topological_sorting(graph)
logger.debug(u'Resolved creation order %s', containers_to_creation) logger.debug('Resolved creation order %s', containers_to_creation)
for container_id in containers_to_creation: for container_id in containers_to_creation:
container = self.container_by_id(container_id) container = self.container_by_id(container_id)
logger.debug(u'Start container %s', container) logger.debug('Start container %s', container)
links = self.get_container_links(container) links = self.get_container_links(container)
@ -367,7 +367,7 @@ class DockerUpgrader(UpgradeEngine):
params = { params = {
'config_name': container['id'], 'config_name': container['id'],
'service_name': self.make_service_name(container['id']), 'service_name': self.make_service_name(container['id']),
'command': u'docker start -a {0}'.format( 'command': 'docker start -a {0}'.format(
container['container_name']), container['container_name']),
'autostart': autostart 'autostart': autostart
} }
@ -423,7 +423,7 @@ class DockerUpgrader(UpgradeEngine):
containers) containers)
for container in containers_to_stop: for container in containers_to_stop:
logger.debug(u'Stop container: %s', container) logger.debug('Stop container: %s', container)
self.stop_container(container['Id']) self.stop_container(container['Id'])
@ -448,7 +448,7 @@ class DockerUpgrader(UpgradeEngine):
:param container_id: container id :param container_id: container id
""" """
logger.debug(u'Stop container: %s', container_id) logger.debug('Stop container: %s', container_id)
try: try:
self.docker_client.stop( self.docker_client.stop(
@ -460,7 +460,7 @@ class DockerUpgrader(UpgradeEngine):
# Here we just want to make sure that # Here we just want to make sure that
# container was stopped. # container was stopped.
logger.warn( logger.warn(
u'Couldn\'t stop ctonainer, try ' 'Couldn\'t stop ctonainer, try '
'to stop it again: %s', container_id) 'to stop it again: %s', container_id)
self.docker_client.stop( self.docker_client.stop(
container_id, self.config.docker['stop_container_timeout']) container_id, self.config.docker['stop_container_timeout'])
@ -471,7 +471,7 @@ class DockerUpgrader(UpgradeEngine):
:param container: container name :param container: container name
:param params: dict of arguments for container starting :param params: dict of arguments for container starting
""" """
logger.debug(u'Start container "%s": %s', container['Id'], params) logger.debug('Start container "%s": %s', container['Id'], params)
self.docker_client.start(container['Id'], **params) self.docker_client.start(container['Id'], **params)
def create_container(self, image_name, **params): def create_container(self, image_name, **params):
@ -491,7 +491,7 @@ class DockerUpgrader(UpgradeEngine):
new_params = deepcopy(params) new_params = deepcopy(params)
new_params['ports'] = self.get_ports(new_params) new_params['ports'] = self.get_ports(new_params)
logger.debug(u'Create container from image %s: %s', logger.debug('Create container from image %s: %s',
image_name, new_params) image_name, new_params)
def func_create(): def func_create():
@ -531,7 +531,7 @@ class DockerUpgrader(UpgradeEngine):
if version is None: if version is None:
version = self.config.new_version version = self.config.new_version
return u'{0}{1}-{2}'.format( return '{0}{1}-{2}'.format(
self.config.container_prefix, version, container_id) self.config.container_prefix, version, container_id)
def make_image_name(self, image_id): def make_image_name(self, image_id):
@ -540,7 +540,7 @@ class DockerUpgrader(UpgradeEngine):
:param image_id: image id from config file :param image_id: image id from config file
:returns: full name :returns: full name
""" """
return u'{0}{1}_{2}'.format( return '{0}{1}_{2}'.format(
self.config.image_prefix, self.config.image_prefix,
image_id, image_id,
self.config.new_version) self.config.new_version)
@ -587,7 +587,7 @@ class DockerUpgrader(UpgradeEngine):
for container in found_containers: for container in found_containers:
self.stop_container(container['Id']) self.stop_container(container['Id'])
logger.debug(u'Delete container %s', container) logger.debug('Delete container %s', container)
# TODO(eli): refactor it and make retries # TODO(eli): refactor it and make retries
# as a decorator # as a decorator
@ -603,7 +603,7 @@ class DockerUpgrader(UpgradeEngine):
def _get_containers_by_name(self, container_name): def _get_containers_by_name(self, container_name):
return filter( return filter(
lambda c: u'/{0}'.format(container_name) in c['Names'], lambda c: '/{0}'.format(container_name) in c['Names'],
self.docker_client.containers(all=True)) self.docker_client.containers(all=True))
def _delete_containers_for_image(self, image): def _delete_containers_for_image(self, image):
@ -620,10 +620,10 @@ class DockerUpgrader(UpgradeEngine):
all_containers) all_containers)
for container in containers: for container in containers:
logger.debug(u'Try to stop container %s which ' logger.debug('Try to stop container %s which '
'depends on image %s', container['Id'], image) 'depends on image %s', container['Id'], image)
self.docker_client.stop(container['Id']) self.docker_client.stop(container['Id'])
logger.debug(u'Delete container %s which ' logger.debug('Delete container %s which '
'depends on image %s', container['Id'], image) 'depends on image %s', container['Id'], image)
self.docker_client.remove_container(container['Id']) self.docker_client.remove_container(container['Id'])
@ -645,4 +645,4 @@ class DockerInitializer(DockerUpgrader):
self.supervisor.restart_and_wait() self.supervisor.restart_and_wait()
def rollback(self): def rollback(self):
logger.warn(u"DockerInitializer doesn't support rollback") logger.warn("DockerInitializer doesn't support rollback")

View File

@ -55,7 +55,7 @@ class MoveKeysHook(PreUpgradeHookBase):
if not utils.file_exists(self.dst_path): if not utils.file_exists(self.dst_path):
os.makedirs(self.dst_path) os.makedirs(self.dst_path)
container_name = u'{0}{1}-astute'.format( container_name = '{0}{1}-astute'.format(
self.config.container_prefix, self.config.from_version) self.config.container_prefix, self.config.from_version)
try: try:
utils.exec_cmd('docker cp {0}:{1} {2}'.format( utils.exec_cmd('docker cp {0}:{1} {2}'.format(

View File

@ -48,13 +48,13 @@ def exec_cmd(cmd):
:param cmd: shell command :param cmd: shell command
""" """
logger.debug(u'Execute command "%s"', cmd) logger.debug('Execute command "%s"', cmd)
child = subprocess.Popen( child = subprocess.Popen(
cmd, stdout=subprocess.PIPE, cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
shell=True) shell=True)
logger.debug(u'Stdout and stderr of command "%s":', cmd) logger.debug('Stdout and stderr of command "%s":', cmd)
for line in child.stdout: for line in child.stdout:
logger.debug(line.rstrip()) logger.debug(line.rstrip())
@ -82,13 +82,13 @@ def exec_cmd_iterator(cmd):
:returns: generator where yeach item :returns: generator where yeach item
is line from stdout is line from stdout
""" """
logger.debug(u'Execute command "%s"', cmd) logger.debug('Execute command "%s"', cmd)
child = subprocess.Popen( child = subprocess.Popen(
cmd, stdout=subprocess.PIPE, cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
shell=True) shell=True)
logger.debug(u'Stdout and stderr of command "%s":', cmd) logger.debug('Stdout and stderr of command "%s":', cmd)
for line in child.stdout: for line in child.stdout:
logger.debug(line.rstrip()) logger.debug(line.rstrip())
yield line yield line
@ -108,10 +108,10 @@ def _wait_and_check_exit_code(cmd, child):
if exit_code != 0: if exit_code != 0:
raise errors.ExecutedErrorNonZeroExitCode( raise errors.ExecutedErrorNonZeroExitCode(
u'Shell command executed with "{0}" ' 'Shell command executed with "{0}" '
'exit code: {1} '.format(exit_code, cmd)) 'exit code: {1} '.format(exit_code, cmd))
logger.debug(u'Command "%s" successfully executed', cmd) logger.debug('Command "%s" successfully executed', cmd)
def get_request(url): def get_request(url):
@ -155,7 +155,7 @@ def topological_sorting(dep_graph):
if cyclic: if cyclic:
raise errors.CyclicDependenciesError( raise errors.CyclicDependenciesError(
u'Cyclic dependencies error {0}'.format(graph)) 'Cyclic dependencies error {0}'.format(graph))
return sorted_nodes return sorted_nodes
@ -215,7 +215,7 @@ def symlink(source, destination, overwrite=True):
:param overwrite: overwrite a destination if True :param overwrite: overwrite a destination if True
""" """
logger.debug( logger.debug(
u'Symlinking "%s" -> "%s" [overwrite=%d]', 'Symlinking "%s" -> "%s" [overwrite=%d]',
source, destination, overwrite) source, destination, overwrite)
if overwrite or not os.path.exists(destination): if overwrite or not os.path.exists(destination):
@ -234,7 +234,7 @@ def symlink_if_src_exists(source, destination, overwrite=True):
""" """
if not os.path.exists(source): if not os.path.exists(source):
logger.debug( logger.debug(
u'Skip creating symlink, because "%s" does not exists', source) 'Skip creating symlink, because "%s" does not exists', source)
return return
symlink(source, destination, overwrite=overwrite) symlink(source, destination, overwrite=overwrite)
@ -247,7 +247,7 @@ def hardlink(source, destination, overwrite=True):
:param overwrite: overwrite a destination if True :param overwrite: overwrite a destination if True
""" """
logger.debug( logger.debug(
u'Creating hardlink "%s" -> "%s" [overwrite=%d]', 'Creating hardlink "%s" -> "%s" [overwrite=%d]',
source, destination, overwrite) source, destination, overwrite)
if overwrite or not os.path.exists(destination): if overwrite or not os.path.exists(destination):
@ -263,7 +263,7 @@ def remove_if_exists(path):
:param path: path to file for removal :param path: path to file for removal
""" """
if os.path.exists(path): if os.path.exists(path):
logger.debug(u'Remove file "%s"', path) logger.debug('Remove file "%s"', path)
os.remove(path) os.remove(path)
@ -277,7 +277,7 @@ def file_contains_lines(file_path, patterns):
False if file doesn't match one or more patterns False if file doesn't match one or more patterns
""" """
logger.debug( logger.debug(
u'Check if file "%s" matches to pattern "%s"', file_path, patterns) 'Check if file "%s" matches to pattern "%s"', file_path, patterns)
regexps = [re.compile(pattern) for pattern in patterns] regexps = [re.compile(pattern) for pattern in patterns]
@ -351,7 +351,7 @@ def copy_file(source, destination, overwrite=True):
:param overwrite: overwrite destination if True :param overwrite: overwrite destination if True
""" """
logger.debug( logger.debug(
u'Copying "%s" -> "%s" [overwrite=%d]', 'Copying "%s" -> "%s" [overwrite=%d]',
source, destination, overwrite) source, destination, overwrite)
# tranform destinatio to path/to/file, not path/to/dir # tranform destinatio to path/to/file, not path/to/dir
@ -375,7 +375,7 @@ def copy_dir(source, destination, overwrite=True, symlinks=True):
:param symlinks: resolve symlinks if True :param symlinks: resolve symlinks if True
""" """
logger.debug( logger.debug(
u'Copying "%s" -> "%s" [overwrite=%d symlinks=%d]', 'Copying "%s" -> "%s" [overwrite=%d symlinks=%d]',
source, destination, overwrite, symlinks) source, destination, overwrite, symlinks)
if overwrite or not os.path.lexists(destination): if overwrite or not os.path.lexists(destination):
@ -393,7 +393,7 @@ def remove(path, ignore_errors=True):
:param path: a file or directory to remove :param path: a file or directory to remove
:param ignore_errors: ignore some errors and non existense if True :param ignore_errors: ignore some errors and non existense if True
""" """
logger.debug(u'Removing "%s"', path) logger.debug('Removing "%s"', path)
if ignore_errors and not os.path.lexists(path): if ignore_errors and not os.path.lexists(path):
return return
@ -410,7 +410,7 @@ def rmtree(source, ignore_errors=True):
:param str source: path to directory :param str source: path to directory
:param bool ignore_errors: ignores error if True :param bool ignore_errors: ignores error if True
""" """
logger.debug(u'Removing %s', source) logger.debug('Removing %s', source)
if os.path.exists(source): if os.path.exists(source):
shutil.rmtree(source, ignore_errors=ignore_errors) shutil.rmtree(source, ignore_errors=ignore_errors)
@ -424,7 +424,7 @@ def rename(source, destination, overwrite=True):
:param str destination: rename to :param str destination: rename to
""" """
logger.debug( logger.debug(
u'Renaming "%s" -> "%s" [overwrite=%d]', 'Renaming "%s" -> "%s" [overwrite=%d]',
source, destination, overwrite) source, destination, overwrite)
if overwrite or not os.path.exists(destination): if overwrite or not os.path.exists(destination):

View File

@ -65,7 +65,7 @@ class VersionFile(object):
* creates new version yaml file * creates new version yaml file
* and creates symlink to /etc/fuel/version.yaml * and creates symlink to /etc/fuel/version.yaml
""" """
logger.info(u'Switch version file to new version') logger.info('Switch version file to new version')
utils.create_dir_if_not_exists(os.path.dirname( utils.create_dir_if_not_exists(os.path.dirname(
self.dst_new_version_file)) self.dst_new_version_file))
@ -81,5 +81,5 @@ class VersionFile(object):
def switch_to_previous(self): def switch_to_previous(self):
"""Switch version file symlink to previous version """Switch version file symlink to previous version
""" """
logger.info(u'Switch current version file to previous version') logger.info('Switch current version file to previous version')
utils.symlink(self.previous_version_file, self.current_version_file) utils.symlink(self.previous_version_file, self.current_version_file)