From 670e5b40001efc7bf74465267d80f2478104ee2e Mon Sep 17 00:00:00 2001 From: Pawel Grudzien Date: Mon, 18 Mar 2019 17:18:09 +0100 Subject: [PATCH] Item: #58 Desc: Initial terraform runner commit --- core/src/epicli/cli/engine/EpiphanyEngine.py | 7 ++++ .../terraform_runner/TerraformGenerator.py | 36 +++++++++++++++++++ .../cli/modules/terraform_runner/__init__.py | 0 3 files changed, 43 insertions(+) create mode 100644 core/src/epicli/cli/modules/terraform_runner/TerraformGenerator.py create mode 100644 core/src/epicli/cli/modules/terraform_runner/__init__.py diff --git a/core/src/epicli/cli/engine/EpiphanyEngine.py b/core/src/epicli/cli/engine/EpiphanyEngine.py index 494902ebda..64bbcbc466 100644 --- a/core/src/epicli/cli/engine/EpiphanyEngine.py +++ b/core/src/epicli/cli/engine/EpiphanyEngine.py @@ -10,6 +10,8 @@ from cli.engine.aws.AWSConfigBuilder import AWSConfigBuilder from cli.modules.template_generator import TemplateGenerator +from core.src.epicli.cli.modules.terraform_runner.TerraformGenerator import TerraformRunner + class EpiphanyEngine: @@ -62,6 +64,11 @@ def run(self): terraform_output_file.write(content) # todo run terraform + # todo set path to terraform files + tf = TerraformRunner() + tf.init() + tf.plan() + tf.apply(auto_approve=True) # todo validate diff --git a/core/src/epicli/cli/modules/terraform_runner/TerraformGenerator.py b/core/src/epicli/cli/modules/terraform_runner/TerraformGenerator.py new file mode 100644 index 0000000000..6a747508ba --- /dev/null +++ b/core/src/epicli/cli/modules/terraform_runner/TerraformGenerator.py @@ -0,0 +1,36 @@ +import subprocess + + +class TerraformRunner: + + def __init__(self): + self.command = "terraform" + self.apply_command = "apply" + self.destroy_command = "destroy" + self.plan_command = "plan" + self.init_command = "init" + + def apply(self, auto_approve): + self.run(self.command, self.apply_command, auto_approve=auto_approve) + + def destroy(self, auto_approve): + self.run(self.command, self.destroy_command, auto_approve=auto_approve) + + def plan(self): + self.run(self.command, self.plan_command) + + def init(self): + self.run(self.command, self.init_command) + + @staticmethod + def run(command, terraform_command, auto_approve=False): + + if auto_approve: + status_run = subprocess.run([command, terraform_command, "--auto-approve"]) + else: + status_run = subprocess.run([command, terraform_command]) + + if status_run.returncode != 0: + print(command + " " + terraform_command + " run failed") + else: + print(command + " " + terraform_command + " run successfully.") diff --git a/core/src/epicli/cli/modules/terraform_runner/__init__.py b/core/src/epicli/cli/modules/terraform_runner/__init__.py new file mode 100644 index 0000000000..e69de29bb2