From 4d708d3cc016dff4647fe56f5532f2543964fa9d Mon Sep 17 00:00:00 2001 From: erzetpe Date: Mon, 8 Apr 2019 16:09:47 +0200 Subject: [PATCH] Add copy of ansible playbooks to new folder structure (#202) * Item: #58 Desc: Change template to match query parameter from ansible inventory creation * Item: #58 Desc: Changed list creation to more pythonic way * Item: #58 Desc: Fix issue with terraform file helper * Item: #58 Desc: Code refactor and moving code to TerraformRunner class * Item: #58 Desc: Add waiting for ansible inventory creation * Item: #58 Desc: Change name of inventory existing variable * Item: #58 Desc: Fixes with paths to libraries * Item: #59 Desc: Add initial template to generate new inventory * Item: #59 Desc: Change ansible inventory to template * Item: #59 Desc: Refactoring of code * Item: #59 Desc: Add runner execution * Item: #59 Desc: Add working ansible runner code * Item: #59 Desc: Add copy of ansible playbooks --- core/src/epicli/cli/engine/AnsibleRunner.py | 25 +++++++++++++-------- core/src/epicli/cli/helpers/build_saver.py | 13 +++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/core/src/epicli/cli/engine/AnsibleRunner.py b/core/src/epicli/cli/engine/AnsibleRunner.py index 09fba7940f..cd7e59687d 100644 --- a/core/src/epicli/cli/engine/AnsibleRunner.py +++ b/core/src/epicli/cli/engine/AnsibleRunner.py @@ -5,10 +5,13 @@ from cli.engine.AnsibleInventoryCreator import AnsibleInventoryCreator from cli.helpers.Step import Step -from cli.helpers.build_saver import get_inventory_path, get_output_path +from cli.helpers.build_saver import get_inventory_path, get_ansible_path, copy_files_recursively class AnsibleRunner(Step): + + ANSIBLE_PLAYBOOKS_PATH = "/../../../../core/src/ansible/" + def __init__(self, cluster_model, config_docs): super().__init__(__name__) self.cluster_model = cluster_model @@ -32,20 +35,24 @@ def run(self): if if_inventory_exists_and_have_content: continue - inventory = self.inventory_creator.create() + self.inventory_creator.create() time.sleep(10) + src = os.path.dirname(__file__) + AnsibleRunner.ANSIBLE_PLAYBOOKS_PATH + + copy_files_recursively(src, get_ansible_path(self.cluster_model.specification.name)) + # todo run ansible playbooks - for i in range(30): - runner = ansible_runner.run(private_data_dir=get_output_path(), host_pattern="all", + for i in range(10): + runner = ansible_runner.run(private_data_dir=get_ansible_path(self.cluster_model.specification.name), host_pattern="all", inventory=inventory_path, module='raw', module_args='sudo apt-get install -y python-simplejson') - - if runner.status == "successful": - continue + print(runner.status) + if runner.status.lower() == "successful": + break time.sleep(10) - - ansible_runner.run(private_data_dir=get_output_path(), host_pattern="all", + + ansible_runner.run(private_data_dir=get_ansible_path(self.cluster_model.specification.name), host_pattern="all", inventory=inventory_path, module='shell', module_args='whoami') diff --git a/core/src/epicli/cli/helpers/build_saver.py b/core/src/epicli/cli/helpers/build_saver.py index 96cc16ad53..0cb552aa6f 100644 --- a/core/src/epicli/cli/helpers/build_saver.py +++ b/core/src/epicli/cli/helpers/build_saver.py @@ -1,3 +1,4 @@ +import distutils import os from cli.helpers.data_loader import load_template_file, types from cli.helpers.yaml_helpers import dump_all @@ -7,6 +8,7 @@ TERRAFORM_OUTPUT_DIR = 'terraform/' MANIFEST_FILE_NAME = 'manifest.yml' INVENTORY_FILE_NAME = 'inventory' +ANSIBLE_OUTPUT_DIR = 'ansible/' def save_manifest(docs, cluster_name): @@ -58,3 +60,14 @@ def get_terraform_path(cluster_name): if not os.path.exists(terraform_dir): os.makedirs(terraform_dir) return terraform_dir + + +def get_ansible_path(cluster_name): + ansible_dir = os.path.join(get_build_path(cluster_name), ANSIBLE_OUTPUT_DIR) + if not os.path.exists(ansible_dir): + os.makedirs(ansible_dir) + return ansible_dir + + +def copy_files_recursively(src, dst): + distutils.dir_util.copy_tree(src, dst)