From 8f85b71063b6cb8ad67a0a66977a3ba38fa4867b Mon Sep 17 00:00:00 2001 From: D10S0VSkY-OSS <79284025+D10S0VSkY-OSS@users.noreply.github.com> Date: Mon, 11 Dec 2023 21:53:28 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9Bfix:=20binary=20download=20terragru?= =?UTF-8?q?nt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../worker/providers/hashicorp/download.py | 37 ++++--- sld-dashboard/app/home/deploy.py | 97 ------------------- sld-dashboard/app/home/forms.py | 10 +- .../app/home/templates/stack-edit.html | 66 ++++++++++++- .../app/home/templates/stacks-new.html | 77 ++++++++++++--- 5 files changed, 153 insertions(+), 134 deletions(-) delete mode 100644 sld-dashboard/app/home/deploy.py diff --git a/sld-api-backend/src/worker/providers/hashicorp/download.py b/sld-api-backend/src/worker/providers/hashicorp/download.py index 28d61124..b3cc8a97 100644 --- a/sld-api-backend/src/worker/providers/hashicorp/download.py +++ b/sld-api-backend/src/worker/providers/hashicorp/download.py @@ -19,12 +19,14 @@ def __init__(self, params: DownloadBinaryParams): def get(self) -> dict: try: if self.iac_type == "tofu": - logging.info(f"Downloading binary iac_type {self.iac_type} version {self.version}") + iac_version = self.version.replace("v", "") + logging.info(f"Downloading binary iac_type {self.iac_type} version {iac_version}") # Download Terraform binary if not already downloaded if not os.path.exists(f"/tmp/{self.version}"): os.mkdir(f"/tmp/{self.version}") if not os.path.isfile(f"/tmp/{self.version}/tofu"): - binary = f"https://github.com/opentofu/opentofu/releases/download/v{self.version}/tofu_{self.version}_linux_amd64.zip" + binary = f"https://github.com/opentofu/opentofu/releases/download/v{iac_version}/tofu_{iac_version}_linux_amd64.zip" + print(binary) req = requests.get(binary, verify=False) _zipfile = zipfile.ZipFile(BytesIO(req.content)) _zipfile.extractall(f"/tmp/{self.version}") @@ -37,12 +39,13 @@ def get(self) -> dict: "stdout": "OpenTofu downloaded and used successfully", } elif self.iac_type == "terraform": - logging.info(f"Downloading binary iac_type {self.iac_type} version {self.version}") + iac_version = self.version.replace("v", "") + logging.info(f"Downloading binary iac_type {self.iac_type} version {iac_version}") # Download Terraform binary if not already downloaded if not os.path.exists(f"/tmp/{self.version}"): os.mkdir(f"/tmp/{self.version}") if not os.path.isfile(f"/tmp/{self.version}/terraform"): - binary = f"{settings.TERRAFORM_BIN_REPO}/{self.version}/terraform_{self.version}_linux_amd64.zip" + binary = f"{settings.TERRAFORM_BIN_REPO}/{iac_version}/terraform_{iac_version}_linux_amd64.zip" req = requests.get(binary, verify=False) _zipfile = zipfile.ZipFile(BytesIO(req.content)) _zipfile.extractall(f"/tmp/{self.version}") @@ -72,17 +75,21 @@ def get(self) -> dict: os.rename(downloaded_binary_path, renamed_binary_path) #terrform - if not os.path.isfile(f"/tmp/{self.version}/terraform"): - binary_terraform = f"{settings.TERRAFORM_BIN_REPO}/{self.version}/terraform_1.6.5_linux_amd64.zip" - req = requests.get(binary_terraform, verify=False) - _zipfile = zipfile.ZipFile(BytesIO(req.content)) - _zipfile.extractall(f"/tmp/{self.version}") - st = os.stat(f"/tmp/{self.version}/terraform") - os.chmod(f"/tmp/{self.version}/terraform", st.st_mode | stat.S_IEXEC) - current_path = os.environ.get('PATH', '') - if binary_directory not in current_path.split(os.pathsep): - updated_path = current_path + os.pathsep + binary_directory - os.environ['PATH'] = updated_path + try: + if not os.path.isfile(f"/tmp/{self.version}/terraform"): + binary_terraform = f"{settings.TERRAFORM_BIN_REPO}/1.6.5/terraform_1.6.5_linux_amd64.zip" + req = requests.get(binary_terraform, verify=False) + _zipfile = zipfile.ZipFile(BytesIO(req.content)) + _zipfile.extractall(f"/tmp/{self.version}") + st = os.stat(f"/tmp/{self.version}/terraform") + os.chmod(f"/tmp/{self.version}/terraform", st.st_mode | stat.S_IEXEC) + current_path = os.environ.get('PATH', '') + if binary_directory not in current_path.split(os.pathsep): + updated_path = current_path + os.pathsep + binary_directory + os.environ['PATH'] = updated_path + except Exception as err: + logging.error(f"Failed to download Terraform binary from {binary_terraform}") + raise err else: logging.error(f"Failed to download Terragrunt binary from {binary_url}") diff --git a/sld-dashboard/app/home/deploy.py b/sld-dashboard/app/home/deploy.py deleted file mode 100644 index df6b031c..00000000 --- a/sld-dashboard/app/home/deploy.py +++ /dev/null @@ -1,97 +0,0 @@ -# -*- encoding: utf-8 -*- -import os -import ast -import json -import time -import logging -from flask import jsonify, render_template, request, url_for, redirect, flash, Response - - -import redis -from app import login_manager -from app.helpers.api_request import (check_unauthorized_token, get_task_id, - request_url) -from app.helpers.config.api import settings -from app.helpers.converter import convert_to_dict -from app.helpers.security import vault_decrypt -from flask import Blueprint -from app.home.forms import (AwsForm, AzureForm, DeployForm, GcpForm, StackForm, - UserForm, CustomProviderForm) -from flask import flash, redirect, render_template, request, url_for -from flask_login import current_user, login_required -from jinja2 import TemplateNotFound - - -# Icons -def list_icons(): - current_path = os.getcwd() - base_path = f'{current_path}/app/base/static/assets/img/gallery' - icons = {} - for root, dirs, files in os.walk(base_path): - for file in files: - if file.endswith('.svg'): - dir_path = os.path.relpath(root, base_path) - if dir_path not in icons: - icons[dir_path] = [] - icons[dir_path].append(file) - return icons - -@vault_decrypt -def decrypt(secreto): - try: - return secreto - except Exception as err: - raise err - - -# Move to config file after testing -r = redis.Redis(host="redis", port=6379, db=1, charset="utf-8", decode_responses=True) -s = redis.Redis(host="redis", port=6379, db=15, charset="utf-8", decode_responses=True) - -external_api_dns = settings.EXTERNAL_DNS_API - - -def pretty_json(value): - return json.dumps(value, indent=4) - -deploy_blueprint = Blueprint('deploy_blueprint', __name__) - - -@deploy_blueprint.route("/deploys-list", defaults={"limit": 0}) -@deploy_blueprint.route("/deploys-list/") -@login_required -def list_deploys(limit): - try: - token = decrypt(r.get(current_user.id)) - # Check if token no expired - check_unauthorized_token(token) - # get stack info - endpoint = f"stacks/?limit={limit}" - if limit == 0: - endpoint = "stacks/" - stack_response = request_url( - verb="GET", uri=f"{endpoint}", headers={"Authorization": f"Bearer {token}"} - ) - stack = stack_response.get("json") - # Get deploy data vars and set var for render - endpoint = f"deploy/?limit={limit}" - if limit == 0: - endpoint = f"deploy/" - response = request_url( - verb="GET", uri=f"{endpoint}", headers={"Authorization": f"Bearer {token}"} - ) - content = response.get("json") - return render_template( - "deploys-list.html", - name="Name", - token=token, - deploys=content, - stacks=stack, - external_api_dns=external_api_dns, - ) - except TemplateNotFound: - return render_template("page-404.html"), 404 - except TypeError: - return redirect(url_for("base_blueprint.logout")) - except Exception: - return render_template("page-500.html"), 500 \ No newline at end of file diff --git a/sld-dashboard/app/home/forms.py b/sld-dashboard/app/home/forms.py index db199a9c..efa41e9e 100644 --- a/sld-dashboard/app/home/forms.py +++ b/sld-dashboard/app/home/forms.py @@ -44,13 +44,11 @@ class StackForm(FlaskForm): validators=[validators.DataRequired()], coerce=lambda x: 'tofu' if x == 'openTofu' else x ) - tf_version = StringField( - "IaC version", - [ - validators.length(min=5, max=15, message="tf version out of reange."), - validators.DataRequired(message="tf version requerid."), - ], + tf_version = SelectField( + "IaC Version", + validators=[validators.DataRequired(message="IaC version required.")] ) + project_path = StringField( "Project_path", [ diff --git a/sld-dashboard/app/home/templates/stack-edit.html b/sld-dashboard/app/home/templates/stack-edit.html index 2b11cb0e..195b6561 100644 --- a/sld-dashboard/app/home/templates/stack-edit.html +++ b/sld-dashboard/app/home/templates/stack-edit.html @@ -55,11 +55,11 @@

{{stack.stack_name}}

- {{ render_field(form.iac_type, autocomplete="off", class='form-control',) }} + {{ render_field(form.iac_type, id='iacTypeSelector',autocomplete="off", class='form-control', value=stack.iac_type) }}
- {{ render_field(form.tf_version, autocomplete="off", class='form-control', value=stack.tf_version) }} + {{ render_field(form.tf_version, id='versionSelector',autocomplete="off", class='form-control', value=stack.tf_version) }}
@@ -148,4 +148,66 @@

{{stack.stack_name}}

}); + + + + + {% endblock javascripts %} diff --git a/sld-dashboard/app/home/templates/stacks-new.html b/sld-dashboard/app/home/templates/stacks-new.html index ccfb6b00..fe772896 100644 --- a/sld-dashboard/app/home/templates/stacks-new.html +++ b/sld-dashboard/app/home/templates/stacks-new.html @@ -56,15 +56,13 @@

Add New Stack

{{ render_field(form.squad_access, autocomplete="off", class='form-control') }}
-
- {{ render_field(form.iac_type, autocomplete="off", class='form-control') }} + {{ render_field(form.iac_type, id='iacTypeSelector', class='form-control') }}
- {{ render_field(form.tf_version, autocomplete="off", class='form-control', value='1.3.2') }} + {{ render_field(form.tf_version, id='versionSelector', class='form-control') }}
-
{{ render_field(form.project_path, autocomplete="off", class='form-control', placeholder='Only complete if you use monorepo') }} @@ -73,16 +71,16 @@

Add New Stack

{{ render_field(form.description, autocomplete="off", class='form-control', placeholder='Description') }}
-
- - - -
- -
-
+
+ + + +
+ +
+
@@ -142,4 +140,55 @@

Add New Stack

}; }); + + + {% endblock javascripts %}