Skip to content

Commit

Permalink
fixup! chore: seperate staging deployments from production
Browse files Browse the repository at this point in the history
  • Loading branch information
SilasPeters committed Sep 28, 2024
1 parent 00f05ad commit a1cea8a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
1 change: 1 addition & 0 deletions ansible/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let
types-requests
pyaml
types-pyyaml
python-dotenv
]);

linuxOnlyTools = if pkgs.stdenv.isLinux then [ pkgs.ansible-lint ] else [];
Expand Down
53 changes: 36 additions & 17 deletions ansible/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

from git.repo import Repo
from typing import Optional
from dotenv import load_dotenv

import scripts.bitwarden as bitwarden

# Import .env file
load_dotenv()
discord_webhook_staging_deployments = os.getenv("DISCORD_WEBHOOK_STAGING_DEPLOYMENTS")
discord_webhook_production_deployments = os.getenv("DISCORD_WEBHOOK_STAGING_DEPLOYMENTS")

Expand Down Expand Up @@ -93,11 +95,7 @@ def deploy(
if check:
arguments.append("--check")

if not tags is None:
arguments.append("--tags")
arguments.append(tags)

# From-until logic:
# First determine all roles defined by from-until logic
with open("main.yml", "r") as yaml_file:
data = yaml.safe_load(yaml_file)

Expand All @@ -117,10 +115,23 @@ def deploy(
roles = roles[: roles.index(until_playbook) + 1]
from_until = True

final_roles = ",".join(roles)
if from_until:
from_until_roles = roles # Filtered by the from_until logic
else:
from_until_roles = []

# Then add all roles specified manually
if tags is not None:
manual_roles = [role.strip() for role in tags.split(',')]
else:
manual_roles = []


# Finally, pass down all roles specified, if any
specified_roles = from_until_roles + manual_roles
if specified_roles:
arguments.append("--tags")
arguments.append(final_roles)
arguments.append(",".join(specified_roles))

arguments.append(playbook)

Expand All @@ -131,19 +142,19 @@ def deploy(
)

if not check:
notify_deploy_start(playbook, host, user, branch, revision, roles, discord_deployment_webhook)
notify_deploy_start(playbook, host, user, branch, revision, specified_roles, discord_deployment_webhook)

print("Running the following playbook:")
print(" ".join(arguments))

try:
subprocess.run(arguments, check=True, env=env)
if not check:
notify_deploy_succes(playbook, host, branch, revision, discord_deployment_webhook)
notify_deploy_succes(playbook, host, user, branch, revision, specified_roles, discord_deployment_webhook)

except subprocess.CalledProcessError:
if not check:
notify_deploy_failure(playbook, host, branch, revision, discord_deployment_webhook)
notify_deploy_failure(playbook, host, user, branch, revision, specified_roles, discord_deployment_webhook)


def current_branch_name() -> str:
Expand All @@ -167,9 +178,9 @@ def notify_deploy_start(
) -> None:
roles_str = ', '.join(roles)
discord_notify(
f"*Deployment of playbook {playbook} in {host} environment started by {user}*\n"
+ f'_Branch: {git_branch} - revision "{git_revision}"_\n'
+ f'_Roles: {roles_str}',
f"**Deployment of playbook {playbook} to {host} started by {user}**\n"
+ f'Branch: {git_branch} - revision "{git_revision}"\n'
+ f'Roles: {roles_str}',
":construction:",
"#46c4ff",
discord_webhook
Expand All @@ -179,13 +190,17 @@ def notify_deploy_start(
def notify_deploy_succes(
playbook: str,
host: str,
user: str,
git_branch: str,
git_revision: str,
roles: [str],
discord_webhook: str
) -> None:
roles_str = ', '.join(roles)
discord_notify(
f"*Deployment of playbook {playbook} in {host} environment succesfully completed*\n"
+ f'_(branch: {git_branch} - revision "{git_revision}")_',
f"**Deployment of playbook {playbook} to {host}, started by {user}, succesfully completed**\n"
+ f'Branch: {git_branch} - revision "{git_revision}"\n'
+ f'Roles: {roles_str}',
":construction:",
"good",
discord_webhook
Expand All @@ -195,13 +210,17 @@ def notify_deploy_succes(
def notify_deploy_failure(
playbook: str,
host: str,
user: str,
git_branch: str,
git_revision: str,
roles: [str],
discord_webhook: str
) -> None:
roles_str = ', '.join(roles)
discord_notify(
f"*Deployment of playbook {playbook} in {host} environment FAILED!*\n"
+ f'_(branch: {git_branch} - revision "{git_revision}")_',
f"**Deployment of playbook {playbook} to {host}, started by {user}, FAILED!**\n"
+ f'Branch: {git_branch} - revision "{git_revision}"\n'
+ f'Roles: {roles_str}',
":exclamation:",
"danger",
discord_webhook
Expand Down

0 comments on commit a1cea8a

Please sign in to comment.