From 5030516b7e0b4c7ab042a9081723041a21c71baf Mon Sep 17 00:00:00 2001 From: Attila Farkas <x394097@fi.muni.cz> Date: Wed, 11 Mar 2020 14:44:53 +0100 Subject: [PATCH] move generated files to a single directory --- create.py | 13 +++++++--- modules/file_manager.py | 56 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/create.py b/create.py index 845d52b..1b36e5a 100644 --- a/create.py +++ b/create.py @@ -6,9 +6,9 @@ of virtual machines and network topology. See the documentation for details. import sys -from modules.file_generator import generate_ansible_files # TODO change this to from modules.ansible_generator import generate_playbooks +from modules.ansible_generator import generate_playbooks from modules.vagrant_generator import generate_vagrantfile -from modules.file_manager import open_yaml +from modules.file_manager import open_yaml, prepare_directories from modules.input_argument_parser import parse_input_args from modules.input_file_validator import validate_device_definitions from modules.preprocessing import preprocess @@ -44,6 +44,13 @@ except Exception: print("Preprocessing was not successful.") sys.exit(1) +""" Preparing the directory structure. """ +try: + prepare_directories(device_definitions) +except Exception: + print("Directory structure was not created.") + sys.exit(1) + """ Generating Vagrantfile. """ try: generate_vagrantfile(device_definitions, flags) @@ -53,7 +60,7 @@ except Exception: """ Generating ansible playbooks. """ try: - generate_ansible_files(device_definitions, flags) + generate_playbooks(device_definitions, flags) except Exception: print("Playbooks could not be created.") sys.exit(1) diff --git a/modules/file_manager.py b/modules/file_manager.py index f1eeb97..268e915 100644 --- a/modules/file_manager.py +++ b/modules/file_manager.py @@ -1,8 +1,13 @@ """ This module handles file imports and creations in general. """ import jinja2 +import os +import shutil import yaml +OUTPUT_DIRECTORY = "sandbox" + + def open_yaml(file_name): """ Opens and returns a file from the argument. """ try: @@ -14,6 +19,7 @@ def open_yaml(file_name): finally: input_file.close() + def generate_file(filename, output_string): """ Generates a file from output string. @@ -23,12 +29,13 @@ def generate_file(filename, output_string): """ try: - new_file = open(filename, "w") + new_file = open(OUTPUT_DIRECTORY + "/" + filename, "w") new_file.write(output_string) except IOError: print("Error: cannot write to this location.") raise + def load_template(template_name): """ Returns a loaded jinja2 template. @@ -39,3 +46,50 @@ def load_template(template_name): template_loader = jinja2.FileSystemLoader(searchpath="templates") template_env = jinja2.Environment(loader=template_loader, trim_blocks=True, lstrip_blocks=True) return template_env.get_template(template_name) + +def _create_provisioning_directories(directory, device_definitions): + + try: + os.mkdir(OUTPUT_DIRECTORY + "/" + directory) + os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles") + + if device_definitions["hosts"]: + os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/hosts") + os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/hosts/tasks") + for host in device_definitions["hosts"]: + os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/" + host["name"]) + os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/" + host["name"] + "/tasks") + + if device_definitions["routers"]: + os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/routers") + os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/routers/tasks") + for router in device_definitions["routers"]: + os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/" + router["name"]) + os.mkdir(OUTPUT_DIRECTORY + "/" + directory + "/roles/" + router["name"] + "/tasks") + + except FileExistsError: + pass + except IOError: + print("Could not create directories for provisioning.") + raise + + + +def prepare_directories(device_definitions): + """ Prepares the necessary directory structure. """ + + shutil.rmtree(OUTPUT_DIRECTORY, True) + + try: + os.mkdir(OUTPUT_DIRECTORY) + except IOError: + print("Could not create directory ./" + OUTPUT_DIRECTORY + ".") + raise + + _create_provisioning_directories("base_provisioning", device_definitions) + _create_provisioning_directories("provisioning", device_definitions) + + + + + -- GitLab