Skip to content

Commit

Permalink
Merge pull request #4 from stack-spot/feat/destroy-rollback
Browse files Browse the repository at this point in the history
add rollback and destroy action
  • Loading branch information
pedrohrfz authored Oct 9, 2024
2 parents 38d9249 + 4f1cf4e commit d24114c
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 256 deletions.
1 change: 0 additions & 1 deletion runtime-destroy-action/.stkignore

This file was deleted.

32 changes: 10 additions & 22 deletions runtime-destroy-action/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ metadata:
description: runtime-destroy-action
version: 0.0.1
spec:
type: shell
type: python
docs:
pt-br: docs/pt-br/docs.md
en-us: docs/en-us/docs.md
repository: https://github.com/stack-spot/workflow-stackspot-actions-runtime-selfhosted.git
inputs:
- label: "Features Log Level"
name: features_level_log
type: text
required: true
required: false

- label: "CLIENT ID"
name: client_id
type: text
Expand Down Expand Up @@ -56,20 +56,17 @@ spec:
name: run_task_id
type: text
required: true
- label: "Deploy Container url"
name: container_url
type: text
default: "stackspot/runtime-job-destroy:latest"
required: false

- label: "Features Terraform Modules"
name: features_terraform_modules
type: text
required: false

- label: "Path to mount inside the docker"
name: path_to_mount
type: text
default: "."
required: true
required: false

- label: "If Runtimes will allow execution of the local-exec command within terraform"
name: localexec_enabled
type: bool
Expand All @@ -79,16 +76,7 @@ spec:
name: tf_log_provider
type: text
required: false
shell:

python:
workdir: .

script:
linux: |
chmod +x {{component_path}}/script.sh
sh {{component_path}}/script.sh
mac: |
chmod +x {{component_path}}/script.sh
sh {{component_path}}/script.sh
windows: |
echo "Not supported"
script: script.py
46 changes: 0 additions & 46 deletions runtime-destroy-action/docs/pt-br/docs.md

This file was deleted.

109 changes: 109 additions & 0 deletions runtime-destroy-action/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import os
import sys
import subprocess
import logging
from typing import List

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')


STK_IAM_DOMAIN = os.getenv("STK_IAM_DOMAIN", "https://idm.stackspot.com")
STK_RUNTIME_MANAGER_DOMAIN = os.getenv("STK_RUNTIME_MANAGER_DOMAIN", "https://runtime-manager.v1.stackspot.com")
CONTAINER_DESTROY_URL = os.getenv("CONTAINER_DESTROY_URL", "stackspot/runtime-job-destroy:latest")

FEATURES_BASEPATH_TMP = "/tmp/runtime/deploys"
FEATURES_BASEPATH_EBS = "/opt/runtime"
FEATURES_TEMPLATES_FILEPATH = "/app/"
FEATURES_BASEPATH_TERRAFORM = "/root/.asdf/shims/terraform"


def check(result: subprocess.Popen) -> None:
"""
Checks the result of a subprocess execution. If the return code is non-zero,
it logs an error message and exits the program.
Args:
result (subprocess.Popen): The result of the subprocess execution.
"""
result.wait() # Wait for the process to complete
if result.returncode != 0:
logging.error(f"Failed to execute: {result.args}")
logging.error(f"Error output: {result.stderr.read()}")
sys.exit(1)


def run_command(command: List[str]) -> subprocess.Popen:
"""
Runs a command using subprocess.Popen and returns the result.
Args:
command (List[str]): The command to be executed as a list of strings.
Returns:
subprocess.Popen: The result of the command execution.
"""
try:
logging.info(f"Running command: {' '.join(command)}")
# Start the process
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

# Read and print output in real-time
for line in process.stdout:
print(line, end="") # Print each line as it is produced

# Check the result after the process completes
check(process)
return process
except Exception as e:
logging.error(f"Exception occurred while running command: {command}")
logging.error(str(e))
sys.exit(1)


def build_flags(inputs: dict) -> list:

docker_flags: dict = dict(
FEATURES_LEVEL_LOG=inputs.get("features_level_log") or "info",
FEATURES_TERRAFORM_LOGPROVIDER=inputs.get("tf_log_provider") or "info",
FEATURES_RELEASE_LOCALEXEC=inputs.get("localexec_enabled") or "False",
FEATURES_TERRAFORM_MODULES=inputs.get("features_terraform_modules") or '[]',

AWS_ACCESS_KEY_ID=inputs['aws_access_key_id'],
AWS_SECRET_ACCESS_KEY=inputs['aws_secret_access_key'],
AWS_SESSION_TOKEN=inputs['aws_session_token'],
AUTHENTICATE_CLIENT_ID=inputs["client_id"],
AUTHENTICATE_CLIENT_SECRET=inputs["client_key"],
AUTHENTICATE_CLIENT_REALMS=inputs["client_realm"],
REPOSITORY_NAME=inputs["repository_name"],
AWS_REGION=inputs["aws_region"],

AUTHENTICATE_URL=STK_IAM_DOMAIN,
FEATURES_API_MANAGER=STK_RUNTIME_MANAGER_DOMAIN,
FEATURES_BASEPATH_TMP=FEATURES_BASEPATH_TMP,
FEATURES_BASEPATH_EBS=FEATURES_BASEPATH_EBS,
FEATURES_TEMPLATES_FILEPATH=FEATURES_TEMPLATES_FILEPATH,
FEATURES_BASEPATH_TERRAFORM=FEATURES_BASEPATH_TERRAFORM
)
flags = []
for k, v in docker_flags.items():
flags += ["-e", f"{k}={v}"]

return flags


def run(metadata):
inputs: dict = metadata.inputs
run_task_id: str = inputs["run_task_id"]
path_to_mount: str = inputs.get("path_to_mount") or "."
path_to_mount = f"{path_to_mount}:/app-volume"

flags = build_flags(inputs)
cmd = ["docker", "run", "--rm", "-v", path_to_mount] + flags + [
"--entrypoint=/app/stackspot-runtime-job-destroy",
CONTAINER_DESTROY_URL,
"start",
f"--run-task-id={run_task_id}",
]

run_command(cmd)
30 changes: 0 additions & 30 deletions runtime-destroy-action/templates/script.sh

This file was deleted.

1 change: 0 additions & 1 deletion runtime-rollback-action/.stkignore

This file was deleted.

54 changes: 0 additions & 54 deletions runtime-rollback-action/docs/pt-br/docs.md

This file was deleted.

Loading

0 comments on commit d24114c

Please sign in to comment.