diff --git a/kolla_cli/api/certificate.py b/kolla_cli/api/certificate.py new file mode 100644 index 0000000..4b18580 --- /dev/null +++ b/kolla_cli/api/certificate.py @@ -0,0 +1,37 @@ +# Copyright(c) 2018, Oracle and/or its affiliates. All Rights Reserved. +# +# 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 kolla_cli.i18n as u + +from kolla_cli.api.job import Job +from kolla_cli.common.ansible import actions +from kolla_cli.common.utils import check_arg + + +class CertificateApi(object): + + @staticmethod + def certificate_init(verbose_level=1): + """Certificate Init. + + Creates a self-signed certificate for secure TLS communication. + + :param verbose_level: the higher the number, the more verbose + :type verbose_level: integer + :return: Job object + :rtype: Job + """ + check_arg(verbose_level, u._('Verbose level'), int) + + ansible_job = actions.certificate_init(verbose_level) + return Job(ansible_job) diff --git a/kolla_cli/api/client.py b/kolla_cli/api/client.py index b9e627a..98999cf 100644 --- a/kolla_cli/api/client.py +++ b/kolla_cli/api/client.py @@ -15,6 +15,7 @@ import logging import sys +from kolla_cli.api.certificate import CertificateApi from kolla_cli.api.control_plane import ControlPlaneApi from kolla_cli.api.group import GroupApi from kolla_cli.api.host import HostApi @@ -30,6 +31,7 @@ VERSION = '0.1' class ClientApi( + CertificateApi, ControlPlaneApi, GroupApi, HostApi, diff --git a/kolla_cli/commands/certificate.py b/kolla_cli/commands/certificate.py new file mode 100644 index 0000000..428e5bb --- /dev/null +++ b/kolla_cli/commands/certificate.py @@ -0,0 +1,50 @@ +# Copyright(c) 2018, Oracle and/or its affiliates. All Rights Reserved. +# +# 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 +import traceback + +from cliff.command import Command + +from kolla_cli.api.client import ClientApi +import kolla_cli.i18n as u + +from kolla_cli.commands.exceptions import CommandError + +CLIENT = ClientApi() +LOG = logging.getLogger(__name__) + + +class CertificateInit(Command): + """Generates self-signed certificate""" + + def take_action(self, parsed_args): + verbose_level = self.app.options.verbose_level + + try: + job = CLIENT.certificate_init(verbose_level) + + # wait for job to complete + status = job.wait() + if verbose_level > 2: + LOG.info('\n\n' + 80 * '=') + LOG.info(u._('DEBUG command output:\n{out}') + .format(out=job.get_console_output())) + if status == 0: + LOG.info(u._('Success')) + else: + raise CommandError(u._('Job failed:\n{msg}') + .format(msg=job.get_error_message())) + + except Exception: + raise Exception(traceback.format_exc()) diff --git a/kolla_cli/common/ansible/actions.py b/kolla_cli/common/ansible/actions.py index 5494257..4b3c81b 100644 --- a/kolla_cli/common/ansible/actions.py +++ b/kolla_cli/common/ansible/actions.py @@ -30,6 +30,21 @@ from kolla_cli.common.utils import is_string_true LOG = logging.getLogger(__name__) +def certificate_init(verbose_level=1): + '''Creates a self-signed certificate''' + playbook = AnsiblePlaybook() + playbook_name = 'certificates.yml' + + kolla_home = get_kolla_ansible_home() + playbook.playbook_path = os.path.join(kolla_home, + 'ansible/' + playbook_name) + playbook.verbose_level = verbose_level + playbook.local_only = True + + job = playbook.run() + return job + + def destroy_hosts(hostnames, destroy_type, verbose_level=1, include_data=False, remove_images=False): diff --git a/kolla_cli/common/ansible/playbook.py b/kolla_cli/common/ansible/playbook.py index 771baab..f54e588 100644 --- a/kolla_cli/common/ansible/playbook.py +++ b/kolla_cli/common/ansible/playbook.py @@ -42,10 +42,17 @@ class AnsiblePlaybook(object): serial = False deploy_id = None # type: str inventory = None # type: Inventory + local_only = False def run(self): try: - self.inventory = Inventory.load() + if self.local_only: + # Create a temporary local inventory with only localhost + self.inventory = Inventory() + self.inventory.set_deploy_mode(False) + self.inventory.add_host('localhost') + else: + self.inventory = Inventory.load() inventory_path = self._make_temp_inventory() cmd = self._get_playbook_cmd(inventory_path) self._log_ansible_cmd(cmd, inventory_path) diff --git a/setup.cfg b/setup.cfg index a0f2cac..80ae5f5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -28,6 +28,7 @@ console_scripts = kolla-cli = kolla_cli.shell:main kolla.cli = + certificate_init = kolla_cli.commands.certificate:CertificateInit deploy = kolla_cli.commands.deploy:Deploy dump = kolla_cli.commands.support:Dump group_add = kolla_cli.commands.group:GroupAdd