-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-postgres.sh
More file actions
executable file
·103 lines (80 loc) · 3.27 KB
/
build-postgres.sh
File metadata and controls
executable file
·103 lines (80 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
# Create a postgres Docker image that contains the Pennsieve API schema.
set -eu
REGISTRY="pennsieve"
REPO="pennsievedb"
ENVIRONMENT="${ENVIRONMENT:-local}"
if [[ "$ENVIRONMENT" != "local" ]]; then
echo "Environment is $ENVIRONMENT. Getting tag from git log and pushing images"
TAG="$(git log --name-only --oneline -2 | tail -n +2 | grep -E migrations/src/main/resources/db | xargs basename -a | grep -E '^V.*' | awk -F '_' '{print $1}' | sort -u | tail -n 1)"
else
echo "Environment is local. Getting tag from local filesystem and not pushing images"
TAG="$(find migrations/src/main/resources/db | xargs basename -a | grep -E '^V.*' | awk -F '_' '{print $1}' | sort -u | tail -n 1)"
fi
###################################
run_migrations() {
echo -e "\nBuilding database container.\n"
docker-compose down -v --remove-orphans
docker-compose rm -f
docker-compose build --pull
sbt migrations/docker
echo -e "\nStarting database container.\n"
docker-compose up -d postgres && sleep 5
container=$(docker-compose ps postgres | tail -n 1 | awk '{print $1}')
echo -e "\nRunning migrations....\n"
docker-compose run -e MIGRATION_TYPE=timeseries -e MIGRATION_BASELINE=true migrations
docker-compose run -e MIGRATION_TYPE=core -e MIGRATION_BASELINE=true migrations
while true; do
HEALTH=$(docker inspect --format='{{.State.Health.Status}}' $container)
[ "$HEALTH" != healthy ] || break
sleep 1
done
echo -e "\nMigrations complete.\n"
}
create_container() {
tag=$1
container=$(docker-compose ps postgres | tail -n 1 | awk '{print $1}')
container_id=$(docker inspect --format='{{.Id}}' $container)
echo -e "\nCreating new pennsieve/pennsievedb:$tag from $container_id\n"
docker-compose stop
if [[ $tag =~ .*seed.* ]]; then
docker commit $container_id $REGISTRY/$REPO:latest-seed
docker tag $REGISTRY/$REPO:latest-seed $REGISTRY/$REPO:$tag
if [[ "$ENVIRONMENT" != "local" ]]; then
echo -e "\nPushing $REGISTRY/$REPO:latest-seed\n"
docker push $REGISTRY/$REPO:latest-seed
echo -e "\nPushing $REGISTRY/$REPO:$tag\n"
docker push $REGISTRY/$REPO:$tag
fi
else
docker commit $container_id $REGISTRY/$REPO
docker tag $REGISTRY/$REPO:latest $REGISTRY/$REPO:$tag
if [[ "$ENVIRONMENT" != "local" ]]; then
echo -e "\nPushing $REGISTRY/$REPO:$tag\n"
docker push $REGISTRY/$REPO:$tag
echo -e "\nPushing $REGISTRY/$REPO:latest\n"
docker push $REGISTRY/$REPO
fi
fi
}
seed_db_container() {
docker-compose up -d postgres && sleep 5
docker-compose run -e MIGRATION_TYPE=organization -e ORGANIZATION_SCHEMA_COUNT=3 migrations
container=$(docker-compose ps postgres | tail -n 1 | awk '{print $1}')
docker cp local-seed.sql $container:/local-seed.sql
docker-compose exec -T postgres sh -c "psql -v ON_ERROR_STOP=1 postgres < /local-seed.sql"
}
###################################
echo -e "\nStarting build-postgres script...\n"
if [[ ! -z $TAG ]]; then
echo -e "\nCreating a new image with tag: $TAG\n"
run_migrations
create_container $TAG
seed_db_container
create_container $TAG-seed
docker-compose down -v
docker-compose rm -f
else
echo -e "\nCould not find a valid tag. Not creating a pennsievedb image.\n"
fi
echo -e "\nThe build-postgres script is complete.\n"