added liquibase for setup of databases and initial values

fixed some table names to be singular
migrated templates to separate repository
added seed data to initial version in liquibase migrations instead of property files (post targets, emotes etc) and created some default tables containing those default values
added separate artifacts to be used containing only the liquibase config
added shell script as a wrapper for ansible deployment, to handle an environment variable defining whether or not the deployment should be executed
added logback scan period
added licenses for ansible, liquibase, docker and docker-compose
This commit is contained in:
Sheldan
2020-07-01 20:44:21 +02:00
parent e8767429bf
commit 9374dfb912
965 changed files with 15775 additions and 2935 deletions

View File

@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>dev.sheldan.abstracto</groupId>
<artifactId>abstracto-application</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>installer</artifactId>
<packaging>pom</packaging>
<profiles>
<profile>
<id>deployment-docker</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.33.0</version>
<configuration>
<pushRegistry>docker.pkg.github.com/sheldan/abstracto</pushRegistry>
<images>
<image>
<name>deployment-container:latest</name>
<build>
<contextDir>${project.basedir}/src/main/docker</contextDir>
<assembly>
<name>resources</name>
<descriptor>docker.xml</descriptor>
</assembly>
</build>
</image>
</images>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
</profiles>
</project>

View File

@@ -0,0 +1,37 @@
FROM ubuntu as base
MAINTAINER Sheldan
ENV DEBIAN_FRONTEND=noninteractive
ARG maven_version=3.6.3
ARG liquibase_version=3.8.9
ARG postgres_driver_version=42.2.14
# Install prerequisities for Ansible
RUN apt-get update \
&& apt-get install -y unzip wget \
&& rm -rf /var/lib/apt/lists/
# Install liquibase
RUN mkdir -p /liqiubase \
&& wget https://github.com/liquibase/liquibase/releases/download/v${liquibase_version}/liquibase-${liquibase_version}.zip -O /tmp/liquibase.zip \
&& unzip /tmp/liquibase.zip -d /liquibase
RUN mkdir -p /java \
&& wget https://corretto.aws/downloads/latest/amazon-corretto-8-x64-linux-jdk.tar.gz -O /tmp/java.tar.gz \
&& tar -xf /tmp/java.tar.gz --strip-components=1 -C /java
# Install postgres driver
RUN mkdir -p /postgres \
&& wget https://jdbc.postgresql.org/download/postgresql-${postgres_driver_version}.jar -O /postgres/driver.jar
# Install ansible and required libraries
FROM python:3.7-slim-buster as runtime
RUN pip3 install --no-cache-dir ansible psycopg2-binary SQLAlchemy lxml
RUN apt-get update && apt-get install unzip && rm -rf /var/lib/apt/lists/
COPY --from=base /liquibase /liquibase
COPY --from=base /postgres /postgres
COPY --from=base /java /java
ENV JAVA_HOME=/java/jre
ADD resources/ /
RUN chmod +x /deploy.sh
ENTRYPOINT ["/deploy.sh"]

View File

@@ -0,0 +1,28 @@
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<outputDirectory>.</outputDirectory>
<directory>${project.basedir}/src/main/resources/ansible</directory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
<fileSet>
<outputDirectory>.</outputDirectory>
<directory>${project.basedir}/src/main/resources/python</directory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
<fileSet>
<outputDirectory>.</outputDirectory>
<directory>${project.basedir}/src/main/resources/wrapper</directory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>

View File

@@ -0,0 +1 @@
ansible_python_interpreter: /usr/bin/python3

View File

@@ -0,0 +1,8 @@
---
- hosts: localhost
connection: local
gather_facts: no
roles:
- role: database

View File

@@ -0,0 +1,35 @@
import sqlalchemy as db
import glob
import os
import sys
from sqlalchemy.sql import text
host = os.getenv('DB_HOST')
port = os.getenv('DB_PORT')
database = os.getenv('DB_NAME')
user_name = os.getenv('DB_USER')
password = os.getenv('DB_PASS')
folder = sys.argv[1]
engine = db.create_engine('postgres://%s:%s@%s:%s/%s' % (user_name, password, host, port, database))
if not os.path.isdir(folder):
print("Given path 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}
templates.append(template)
print('Deploying %s templates from folder %s' % (len(templates), folder))
with engine.connect() as con:
statement = text("""INSERT INTO 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)

View File

@@ -0,0 +1,10 @@
---
- include_tasks: deploy_template_artifact.yaml
name: Deploying translations
vars:
group_id: "{{ artifact.group_id }}"
version: "{{ artifact.version }}"
with_items: "{{ artifact.modules }}"
loop_control:
loop_var: module

View File

@@ -0,0 +1,46 @@
---
- name: Prepare target directoy
delegate_to: localhost
file:
path: "{{ installer_base_dir }}/liquibase/{{ artifact.group_id }}/{{ artifact.module }}"
state: directory
mode: '0755'
- name: "Download liquibase artifact {{ artifact.module }} in {{ artifact.group_id }}"
delegate_to: localhost
maven_artifact:
group_id: "{{ artifact.group_id }}"
artifact_id: "{{ artifact.module }}"
dest: "{{ installer_base_dir }}/liquibase/{{ artifact.group_id }}-{{ artifact.module }}.zip"
extension: zip
version: "{{ artifact.version }}"
repository_url: "file://{{ lookup('env','HOME') }}/.m2/repository"
classifier: liquibase
- name: Extract artifact zip
delegate_to: localhost
unarchive:
src: "{{ installer_base_dir }}/liquibase/{{ artifact.group_id }}-{{ artifact.module }}.zip"
dest: "{{ installer_base_dir }}/liquibase/{{ artifact.group_id }}/{{ artifact.module }}"
- name: Render liquibase.properties template
template:
src: liquibase.properties.j2
dest: "{{ installer_base_dir }}/liquibase/{{ artifact.group_id }}/{{ artifact.module }}/liquibase.properties"
vars:
- change_log_file: "{{ installer_base_dir }}//liquibase/{{ artifact.group_id }}/{{ artifact.module }}/{{ artifact.file }}"
- name: Run liquibase migration
shell: "{{ liquibase_path }}/liquibase --defaultsFile={{ installer_base_dir }}/liquibase/{{ artifact.group_id }}/{{ artifact.module }}/liquibase.properties --logLevel=info update"
- name: Cleanup folder
file:
path: "{{ installer_base_dir }}/liquibase/{{ artifact.group_id }}"
state: absent
- name: Cleanup zip
file:
path: "{{ installer_base_dir }}/liquibase/{{ artifact.group_id }}-{{ artifact.module }}.zip"
state: absent

View File

@@ -0,0 +1,35 @@
---
- name: Prepare target directoy
delegate_to: localhost
file:
path: "{{ installer_base_dir }}/templates/{{ group_id }}/{{ module }}"
state: directory
mode: '0755'
- name: Download template artifact {{ module }} in {{ group_id }}
delegate_to: localhost
maven_artifact:
group_id: "{{ group_id }}"
artifact_id: "{{ module }}"
dest: "{{ installer_base_dir }}/templates/{{ group_id }}-{{ module }}.zip"
extension: zip
version: "{{ version }}"
repository_url: "file://{{ lookup('env','HOME') }}/.m2/repository"
- name: Extract artifact zip
delegate_to: localhost
unarchive:
src: "{{ installer_base_dir }}/templates/{{ group_id }}-{{ module }}.zip"
dest: "{{ installer_base_dir }}/templates/{{ group_id }}/{{ module }}"
- name: Deploy templates
script: deploy_templates.py "{{ installer_base_dir }}/templates/{{ group_id }}/{{ module }}"
args:
executable: "{{ ansible_python_interpreter }}"
- name: Cleanup folder
file:
path: "{{ installer_base_dir }}/templates/"
state: absent

View File

@@ -0,0 +1,23 @@
---
- name: Include custom variables
include_vars:
dir: "{{ installer_base_dir }}/ansible"
depth: 1
- include_tasks: deploy_liquibase_change_log.yaml
name: Deploying abstracto liquibase changelogs
with_items: "{{ liquibase_artifacts }}"
loop_control:
loop_var: artifact
when: execute_liquibase
- include_tasks: deploy_artifacts.yaml
name: Deploying abstracto templates
with_items: "{{ template_artifacts }}"
loop_control:
loop_var: artifact
when: execute_templates

View File

@@ -0,0 +1,6 @@
changeLogFile: {{ change_log_file }}
driver: org.postgresql.Driver
url: jdbc:postgresql://{{ db_credentials.host }}:{{ db_credentials.port }}/{{ db_credentials.database }}
username: {{ db_credentials.user }}
password: {{ db_credentials.password }}
classpath: {{ postgres_driver_path }}

View File

@@ -0,0 +1,19 @@
custom_template_artifacts: []
liquibase_artifacts: []
db_credentials:
host: "{{ lookup('env','DB_HOST') }}"
port: "{{ lookup('env','DB_PORT') }}"
database: "{{ lookup('env','DB_NAME') }}"
user: "{{ lookup('env','DB_USER') }}"
password: "{{ lookup('env','DB_PASS') }}"
liquibase_path: "{{ lookup('env','LIQUIBASE_PATH') }}"
postgres_driver_path: "{{ lookup('env','POSTGRES_DRIVER_PATH') }}"
installer_base_dir: "/tmp/installer"
execute_liquibase_input: yes
execute_templates_input: yes
execute_liquibase: "{{ True if (execute_liquibase_input | bool) else False }}"
execute_templates: "{{ True if (execute_templates_input | bool) else False }}"

View File

@@ -0,0 +1,17 @@
#!/bin/sh
DEPLOY_LIQUIBASE=no
DEPLOY_TEMPLATES=no
if [ "x$EXECUTE_LIQUIBASE" = 'xtrue' ]; then
DEPLOY_LIQUIBASE=yes
fi
if [ "x$EXECUTE_TEMPLATES" = 'xtrue' ]; then
DEPLOY_TEMPLATES=yes
fi
if [ "x$EXECUTE_DEPLOYMENT" = 'xtrue' ]; then
ansible-playbook playbook.yaml -e "execute_liquibase_input=${DEPLOY_LIQUIBASE}" -e "execute_templates_input=${DEPLOY_TEMPLATES}";
fi