Several stylistic fixes for upgarde system

* use unicode strings
* removed bin helpers which were used for development,
  use python setup.py instead
* import os module instead single path method
* fix imports order in utils.py file

Related to blueprint fuel-upgrade

Change-Id: Id9a906b05463c6bcb3abc51206e448264a920b40
This commit is contained in:
Evgeniy L 2014-03-24 18:06:06 +04:00
parent 4d81ad1c98
commit 1a090d52c7
9 changed files with 21 additions and 79 deletions

View File

@ -1,28 +0,0 @@
#!/usr/bin/env python
# Copyright 2014 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import sys
sys.path[:0] = [os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))]
from fuel_update_downloader.cli import main
if __name__ == '__main__':
main()

View File

@ -24,7 +24,7 @@ from fuel_update_downloader import errors
def handle_exception(exc): def handle_exception(exc):
if isinstance(exc, errors.FuelUpgradeException): if isinstance(exc, errors.FuelUpgradeException):
sys.stderr.write(str(exc) + "\n") sys.stderr.write(exc.message + "\n")
sys.exit(-1) sys.exit(-1)
else: else:
traceback.print_exc(exc) traceback.print_exc(exc)

View File

@ -53,7 +53,7 @@ class Downloader(object):
free_space = calculate_free_space(self.dst_path) free_space = calculate_free_space(self.dst_path)
if free_space < self.required_free_space: if free_space < self.required_free_space:
raise errors.NotEnoughFreeSpace( raise errors.NotEnoughFreeSpace(
'Not enough free space, path - "{0}", ' u'Not enough free space, path - "{0}", '
'free space - "{1}", ' 'free space - "{1}", '
'required free space - "{2}"'.format( 'required free space - "{2}"'.format(
self.dst_path, free_space, self.required_free_space)) self.dst_path, free_space, self.required_free_space))
@ -66,6 +66,6 @@ class Downloader(object):
calculated_checksum = calculate_md5sum(self.dst_path) calculated_checksum = calculate_md5sum(self.dst_path)
if calculated_checksum != self.checksum: if calculated_checksum != self.checksum:
raise errors.WrongChecksum( raise errors.WrongChecksum(
'File "{0}" has wrong checkum, actual ' u'File "{0}" has wrong checkum, actual '
'checksum "{1}" expected checksum "{2}"'.format( 'checksum "{1}" expected checksum "{2}"'.format(
self.dst_path, calculated_checksum, self.checksum)) self.dst_path, calculated_checksum, self.checksum))

View File

@ -22,7 +22,7 @@ from setuptools import setup
def find_requires(): def find_requires():
dir_path = os.path.dirname(os.path.realpath(__file__)) dir_path = os.path.dirname(os.path.realpath(__file__))
requirements = [] requirements = []
with open('{0}/requirements.txt'.format(dir_path), 'r') as reqs: with open(u'{0}/requirements.txt'.format(dir_path), 'r') as reqs:
requirements = reqs.readlines() requirements = reqs.readlines()
return requirements return requirements

View File

@ -1,28 +0,0 @@
#!/usr/bin/env python
# Copyright 2014 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import sys
sys.path[:0] = [os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))]
from fuel_upgrade.cli import main
if __name__ == '__main__':
main()

View File

@ -14,16 +14,14 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from fuel_upgrade.logger import configure_logger
# TODO(eli): move to config
logger = configure_logger('/tmp/file.log')
import argparse import argparse
import sys import sys
import traceback import traceback
from fuel_upgrade.logger import configure_logger
# TODO(eli): move to config
logger = configure_logger('/tmp/file.log')
from fuel_upgrade import errors from fuel_upgrade import errors
from fuel_upgrade.upgrade import PuppetUpgrader from fuel_upgrade.upgrade import PuppetUpgrader
from fuel_upgrade.upgrade import Upgrade from fuel_upgrade.upgrade import Upgrade
@ -31,8 +29,7 @@ from fuel_upgrade.upgrade import Upgrade
def handle_exception(exc): def handle_exception(exc):
if isinstance(exc, errors.FuelUpgradeException): if isinstance(exc, errors.FuelUpgradeException):
err_msg = str(exc) logger.error(exc.message)
logger.error(err_msg)
sys.exit(-1) sys.exit(-1)
else: else:
traceback.print_exc(exc) traceback.print_exc(exc)

View File

@ -15,10 +15,9 @@
# under the License. # under the License.
import logging import logging
import os
import traceback import traceback
from os import path
from fuel_upgrade.utils import exec_cmd from fuel_upgrade.utils import exec_cmd
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -33,8 +32,10 @@ class PuppetUpgrader(object):
puppet_modules_dir = 'puppet_modules' puppet_modules_dir = 'puppet_modules'
def __init__(self, update_path): def __init__(self, update_path):
puppet_modules_path = path.join(update_path, self.puppet_modules_dir) puppet_modules_path = os.path.join(
self.puppet_cmd = 'puppet apply --debug --modulepath={0} -e '.format( update_path, self.puppet_modules_dir)
self.puppet_cmd = u'puppet apply --debug --modulepath={0} -e '.format(
puppet_modules_path) puppet_modules_path)
def upgrade(self): def upgrade(self):
@ -58,7 +59,7 @@ class Upgrade(object):
disable_rollback=False): disable_rollback=False):
logger.debug( logger.debug(
'Create Upgrade object with update path "{0}", ' u'Create Upgrade object with update path "{0}", '
'working directory "{1}", ' 'working directory "{1}", '
'upgrade engine "{2}", ' 'upgrade engine "{2}", '
'disable rollback is "{3}"'.format( 'disable rollback is "{3}"'.format(
@ -78,7 +79,7 @@ class Upgrade(object):
self.upgrade() self.upgrade()
self.after_upgrade() self.after_upgrade()
except Exception as exc: except Exception as exc:
logger.error('Upgrade failed: {0}'.format(exc)) logger.error(u'Upgrade failed: {0}'.format(exc))
logger.error(traceback.format_exc()) logger.error(traceback.format_exc())
if not self.disable_rollback: if not self.disable_rollback:
self.rollback() self.rollback()

View File

@ -30,13 +30,13 @@ def exec_cmd(cmd):
:param cmd: shell command :param cmd: shell command
:raises: ExecutedErrorNonZeroExitCode :raises: ExecutedErrorNonZeroExitCode
""" """
logger.debug('Execute command "{0}"'.format(cmd)) logger.debug(u'Execute command "{0}"'.format(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('Stdout and stderr of command "{0}":'.format(cmd)) logger.debug(u'Stdout and stderr of command "{0}":'.format(cmd))
for line in child.stdout: for line in child.stdout:
logger.debug(line.rstrip()) logger.debug(line.rstrip())
@ -45,7 +45,7 @@ def exec_cmd(cmd):
if return_code != 0: if return_code != 0:
raise errors.ExecutedErrorNonZeroExitCode( raise errors.ExecutedErrorNonZeroExitCode(
'Shell command executed with "{0}" ' u'Shell command executed with "{0}" '
'exit code: {1} '.format(return_code, cmd)) 'exit code: {1} '.format(return_code, cmd))
logger.debug('Command "{0}" successfully executed'.format(cmd)) logger.debug(u'Command "{0}" successfully executed'.format(cmd))

View File

@ -22,7 +22,7 @@ from setuptools import setup
def find_requires(): def find_requires():
dir_path = os.path.dirname(os.path.realpath(__file__)) dir_path = os.path.dirname(os.path.realpath(__file__))
requirements = [] requirements = []
with open('{0}/requirements.txt'.format(dir_path), 'r') as reqs: with open(u'{0}/requirements.txt'.format(dir_path), 'r') as reqs:
requirements = reqs.readlines() requirements = reqs.readlines()
return requirements return requirements