diff --git a/scripts/helm/helm/database-migrate/files/chalice.sh b/scripts/helm/helm/database-migrate/files/chalice.sh new file mode 100644 index 000000000..a9bf588e2 --- /dev/null +++ b/scripts/helm/helm/database-migrate/files/chalice.sh @@ -0,0 +1 @@ +#!/bin/bash diff --git a/scripts/helm/helm/database-migrate/files/dbops.sh b/scripts/helm/helm/database-migrate/files/dbops.sh new file mode 100644 index 000000000..08ca14547 --- /dev/null +++ b/scripts/helm/helm/database-migrate/files/dbops.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +cd $(dirname $0) + +function migration() { + ls -la /opt/openreplay/openreplay + db=$1 + old_version=$2 + + if [[ old_version == $CHART_APP_VERSION ]]; then + echo "No application version change. Not upgrading." + exit 0 + fi + + # Checking migration versions + cd /opt/openreplay/openreplay/scripts/helm + migration_versions=(`ls -l db/init_dbs/$db | grep -E ^d | awk -v number=${old_version} '$NF > number {print $NF}' | grep -v create`) + echo "Migration version: $migration_versions" + + cd - + + case "$1" in + postgresql) + /bin/bash postgresql.sh migrate $migration_versions + ;; + chalice) + /bin/bash chalice.sh migrate $migration_versions + ;; + kafka) + /bin/bash kafka.sh migrate $migration_versions + ;; + *) + echo "Unknown operation for db migration; exiting." + exit 1 + ;; + esac +} + +function init(){ + case $1 in + postgresql) + /bin/bash postgresql.sh init + ;; + *) + echo "Unknown operation for db init; exiting." + exit 1 + ;; + + esac +} + + +# dbops.sh true(upgrade) chalice +case "$1" in + "false") + init $2 + ;; + "true") + migration $2 $CHART_APP_VERSION + ;; + *) + echo "Unknown operation for db migration; exiting." + exit 1 + ;; +esac diff --git a/scripts/helm/helm/database-migrate/files/kafka.sh b/scripts/helm/helm/database-migrate/files/kafka.sh new file mode 100644 index 000000000..a9bf588e2 --- /dev/null +++ b/scripts/helm/helm/database-migrate/files/kafka.sh @@ -0,0 +1 @@ +#!/bin/bash diff --git a/scripts/helm/helm/database-migrate/files/postgresql.sh b/scripts/helm/helm/database-migrate/files/postgresql.sh new file mode 100644 index 000000000..a12a65c44 --- /dev/null +++ b/scripts/helm/helm/database-migrate/files/postgresql.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +set -e + +pgdir=/opt/openreplay/openreplay/scripts/helm/db/init_dbs/postgresql + +# ENV variables +# Ref: https://www.postgresql.org/docs/current/libpq-envars.html +# $PGHOST +# $PGPORT +# $PGDATABASE +# $PGUSER +# $PGPASSWORD + +function migrate() { + echo "Starting postgresql migration" + migration_versions=$1 + for version in $migration_versions; do + echo "Migrating postgresql version $version" + psql -f ${pgdir}/${version}/${version}.sql + done +} + +function init() { + echo "Initializing postgresql" + psql -f ${pgdir}/init_schema.sql +} + +# /bin/bash postgresql.sh migrate $migration_versions +case "$1" in + migrate) + migrate $2 + ;; + init) + init + ;; + *) + echo "Unknown operation for postgresql migration; exiting." + exit 1 + ;; +esac diff --git a/scripts/helm/helm/database-migrate/templates/Job.yaml b/scripts/helm/helm/database-migrate/templates/Job.yaml index d2b7335c3..469104d8f 100644 --- a/scripts/helm/helm/database-migrate/templates/Job.yaml +++ b/scripts/helm/helm/database-migrate/templates/Job.yaml @@ -5,56 +5,16 @@ metadata: name: db-migration-script annotations: "helm.sh/hook": pre-install, pre-upgrade - "helm.sh/hook-weight": "-5" + "helm.sh/hook-weight": "-6" # Higher precidence, so the first the config map will get created. data: - pgmigration.sh: |- - set -x - - function chalice_migration() { - - } - - function migration() { - ls -la /opt/openreplay/openreplay - db=$1 - old_version=$2 - - if [[ old_version == {{ .Chart.AppVersion }} ]]; then - echo "No application version change. Not upgrading." - exit 0 - fi - chalice - - cd /opt/openreplay/openreplay/scripts/helm - migration_versions=(`ls -l db/init_dbs/$db | grep -E ^d | awk -v number=${old_version} '$NF > number {print $NF}' | grep -v create`) - echo $migration_versions - } - - function init(){ - echo init db - } - - # migration.sh true/false chalice - case "$1" in - "false") - init - ;; - "true") - migration $2 {{ .Chart.AppVersion }} - ;; - *) - echo "Unknown operation for db migration; exiting." - exit 1 - ;; - esac - + {{- (.Files.Glob "files/*").AsConfig | nindent 2 }} --- apiVersion: batch/v1 kind: Job metadata: - name: postgresql-migrate + name: databases-migrate labels: - app: postgresql + app: databases annotations: "helm.sh/hook": pre-install, pre-upgrade "helm.sh/hook-weight": "-5" @@ -80,17 +40,30 @@ spec: mountPath: /opt/openreplay containers: - name: postgres + env: + - name: CHART_APP_VERSION + value: "{{ .Chart.AppVersion }}" + - name: PGHOST + value: "{{ .Values.postgresql.postgresqlHost }}" + - name: PGPORT + value: "{{ .Values.postgresql.postgresqlPort }}" + - name: PGDATABASE + value: "{{ .Values.postgresql.postgresqlDatabase }}" + - name: PGUSER + value: "{{ .Values.postgresql.postgresqlUser }}" + - name: PGPASSWORD + value: "{{ .Values.postgresql.postgresqlPassword }}" image: bitnami/postgresql:13.3.0-debian-10-r53 command: - /bin/bash - - /opt/migration.sh + - /opt/migrations/dbops.sh - "{{ .Release.IsUpgrade }}" + - "postgresql" volumeMounts: - name: shared mountPath: /opt/openreplay - - name: pgmigrationscript - mountPath: /opt/migration.sh - subPath: pgmigration.sh + - name: dbmigrationscript + mountPath: /opt/migrations/ - name: clickhouse image: yandex/clickhouse-server:20.9 command: @@ -100,9 +73,8 @@ spec: volumeMounts: - name: shared mountPath: /opt/openreplay - - name: clickhousemigrationscript - mountPath: /opt/migration.sh - subPath: pgmigration.sh + - name: dbmigrationscript + mountPath: /opt/migrations/ - name: kafka image: bitnami/kafka:2.6.0-debian-10-r30 command: @@ -112,17 +84,10 @@ spec: volumeMounts: - name: shared mountPath: /opt/openreplay - - name: kafkamigrationscript - mountPath: /opt/migration.sh - subPath: pgmigration.sh + - name: dbmigrationscript + mountPath: /opt/migrations/ volumes: - - name: clickhousemigrationscript - configMap: - name: db-migration-script - - name: kafkamigrationscript - configMap: - name: db-migration-script - - name: pgmigrationscript + - name: dbmigrationscript configMap: name: db-migration-script - name: shared diff --git a/scripts/helm/helm/values.yaml b/scripts/helm/helm/values.yaml index 72306902c..496cff708 100644 --- a/scripts/helm/helm/values.yaml +++ b/scripts/helm/helm/values.yaml @@ -1,12 +1,16 @@ postgresql: # For genrating psswords # `openssl rand -hex 20` - postgresqlPassword: "" + postgresqlPassword: "asdfasdfasdcasdc" + postgresqlHost: "postgresql.db.svc.cluster.local" + postgresqlPort: "5432" + postgresqlUser: "postgres" + postgresqlDatabase: "postgres" minio: global: minio: # For genrating psswords # `openssl rand -hex 20` - accessKey: "" - secretKey: "" + accessKey: "sasdcasdcasd" + secretKey: "sdvasdfasdfasdf"