From a54002252c0e24257ccd7d41e767a48917edb888 Mon Sep 17 00:00:00 2001 From: lars Date: Fri, 6 Oct 2023 15:40:34 -0500 Subject: [PATCH] Some deploy updates --- deploy/portainer/.env.app | 1 + deploy/portainer/{.env => .env.deploy} | 3 +- deploy/portainer/deploy.py | 33 ++++++++++--------- .../portainer_deploy.docker-compose.yml | 4 +-- deploy/portainer/undeploy.py | 14 ++++---- 5 files changed, 27 insertions(+), 28 deletions(-) create mode 100644 deploy/portainer/.env.app rename deploy/portainer/{.env => .env.deploy} (81%) diff --git a/deploy/portainer/.env.app b/deploy/portainer/.env.app new file mode 100644 index 0000000..a6a93cf --- /dev/null +++ b/deploy/portainer/.env.app @@ -0,0 +1 @@ +HOST=dvdemo.privatedns.org \ No newline at end of file diff --git a/deploy/portainer/.env b/deploy/portainer/.env.deploy similarity index 81% rename from deploy/portainer/.env rename to deploy/portainer/.env.deploy index 4a9c609..db30f96 100644 --- a/deploy/portainer/.env +++ b/deploy/portainer/.env.deploy @@ -3,5 +3,4 @@ PORTAINER_API_KEY=ptr_RwxH2Cd+htdD2FoFiG46erT9beyvj9VoF3BrQPtDH3Q= PORTAINER_EP=CICD-runner GITEA_API_KEY=f449c74ec7f04e54fe1e481eae43492b34cea406 DEPLOY_REPO_URL=https://dvgit.privatedns.org/lars/DeployTests -DEPLOY_BRANCH=main -DEPLOY_HOST=dvdemo.privatedns.org \ No newline at end of file +DEPLOY_BRANCH=develop \ No newline at end of file diff --git a/deploy/portainer/deploy.py b/deploy/portainer/deploy.py index dcb589c..500c249 100644 --- a/deploy/portainer/deploy.py +++ b/deploy/portainer/deploy.py @@ -6,12 +6,12 @@ import argparse import requests import json import uuid -from dotenv import load_dotenv +from dotenv import load_dotenv, dotenv_values from string import Template from pathlib import Path from urllib.parse import urlparse -load_dotenv() +load_dotenv('.env.deploy') required_env_vars = { 'PORTAINER': 'The portainer instance to deploy to', @@ -20,7 +20,6 @@ required_env_vars = { 'GITEA_API_KEY': 'API-Key to access Gitea instance', 'DEPLOY_REPO_URL': 'The repository URL to deploy', 'DEPLOY_BRANCH': 'The branch to deploy', - 'DEPLOY_HOST': 'The host name under which the deployment will be reachable', } # Try getting all arguments from (in order): 1 command line, 2 .env file, 3 Environment @@ -58,7 +57,7 @@ try: json_endpoints = response.json() except requests.exceptions.RequestException as err: - raise Exception(f'Could not retrieve portainer endpoints: {err}') + raise Exception(f'Could not retrieve portainer endpoints: {err}. \n\n Response: {response.content}') endpoint_id = None for endpoint in json_endpoints: @@ -78,6 +77,17 @@ owner = repo_parts[0] repo = repo_parts[1] ### Template substitution for the portainer stack deployment ### + +app_env = dotenv_values('.env.app') +portainer_deploy_env = [{ + "name": "COMPOSE_PROJECT_NAME", + "value": args.DEPLOY_BRANCH +}] +for key, value in app_env.items(): + portainer_deploy_env.append({ + "name": key, + "value": os.getenv(value) if value.startswith("$") else value +}) portainer_deploy_payload = { "additionalFiles": [ "deploy/portainer/portainer_deploy.docker-compose.yml" @@ -86,16 +96,7 @@ portainer_deploy_payload = { "webhook": deploy_webhook }, "composeFile": "docker-compose.yml", - "env": [ - { - "name": "HOST", - "value": args.DEPLOY_HOST - }, - { - "name": "COMPOSE_PROJECT_NAME", - "value": args.DEPLOY_BRANCH - } - ], + "env": portainer_deploy_env, "fromAppTemplate": False, "name": f"{owner}_{repo}_{args.DEPLOY_BRANCH.replace('/', '_')}".lower(), "repositoryAuthentication": True, @@ -114,7 +115,7 @@ try: deploy_response = response.json() except requests.exceptions.RequestException as err: - raise Exception(f'Could not deploy portainer stack: {err}') + raise Exception(f'Could not deploy portainer stack: {err}.\n\n Response: {response.content}') ### Add Webhook to Gitea ### webhook_payload = { @@ -138,5 +139,5 @@ try: webhook_response = response.json() except requests.exceptions.RequestException as err: - raise Exception(f'Could not add webhook to Gitea: {err}') + raise Exception(f'Could not add webhook to Gitea: {err}.\n\n Response: {response.content}') print(f'Successfully deployed project') \ No newline at end of file diff --git a/deploy/portainer/portainer_deploy.docker-compose.yml b/deploy/portainer/portainer_deploy.docker-compose.yml index 5210db5..3c61aa2 100644 --- a/deploy/portainer/portainer_deploy.docker-compose.yml +++ b/deploy/portainer/portainer_deploy.docker-compose.yml @@ -1,10 +1,10 @@ -version: '3.4' +version: '3' services: http-echo: networks: - cicd - + networks: cicd: driver: bridge diff --git a/deploy/portainer/undeploy.py b/deploy/portainer/undeploy.py index b13756a..6be43e5 100644 --- a/deploy/portainer/undeploy.py +++ b/deploy/portainer/undeploy.py @@ -11,7 +11,7 @@ from string import Template from pathlib import Path from urllib.parse import urlparse -load_dotenv() +load_dotenv('.env.deploy') required_env_vars = { 'PORTAINER': 'The portainer instance to deploy to', @@ -53,7 +53,7 @@ try: json_endpoints = endpoint_response.json() except requests.exceptions.RequestException as err: - raise Exception(f'Could not retrieve portainer endpoints: {err}') + raise Exception(f'Could not retrieve portainer endpoints: {err}.\n\n Response: {endpoint_response.content}') endpoint_id = None for endpoint in json_endpoints: @@ -73,7 +73,7 @@ try: json_stacks = stacks_response.json() except requests.exceptions.RequestException as err: - raise Exception(f'Could not retrieve portainer stacks: {err}') + raise Exception(f'Could not retrieve portainer stacks: {err}.\n\n Response: {stacks_response.content}') stack_id = None stack_webhook = None @@ -112,7 +112,7 @@ try: json_webhooks = get_webhooks_response.json() except requests.exceptions.RequestException as err: - raise Exception(f'Could not get webhooks from Gitea: {err}') + raise Exception(f'Could not get webhooks from Gitea: {err}.\n\n Response:{get_webhooks_response.content}') webhook_id = None for webhook in json_webhooks: @@ -128,21 +128,19 @@ else: ### Remove Webhook from Gitea ### remove_webhook_url = f"{gitea}/api/v1/repos/{repo_path}/hooks/{webhook_id}" try: - #TODO: Webhooks are returned paginated, this only checks first page del_webhooks_response = requests.delete(remove_webhook_url, headers=gitea_headers) del_webhooks_response.raise_for_status() # Raise HTTPError for bad requests except requests.exceptions.RequestException as err: - raise Exception(f"Could not delete webhook '{webhook_id}' from Gitea: {err}") + raise Exception(f"Could not delete webhook '{webhook_id}' from Gitea: {err}.\n\n Response: {del_webhooks_response.content}") ## Remove Stack from Portainer ### remove_stack_url = f"{args.PORTAINER}/api/stacks/{stack_id}?endpointId={endpoint_id}" try: - #TODO: Webhooks are returned paginated, this only checks first page del_stack_response = requests.delete(remove_stack_url, headers=portainer_headers) del_stack_response.raise_for_status() # Raise HTTPError for bad requests except requests.exceptions.RequestException as err: - raise Exception(f"Could not delete stack '{stack_id}' from Portainer: {err}") + raise Exception(f"Could not delete stack '{stack_id}' from Portainer: {err}.\n\n Response: {del_stack_response.content}") print(f'Successfully undeployed project') \ No newline at end of file