-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code refactoring - move code from EpiphanyEngine to separate class (#194
) * 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
Showing
3 changed files
with
82 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
70
core/src/epicli/cli/modules/terraform_runner/TerraformRunner.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |