From 4da6ecdba6a7efdc0c8b7be8507898fad9217bc5 Mon Sep 17 00:00:00 2001 From: Dougal Matthews Date: Tue, 29 Nov 2016 17:23:40 +0000 Subject: [PATCH] Add a new command to get the overcloudrc files If a user deploys via the GUI, then this file wont automatically be created. We can now create it via a Mistral action - this command provides CLI users a way to call that action and save the file. While adding tests a typo was spotted in utils.py, this is also resolved by this patch and covered by the tests. Change-Id: I7808cd2c43a95bbb154ec6c6f77896b97551e354 --- ...-credentials-command-f9e8d7439fee02d5.yaml | 10 +++ setup.cfg | 1 + .../tests/test_overcloud_credentials.py | 88 +++++++++++++++++++ tripleoclient/utils.py | 2 +- tripleoclient/v1/overcloud_credentials.py | 43 +++++++++ 5 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/overcloud-credentials-command-f9e8d7439fee02d5.yaml create mode 100644 tripleoclient/tests/test_overcloud_credentials.py create mode 100644 tripleoclient/v1/overcloud_credentials.py diff --git a/releasenotes/notes/overcloud-credentials-command-f9e8d7439fee02d5.yaml b/releasenotes/notes/overcloud-credentials-command-f9e8d7439fee02d5.yaml new file mode 100644 index 000000000..9079770d8 --- /dev/null +++ b/releasenotes/notes/overcloud-credentials-command-f9e8d7439fee02d5.yaml @@ -0,0 +1,10 @@ +--- +features: + - | + A new command, "openstack overcloud credentials" has been added to create + the overcloudrc and overcloudrc.v3 files for your deployment. This is + particularly useful if the deploy is started from the GUI. +issues: + - | + Fixed an issue with the permissions of the overcloudrc.v3 file. The chmod + call was not being used on it correctly. diff --git a/setup.cfg b/setup.cfg index 50da6554d..af2d7784c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -64,6 +64,7 @@ openstack.tripleoclient.v1 = overcloud_netenv_validate = tripleoclient.v1.overcloud_netenv_validate:ValidateOvercloudNetenv overcloud_container_image_upload = tripleoclient.v1.container_image:UploadImage overcloud_delete = tripleoclient.v1.overcloud_delete:DeleteOvercloud + overcloud_credentials = tripleoclient.v1.overcloud_credentials:OvercloudCredentials overcloud_deploy = tripleoclient.v1.overcloud_deploy:DeployOvercloud overcloud_image_build = tripleoclient.v1.overcloud_image:BuildOvercloudImage overcloud_image_upload = tripleoclient.v1.overcloud_image:UploadOvercloudImage diff --git a/tripleoclient/tests/test_overcloud_credentials.py b/tripleoclient/tests/test_overcloud_credentials.py new file mode 100644 index 000000000..c0e5b63c9 --- /dev/null +++ b/tripleoclient/tests/test_overcloud_credentials.py @@ -0,0 +1,88 @@ +# Copyright 2016 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. +import json +import mock +import shutil +import tempfile + +from tripleoclient.tests.v1 import test_plugin +from tripleoclient.v1 import overcloud_credentials + + +class TestOvercloudCredentials(test_plugin.TestPluginV1): + + def setUp(self): + super(TestOvercloudCredentials, self).setUp() + + self.cmd = overcloud_credentials.OvercloudCredentials(self.app, None) + self.workflow = self.app.client_manager.workflow_engine + self.workflow.action_executions.create.return_value = mock.MagicMock( + output=json.dumps({ + "result": { + "overcloudrc": "OVERCLOUDRC CONTENTS", + "overcloudrc.v3": "OVERCLOUDRC.v3 CONTENTS", + } + }) + ) + + @mock.patch('os.chmod') + def test_ok(self, mock_chmod): + arglist = ['overcloud', ] + verifylist = [ + ('plan', 'overcloud'), + ('directory', '.') + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + with mock.patch("tripleoclient.utils.open", create=True) as m: + self.cmd.take_action(parsed_args) + + self.assertIn(mock.call('./overcloudrc', 'w'), m.call_args_list) + self.assertIn(mock.call('./overcloudrc.v3', 'w'), m.call_args_list) + mock_chmod.assert_has_calls([ + mock.call('./overcloudrc', 384), + mock.call('./overcloudrc.v3', 384)]) + + self.workflow.action_executions.create.assert_called_once_with( + 'tripleo.deployment.overcloudrc', {'container': 'overcloud'}, + run_sync=True, save_result=True) + + @mock.patch('os.chmod') + def test_okay_custom_dir(self, mock_chmod): + + temp = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, temp) + + arglist = ['overcloud', '--directory', temp] + verifylist = [ + ('plan', 'overcloud'), + ('directory', temp) + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + with mock.patch("tripleoclient.utils.open", create=True) as m: + self.cmd.take_action(parsed_args) + + path = "{}/overcloudrc".format(temp) + pathv3 = "{}/overcloudrc.v3".format(temp) + + self.assertIn(mock.call(path, 'w'), m.call_args_list) + self.assertIn(mock.call(pathv3, 'w'), m.call_args_list) + mock_chmod.assert_has_calls([ + mock.call(path, 384), + mock.call(pathv3, 384)]) + + self.workflow.action_executions.create.assert_called_once_with( + 'tripleo.deployment.overcloudrc', {'container': 'overcloud'}, + run_sync=True, save_result=True) diff --git a/tripleoclient/utils.py b/tripleoclient/utils.py index 69fc5a153..a8bddcd26 100644 --- a/tripleoclient/utils.py +++ b/tripleoclient/utils.py @@ -75,7 +75,7 @@ def write_overcloudrc(stack_name, overcloudrcs, config_directory='.'): with open(rcv3path, 'w') as rcv3file: rcv3file.write(overcloudrcs['overcloudrc.v3']) - os.chmod(rcpath, 0o600) + os.chmod(rcv3path, 0o600) def create_tempest_deployer_input(config_name='tempest-deployer-input.conf'): diff --git a/tripleoclient/v1/overcloud_credentials.py b/tripleoclient/v1/overcloud_credentials.py new file mode 100644 index 000000000..362b9e134 --- /dev/null +++ b/tripleoclient/v1/overcloud_credentials.py @@ -0,0 +1,43 @@ +# 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 logging + +from osc_lib.command import command + +from tripleoclient import utils +from tripleoclient.workflows import deployment + + +class OvercloudCredentials(command.Command): + """Create the overcloudrc and overcloudrc.v3 files""" + + log = logging.getLogger(__name__ + ".OvercloudCredentials") + + def get_parser(self, prog_name): + parser = super(OvercloudCredentials, self).get_parser(prog_name) + parser.add_argument('plan', help=("The name of the plan you want to " + "create rc files for.")) + parser.add_argument('--directory', default=".", nargs='?', help=( + "The directory to create the rc files. Defaults to the current " + "directory.")) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)" % parsed_args) + + workflow_engine = self.app.client_manager.workflow_engine + plan = parsed_args.plan + dir_ = parsed_args.directory + + overcloudrcs = deployment.overcloudrc(workflow_engine, container=plan) + utils.write_overcloudrc(plan, overcloudrcs, dir_)