Skip to content

Commit 4f4a7b4

Browse files
authored
dumping DB + bug fix (#695)
* script to dump database * docker push + cron * only push to dockerhub if username/password set * only run on PecanProject/bety * script to easily add users to BETY * make sure to use environment variable for secret The environment variable SECRET_KEY_BASE was ignored. This fixes #696. If you had SECRET_KEY_BASE set, please unset it now, otherwise you can no longer login. load-bety.sh script now uses script/betyuser.sh to add users with correct password.
1 parent 6fdf283 commit 4f4a7b4

File tree

12 files changed

+147
-35
lines changed

12 files changed

+147
-35
lines changed

Diff for: .github/workflows/dbdump.yml

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: DB Dump
2+
3+
on:
4+
schedule:
5+
- cron: '0 0 * * *'
6+
7+
jobs:
8+
sync:
9+
if: github.repository == 'PecanProject/bety'
10+
11+
runs-on: ubuntu-latest
12+
13+
services:
14+
postgres:
15+
image: mdillon/postgis:9.5
16+
ports:
17+
- 5432:5432
18+
options: >-
19+
--health-cmd pg_isready
20+
--health-interval 10s
21+
--health-timeout 5s
22+
--health-retries 5
23+
steps:
24+
- uses: actions/checkout@v2
25+
26+
- name: Setup Database
27+
run: |
28+
psql -h localhost -U postgres -c "CREATE ROLE bety WITH LOGIN CREATEDB NOSUPERUSER NOCREATEROLE PASSWORD 'bety'"
29+
psql -h localhost -U postgres -c "CREATE DATABASE bety WITH OWNER bety"
30+
psql -h localhost -U postgres -d bety -c "CREATE EXTENSION postgis;"
31+
32+
- name: Sync with EBI
33+
run: script/load.bety.sh -a "postgres" -p "-h localhost" -d "bety" -o bety -m 99 -r 0 -c -w https://ebi-forecast.igb.illinois.edu/pecan/dump/bety.tar.gz
34+
35+
- name: Sync with BU
36+
run: script/load.bety.sh -a "postgres" -p "-h localhost" -d "bety" -o bety -m 99 -r 1
37+
38+
- name: Sync with BNL
39+
run: script/load.bety.sh -a "postgres" -p "-h localhost" -d "bety" -o bety -m 99 -r 2 -w ftp://anon:[email protected]/outgoing/betydb/bety.tar.gz
40+
41+
- name: Sync with Wisconsin
42+
run: script/load.bety.sh -a "postgres" -p "-h localhost" -d "bety" -o bety -m 99 -r 5 -w http://tree.aos.wisc.edu:6480/sync/dump/bety.tar.gz
43+
44+
- name: Dump Database
45+
run: pg_dump -h localhost -U postgres -F c bety > initdb/db.dump
46+
47+
- name: Build Docker with Database dump
48+
run: |
49+
cd initdb
50+
docker build --tag image --file Dockerfile .
51+
52+
- name: Login into registry
53+
run: |
54+
echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
55+
if [ -n "${{ secrets.DOCKERHUB_USERNAME }}" -a -n "${{ secrets.DOCKERHUB_PASSWORD }}"]; then
56+
echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
57+
fi
58+
59+
- name: Push docker image
60+
run: |
61+
for T in $(date +'%Y-%V') 'latest'; do
62+
docker tag image docker.pkg.github.com/${{ github.repository }}/db:$T
63+
docker push docker.pkg.github.com/${{ github.repository }}/db:$T
64+
if [ -n "${{ secrets.DOCKERHUB_USERNAME }}" -a -n "${{ secrets.DOCKERHUB_PASSWORD }}"]; then
65+
docker tag image pecan/db:$T
66+
docker push pecan/db:$T
67+
fi
68+
done

Diff for: .github/workflows/release.yml

+7-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ jobs:
2424
- name: Login into registry
2525
run: |
2626
echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
27-
echo "${{ secrets.DOCKERHUB }}" | docker login -u kooper --password-stdin
27+
if [ -n "${{ secrets.DOCKERHUB_USERNAME }}" -a -n "${{ secrets.DOCKERHUB_PASSWORD }}" ]; then
28+
echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
29+
fi
2830
- name: Push image
2931
run: |
3032
# image all lowercase
@@ -62,6 +64,8 @@ jobs:
6264
for T in $TAGS; do
6365
docker tag image $IMAGE_ID:$T
6466
docker push $IMAGE_ID:$T
65-
docker tag image pecan/bety:$T
66-
docker push pecan/bety:$T
67+
if [ -n "${{ secrets.DOCKERHUB_USERNAME }}" -a -n "${{ secrets.DOCKERHUB_PASSWORD }}" ]; then
68+
docker tag image pecan/bety:$T
69+
docker push pecan/bety:$T
70+
fi
6771
done

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ In this case you can simply pull down the version you want, and run `docker-comp
2020
## [Unreleased]
2121

2222
### Fixes
23+
- #696 : actually now uses environment variable SECRET_KEY_BASE. If this was set, unset otherwise you can not login to BETY.
2324
- #551 : remove GUnload message from console on page change.
2425
- #672 : Added activemodel-serializers-xml Gem to restore functionality of "original" API XML endpoints.
2526
- #674 : Upgraded comma Gem to restore CSV file downloads.
@@ -30,8 +31,10 @@ In this case you can simply pull down the version you want, and run `docker-comp
3031
- upgrade nokogiri to 1.10.8 (dependbot fix)
3132

3233
### Added
34+
- script to add new users to bety
3335
- dockerfile to dump database in docker image for faster restore of database.
3436
- switched to github actions instead of Travis
37+
- cronjob on github actions to create pecan/db docker image nightly, will safe also with year-weak (i.e 2020-15)
3538

3639
## [5.2.2] - 2019-12-06
3740

Diff for: Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ ENV LOCAL_SERVER=99 \
4545
INITIALIZE_URL="-w https://ebi-forecast.igb.illinois.edu/pecan/dump/all/bety.tar.gz" \
4646
RAILS_ENV="production" \
4747
RAILS_RELATIVE_URL_ROOT="" \
48-
SECRET_KEY_BASE="ThisIsNotReallySuchAGreatSecret" \
48+
SECRET_KEY_BASE="thisisnotasecret" \
4949
UNICORN_WORKER_PROCESSES="3" \
5050
UNICORN_PORT="8000" \
5151
BETY_GIT_TAGS=${BETY_GIT_TAGS} \

Diff for: config/application.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class Application < Rails::Application
1616
using SymbolizeHelper
1717

1818
# Define top-level Hash constant CONFIG by merging settings in defaults.yml and application.yml.
19-
::CONFIG = YAML.load(File.read(File.expand_path('../defaults.yml', __FILE__))).deep_symbolize_keys
19+
::CONFIG = YAML.load(ERB.new(File.read(File.expand_path('../defaults.yml', __FILE__))).result).deep_symbolize_keys
2020
if File.exists?(File.expand_path('../application.yml', __FILE__))
21-
customizations = YAML.load(File.read(File.expand_path('../application.yml', __FILE__))).deep_symbolize_keys
21+
customizations = YAML.load(ERB.new(File.read(File.expand_path('../application.yml', __FILE__))).result).deep_symbolize_keys
2222
::CONFIG.update customizations
2323
::CONFIG.merge! CONFIG.fetch(Rails.env, {})
2424
end

Diff for: config/defaults.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ homepage_body:
9595
show_crop_map_links: true
9696

9797
# Override this with a secret key to run a secure site:
98-
rest_auth_site_key: 'thisisnotasecret'
98+
rest_auth_site_key: <%= ENV["SECRET_KEY_BASE"] || 'thisisnotasecret' %>

Diff for: docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
- 8000
1111
environment:
1212
- UNICORN_WORKER_PROCESSES=1
13-
- SECRET_KEY_BASE=thisissomereallllllylongsecretkeyandshouldbelongerthanthis
13+
- SECRET_KEY_BASE=thisisnotasecret
1414
depends_on:
1515
- postgres
1616
restart: unless-stopped

Diff for: docker/entrypoint.sh

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ case $1 in
1515
./script/load.bety.sh -a "postgres" -d "bety" -p "-h postgres -p 5432" -o bety -r ${r}
1616
done
1717
;;
18+
"fix" )
19+
echo "Fixing database ID"
20+
./script/load.bety.sh -a "postgres" -d "bety" -p "-h postgres -p 5432" -o bety -f -m ${LOCAL_SERVER} -r -1
21+
;;
1822
"dump" )
1923
echo "Dump data from server ${LOCAL_SERVER}"
2024
./script/dump.bety.sh -d "bety" -p "-h postgres -p 5432 -U postgres" -m ${LOCAL_SERVER} -o dump
@@ -65,6 +69,10 @@ case $1 in
6569
echo "Start running BETY (unicorn)"
6670
exec bundle exec unicorn -c config/unicorn.rb
6771
;;
72+
"user" )
73+
shift
74+
./script/betyuser.sh "$@"
75+
;;
6876
"help" )
6977
echo "initialize : create a new database and initialize with all data from server 0"
7078
echo "sync : synchronize database with remote servers ${REMOTE_SERVERS}"
@@ -78,6 +86,7 @@ case $1 in
7886
echo "vacuum-all : maintenance: vaccum the entire database (not VACUUM FULL)"
7987
echo "vacuum-full: maintenance: full vaccum of the database. Specify rarely, if ever"
8088
echo "autoserver : runs the server (using unicorn) after running a migrate"
89+
echo "user : add a new user to BETY database"
8190
echo "help : this text"
8291
echo ""
8392
echo "Default is to run bety using unicorn. no automatic migrations."

Diff for: initdb/Dockerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# syntax = docker/dockerfile:experimental
22
FROM alpine
33

4-
RUN apk --no-cache add postgresql \
5-
&& pg_dump -h postgres -U postgres -F c bety > /db.dump
6-
COPY initdb.sh /
4+
RUN apk --no-cache add postgresql
5+
ADD initdb.sh db.dump* /
6+
RUN if [ ! -e /db.dump ]; then pg_dump -F c ${PGDATABASE} > /db.dump; fi
77

88
CMD /initdb.sh

Diff for: initdb/initdb.sh

+8
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ psql -h postgres -p 5432 -U postgres -c "CREATE ROLE bety WITH LOGIN CREATEDB NO
66

77
# load database from dump
88
pg_restore -c -C -v -h postgres -U postgres -d postgres -F c /db.dump
9+
10+
# print some hints on what to do next
11+
echo ""
12+
echo "To fix the database id to be 77 instead of the default of 99:"
13+
echo "docker-compose run -e LOCAL_SERVER=77 bety fix"
14+
echo ""
15+
echo "To add a user, you can use:"
16+
echo "docker-compose run bety user 'login' 'password' 'full name' 'email' 1 1"

Diff for: script/betyuser.sh

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
if [ $# != 6 ]; then
4+
echo "$0 username password fullname email data_access page_access"
5+
echo "data access : 1=Restricted, 2=Internal, 3=External, 4=Public"
6+
echo "page_access : 1=Admin, 2=Manager, 3=Creator, 4=Viewer"
7+
exit 1
8+
fi
9+
10+
LOGIN="$1"
11+
PASSWORD="$2"
12+
NAME="$3"
13+
EMAIL="$4"
14+
ACCESS=${5:-2}
15+
PAGE=${6:-2}
16+
17+
SALT="${LOGIN}"
18+
19+
COUNT=10
20+
21+
DIGEST="${SECRET_KEY_BASE}"
22+
for x in $(seq ${COUNT}); do
23+
DIGEST=$(echo -n "${DIGEST}--${SALT}--${PASSWORD}--${SECRET_KEY_BASE}" | sha1sum | awk '{print $1}')
24+
done
25+
26+
psql -q -h postgres -U bety -t -c "INSERT INTO users (login, name, email, crypted_password, salt, access_level, page_access_level, created_at, updated_at) VALUES ('${LOGIN}', '${NAME}', '${EMAIL}', '${DIGEST}', '${SALT}', ${ACCESS}, ${PAGE}, NOW(), NOW())"
27+
28+
if [ $? == 0 ]; then
29+
echo "User ($LOGIN) has been added to database"
30+
else
31+
echo "Could not add user to database"
32+
fi

Diff for: script/load.bety.sh

+12-24
Original file line numberDiff line numberDiff line change
@@ -238,27 +238,29 @@ fi
238238

239239
# check the database for information first
240240
if [ -z "${DUMPURL}" ]; then
241-
DUMPURL=$(psql ${PG_OPT} ${PG_USER} -q -d "${DATABASE}" -t -c "SELECT sync_url FROM machines WHERE sync_host_id = ${REMOTESITE};" | xargs )
241+
DUMPURL=$(psql ${PG_OPT} ${PG_USER} -q -d "${DATABASE}" -t -c "SELECT sync_url FROM machines WHERE sync_host_id = ${REMOTESITE};" 2>/dev/null | xargs )
242242
fi
243243
if [ -z "${DUMPURL}" ]; then
244-
echo "Did not find a sync_url in database, please update database, or provide script with DUMPURL."
245-
exit -1
244+
if [ "${FIXSEQUENCE}" != "YES" -o "${CREATE}" == "YES" ]; then
245+
echo "Did not find a sync_url in database, please update database, or provide script with DUMPURL."
246+
exit -1
247+
fi
246248
fi
247249

248-
MY_START_ID=$(psql ${PG_OPT} ${PG_USER} -q -d "${DATABASE}" -t -c "SELECT sync_start FROM machines WHERE sync_host_id = ${MYSITE};" | xargs )
250+
MY_START_ID=$(psql ${PG_OPT} ${PG_USER} -q -d "${DATABASE}" -t -c "SELECT sync_start FROM machines WHERE sync_host_id = ${MYSITE};" 2>/dev/null | xargs )
249251
if [ -z "${MY_START_ID}" ]; then
250252
MY_START_ID=$(( MYSITE * ID_RANGE + 1 ))
251253
fi
252-
MY_LAST_ID=$(psql ${PG_OPT} ${PG_USER} -q -d "${DATABASE}" -t -c "SELECT sync_end FROM machines WHERE sync_host_id = ${MYSITE};" | xargs )
254+
MY_LAST_ID=$(psql ${PG_OPT} ${PG_USER} -q -d "${DATABASE}" -t -c "SELECT sync_end FROM machines WHERE sync_host_id = ${MYSITE};" 2>/dev/null | xargs )
253255
if [ -z "${MY_LAST_ID}" ]; then
254256
MY_LAST_ID=$(( MY_START_ID + ID_RANGE - 2 ))
255257
fi
256258

257-
REM_START_ID=$(psql ${PG_OPT} ${PG_USER} -q -d "${DATABASE}" -t -c "SELECT sync_start FROM machines WHERE sync_host_id = ${REMOTESITE};" | xargs )
259+
REM_START_ID=$(psql ${PG_OPT} ${PG_USER} -q -d "${DATABASE}" -t -c "SELECT sync_start FROM machines WHERE sync_host_id = ${REMOTESITE};" 2>/dev/null | xargs )
258260
if [ -z "${REM_START_ID}" ]; then
259-
REM_START_ID=$(( MYSITE * ID_RANGE + 1 ))
261+
REM_START_ID=$(( REMOTESITE * ID_RANGE + 1 ))
260262
fi
261-
REM_LAST_ID=$(psql ${PG_OPT} ${PG_USER} -q -d "${DATABASE}" -t -c "SELECT sync_end FROM machines WHERE sync_host_id = ${REMOTESITE};" | xargs )
263+
REM_LAST_ID=$(psql ${PG_OPT} ${PG_USER} -q -d "${DATABASE}" -t -c "SELECT sync_end FROM machines WHERE sync_host_id = ${REMOTESITE};" 2>/dev/null | xargs )
262264
if [ -z "${REM_LAST_ID}" ]; then
263265
REM_LAST_ID=$(( REM_START_ID + ID_RANGE - 2 ))
264266
fi
@@ -454,25 +456,11 @@ if [ "${USERS}" == "YES" ]; then
454456
echo "SELECT count(id) FROM users WHERE login='carya';" >&3 && read RESULT <&4
455457
if [ ${RESULT} -eq 0 ]; then
456458
echo "SELECT nextval('users_id_seq');" >&3 && read ID <&4
457-
echo "INSERT INTO users (login, name, email, crypted_password, salt, city, state_prov, postal_code, country, area, access_level, page_access_level, created_at, updated_at, apikey, remember_token, remember_token_expires_at) VALUES ('carya', 'carya', 'betydb+${ID}@gmail.com', 'df8428063fb28d75841d719e3447c3f416860bb7', 'carya', 'Urbana', 'IL', '61801', 'USA', '', 1, 1, NOW(), NOW(), NULL, NULL, NULL);" >&3
459+
$(dirname $0)/betyuser.sh "carya" "illinois" "carya" "betydb+${ID}@gmail.com" 1 1
458460
if [ "${QUIET}" != "YES" ]; then
459461
echo "Added carya with admin privileges with id=${ID}"
460462
fi
461463
fi
462-
463-
# add other users with specific rights
464-
for f in 1 2 3 4; do
465-
for g in 1 2 3 4; do
466-
echo "SELECT count(id) FROM users WHERE login='carya${f}${g}';" >&3 && read RESULT <&4
467-
if [ ${RESULT} -eq 0 ]; then
468-
echo "SELECT nextval('users_id_seq');" >&3 && read ID <&4
469-
echo "INSERT INTO users (login, name, email, crypted_password, salt, city, state_prov, postal_code, country, area, access_level, page_access_level, created_at, updated_at, apikey, remember_token, remember_token_expires_at) VALUES ('carya${f}${g}', 'carya${f}${g}', 'betydb+${ID}@gmail.com', 'df8428063fb28d75841d719e3447c3f416860bb7', 'carya', 'Urbana', 'IL', '61801', 'USA', '', $f, $g, NOW(), NOW(), NULL, NULL, NULL);" >&3
470-
if [ "${QUIET}" != "YES" ]; then
471-
echo "Added carya$f$g with access_level=$f and page_access_level=$g with id=${ID}"
472-
fi
473-
fi
474-
done
475-
done
476464
fi
477465

478466
# Add guest user
@@ -481,7 +469,7 @@ if [ "${GUESTUSER}" == "YES" ]; then
481469
echo "SELECT count(id) FROM users WHERE login='guestuser';" >&3 && read RESULT <&4
482470
if [ ${RESULT} -eq 0 ]; then
483471
echo "SELECT nextval('users_id_seq');" >&3 && read ID <&4
484-
echo "INSERT INTO users (login, name, email, crypted_password, salt, city, state_prov, postal_code, country, area, access_level, page_access_level, created_at, updated_at, apikey, remember_token, remember_token_expires_at) VALUES ('guestuser', 'guestuser', 'betydb+${ID}@gmail.com', '994363a949b6486fc7ea54bf40335127f5413318', 'bety', 'Urbana', 'IL', '61801', 'USA', '', 4, 4, NOW(), NOW(), NULL, NULL, NULL);" >&3
472+
$(dirname $0)/betyuser.sh "guestuser" "guestuser" "guestuser" "betydb+${ID}@gmail.com" 4 4
485473
if [ "${QUIET}" != "YES" ]; then
486474
echo "Added guestuser with access_level=4 and page_access_level=4 with id=${ID}"
487475
fi

0 commit comments

Comments
 (0)