From ffb22c36d81d8db14b8f46a304fa6b8c09d1dddb Mon Sep 17 00:00:00 2001 From: Moritz Eysholdt Date: Tue, 13 Jul 2021 09:17:48 +0000 Subject: [PATCH] Make TypeORM accessible --- components/gitpod-db/BUILD.yaml | 2 ++ components/gitpod-db/leeway.Dockerfile | 2 ++ components/gitpod-db/migrate.sh | 3 +-- components/gitpod-db/migrate_gcp.sh | 13 +--------- components/gitpod-db/typeorm.sh | 9 +++++++ components/gitpod-db/typeorm_gcp.sh | 33 ++++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 14 deletions(-) create mode 100755 components/gitpod-db/typeorm.sh create mode 100755 components/gitpod-db/typeorm_gcp.sh diff --git a/components/gitpod-db/BUILD.yaml b/components/gitpod-db/BUILD.yaml index 68827965123e11..ca5afb33367899 100644 --- a/components/gitpod-db/BUILD.yaml +++ b/components/gitpod-db/BUILD.yaml @@ -78,6 +78,8 @@ packages: srcs: - migrate.sh - migrate_gcp.sh + - typeorm.sh + - typeorm_gcp.sh deps: - :migrations argdeps: diff --git a/components/gitpod-db/leeway.Dockerfile b/components/gitpod-db/leeway.Dockerfile index e9f9cb993b2e2e..3df5fa9512c8f8 100644 --- a/components/gitpod-db/leeway.Dockerfile +++ b/components/gitpod-db/leeway.Dockerfile @@ -14,6 +14,8 @@ RUN wget https://storage.googleapis.com/cloudsql-proxy/v1.23.0/cloud_sql_proxy.l FROM node:12.18.3-slim COPY migrate.sh /app/migrate.sh COPY migrate_gcp.sh /app/migrate_gcp.sh +COPY typeorm.sh /app/typeorm.sh +COPY typeorm_gcp.sh /app/typeorm_gcp.sh RUN mkdir /home/jenkins && chown -R 10000 /home/jenkins COPY --from=proxy /bin/cloud_sql_proxy /bin/cloud_sql_proxy COPY --from=proxy /etc/ssl/certs/ /etc/ssl/certs/ diff --git a/components/gitpod-db/migrate.sh b/components/gitpod-db/migrate.sh index c4c64081c3ee25..cb389eb410c397 100755 --- a/components/gitpod-db/migrate.sh +++ b/components/gitpod-db/migrate.sh @@ -5,5 +5,4 @@ set -euo pipefail -yarn --cwd /app/node_modules/@gitpod/gitpod-db run wait-for-db -yarn --cwd /app/node_modules/@gitpod/gitpod-db typeorm migrations:run \ No newline at end of file +/app/typeorm.sh migrations:run \ No newline at end of file diff --git a/components/gitpod-db/migrate_gcp.sh b/components/gitpod-db/migrate_gcp.sh index 4ea3f8e98b2698..10f3e7c8771349 100755 --- a/components/gitpod-db/migrate_gcp.sh +++ b/components/gitpod-db/migrate_gcp.sh @@ -19,15 +19,4 @@ set -euo pipefail -echo "$GOOGLE_APPLICATION_CREDENTIALS_DATA" > /tmp/gcp.json - -# start the proxy and background it -GOOGLE_APPLICATION_CREDENTIALS=/tmp/gcp.json cloud_sql_proxy -instances="$GCP_DATABASE=tcp:3306" & -proxy_pid=$! - -# run db-migrations -DB_PORT=3306 /app/migrate.sh - -# stop the proxy -kill $proxy_pid -wait $proxy_pid +/app/typeorm_gcp.sh migrations:run \ No newline at end of file diff --git a/components/gitpod-db/typeorm.sh b/components/gitpod-db/typeorm.sh new file mode 100755 index 00000000000000..5f421ffd3b99eb --- /dev/null +++ b/components/gitpod-db/typeorm.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# Copyright (c) 2021 Gitpod GmbH. All rights reserved. +# Licensed under the GNU Affero General Public License (AGPL). +# See License-AGPL.txt in the project root for license information. + +set -euo pipefail + +yarn --cwd /app/node_modules/@gitpod/gitpod-db run wait-for-db +yarn --cwd /app/node_modules/@gitpod/gitpod-db typeorm "$@" \ No newline at end of file diff --git a/components/gitpod-db/typeorm_gcp.sh b/components/gitpod-db/typeorm_gcp.sh new file mode 100755 index 00000000000000..a88fcc17d148de --- /dev/null +++ b/components/gitpod-db/typeorm_gcp.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# Copyright (c) 2021 Gitpod GmbH. All rights reserved. +# Licensed under the GNU Affero General Public License (AGPL). +# See License-AGPL.txt in the project root for license information. + +# This scipt connects via Google's cloud_sql_proxy to a database and runs typeorm, e.g. to run or revert db-migrations + +# ENV variables for configuration: +# * GOOGLE_APPLICATION_CREDENTIALS_DATA: contents of the crendetials files that cloud_sql_proxy uses for authentication +# * GCP_DATABASE: database name +# * DB_PASSWORD: database password + +# Example usage: +# docker run --rm \ +# --env GOOGLE_APPLICATION_CREDENTIALS_DATA='...' \ +# --env GCP_DATABASE="gitpod-foobar:europe-west1:gitpod-foobar-baz" \ +# --env DB_PASSWORD="..." \ +# gcr.io/gitpod-core-dev/build/db-migrations:x1 /app/typeorm_gcp.sh migrations:run + +set -euo pipefail + +echo "$GOOGLE_APPLICATION_CREDENTIALS_DATA" > /tmp/gcp.json + +# start the proxy and background it +GOOGLE_APPLICATION_CREDENTIALS=/tmp/gcp.json cloud_sql_proxy -instances="$GCP_DATABASE=tcp:3306" & +proxy_pid=$! + +# run db-migrations +DB_PORT=3306 /app/typeorm.sh "$@" + +# stop the proxy +kill $proxy_pid +wait $proxy_pid