diff --git a/tripleoclient/command.py b/tripleoclient/command.py new file mode 100644 index 000000000..f4fb20681 --- /dev/null +++ b/tripleoclient/command.py @@ -0,0 +1,29 @@ +# Copyright 2017 Red Hat, 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. + +from osc_lib.command import command + + +from tripleoclient import utils + + +class Command(command.Command): + + def run(self, parsed_args): + utils.store_cli_param(self.cmd_name, parsed_args) + super(Command, self).run(parsed_args) + + +class Lister(Command, command.Lister): + pass diff --git a/tripleoclient/tests/test_utils.py b/tripleoclient/tests/test_utils.py index c56ff1223..d7ac3bd61 100644 --- a/tripleoclient/tests/test_utils.py +++ b/tripleoclient/tests/test_utils.py @@ -13,13 +13,13 @@ # under the License. # -from uuid import uuid4 import argparse +import datetime import mock -from mock import call import os.path import tempfile +from uuid import uuid4 from unittest import TestCase import yaml @@ -588,7 +588,8 @@ class TestStoreCliParam(TestCase): def test_fail_to_create_file(self, mock_exists, mock_mkdir): mock_exists.return_value = False mock_mkdir.side_effect = OSError() - self.assertRaises(OSError, utils.store_cli_param, self.args) + command = "undercloud install" + self.assertRaises(OSError, utils.store_cli_param, command, self.args) @mock.patch('os.path.isdir') @mock.patch('os.path.exists') @@ -597,18 +598,31 @@ class TestStoreCliParam(TestCase): mock_isdir.return_value = False self.assertRaises(exceptions.InvalidConfiguration, utils.store_cli_param, - self.args) + "overcloud deploy", self.args) - @mock.patch('six.moves.builtins.open') @mock.patch('os.path.isdir') @mock.patch('os.path.exists') - def test_write_cli_param(self, mock_exists, mock_isdir, mock_open): + def test_write_cli_param(self, mock_exists, mock_isdir): history_path = os.path.join(os.path.expanduser("~"), '.tripleo') mock_exists.return_value = True mock_isdir.return_value = True - utils.store_cli_param(self.args) - expected_call = [call("%s/history" % history_path, 'a')] - mock_open.assert_has_calls(expected_call) + mock_file = mock.mock_open() + + class ArgsFake(object): + def __init__(self): + self.a = 1 + + dt = datetime.datetime(2017, 11, 22) + with mock.patch("six.moves.builtins.open", mock_file): + with mock.patch('tripleoclient.utils.datetime') as mock_date: + mock_date.datetime.now.return_value = dt + utils.store_cli_param("overcloud plan list", ArgsFake()) + + expected_call = [ + mock.call("%s/history" % history_path, 'a'), + mock.call().write('2017-11-22 00:00:00 overcloud-plan-list a=1 \n') + ] + mock_file.assert_has_calls(expected_call, any_order=True) @mock.patch('six.moves.builtins.open') @mock.patch('os.path.isdir') @@ -617,4 +631,4 @@ class TestStoreCliParam(TestCase): mock_exists.return_value = True mock_isdir.return_value = True mock_open.side_effect = IOError() - self.assertRaises(IOError, utils.store_cli_param, self.args) + self.assertRaises(IOError, utils.store_cli_param, "command", self.args) diff --git a/tripleoclient/utils.py b/tripleoclient/utils.py index d9f2ae8e7..648169e95 100644 --- a/tripleoclient/utils.py +++ b/tripleoclient/utils.py @@ -64,9 +64,14 @@ def write_overcloudrc(stack_name, overcloudrcs, config_directory='.'): os.chmod(rcv3path, 0o600) -def store_cli_param(parsed_args): +def store_cli_param(command_name, parsed_args): """write the cli parameters into an history file""" + # The command name is the part after "openstack" with spaces. Switching + # to "-" makes it easier to read. "openstack undercloud install" will be + # stored as "undercloud-install" for example. + command_name = command_name.replace(" ", "-") + history_path = os.path.join(os.path.expanduser("~"), '.tripleo') if not os.path.exists(history_path): try: @@ -83,7 +88,7 @@ def store_cli_param(parsed_args): used_args = ', '.join('%s=%s' % (key, value) for key, value in args.items()) history.write(' '.join([str(datetime.datetime.now()), - used_args])) + str(command_name), used_args, "\n"])) except IOError as e: messages = "Unable to write into TripleO history file: " "{0}, {1}".format(history_path, e) diff --git a/tripleoclient/v1/baremetal.py b/tripleoclient/v1/baremetal.py index eb01be85b..fd43f7af4 100644 --- a/tripleoclient/v1/baremetal.py +++ b/tripleoclient/v1/baremetal.py @@ -21,9 +21,9 @@ import simplejson import time import ironic_inspector_client -from osc_lib.command import command from osc_lib.i18n import _ +from tripleoclient import command from tripleoclient import exceptions from tripleoclient import utils from tripleoclient.workflows import baremetal diff --git a/tripleoclient/v1/container_image.py b/tripleoclient/v1/container_image.py index 48b1d793c..972954b30 100644 --- a/tripleoclient/v1/container_image.py +++ b/tripleoclient/v1/container_image.py @@ -23,7 +23,6 @@ import tempfile from heatclient.common import template_utils from heatclient.common import utils as heat_utils -from osc_lib.command import command from osc_lib import exceptions as oscexc from osc_lib.i18n import _ import requests @@ -33,6 +32,7 @@ import yaml from tripleo_common.image import image_uploader from tripleo_common.image import kolla_builder +from tripleoclient import command from tripleoclient import constants from tripleoclient import utils diff --git a/tripleoclient/v1/overcloud_config.py b/tripleoclient/v1/overcloud_config.py index bfe09b72e..dd5102b3d 100644 --- a/tripleoclient/v1/overcloud_config.py +++ b/tripleoclient/v1/overcloud_config.py @@ -13,11 +13,11 @@ import logging import os -from osc_lib.command import command from osc_lib.i18n import _ - from tripleo_common.utils import config as ooo_config +from tripleoclient import command + class DownloadConfig(command.Command): """Download Overcloud Config""" diff --git a/tripleoclient/v1/overcloud_credentials.py b/tripleoclient/v1/overcloud_credentials.py index 362b9e134..dd99ccd6e 100644 --- a/tripleoclient/v1/overcloud_credentials.py +++ b/tripleoclient/v1/overcloud_credentials.py @@ -12,8 +12,7 @@ import logging -from osc_lib.command import command - +from tripleoclient import command from tripleoclient import utils from tripleoclient.workflows import deployment diff --git a/tripleoclient/v1/overcloud_delete.py b/tripleoclient/v1/overcloud_delete.py index 8e0e01fd3..211a72ec8 100644 --- a/tripleoclient/v1/overcloud_delete.py +++ b/tripleoclient/v1/overcloud_delete.py @@ -15,11 +15,11 @@ import logging -from osc_lib.command import command from osc_lib import exceptions as oscexc from osc_lib.i18n import _ from osc_lib import utils as osc_utils +from tripleoclient import command from tripleoclient import utils from tripleoclient.workflows import plan_management from tripleoclient.workflows import stack_management diff --git a/tripleoclient/v1/overcloud_deploy.py b/tripleoclient/v1/overcloud_deploy.py index 031b17de0..147ce3c98 100644 --- a/tripleoclient/v1/overcloud_deploy.py +++ b/tripleoclient/v1/overcloud_deploy.py @@ -27,12 +27,12 @@ import yaml from heatclient.common import template_utils from heatclient import exc as hc_exc -from osc_lib.command import command from osc_lib import exceptions as oscexc from osc_lib.i18n import _ from swiftclient.exceptions import ClientException from tripleo_common import update +from tripleoclient import command from tripleoclient import constants from tripleoclient import exceptions from tripleoclient import utils @@ -928,7 +928,6 @@ class DeployOvercloud(command.Command): sc_logger.setLevel(logging.CRITICAL) self._validate_args(parsed_args) - utils.store_cli_param(parsed_args) stack = utils.get_stack(self.orchestration_client, parsed_args.stack) diff --git a/tripleoclient/v1/overcloud_execute.py b/tripleoclient/v1/overcloud_execute.py index b90bf3d4a..00034c311 100644 --- a/tripleoclient/v1/overcloud_execute.py +++ b/tripleoclient/v1/overcloud_execute.py @@ -18,7 +18,7 @@ import logging import os.path import re -from osc_lib.command import command +from tripleoclient import command class RemoteExecute(command.Command): diff --git a/tripleoclient/v1/overcloud_image.py b/tripleoclient/v1/overcloud_image.py index 8c57cf395..69020b778 100644 --- a/tripleoclient/v1/overcloud_image.py +++ b/tripleoclient/v1/overcloud_image.py @@ -21,13 +21,13 @@ import re import subprocess import sys -from osc_lib.command import command from osc_lib import exceptions from osc_lib.i18n import _ from osc_lib import utils from prettytable import PrettyTable - from tripleo_common.image import build + +from tripleoclient import command from tripleoclient import utils as plugin_utils diff --git a/tripleoclient/v1/overcloud_netenv_validate.py b/tripleoclient/v1/overcloud_netenv_validate.py index 34f77d8f7..cc9a19031 100644 --- a/tripleoclient/v1/overcloud_netenv_validate.py +++ b/tripleoclient/v1/overcloud_netenv_validate.py @@ -19,11 +19,12 @@ import logging import os import ipaddress -from osc_lib.command import command from osc_lib.i18n import _ import six import yaml +from tripleoclient import command + class ValidateOvercloudNetenv(command.Command): """Validate the network environment file.""" diff --git a/tripleoclient/v1/overcloud_node.py b/tripleoclient/v1/overcloud_node.py index 1d7f54235..7acec90a8 100644 --- a/tripleoclient/v1/overcloud_node.py +++ b/tripleoclient/v1/overcloud_node.py @@ -16,10 +16,10 @@ import argparse import logging -from osc_lib.command import command from osc_lib.i18n import _ from osc_lib import utils +from tripleoclient import command from tripleoclient import constants from tripleoclient.exceptions import InvalidConfiguration from tripleoclient import utils as oooutils diff --git a/tripleoclient/v1/overcloud_parameters.py b/tripleoclient/v1/overcloud_parameters.py index b58f27792..ee99563bb 100644 --- a/tripleoclient/v1/overcloud_parameters.py +++ b/tripleoclient/v1/overcloud_parameters.py @@ -16,9 +16,9 @@ import os import simplejson import yaml -from osc_lib.command import command from osc_lib.i18n import _ +from tripleoclient import command from tripleoclient import exceptions from tripleoclient import utils from tripleoclient.workflows import base diff --git a/tripleoclient/v1/overcloud_plan.py b/tripleoclient/v1/overcloud_plan.py index de3358591..2623c7cfe 100644 --- a/tripleoclient/v1/overcloud_plan.py +++ b/tripleoclient/v1/overcloud_plan.py @@ -14,10 +14,10 @@ import json import logging import os.path -from osc_lib.command import command from osc_lib.i18n import _ from six.moves.urllib import request +from tripleoclient import command from tripleoclient import constants from tripleoclient import exceptions from tripleoclient import utils diff --git a/tripleoclient/v1/overcloud_profiles.py b/tripleoclient/v1/overcloud_profiles.py index e62ab53d2..5f043f268 100644 --- a/tripleoclient/v1/overcloud_profiles.py +++ b/tripleoclient/v1/overcloud_profiles.py @@ -14,9 +14,9 @@ import logging -from osc_lib.command import command from osc_lib.i18n import _ +from tripleoclient import command from tripleoclient import exceptions from tripleoclient import utils diff --git a/tripleoclient/v1/overcloud_raid.py b/tripleoclient/v1/overcloud_raid.py index 7305ea62c..55d9901a3 100644 --- a/tripleoclient/v1/overcloud_raid.py +++ b/tripleoclient/v1/overcloud_raid.py @@ -16,10 +16,10 @@ import logging import os -from osc_lib.command import command from osc_lib.i18n import _ import yaml +from tripleoclient import command from tripleoclient.workflows import baremetal diff --git a/tripleoclient/v1/overcloud_roles.py b/tripleoclient/v1/overcloud_roles.py index 426279d7d..c085aa110 100644 --- a/tripleoclient/v1/overcloud_roles.py +++ b/tripleoclient/v1/overcloud_roles.py @@ -19,9 +19,10 @@ import collections import os import sys -from osc_lib.command import command from tripleo_common.exception import NotFound from tripleo_common.utils import roles as rolesutils + +from tripleoclient import command from tripleoclient.constants import TRIPLEO_HEAT_TEMPLATES diff --git a/tripleoclient/v1/overcloud_support.py b/tripleoclient/v1/overcloud_support.py index e411a1c10..a486c7c56 100644 --- a/tripleoclient/v1/overcloud_support.py +++ b/tripleoclient/v1/overcloud_support.py @@ -15,11 +15,11 @@ import logging -from tripleoclient.workflows import support - -from osc_lib.command import command from osc_lib.i18n import _ +from tripleoclient import command +from tripleoclient.workflows import support + class ReportExecute(command.Command): """Run sosreport on selected servers.""" diff --git a/tripleoclient/v1/overcloud_update.py b/tripleoclient/v1/overcloud_update.py index e1fe33400..74ace2de4 100644 --- a/tripleoclient/v1/overcloud_update.py +++ b/tripleoclient/v1/overcloud_update.py @@ -18,10 +18,10 @@ import os import yaml -from osc_lib.command import command from osc_lib.i18n import _ from oslo_concurrency import processutils +from tripleoclient import command from tripleoclient import constants from tripleoclient import exceptions from tripleoclient import utils as oooutils diff --git a/tripleoclient/v1/undercloud.py b/tripleoclient/v1/undercloud.py index 8e01bfb64..8efe130b2 100644 --- a/tripleoclient/v1/undercloud.py +++ b/tripleoclient/v1/undercloud.py @@ -20,7 +20,8 @@ import logging import subprocess from openstackclient.i18n import _ -from osc_lib.command import command + +from tripleoclient import command from tripleoclient import utils from tripleoclient.v1 import undercloud_config