Merge "Ask for password during upgrade"

This commit is contained in:
Jenkins 2014-10-30 13:34:08 +00:00 committed by Gerrit Code Review
commit 7392b6f195
6 changed files with 108 additions and 13 deletions

View File

@ -15,11 +15,12 @@
# under the License.
import argparse
import getpass
import logging
import requests
import sys
from fuel_upgrade.logger import configure_logger
logger = configure_logger('/var/log/fuel_upgrade.log')
from fuel_upgrade import errors
from fuel_upgrade import messages
@ -37,6 +38,8 @@ from fuel_upgrade.engines.openstack import OpenStackUpgrader
from fuel_upgrade.pre_upgrade_hooks import PreUpgradeHookManager
logger = logging.getLogger(__name__)
#: A dict with supported systems.
#: The key is used for system option in CLI.
SUPPORTED_SYSTEMS = {
@ -69,11 +72,13 @@ def handle_exception(exc):
print(messages.nailgun_is_not_running)
elif isinstance(exc, errors.OstfIsNotRunningError):
print(messages.ostf_is_not_running)
elif isinstance(exc, errors.CommandError):
print(exc)
sys.exit(-1)
def parse_args():
def parse_args(args):
"""Parse arguments and return them
"""
parser = argparse.ArgumentParser(
@ -90,8 +95,10 @@ def parse_args():
parser.add_argument(
'--no-rollback', action='store_true',
help='do not rollback in case of errors')
parser.add_argument(
'--password', help="admin user password")
rv = parser.parse_args()
rv = parser.parse_args(args)
# check input systems for compatibility
for uncompatible_systems in UNCOMPATIBLE_SYSTEMS:
@ -129,8 +136,16 @@ def run_upgrade(args):
:param args: argparse object
"""
# Get admin password
if not args.password:
args.password = getpass.getpass('Admin Password: ')
# recheck pasword again
if not args.password:
raise errors.CommandError(messages.no_password_provided)
# Initialize config
config = build_config(args.src)
config = build_config(args.src, args.password)
logger.debug('Configuration data: {0}'.format(config))
# Initialize upgrade engines
@ -155,7 +170,8 @@ def run_upgrade(args):
def main():
"""Entry point
"""
configure_logger('/var/log/fuel_upgrade.log')
try:
run_upgrade(parse_args())
run_upgrade(parse_args(sys.argv[1:]))
except Exception as exc:
handle_exception(exc)

View File

@ -78,13 +78,14 @@ def get_version_from_config(path):
return read_yaml_config(path)['VERSION']['release']
def build_config(update_path):
def build_config(update_path, admin_password):
"""Builds config
:param str update_path: path to upgrade
:param str admin_password: admin user password
:returns: :class:`Config` object
"""
return Config(config(update_path))
return Config(config(update_path, admin_password))
def from_fuel_version(current_version_path, from_version_path):
@ -102,7 +103,7 @@ def from_fuel_version(current_version_path, from_version_path):
return get_version_from_config(current_version_path)
def get_endpoints(astute_config):
def get_endpoints(astute_config, admin_password):
"""Returns services endpoints
:returns: dict where key is the a name of endpoint
@ -115,7 +116,7 @@ def get_endpoints(astute_config):
# 5.0.X releases we didn't have this data
# in astute file
fuel_access = astute_config.get(
'FUEL_ACCESS', {'user': 'admin', 'password': 'admin'})
'FUEL_ACCESS', {'user': 'admin'})
rabbitmq_access = astute_config.get(
'astute', {'user': 'naily', 'password': 'naily'})
rabbitmq_mcollective_access = astute_config.get(
@ -123,7 +124,7 @@ def get_endpoints(astute_config):
keystone_credentials = {
'username': fuel_access['user'],
'password': fuel_access['password'],
'password': admin_password,
'auth_url': 'http://{0}:5000/v2.0/tokens'.format(master_ip),
'tenant_name': 'admin'}
@ -217,10 +218,11 @@ def get_host_system(update_path, new_version):
'/var/www/nailgun', openstack_version, 'centos/x86_64')}}
def config(update_path):
def config(update_path, admin_password):
"""Generates configuration data for upgrade
:param str update_path: path to upgrade
:param str admin_password: admin user password
:retuns: huuuge dict with all required
for ugprade parameters
"""
@ -267,7 +269,7 @@ def config(update_path):
'timeout': 900,
'interval': 3}
endpoints = get_endpoints(astute)
endpoints = get_endpoints(astute, admin_password)
# Configuration data for docker client
docker = {

View File

@ -25,6 +25,10 @@ class CannotRunUpgrade(FuelUpgradeException):
pass
class CommandError(FuelUpgradeException):
pass
class DockerExecutedErrorNonZeroExitCode(FuelUpgradeException):
pass

View File

@ -46,6 +46,11 @@ pre-upgrade checks.
"""
no_password_provided = """
Expecting a password provided via --password or prompted response
"""
health_checker_failed = """
Couldn't start some of the services, try to run upgrade again.
"""

View File

@ -71,7 +71,8 @@ class BaseTestCase(TestCase):
return_value={'ADMIN_NETWORK': {'ipaddress': '0.0.0.0'}})
def fake_config(self, _, __, ___, ____):
update_path = '/tmp/upgrade_path'
conf = config.build_config(update_path)
admin_password = 'admin'
conf = config.build_config(update_path, admin_password)
conf.astute = {
'ADMIN_NETWORK': {

View File

@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
# 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 mock
from fuel_upgrade import errors
from fuel_upgrade import messages
from fuel_upgrade.cli import parse_args
from fuel_upgrade.cli import run_upgrade
from fuel_upgrade.tests.base import BaseTestCase
@mock.patch('fuel_upgrade.cli.CheckerManager', mock.Mock())
@mock.patch('fuel_upgrade.cli.PreUpgradeHookManager', mock.Mock())
@mock.patch('fuel_upgrade.cli.UpgradeManager', mock.Mock())
@mock.patch('fuel_upgrade.cli.build_config')
class TestAdminPassword(BaseTestCase):
default_args = ['host-system', '--src', '/path']
def get_args(self, args):
return parse_args(args)
def test_use_password_arg(self, mbuild_config):
password = '12345678'
args = self.get_args(self.default_args + ['--password', password])
run_upgrade(args)
mbuild_config.assert_called_once_with(
mock.ANY, password
)
@mock.patch('fuel_upgrade.cli.getpass')
def test_ask_for_password(self, mgetpass, mbuild_config):
password = '987654321'
mgetpass.getpass.return_value = password
args = self.get_args(self.default_args)
run_upgrade(args)
mbuild_config.assert_called_once_with(
mock.ANY, password
)
@mock.patch('fuel_upgrade.cli.getpass')
def test_no_password_provided(self, mgetpass, mbuild_config):
password = ''
mgetpass.getpass.return_value = password
with self.assertRaisesRegexp(errors.CommandError,
messages.no_password_provided):
args = self.get_args(self.default_args)
run_upgrade(args)