Skip to content

Commit

Permalink
Code refactoring - move code from EpiphanyEngine to separate class (#194
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
erzetpe authored Apr 1, 2019
1 parent 2252c50 commit c7fba1a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 63 deletions.
22 changes: 3 additions & 19 deletions core/src/epicli/cli/engine/EpiphanyEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
from cli.helpers.config_merger import merge_with_defaults
from cli.engine.aws.AWSConfigBuilder import AWSConfigBuilder
from cli.helpers.yaml_helpers import safe_load_all
from cli.modules.template_generator import TemplateGenerator
from cli.modules.terraform_runner.TerraformRunner import TerraformRunner
from engine.DocumentMerger import DocumentMerger
from engine.SchemaValidator import SchemaValidator
import cli.config.template_generator_config as template_generator_config
import cli.helpers.terraform_file_helper as terraform_file_helper
from cli.engine.AnsibleRunner import AnsibleRunner
from modules.terraform_runner.TerraformRunner import TerraformRunner


class EpiphanyEngine:
Expand Down Expand Up @@ -63,22 +59,10 @@ def run(self):
script_dir = os.path.dirname(__file__)
terraform_build_directory = os.path.join(script_dir, self.BUILD_FOLDER_PATH, self.context, "terraform")

terraform_file_helper.create_terraform_output_dir(terraform_build_directory)

template_generator = TemplateGenerator.TemplateGenerator()

terraform_file_helper.generate_terraform_file([cluster_model], template_generator,
template_generator_config, terraform_build_directory)

terraform_file_helper.generate_terraform_file(infrastructure, template_generator, template_generator_config,
terraform_build_directory)

# todo run terraform
# todo set path to terraform files
tf = TerraformRunner(terraform_build_directory)
tf.init()
tf.plan()
tf.apply(auto_approve=True)
with TerraformRunner(terraform_build_directory, cluster_model, infrastructure) as tf_runner:
tf_runner.run()

# todo validate
print("Running ansible.")
Expand Down
53 changes: 53 additions & 0 deletions core/src/epicli/cli/modules/terraform_runner/Terraform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
import subprocess


def run(command, terraform_command, working_directory, auto_approve=False):
if auto_approve:
status_run = subprocess.run([command, terraform_command, "--auto-approve", working_directory])
else:
status_run = subprocess.run([command, terraform_command, working_directory])

if status_run.returncode != 0:
print(command + " " + terraform_command + " run failed")
else:
print(command + " " + terraform_command + " run successfully.")


class Terraform:

def __init__(self, working_directory=os.path.dirname(__file__)):
self.COMMAND = "terraform"
self.APPLY_COMMAND = "apply"
self.DESTROY_COMMAND = "destroy"
self.PLAN_COMMAND = "plan"
self.INIT_COMMAND = "init"
self.working_directory = working_directory

def run(self):
self.init()

def apply(self, auto_approve=False):
if auto_approve:
status_run = subprocess.run([self.COMMAND, self.APPLY_COMMAND,
"--auto-approve", "-state=" + self.working_directory + "/terraform.tfstate",
self.working_directory])
else:
status_run = subprocess.run([self.COMMAND, self.APPLY_COMMAND,
"-state=" + self.working_directory + "/terraform.tfstate",
self.working_directory])

if status_run.returncode != 0:
print(self.COMMAND + " " + self.APPLY_COMMAND + " run failed")
else:
print(self.COMMAND + " " + self.APPLY_COMMAND + " run successfully.")

def destroy(self, auto_approve=False):
run(self.COMMAND, self.DESTROY_COMMAND, working_directory=self.working_directory,
auto_approve=auto_approve)

def plan(self):
run(self.COMMAND, self.PLAN_COMMAND, working_directory=self.working_directory)

def init(self):
run(self.COMMAND, self.INIT_COMMAND, working_directory=self.working_directory)
70 changes: 26 additions & 44 deletions core/src/epicli/cli/modules/terraform_runner/TerraformRunner.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,32 @@
import os
import subprocess
from config import template_generator_config
from helpers import terraform_file_helper
from modules.template_generator import TemplateGenerator
from modules.terraform_runner.Terraform import Terraform


def run(command, terraform_command, working_directory, auto_approve=False):
if auto_approve:
status_run = subprocess.run([command, terraform_command, "--auto-approve", working_directory])
else:
status_run = subprocess.run([command, terraform_command, working_directory])
class TerraformRunner:

if status_run.returncode != 0:
print(command + " " + terraform_command + " run failed")
else:
print(command + " " + terraform_command + " run successfully.")
def __init__(self, terraform_build_directory, cluster_model, infrastructure):
self.terraform_build_directory = terraform_build_directory
self.terraform = Terraform(terraform_build_directory)
self.template_generator = TemplateGenerator.TemplateGenerator()
self.template_generator_config = template_generator_config
self.cluster_model = [cluster_model]
self.infrastructure = infrastructure

def __enter__(self):
return self

class TerraformRunner:
def __exit__(self, exc_type, exc_value, traceback):
return

def run(self):
terraform_file_helper.create_terraform_output_dir(self.terraform_build_directory)
terraform_file_helper.generate_terraform_file(self.cluster_model, self.template_generator,
self.template_generator_config, self.terraform_build_directory)

terraform_file_helper.generate_terraform_file(self.infrastructure, self.template_generator,
self.template_generator_config, self.terraform_build_directory)

def __init__(self, working_directory=os.path.dirname(__file__)):
self.COMMAND = "terraform"
self.APPLY_COMMAND = "apply"
self.DESTROY_COMMAND = "destroy"
self.PLAN_COMMAND = "plan"
self.INIT_COMMAND = "init"
self.working_directory = working_directory

def apply(self, auto_approve=False):
if auto_approve:
status_run = subprocess.run([self.COMMAND, self.APPLY_COMMAND,
"--auto-approve", "-state=" + self.working_directory + "/terraform.tfstate",
self.working_directory])
else:
status_run = subprocess.run([self.COMMAND, self.APPLY_COMMAND,
"-state=" + self.working_directory + "/terraform.tfstate",
self.working_directory])

if status_run.returncode != 0:
print(self.COMMAND + " " + self.APPLY_COMMAND + " run failed")
else:
print(self.COMMAND + " " + self.APPLY_COMMAND + " run successfully.")

def destroy(self, auto_approve=False):
run(self.COMMAND, self.DESTROY_COMMAND, working_directory=self.working_directory,
auto_approve=auto_approve)

def plan(self):
run(self.COMMAND, self.PLAN_COMMAND, working_directory=self.working_directory)

def init(self):
run(self.COMMAND, self.INIT_COMMAND, working_directory=self.working_directory)
self.terraform.init()
self.terraform.apply(auto_approve=True)

0 comments on commit c7fba1a

Please sign in to comment.