mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-10 01:19:07 +00:00
[AB-xxx] refactoring rest-api to not be a maven project and restructuring python tool file structure
This commit is contained in:
10
python/deployment/installer/template-deployment/Dockerfile
Normal file
10
python/deployment/installer/template-deployment/Dockerfile
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
FROM python:3.7-slim-buster as runtime
|
||||
MAINTAINER Sheldan
|
||||
ARG sql_alchemy_version=1.4.46
|
||||
ARG psycopg2_version=2.9.5
|
||||
RUN pip3 install --no-cache-dir psycopg2-binary==${psycopg2_version} SQLAlchemy==${sql_alchemy_version}
|
||||
ADD python /python
|
||||
ADD wrapper /
|
||||
|
||||
ENTRYPOINT [ "/bin/sh", "/deploy.sh" ]
|
||||
@@ -0,0 +1,50 @@
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import templates_deploy
|
||||
from zipfile import ZipFile
|
||||
|
||||
|
||||
use_folder = False
|
||||
local_folder = None
|
||||
config_dir = sys.argv[1]
|
||||
if len(sys.argv) == 3:
|
||||
use_folder = True
|
||||
local_folder = sys.argv[2]
|
||||
|
||||
|
||||
class DbConfig:
|
||||
def __init__(self):
|
||||
self.host = os.getenv('DB_HOST')
|
||||
self.port = os.getenv('DB_PORT')
|
||||
self.database = os.getenv('DB_NAME')
|
||||
self.user = os.getenv('DB_USER')
|
||||
self.password = os.getenv('DB_PASS')
|
||||
self.scheme = os.getenv('DB_SCHEME')
|
||||
|
||||
|
||||
db_config = DbConfig()
|
||||
if not use_folder:
|
||||
print("Not deploying with folder.")
|
||||
print("Loading versions.")
|
||||
with open(config_dir + 'artifact_versions.json') as artifact_config_file:
|
||||
artifact_config = json.load(artifact_config_file)
|
||||
|
||||
print("Deploying templates.")
|
||||
for template_artifact in artifact_config['template_artifacts']:
|
||||
folder_name = config_dir + '/' + template_artifact + "-templates"
|
||||
os.mkdir(folder_name)
|
||||
with ZipFile(config_dir + 'templates/' + template_artifact + '.zip', 'r') as template_zip:
|
||||
template_zip.extractall(folder_name)
|
||||
templates_deploy.deploy_template_folder(db_config, folder_name)
|
||||
|
||||
print("Deploying translation templates")
|
||||
for template_artifact in artifact_config['translation_artifacts']:
|
||||
folder_name = config_dir + '/' + template_artifact + "-translations"
|
||||
with ZipFile(config_dir + 'translations/' + template_artifact + '.zip', 'r') as template_zip:
|
||||
template_zip.extractall(folder_name)
|
||||
templates_deploy.deploy_template_folder(db_config, folder_name)
|
||||
|
||||
if use_folder:
|
||||
print("Only deploying folder.")
|
||||
templates_deploy.deploy_template_folder(db_config, local_folder)
|
||||
@@ -0,0 +1,31 @@
|
||||
import glob
|
||||
import os
|
||||
import sqlalchemy as db
|
||||
from sqlalchemy.sql import text
|
||||
|
||||
|
||||
def deploy_template_folder(db_config, folder):
|
||||
engine = db.create_engine('postgresql://%s:%s@%s:%s/%s' % (db_config.user, db_config.password, db_config.host, db_config.port, db_config.database))
|
||||
|
||||
if not os.path.isdir(folder):
|
||||
print(f'Given path {folder} was not a folder. Exiting.')
|
||||
exit(1)
|
||||
|
||||
files = glob.glob(folder + '/**/*.ftl', recursive=True)
|
||||
templates = []
|
||||
for file in files:
|
||||
with open(file) as template_file:
|
||||
file_content = template_file.read()
|
||||
template_key = os.path.splitext(os.path.basename(file))[0]
|
||||
template = {'key': template_key, 'content': file_content}
|
||||
print(f'Deployment template {template}')
|
||||
templates.append(template)
|
||||
|
||||
print(f'Deploying {len(templates)} templates from folder {folder}')
|
||||
|
||||
with engine.connect() as con:
|
||||
with con.begin():
|
||||
statement = text(f"""INSERT INTO {db_config.scheme}template(key, content, last_modified) VALUES(:key, :content, NOW()) ON CONFLICT (key) DO UPDATE SET content = :content""")
|
||||
|
||||
for line in templates:
|
||||
con.execute(statement, **line)
|
||||
@@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
|
||||
echo "Starting deployment."
|
||||
|
||||
target_dir=$1
|
||||
|
||||
python3 -u python/main.py "${target_dir}"
|
||||
exit_code=$?
|
||||
|
||||
echo "Finished deployment."
|
||||
exit $exit_code
|
||||
Reference in New Issue
Block a user