Skip to content

Commit d24114c

Browse files
authored
Merge pull request #4 from stack-spot/feat/destroy-rollback
add rollback and destroy action
2 parents 38d9249 + 4f1cf4e commit d24114c

File tree

9 files changed

+243
-256
lines changed

9 files changed

+243
-256
lines changed

runtime-destroy-action/.stkignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

runtime-destroy-action/action.yaml

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ metadata:
66
description: runtime-destroy-action
77
version: 0.0.1
88
spec:
9-
type: shell
9+
type: python
1010
docs:
11-
pt-br: docs/pt-br/docs.md
1211
en-us: docs/en-us/docs.md
1312
repository: https://github.com/stack-spot/workflow-stackspot-actions-runtime-selfhosted.git
1413
inputs:
1514
- label: "Features Log Level"
1615
name: features_level_log
1716
type: text
18-
required: true
17+
required: false
18+
1919
- label: "CLIENT ID"
2020
name: client_id
2121
type: text
@@ -56,20 +56,17 @@ spec:
5656
name: run_task_id
5757
type: text
5858
required: true
59-
- label: "Deploy Container url"
60-
name: container_url
61-
type: text
62-
default: "stackspot/runtime-job-destroy:latest"
63-
required: false
59+
6460
- label: "Features Terraform Modules"
6561
name: features_terraform_modules
6662
type: text
6763
required: false
64+
6865
- label: "Path to mount inside the docker"
6966
name: path_to_mount
7067
type: text
71-
default: "."
72-
required: true
68+
required: false
69+
7370
- label: "If Runtimes will allow execution of the local-exec command within terraform"
7471
name: localexec_enabled
7572
type: bool
@@ -79,16 +76,7 @@ spec:
7976
name: tf_log_provider
8077
type: text
8178
required: false
82-
shell:
79+
80+
python:
8381
workdir: .
84-
85-
script:
86-
linux: |
87-
chmod +x {{component_path}}/script.sh
88-
sh {{component_path}}/script.sh
89-
mac: |
90-
chmod +x {{component_path}}/script.sh
91-
sh {{component_path}}/script.sh
92-
windows: |
93-
echo "Not supported"
94-
82+
script: script.py

runtime-destroy-action/docs/pt-br/docs.md

Lines changed: 0 additions & 46 deletions
This file was deleted.

runtime-destroy-action/script.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import os
2+
import sys
3+
import subprocess
4+
import logging
5+
from typing import List
6+
7+
# Configure logging
8+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
9+
10+
11+
STK_IAM_DOMAIN = os.getenv("STK_IAM_DOMAIN", "https://idm.stackspot.com")
12+
STK_RUNTIME_MANAGER_DOMAIN = os.getenv("STK_RUNTIME_MANAGER_DOMAIN", "https://runtime-manager.v1.stackspot.com")
13+
CONTAINER_DESTROY_URL = os.getenv("CONTAINER_DESTROY_URL", "stackspot/runtime-job-destroy:latest")
14+
15+
FEATURES_BASEPATH_TMP = "/tmp/runtime/deploys"
16+
FEATURES_BASEPATH_EBS = "/opt/runtime"
17+
FEATURES_TEMPLATES_FILEPATH = "/app/"
18+
FEATURES_BASEPATH_TERRAFORM = "/root/.asdf/shims/terraform"
19+
20+
21+
def check(result: subprocess.Popen) -> None:
22+
"""
23+
Checks the result of a subprocess execution. If the return code is non-zero,
24+
it logs an error message and exits the program.
25+
26+
Args:
27+
result (subprocess.Popen): The result of the subprocess execution.
28+
"""
29+
result.wait() # Wait for the process to complete
30+
if result.returncode != 0:
31+
logging.error(f"Failed to execute: {result.args}")
32+
logging.error(f"Error output: {result.stderr.read()}")
33+
sys.exit(1)
34+
35+
36+
def run_command(command: List[str]) -> subprocess.Popen:
37+
"""
38+
Runs a command using subprocess.Popen and returns the result.
39+
40+
Args:
41+
command (List[str]): The command to be executed as a list of strings.
42+
43+
Returns:
44+
subprocess.Popen: The result of the command execution.
45+
"""
46+
try:
47+
logging.info(f"Running command: {' '.join(command)}")
48+
# Start the process
49+
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
50+
51+
# Read and print output in real-time
52+
for line in process.stdout:
53+
print(line, end="") # Print each line as it is produced
54+
55+
# Check the result after the process completes
56+
check(process)
57+
return process
58+
except Exception as e:
59+
logging.error(f"Exception occurred while running command: {command}")
60+
logging.error(str(e))
61+
sys.exit(1)
62+
63+
64+
def build_flags(inputs: dict) -> list:
65+
66+
docker_flags: dict = dict(
67+
FEATURES_LEVEL_LOG=inputs.get("features_level_log") or "info",
68+
FEATURES_TERRAFORM_LOGPROVIDER=inputs.get("tf_log_provider") or "info",
69+
FEATURES_RELEASE_LOCALEXEC=inputs.get("localexec_enabled") or "False",
70+
FEATURES_TERRAFORM_MODULES=inputs.get("features_terraform_modules") or '[]',
71+
72+
AWS_ACCESS_KEY_ID=inputs['aws_access_key_id'],
73+
AWS_SECRET_ACCESS_KEY=inputs['aws_secret_access_key'],
74+
AWS_SESSION_TOKEN=inputs['aws_session_token'],
75+
AUTHENTICATE_CLIENT_ID=inputs["client_id"],
76+
AUTHENTICATE_CLIENT_SECRET=inputs["client_key"],
77+
AUTHENTICATE_CLIENT_REALMS=inputs["client_realm"],
78+
REPOSITORY_NAME=inputs["repository_name"],
79+
AWS_REGION=inputs["aws_region"],
80+
81+
AUTHENTICATE_URL=STK_IAM_DOMAIN,
82+
FEATURES_API_MANAGER=STK_RUNTIME_MANAGER_DOMAIN,
83+
FEATURES_BASEPATH_TMP=FEATURES_BASEPATH_TMP,
84+
FEATURES_BASEPATH_EBS=FEATURES_BASEPATH_EBS,
85+
FEATURES_TEMPLATES_FILEPATH=FEATURES_TEMPLATES_FILEPATH,
86+
FEATURES_BASEPATH_TERRAFORM=FEATURES_BASEPATH_TERRAFORM
87+
)
88+
flags = []
89+
for k, v in docker_flags.items():
90+
flags += ["-e", f"{k}={v}"]
91+
92+
return flags
93+
94+
95+
def run(metadata):
96+
inputs: dict = metadata.inputs
97+
run_task_id: str = inputs["run_task_id"]
98+
path_to_mount: str = inputs.get("path_to_mount") or "."
99+
path_to_mount = f"{path_to_mount}:/app-volume"
100+
101+
flags = build_flags(inputs)
102+
cmd = ["docker", "run", "--rm", "-v", path_to_mount] + flags + [
103+
"--entrypoint=/app/stackspot-runtime-job-destroy",
104+
CONTAINER_DESTROY_URL,
105+
"start",
106+
f"--run-task-id={run_task_id}",
107+
]
108+
109+
run_command(cmd)

runtime-destroy-action/templates/script.sh

Lines changed: 0 additions & 30 deletions
This file was deleted.

runtime-rollback-action/.stkignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

runtime-rollback-action/docs/pt-br/docs.md

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)