Skip to content

Commit

Permalink
Merge pull request #27 from stack-spot/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
kelsoncarmozinezup authored Oct 18, 2024
2 parents 8ec63e3 + aeb4a56 commit 94ccd35
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 30 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# runtime-manager-action

[![Action test Ubuntu](https://github.com/stack-spot/runtime-manager-action/actions/workflows/action-test-ubuntu.yaml/badge.svg)](https://github.com/stack-spot/runtime-manager-action/actions/workflows/action-test-ubuntu.yaml) [![Action test MacOS](https://github.com/stack-spot/runtime-manager-action/actions/workflows/action-test-macos.yaml/badge.svg)](https://github.com/stack-spot/runtime-manager-action/actions/workflows/action-test-macos.yaml) [![Action test Windows](https://github.com/stack-spot/runtime-manager-action/actions/workflows/action-test-windows.yaml/badge.svg)](https://github.com/stack-spot/runtime-manager-action/actions/workflows/action-test-windows.yaml)

GitHub action to authenticate and consume StackSpot Runtime API.

_**Note**: This action is supported on all runners operating systems (`ubuntu`, `macos`, `windows`)_
Expand Down
81 changes: 72 additions & 9 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ inputs:
description: Path to the directory where the .stk is located.
required: false
default: "./"
BETA_CLI_ENVIRONMENT:
description: Environment in which cli will be executed. (default, stg, dev).
required: false
type: string
default: default

outputs:
tasks:
Expand Down Expand Up @@ -89,17 +94,35 @@ runs:
- name: Setup Python
uses: actions/setup-python@v4.7.1
with:
python-version: '3.10'
python-version: '3.10'

- name: Install python libraries
run: pip install requests ruamel-yaml==0.17.33
shell: bash

- name: Setup STK CLI
run: |
curl -fsSL https://stk.stackspot.com/install.sh | bash
$HOME/.stk/bin/stk login --client-id=${{ inputs.CLIENT_ID }} --client-key=${{ inputs.CLIENT_KEY }} --realm=${{ inputs.CLIENT_REALM }}
shell: bash
if: inputs.BETA_CLI_ENVIRONMENT == 'default'
uses: stack-spot/stk-cli-action@main
with:
client_id: ${{ inputs.CLIENT_ID }}
client_key: ${{ inputs.CLIENT_KEY }}
realm: ${{ inputs.CLIENT_REALM }}

- name: Setup STK CLI STG
if: inputs.BETA_CLI_ENVIRONMENT == 'stg'
uses: stack-spot/stk-cli-action@stg
with:
client_id: ${{ inputs.CLIENT_ID }}
client_key: ${{ inputs.CLIENT_KEY }}
realm: ${{ inputs.CLIENT_REALM }}

- name: Setup STK CLI DEV
if: inputs.BETA_CLI_ENVIRONMENT == 'dev'
uses: stack-spot/stk-cli-action@dev
with:
client_id: ${{ inputs.CLIENT_ID }}
client_key: ${{ inputs.CLIENT_KEY }}
realm: ${{ inputs.CLIENT_REALM }}

- name: Generate manifesto
working-directory: ${{ inputs.WORKDIR }}
Expand All @@ -108,8 +131,22 @@ runs:
LANGUAGE: C.UTF-8
LC_ALL: C.UTF-8
PYTHONIOENCODING: utf-8
run: |
$HOME/.stk/bin/stk use workspace ${{ inputs.WORKSPACE }}
run: |
if [ "${{ inputs.BETA_CLI_ENVIRONMENT }}" == "default" ]; then
CLI="stk"
elif [ "${{ inputs.BETA_CLI_ENVIRONMENT }}" == "stg" ]; then
CLI="stk-stg"
elif [ "${{ inputs.BETA_CLI_ENVIRONMENT }}" == "dev" ]; then
CLI="stk-dev"
else
echo "Invalid environment: ${{ inputs.BETA_CLI_ENVIRONMENT }}"
exit 1
fi
$CLI use workspace ${{ inputs.WORKSPACE }}
FLAGS=$(echo "--env ${{ inputs.ENVIRONMENT }} --target ${{ github.action_path }} --version ${{ inputs.VERSION_TAG }}")
Expand All @@ -126,16 +163,42 @@ runs:
fi
if [ ! -z "${{ inputs.VERBOSE }}" ]; then
echo "STK DEPLOY PLAN FLAGS = $FLAGS"
echo "DEPLOY PLAN FLAGS = $FLAGS"
fi
$HOME/.stk/bin/stk deploy plan $FLAGS
$CLI deploy plan $FLAGS
shell: bash

- name: Define Domains
id: stackspot-url
run: |
if [ "${{ inputs.BETA_CLI_ENVIRONMENT }}" == "default" ]; then
STACKSPOT_IAM_URL="https://auth.stackspot.com"
STACKSPOT_RUNTIME_MANAGER_URL="https://runtime-manager.v1.stackspot.com"
elif [ "${{ inputs.BETA_CLI_ENVIRONMENT }}" == "stg" ]; then
STACKSPOT_IAM_URL="https://iam-auth-ssr.stg.stackspot.com"
STACKSPOT_RUNTIME_MANAGER_URL="https://runtime-manager.stg.stackspot.com"
elif [ "${{ inputs.BETA_CLI_ENVIRONMENT }}" == "dev" ]; then
STACKSPOT_IAM_URL="https://iam-auth-ssr.dev.stackspot.com"
STACKSPOT_RUNTIME_MANAGER_URL="https://runtime-manager.dev.stackspot.com"
else
echo "Invalid environment: ${{ inputs.BETA_CLI_ENVIRONMENT }}"
exit 1
fi
echo "iam=$STACKSPOT_IAM_URL" >> "$GITHUB_OUTPUT"
echo "runtime_manager=$STACKSPOT_RUNTIME_MANAGER_URL" >> "$GITHUB_OUTPUT"
shell: bash

- name: Start Self Hosted DEPLOY run with Runtime
id: deploy
working-directory: ${{ inputs.WORKDIR }}
env:
STACKSPOT_IAM_URL: ${{ steps.stackspot-url.outputs.iam }}
STACKSPOT_RUNTIME_MANAGER_URL: ${{ steps.stackspot-url.outputs.runtime_manager }}
ACTION_PATH: ${{ github.action_path }}
CLIENT_ID: ${{ inputs.CLIENT_ID }}
CLIENT_KEY: ${{ inputs.CLIENT_KEY }}
Expand Down
49 changes: 28 additions & 21 deletions runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def build_pipeline_url() -> str:
url = f"{GITHUB_SERVER_URL}/{GITHUB_REPOSITORY}/actions/runs/{GITHUB_RUN_ID}"
return url


STACKSPOT_IAM_URL = os.getenv("STACKSPOT_IAM_URL")
STACKSPOT_RUNTIME_MANAGER_URL = os.getenv("STACKSPOT_RUNTIME_MANAGER_URL")
ACTION_PATH = os.getenv("ACTION_PATH")
CLIENT_ID = os.getenv("CLIENT_ID")
CLIENT_KEY = os.getenv("CLIENT_KEY")
Expand All @@ -46,13 +47,14 @@ def build_pipeline_url() -> str:
IAC_REGION = os.getenv("IAC_REGION")
VERBOSE = os.getenv("VERBOSE")

inputs_list = [ACTION_PATH, CLIENT_ID, CLIENT_KEY, CLIENT_REALM, TF_STATE_BUCKET_NAME, TF_STATE_REGION, IAC_BUCKET_NAME, IAC_REGION]

inputs_list = [ACTION_PATH, CLIENT_ID, CLIENT_KEY, CLIENT_REALM, TF_STATE_BUCKET_NAME, TF_STATE_REGION, IAC_BUCKET_NAME,
IAC_REGION]
build_pipeline_url()
if None in inputs_list:
print("- Some mandatory input is empty. Please, check the input list.")
exit(1)

with open(Path(ACTION_PATH+'/manifest.yaml'), 'r') as file:
with open(Path(ACTION_PATH + '/manifest.yaml'), 'r') as file:
manifesto_yaml = file.read()

manifesto_dict = safe_load(manifesto_yaml)
Expand All @@ -65,17 +67,19 @@ def build_pipeline_url() -> str:

print(f"{manifestoType} project identified, with ID: {appOrInfraId}")

iam_url = f"https://auth.stackspot.com/{CLIENT_REALM}/oidc/oauth/token"
iam_url = f"{STACKSPOT_IAM_URL}/{CLIENT_REALM}/oidc/oauth/token"
iam_headers = {'Content-Type': 'application/x-www-form-urlencoded'}
iam_data = {"client_id": f"{CLIENT_ID}", "grant_type": "client_credentials", "client_secret": f"{CLIENT_KEY}"}


print("Authenticating...")
r1 = requests.post(
url=iam_url,
headers=iam_headers,
data=iam_data
)


if r1.status_code == 200:
d1 = r1.json()
access_token = d1["access_token"]
Expand All @@ -84,30 +88,30 @@ def build_pipeline_url() -> str:
version_tag = manifesto_dict["versionTag"]
if version_tag is None:
print("- Version Tag not informed or couldn't be extracted.")
exit(1)
exit(1)

is_api = manifesto_dict["isApi"]
if is_api is None:
print("- API TYPE not informed or couldn't be extracted.")
exit(1)
exit(1)

envId = manifesto_dict["envId"]
if envId is None:
print("- ENVIRONMENT ID not informed or couldn't be extracted.")
exit(1)
wksId = manifesto_dict["workspaceId"]
exit(1)

wksId = manifesto_dict["workspaceId"]
if wksId is None:
print("- WORKSPACE ID not informed or couldn't be extracted.")
exit(1)
exit(1)

branch = None
if "runConfig" in manifesto_dict:
if "runConfig" in manifesto_dict:
branch = manifesto_dict["runConfig"]["checkoutBranch"]
print("Branch informed:", branch)

api_contract_path = None
if "apiContractPath" in manifesto_dict:
if "apiContractPath" in manifesto_dict:
api_contract_path = manifesto_dict["apiContractPath"]
print("API contract path informed:", api_contract_path)

Expand All @@ -124,7 +128,8 @@ def build_pipeline_url() -> str:
"bucket": IAC_BUCKET_NAME,
"region": IAC_REGION
}
}
},
"pipelineUrl": build_pipeline_url(),
}
)

Expand All @@ -137,13 +142,14 @@ def build_pipeline_url() -> str:
**request_data,
**json.loads(config_data),
**pipeline_url,

}

if branch is not None:
branch_data = json.dumps(
{
"runConfig": {
"branch": branch
"branch": branch
}
}
)
Expand All @@ -161,27 +167,28 @@ def build_pipeline_url() -> str:

if VERBOSE is not None:
print("- DEPLOY RUN REQUEST DATA:", request_data)

deploy_headers = {"Authorization": f"Bearer {access_token}", "Content-Type": "application/json"}

print("Deploying Self-Hosted...")

if manifestoType == 'application':
self_hosted_deploy_app_url = "https://runtime-manager.v1.stackspot.com/v1/run/self-hosted/deploy/app"
self_hosted_deploy_app_url = f"{STACKSPOT_RUNTIME_MANAGER_URL}/v1/run/self-hosted/deploy/app"
r2 = requests.post(
url=self_hosted_deploy_app_url,
headers=deploy_headers,
data=request_data
)
elif manifestoType == 'shared-infrastructure':
self_hosted_deploy_infra_url = "https://runtime-manager.v1.stackspot.com/v1/run/self-hosted/deploy/infra"
self_hosted_deploy_infra_url = f"{STACKSPOT_RUNTIME_MANAGER_URL}/v1/run/self-hosted/deploy/infra"
r2 = requests.post(
url=self_hosted_deploy_infra_url,
headers=deploy_headers,
data=request_data
)
else:
print("- MANIFESTO TYPE not recognized. Please, check the input.")

exit(1)

if r2.status_code == 201:
Expand Down

0 comments on commit 94ccd35

Please sign in to comment.