|
1 | 1 | #!/bin/bash
|
2 | 2 | set -euxo pipefail
|
3 | 3 |
|
4 |
| -DIR=${0%/*} |
5 |
| -IMAGE2TEST="registry.gitlab.com/postgres-ai/database-lab/dblab-server:master" |
| 4 | +TAG="${TAG:-"master"}" |
| 5 | +IMAGE2TEST="registry.gitlab.com/postgres-ai/database-lab/dblab-server:${TAG}" |
6 | 6 | SOURCE_DBNAME="${SOURCE_DBNAME:-test}"
|
7 |
| -SOURCE_HOST="${SOURCE_HOST:-172.31.39.142}" |
| 7 | +SOURCE_HOST="${SOURCE_HOST:-172.17.0.1}" |
| 8 | +SOURCE_PORT="${SOURCE_PORT:-7432}" |
8 | 9 | SOURCE_USERNAME="${SOURCE_USERNAME:-postgres}"
|
9 |
| -SOURCE_PASSWORD="${SOURCE_PASSWORD:-qwerty}" |
10 |
| -POSTGRES_VERSION="${POSTGRES_VERSION:-10}" |
| 10 | +SOURCE_PASSWORD="${SOURCE_PASSWORD:-secretpassword}" |
| 11 | +POSTGRES_VERSION="${POSTGRES_VERSION:-13}" |
| 12 | + |
| 13 | +DIR=${0%/*} |
| 14 | + |
| 15 | +if [[ "${SOURCE_HOST}" = "172.17.0.1" ]]; then |
| 16 | +### Step 0. Create source database |
| 17 | + sudo rm -rf "$(pwd)"/postgresql/"${POSTGRES_VERSION}"/test || true |
| 18 | + sudo docker run \ |
| 19 | + --name postgres"${POSTGRES_VERSION}" \ |
| 20 | + --label pgdb \ |
| 21 | + --privileged \ |
| 22 | + --publish 172.17.0.1:"${SOURCE_PORT}":5432 \ |
| 23 | + --env PGDATA=/var/lib/postgresql/pgdata \ |
| 24 | + --env POSTGRES_USER="${SOURCE_USERNAME}" \ |
| 25 | + --env POSTGRES_PASSWORD="${SOURCE_PASSWORD}" \ |
| 26 | + --env POSTGRES_DB="${SOURCE_DBNAME}" \ |
| 27 | + --env POSTGRES_HOST_AUTH_METHOD=md5 \ |
| 28 | + --volume "$(pwd)"/postgresql/"${POSTGRES_VERSION}"/test:/var/lib/postgresql/pgdata \ |
| 29 | + --detach \ |
| 30 | + postgres:"${POSTGRES_VERSION}-alpine" |
11 | 31 |
|
12 |
| -### Step 1: Prepare a machine with two disks, Docker and ZFS |
| 32 | + for i in {1..300}; do |
| 33 | + sudo docker exec -it postgres"${POSTGRES_VERSION}" psql -d "${SOURCE_DBNAME}" -U postgres -c 'select' > /dev/null 2>&1 && break || echo "test database is not ready yet" |
| 34 | + sleep 1 |
| 35 | + done |
13 | 36 |
|
| 37 | + # Generate data in the test database using pgbench |
| 38 | + # 1,000,000 accounts, ~0.14 GiB of data. |
| 39 | + sudo docker exec -it postgres"${POSTGRES_VERSION}" pgbench -U postgres -i -s 10 "${SOURCE_DBNAME}" |
| 40 | + |
| 41 | + # Database info |
| 42 | + sudo docker exec -it postgres"${POSTGRES_VERSION}" psql -U postgres -c "\l+ ${SOURCE_DBNAME}" |
| 43 | +fi |
| 44 | + |
| 45 | +### Step 1. Prepare a machine with disk, Docker, and ZFS |
14 | 46 | source "${DIR}/_prerequisites.ubuntu.sh"
|
15 | 47 | source "${DIR}/_zfs.file.sh"
|
16 | 48 |
|
17 |
| -### Step 2. Prepare database data directory |
18 | 49 |
|
19 |
| -### Step ?. Configure and launch the Database Lab server |
| 50 | +### Step 2. Configure and launch the Database Lab Engine |
| 51 | + |
| 52 | +# Copy the contents of configuration example |
20 | 53 | mkdir -p ~/.dblab
|
21 |
| -cp ./configs/config.example.logical_generic.yml ~/.dblab/server_test.yml |
22 |
| -sed -ri 's/^(\s\s)(port:.*$)/\1port: 12345/' ~/.dblab/server_test.yml |
23 |
| -sed -ri 's/^(\s*)(debug:.*$)/\1debug: true/' ~/.dblab/server_test.yml |
24 |
| -sed -ri 's/^(\s*)(pool:.*$)/\1pool: "test_pool"/' ~/.dblab/server_test.yml |
25 |
| -sed -ri "s/^(\s*)(host: 34\.56\.78\.90$)/\1host: \"${SOURCE_HOST}\"/" ~/.dblab/server_test.yml |
26 |
| -sed -ri "s/^(\s*)(dbname:.*$)/\1dbname: \"${SOURCE_DBNAME}\"/" ~/.dblab/server_test.yml |
27 |
| -sed -ri "s/^(\s*)(username: postgres$)/\1username: \"${SOURCE_USERNAME}\"/" ~/.dblab/server_test.yml |
28 |
| -sed -ri "s/^(\s*)(password:.*$)/\1password: \"${SOURCE_PASSWORD}\"/" ~/.dblab/server_test.yml |
29 |
| - |
30 |
| -### Step ?. Create source database |
31 |
| -export PGPASSWORD=${SOURCE_PASSWORD} |
32 |
| -psql -h ${SOURCE_HOST} -U ${SOURCE_USERNAME} -c 'drop database test' || echo "database test does not exist" |
33 |
| -psql -h ${SOURCE_HOST} -U ${SOURCE_USERNAME} -c 'create database test' |
34 |
| -pgbench -h ${SOURCE_HOST} -U ${SOURCE_USERNAME} -i -s 10 test |
35 |
| - |
36 |
| -### Step ? Run dblab |
| 54 | + |
| 55 | +curl https://gitlab.com/postgres-ai/database-lab/-/raw/"${TAG}"/configs/config.example.logical_generic.yml \ |
| 56 | + --output ~/.dblab/server.yml |
| 57 | + |
| 58 | +# Edit the following options |
| 59 | +sed -ri "s/^(\s*)(debug:.*$)/\1debug: true/" ~/.dblab/server.yml |
| 60 | +sed -ri "s/^(\s*)(dbname:.*$)/\1dbname: ${SOURCE_DBNAME}/" ~/.dblab/server.yml |
| 61 | +sed -ri "s/^(\s*)(host: 34.56.78.90$)/\1host: ${SOURCE_HOST}/" ~/.dblab/server.yml |
| 62 | +sed -ri "s/^(\s*)(port: 5432$)/\1port: ${SOURCE_PORT}/" ~/.dblab/server.yml |
| 63 | +sed -ri "s/^(\s*)(username: postgres$)/\1username: ${SOURCE_USERNAME}/" ~/.dblab/server.yml |
| 64 | +sed -ri "s/^(\s*)(password:.*$)/\1password: ${SOURCE_PASSWORD}/" ~/.dblab/server.yml |
| 65 | +# replace postgres version |
| 66 | +sed -ri "s/:13/:${POSTGRES_VERSION}/g" ~/.dblab/server.yml |
| 67 | + |
| 68 | +## Launch Database Lab server |
37 | 69 | sudo docker run \
|
38 |
| - --detach \ |
39 |
| - --name dblab_test \ |
| 70 | + --name dblab_server \ |
40 | 71 | --label dblab_control \
|
41 | 72 | --privileged \
|
42 |
| - --publish 12345:12345 \ |
| 73 | + --publish 2345:2345 \ |
43 | 74 | --volume /var/run/docker.sock:/var/run/docker.sock \
|
44 |
| - --volume /var/lib/dblab:/var/lib/dblab:rshared \ |
45 |
| - --volume /var/lib/dblab/db.dump:/var/lib/dblab/db.dump \ |
46 |
| - --volume ~/.dblab/server_test.yml:/home/dblab/configs/config.yml \ |
| 75 | + --volume /var/lib/dblab/dblab_pool/dump:/var/lib/dblab/dblab_pool/dump \ |
| 76 | + --volume /var/lib/dblab:/var/lib/dblab/:rshared \ |
| 77 | + --volume ~/.dblab/server.yml:/home/dblab/configs/config.yml \ |
| 78 | + --env DOCKER_API_VERSION=1.39 \ |
| 79 | + --detach \ |
47 | 80 | "${IMAGE2TEST}"
|
48 | 81 |
|
49 |
| -sudo docker logs -f dblab_test 2>&1 | awk '{print "[CONTAINER dblab_test]: "$0}' & |
| 82 | +# Check the Database Lab Engine logs |
| 83 | +sudo docker logs dblab_server -f 2>&1 | awk '{print "[CONTAINER dblab_server]: "$0}' & |
50 | 84 |
|
51 |
| -### Waiting fori dblab initialization |
52 |
| -for i in {1..30}; do |
53 |
| - curl http://localhost:12345 > /dev/null 2>&1 && break || echo "dblab is not ready yet" |
54 |
| - sleep 10 |
| 85 | +### Waiting for the Database Lab Engine initialization. |
| 86 | +for i in {1..300}; do |
| 87 | + curl http://localhost:2345 > /dev/null 2>&1 && break || echo "dblab is not ready yet" |
| 88 | + sleep 1 |
55 | 89 | done
|
56 | 90 |
|
57 |
| -### Step ?. Setup Dnd init atabase Lab client CLI |
| 91 | + |
| 92 | +### Step 3. Start cloning |
| 93 | + |
| 94 | +# Install Database Lab client CLI |
58 | 95 | curl https://gitlab.com/postgres-ai/database-lab/-/raw/master/scripts/cli_install.sh | bash
|
| 96 | +sudo mv ~/.dblab/dblab /usr/local/bin/dblab |
| 97 | + |
59 | 98 | dblab --version
|
60 |
| -dblab init --url http://localhost:12345 --token secret_token --environment-id test |
| 99 | + |
| 100 | +# Initialize CLI configuration |
| 101 | +dblab init \ |
| 102 | + --environment-id=test \ |
| 103 | + --url=http://localhost:2345 \ |
| 104 | + --token=secret_token \ |
| 105 | + --insecure |
| 106 | + |
| 107 | +# Check the configuration by fetching the status of the instance: |
61 | 108 | dblab instance status
|
62 | 109 |
|
63 |
| -### Step ?. Create clone and connect to it |
64 |
| -dblab clone create --username testuser --password testuser --id testclone |
65 |
| -dblab clone list |
66 |
| -export PGPASSWORD=testuser |
67 |
| -psql "host=localhost port=6000 user=testuser dbname=test" -c '\l' |
68 | 110 |
|
| 111 | +## Create a clone |
| 112 | +dblab clone create \ |
| 113 | + --username dblab_user_1 \ |
| 114 | + --password secret_password \ |
| 115 | + --id testclone |
| 116 | + |
| 117 | +# Connect to a clone and check the available table |
| 118 | +PGPASSWORD=secret_password psql \ |
| 119 | + "host=localhost port=6000 user=dblab_user_1 dbname=${SOURCE_DBNAME}" -c '\dt+' |
| 120 | + |
| 121 | +# Drop table |
| 122 | +PGPASSWORD=secret_password psql \ |
| 123 | + "host=localhost port=6000 user=dblab_user_1 dbname=${SOURCE_DBNAME}" -c 'drop table pgbench_accounts' |
| 124 | + |
| 125 | +PGPASSWORD=secret_password psql \ |
| 126 | + "host=localhost port=6000 user=dblab_user_1 dbname=${SOURCE_DBNAME}" -c '\dt+' |
69 | 127 |
|
70 |
| -### Step 6. Reset clone |
71 |
| -psql "host=localhost port=6000 user=testuser dbname=test" -c 'create database reset_database'; |
72 |
| -psql "host=localhost port=6000 user=testuser dbname=test" -c '\l' |
| 128 | +## Reset clone |
73 | 129 | dblab clone reset testclone
|
| 130 | + |
| 131 | +# Check the status of the clone |
74 | 132 | dblab clone status testclone
|
75 |
| -psql "host=localhost port=6000 user=testuser dbname=test" -c '\l' |
76 |
| -dblab clone destroy testclone |
77 | 133 |
|
78 |
| -### Step 7. Destroy clone |
79 |
| -dblab clone create --username testuser --password testuser --id testclone2 |
80 |
| -dblab clone list |
81 |
| -dblab clone destroy testclone2 |
| 134 | +# Check the database objects (everything should be the same as when we started) |
| 135 | +PGPASSWORD=secret_password psql \ |
| 136 | + "host=localhost port=6000 user=dblab_user_1 dbname=${SOURCE_DBNAME}" -c '\dt+' |
| 137 | + |
| 138 | +### Step 4. Destroy clone |
| 139 | +dblab clone destroy testclone |
82 | 140 | dblab clone list
|
| 141 | + |
| 142 | +### Finish. clean up |
| 143 | +source "${DIR}/_cleanup.sh" |
0 commit comments