Skip to content

Commit

Permalink
skip: update CI 29
Browse files Browse the repository at this point in the history
  • Loading branch information
vit-corp committed Jun 20, 2024
1 parent 4d96781 commit d74b293
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 2 deletions.
2 changes: 1 addition & 1 deletion auto_policy_testing/green/common_resources/random_int.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "random_integer" "this" {
min = 1
max = 104
max = 105
}
2 changes: 1 addition & 1 deletion auto_policy_testing/red/common_resources/random_int.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "random_integer" "this" {
min = 1
max = 104
max = 106
}
119 changes: 119 additions & 0 deletions auto_policy_testing/scripts/terraform_infra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import os
import sys
import json
import timer
import traceback
import subprocess


# control text style
class Color:
# text style
RESET = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

# text color
BLACK = '\033[30m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'


def auto_approve(path, verbosity=False, up=False, remove=False):
command = f"cd {path} ; terraform {'apply' if up else 'destroy'} -auto-approve -no-color {'> /dev/null' if not verbosity else ''}"
result = subprocess.run(command, shell=True, capture_output=True, text=True, check=True)
state_list = subprocess.run(f"cd {path} ; terraform state list", shell=True, capture_output=True, text=True, check=True)
print("list of active resources:\n", state_list.stdout if state_list.stdout else "No active resources")
if up is not True and remove and result.returncode == 0:
subprocess.run(f"cd {path} ; rm -rfv .terraform* terraform.tfstate*", shell=True, capture_output=True, text=True, check=True)


def tf_up(resource, path, cloud, infra_color):
print(f"\nTerraform apply '{cloud.lower()}.{resource}...'\n")
tf_up_subprocess_result = green_red_infrastructures_up_down(
path,
infra_color,
up=True)
return tf_up_subprocess_result


def tf_down(resource, path, cloud, infra_color):
print(f"\nTerraform destroy '{cloud.lower()}.{resource}...'\n")
tf_down_subprocess_result = green_red_infrastructures_up_down(
path,
infra_color,
down=True,
remove=True)
return tf_down_subprocess_result


def common_tf_up(rulepack_testing_path, infra_color):
print("\nTerraform apply common resources\n")
tf_up_common_subprocess_result = green_red_infrastructures_up_down(
os.path.join(rulepack_testing_path, infra_color, 'common_resources'),
infra_color,
up=True)
return tf_up_common_subprocess_result


def common_tf_down(rulepack_testing_path, infra_color):
print("\nTerraform destroy common resources\n")
tf_down_common_subprocess_result = green_red_infrastructures_up_down(
os.path.join(rulepack_testing_path, infra_color, 'common_resources'),
infra_color,
down=True,
remove=True)
return tf_down_common_subprocess_result


# Create/destroy terraform green/red infrastructure
@timer.time_decorator
def green_red_infrastructures_up_down(path, infra_color, up=False, down=False, verbosity=False, remove=False):
try:
color = infra_color

print(f"{Color.GREEN + 3*'+' + Color.YELLOW + ' Up' if up else Color.RED + 3*'-' + Color.YELLOW + ' Down'}"
f"{Color.RED if color == 'red' else Color.GREEN} {color} {path}"
f"{'' if not verbosity else 'Full path: ' + Color.YELLOW + path} {Color.RESET}")
if path is not None:
if up:
command = f"cd {path} ; terraform init -no-color {'> /dev/null' if not verbosity else ''}; \
terraform validate -no-color {'> /dev/null' if not verbosity else ''}; \
terraform fmt -no-color {'> /dev/null' if not verbosity else ''}"
subprocess.run([command], shell=True, capture_output=True, text=True, check=True)
auto_approve(path, verbosity=verbosity, up=up)
elif down:
auto_approve(path, verbosity=verbosity, remove=remove)
except subprocess.CalledProcessError as error:
return False, error.stderr
return True, None


def output(path, policy_name, resource):
# Run the Terraform command to get output in JSON format
try:
process = subprocess.Popen(['terraform', 'output', '-json'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=f"{path}")
output, error = process.communicate()

# Check if there's any error
if process.returncode != 0:
raise RuntimeError(f"Terraform command failed with error: {error.decode()}")

# Load the JSON output into a variable
terraform_output = json.loads(output.decode())
if terraform_output:
terraform_output = terraform_output[next(iter(terraform_output))]['value']
else:
print(f"Terraform output is empty for policy: {policy_name}")
sys.exit(1)
resource_id = ""
if policy_name in terraform_output:
resource_id = terraform_output[policy_name]
elif resource in terraform_output:
resource_id = terraform_output[resource]
return resource_id
except Exception:
print(f"An exception occurred with policy {policy_name}")
traceback.print_exc()
sys.exit(1)

0 comments on commit d74b293

Please sign in to comment.