Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions runtime-unified-action/.stkignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Este arquivo pode ser usado para ignorar arquivos e pastas ao publicar um artefato.
99 changes: 99 additions & 0 deletions runtime-unified-action/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
schema-version: v3
kind: action
metadata:
name: runtime-unified-action
display-name: runtime-unified-action
description: Descreva sua action explicando o propósito.
version: 0.0.1
spec:
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: "CLIENT ID"
name: client_id
type: text
required: true

- label: "CLIENT KEY"
name: client_key
type: text
required: true

- label: "CLIENT REALM"
name: client_realm
type: text
required: true

- label: "Git Repository Name"
name: repository_name
type: text
required: true

- label: "AWS ACCESS KEY ID from console"
name: aws_access_key_id
type: text
required: true

- label: "AWS SECRET ACCESS KEY from console"
name: aws_secret_access_key
type: text
required: true

- label: "AWS SESSION TOKEN from console"
name: aws_session_token
type: text
required: true

- label: "AWS REGION"
name: aws_region
type: text
required: true

- label: "Terraform parallelism order"
name: tf_parallelism
type: text
default: "10"
required: false

- label: "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: false

- label: "If Runtimes will allow execution of the local-exec command within terraform"
name: localexec_enabled
type: bool
required: false

- label: "Level tf log provider - info, debug, warn or trace"
name: tf_log_provider
type: text
required: false

- label: "Features Log Level"
name: features_level_log
type: text
required: false

- label: "Base Path Output"
name: base_path_output
type: text
required: false

- label: "Run ID that contains its tasks"
name: run_id
type: text
required: true

python:
workdir: .
script: script.py
54 changes: 54 additions & 0 deletions runtime-unified-action/docs/en-us/docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!--
******************************************

- THIS IS AN EXAMPLE OF HOW TO FILL OUT YOUR DOCUMENTATION OF CONTENT.

- FILL OUT THE TEMPLATE BELOW WITH YOUR INFORMATION SO OTHER PEOPLE CAN USE IT. THIS DOCUMENTATION WILL APPEAR ON THE SECTION OF THE STACKSPOT PORTAL.

******************************************
-->
## Action name

<!-- Write concisely describing your Action. -->

## Requirements

<!--
[This is a guideline; delete this content and write your information outside this markup. <!-- ]

- Describe the requirements that the user needs to know before using the Action.
-->

## Usage

<!--
[This is a guideline; delete this content and write your information outside this markup. <!-- ]

Add the steps for the user to use your Action:

- What are the inputs?
- Which methods should we know?
- What are the resources?
- Add the Action dependencies, if necessary.

Example:
On your application’s folder, run the **action-doc-template** action and follow the instructions:
1. Execute the command:
`
stk run action /Users/Home/action-doc-template
`
-->

## Release Notes

<!--
[This is a guideline; delete this content and write your information outside this markup. <!-- ]

This section is only necessary if you publish a new Action version. Add what was changed, fixed, and the new features.

Example:
### action-doc-template v1.0.0

#### Features
Added new templates
-->
54 changes: 54 additions & 0 deletions runtime-unified-action/docs/pt-br/docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!--
******************************************

- ESTE É APENAS UM EXEMPLO DE COMO PREENCHER A DOCUMENTAÇÃO DO SEU CONTEUDO.

- PREENCHA O TEMPLATE COM AS INFORMAÇÕES DO SEU CONTEUDO PARA QUE OUTROS USUÁRIO CONSIGAM UTILIZÁ-LO. ESSA DOCUMENTAÇÃO SERÁ EXPOSTA NA PÁGINA DO CONTEUDO NO PORTAL DA STACKSPOT.

******************************************
-->
## Nome Action

<!-- Escreva uma descrição clara e breve sobre a sua Action. -->

## Pré-requisitos

<!--
[Isto é uma orientação, apague essa o conteúdo e escreva suas informações fora desta marcação <!-- ]

- Descreva quais os requisitos que o usuário precisa saber antes de usar a Action.
-->

## Uso

<!--
[Isto é uma orientação, apague essa o conteúdo e escreva suas informações fora desta marcação <!-- ]

Descreva as etapas para o usuário utilizar esta Action:

- Quais as entradas
- Quais os métodos usar
- Quais os recursos
- E se necessário, adicione as dependências de sua Action.

Exemplo:
Na pasta do seu aplicativo, execute a **action-doc-template** para preencher os arquivos abaixo:
1. Execute o comando:
`
stk run action /Users/Home/action-doc-template
`
-->

## Release Notes

<!--
[Isto é uma orientação, apague essa o conteúdo e escreva suas informações fora desta marcação <!-- ]

Esta seção só é necessária se você publicar uma nova versão da Action. Apenas adicione o que foi modificado ou adicionado.

Exemplo:
### action-doc-template v1.0.0

#### Features
Novos templates foram adicionados.
-->
126 changes: 126 additions & 0 deletions runtime-unified-action/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
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_UNIFIED_URL = os.getenv(
"CONTAINER_UNIFIED_URL", "stackspot/runtime-job-unified: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:

TF_PARALLELISM = f"-parallelism={inputs.get('tf_parallelism') or '10'}"

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,
TF_CLI_ARGS_apply=TF_PARALLELISM,
TF_CLI_ARGS_plan=TF_PARALLELISM,
TF_CLI_ARGS_destroy=TF_PARALLELISM,
)
flags = []
for k, v in docker_flags.items():
flags += ["-e", f"{k}={v}"]

return flags


def run(metadata):
inputs: dict = metadata.inputs
run_id: str = inputs["run_id"]
base_path_output: str = inputs.get("base_path_output") or "."
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",
CONTAINER_UNIFIED_URL,
"start",
f"--run-id={run_id}",
f"--base-path-output={base_path_output}",
]
)

run_command(cmd)
11 changes: 9 additions & 2 deletions script.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,18 @@ def run_tasks(file_tasks: str, run_action: RunAction):
IAC_SELF_HOSTED=lambda **i: run_action("runtime-iac-action", **i),
DEPLOY_SELF_HOSTED=lambda **i: run_action("runtime-deploy-action", **i),
DESTROY_SELF_HOSTED=lambda **i: run_action("runtime-destroy-action", **i),
UNIFIED_IAC=lambda **i: run_action("runtime-unified-action", **i),
UNIFIED_DEPLOY=lambda **i: run_action("runtime-unified-action", **i),
UNIFIED_DESTROY=lambda **i: run_action("runtime-unified-action", **i),
)

for t in data.get("tasks") or []:
runner = task_runners.get(t["taskType"])
runner and runner(run_task_id=t["runTaskId"])
task_type = t["taskType"]
runner = task_runners.get(task_type)
if "UNIFIED" in task_type:
runner and runner(run_id=data.get("runId"))
else:
runner and runner(run_task_id=t["runTaskId"])


def run(metadata):
Expand Down
Loading