Add each command to the history file

Change-Id: I1e93305426dd94dbbcc8ca70f446dae4a1fa0434
This commit is contained in:
Dougal Matthews 2017-11-21 14:27:47 +00:00
parent f7c0214aee
commit 53d06b7f52
21 changed files with 85 additions and 36 deletions

29
tripleoclient/command.py Normal file
View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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"""

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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):

View File

@ -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

View File

@ -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."""

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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."""

View File

@ -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

View File

@ -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