-
Notifications
You must be signed in to change notification settings - Fork 242
Add jobs for database migration and auto-registration #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6a48497
fd73f3b
eac309d
9d80743
b9bded1
c2b0816
84922c9
0977afd
1368d75
40ba6a9
0e29d1a
ebc9922
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #!/bin/sh | ||
|
|
||
| # Install deps | ||
| apk add postgresql-client curl jq | ||
|
|
||
| # Extract the database name from the end of the PSQL URL, and check it's there | ||
| DB_NAME=`echo ${PSQL_URL} | sed 's/^.*\///'` | ||
| COLONS=`echo -n $DB_NAME | sed 's/[^:]//g'` | ||
| echo "Database name: '${DB_NAME}'" | ||
| if [ -z "${DB_NAME}" ] || [ -n "${COLONS}" ] | ||
| then | ||
| echo "Postgres URL does not appear to contain a database name" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Build a URL that doesn't have the database name | ||
| PSQL_URL_NO_DB=`echo ${PSQL_URL} | sed "s/\/${DB_NAME}//"` | ||
|
|
||
| # Check we can connect to the PSQL Server | ||
| until psql -c "SELECT 1;" ${PSQL_URL_NO_DB}; do | ||
| echo "Waiting for database..." | ||
| sleep 1 | ||
| done | ||
|
|
||
| # Create the database if it doesn't exist | ||
| if ! psql -c "SELECT datname FROM pg_database WHERE datname = '${DB_NAME}';" ${PSQL_URL_NO_DB} | grep ${DB_NAME} | ||
| then | ||
| psql -c "CREATE DATABASE ${DB_NAME};" ${PSQL_URL_NO_DB} | ||
| fi | ||
|
|
||
| # Wait for the database itself to be available | ||
| until psql -c "SELECT 1;" ${PSQL_URL}; do | ||
| echo "Waiting for database..." | ||
| sleep 1 | ||
| done | ||
|
|
||
| # Download the latest migration tool | ||
| MIGRATE_RELEASE=$(curl -sL https://api.github.com/repos/golang-migrate/migrate/releases/latest | jq -r '.name') | ||
| curl -sL https://github.com/golang-migrate/migrate/releases/download/${MIGRATE_RELEASE}/migrate.linux-amd64.tar.gz | tar xz | ||
|
|
||
| # Do the migrations | ||
| ./migrate -database ${PSQL_URL} -path db/migrations/postgres up | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #!/bin/sh | ||
|
|
||
| apk add curl jq | ||
|
|
||
| until STATUS=$(curl ${FF_URL}/api/v1/status); do | ||
| echo "Waiting for FireFly..." | ||
| sleep 5 | ||
| done | ||
|
|
||
| if [ `echo $STATUS | jq -r .org.registered` != "true" ]; then | ||
|
|
||
| echo "Registering organization" | ||
| HTTP_CODE=`curl --silent --output /dev/stderr --write-out "%{http_code}" \ | ||
| -X POST -d '{}' -H 'Content-Type: application/json' \ | ||
| "${FF_URL}/api/v1/network/organizations/self?confirm"` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this API synchronous now? I remember when we wrote the original internal registration job, mimicking what was in ff-cli, we'd poll after org registration to ensure that the org had be published before going onto the node registration.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this was answered by #294 (comment) but could use confirmation from @peterbroadhurst
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yep, with the |
||
| if [ "$HTTP_CODE" -ne 200 ]; then | ||
| echo "Failed to register with code ${HTTP_CODE}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| fi | ||
|
|
||
| if [ `echo $STATUS | jq -r .node.registered` != "true" ]; then | ||
|
|
||
| echo "Registering node" | ||
| HTTP_CODE=`curl --silent --output /dev/stderr --write-out "%{http_code}" \ | ||
| -X POST -d '{}' -H 'Content-Type: application/json' \ | ||
| "${FF_URL}/api/v1/network/nodes/self?confirm"` | ||
| if [ "$HTTP_CODE" -ne 200 ]; then | ||
| echo "Failed to register with code ${HTTP_CODE}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| else | ||
|
|
||
| echo "Already registered. Nothing to do" | ||
|
|
||
| fi | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| {{- if .Values.config.postgresMigrationJob -}} | ||
| apiVersion: batch/v1 | ||
| kind: Job | ||
| metadata: | ||
| name: "{{ template "firefly.fullname" . }}-{{ .Values.core.image.tag }}-migrations" | ||
| spec: | ||
| backoffLimit: 5 | ||
| activeDeadlineSeconds: 12000 | ||
| template: | ||
| spec: | ||
| containers: | ||
| - name: migration | ||
| image: "{{ .Values.core.image.repository }}:{{ .Values.core.image.tag | default .Chart.AppVersion }}" | ||
| command: | ||
| - sh | ||
| - -ce | ||
| - | | ||
| {{ .Files.Get "scripts/ff-db-migrations.sh" | indent 10 }} | ||
| env: | ||
| - name: PSQL_URL | ||
| valueFrom: | ||
| secretKeyRef: | ||
| name: {{ include "firefly.fullname" . }}-config | ||
| key: psql_url | ||
| restartPolicy: Never | ||
| {{- end }} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| {{- if .Values.config.registrationJob -}} | ||
| apiVersion: batch/v1 | ||
| kind: Job | ||
| metadata: | ||
| name: "{{ template "firefly.fullname" . }}-{{ .Values.config.organizationName | lower }}-registration" | ||
| spec: | ||
| backoffLimit: 5 | ||
| activeDeadlineSeconds: 12000 | ||
| template: | ||
| spec: | ||
| containers: | ||
| - name: registration | ||
| image: "{{ .Values.core.image.repository }}:{{ .Values.core.image.tag | default .Chart.AppVersion }}" | ||
| command: | ||
| - sh | ||
| - -ce | ||
| - | | ||
| {{ .Files.Get "scripts/ff-registration.sh" | indent 10 }} | ||
| env: | ||
| - name: FF_URL | ||
| value: "http://{{ include "firefly.fullname" . }}:{{ .Values.core.service.httpPort }}" | ||
| restartPolicy: Never | ||
| {{- end }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clearly doesn't affect the actual job from running but to make the script executable locally would ask the shebang get added.