-
Notifications
You must be signed in to change notification settings - Fork 0
/
op
executable file
·220 lines (207 loc) · 7.9 KB
/
op
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash
#
# This is the list of Pg versions that we'll build
# and create containers for.
#
declare -a v9_4=("9.4.0" "9.4.1" "9.4.2" "9.4.3" "9.4.4" "9.4.5" "9.4.6" "9.4.7" "9.4.8" "9.4.9")
declare -a v9_5=("9.5.0" "9.5.1" "9.5.2" "9.5.3" "9.5.4" "9.5.5" "9.5.6" "9.5.7" "9.5.8" "9.5.9")
declare -a v9_6=("9.6.0" "9.6.1" "9.6.2" "9.6.3" "9.6.4" "9.6.5" "9.6.6" "9.6.7" "9.6.8" "9.6.9")
# declare -a versions=( "9.4.0" )
# declare -a versions=( "${v9_4[@]}" "${v9_6[@]}")
declare -a versions=( "${v9_4[@]}" "${v9_5[@]}" "${v9_6[@]}" )
# declare -a versions=("${v9_6[@]}")
#
# This function is intended to extract from the Pg server logs, the latest
# chunk of logs that was generated by a pg_restore operation.
#
extract_latest_pgrestore_logs() {
perl -ne '
BEGIN{
@a=(0);
$i=-1;
$s=0;
};
if(m{restore_start}) {
$i++;
$a[$i].=$_;
$s=1;
} elsif(m{restore_end} && $s == 1) {
$a[$i].=$_;
$s=0;
} elsif($s) {
$a[$i].=$_;
};
END {
print $a[-1];
}
'
}
case "$1" in
run)
# Run a command on all containers
# Example: ./op run /bin/ls /usr/local/pgsql/bin/psql
# Leave only the command and args to run in $@
shift 1
for ver in "${versions[@]}"; do
echo "Running for pg-$ver"
cmd=$(IFS=$' '; echo "${@}" )
docker exec -t -i "pg-$ver" bash -c "$cmd"
done
;;
make_image)
# Build the image called "pg-test" from the Dockerfile
docker build -f ./Dockerfile -t pg-test .
;;
make_containers)
# Create containers using the "pg-test" image that was created
for ver in "${versions[@]}"; do
echo "Making pg-$ver"
docker create --name "pg-$ver" pg-test
docker start "pg-$ver"
done
;;
start_containers)
for ver in "${versions[@]}"; do
echo "Starting container pg-$ver"
docker start "pg-$ver"
done
;;
stop_containers)
for ver in "${versions[@]}"; do
echo "Stopping container pg-$ver"
docker stop "pg-$ver"
done
;;
rm_containers)
# Delete all containers
for ver in "${versions[@]}"; do
docker stop "pg-$ver"
docker rm "pg-$ver"
done
;;
make_pg)
# Builds the PostgreSQL on each of the containers
# (each container should have a different version of PostgreSQL)
for ver in "${versions[@]}"; do
# use naming convention for Pg tags for releases
git_tag="REL"$(echo $ver | sed -e 's/\./_/g')
git_branch="rel-$ver"
docker exec -t -i "pg-$ver" bash -c "cd /root/postgres; git checkout -b $git_branch $git_tag"
docker cp buildpg pg-$ver:/root/postgres/
docker exec -d -t -i "pg-$ver" /root/postgres/buildpg
done
;;
make_pg_cluster)
# Creates the PostgreSQL cluster (data directory) on all the containers
for ver in "${versions[@]}"; do
# create a /var/lib/postgresql/ from scratch if one is already present, this is going to
# be the data directory.
docker exec -t -i pg-$ver bash -c 'rm -rf /var/lib/postgresql/; mkdir /var/lib/postgresql/; chown -R postgres:postgres /var/lib/postgresql/'
# create the pg cluster
docker exec -u postgres -t -i pg-$ver bash -c '/usr/local/pgsql/bin/pg_ctl init -D /var/lib/postgresql/'
# copy the configs and change ower/group to postgres
docker cp postgresql.custom.conf pg-$ver:/var/lib/postgresql/postgresql.conf
docker exec -t -i pg-$ver chown postgres:postgres /var/lib/postgresql/postgresql.conf
done
;;
shell_pg)
# Shell using the postgres user into the container
# Ex: ./op shell_pg 9.6.8
docker exec -u postgres -t -i pg-$2 /bin/bash -c "export COLUMNS=`tput cols`; export LINES=`tput lines`; exec bash"
;;
shell_root)
# Root shell into the container
# Ex: ./op shell_root 9.6.8
docker exec -t -i pg-$2 /bin/bash -c "export COLUMNS=`tput cols`; export LINES=`tput lines`; exec bash"
;;
start_pg)
# Starts the PostgreSQL server on all the containers
# (also provide them with the right config file using postgresql.custom.conf)
for ver in "${versions[@]}"; do
echo "Starting pg on container pg-$ver"
docker exec -u postgres -d -t -i pg-$ver bash -c 'nohup /usr/local/pgsql/bin/pg_ctl -D /var/lib/postgresql -l /var/log/postgresql/logfile start'
done
;;
stop_pg)
# Stop the PostgreSQL server on all containers
for ver in "${versions[@]}"; do
echo "Stopping pg on container pg-$ver"
docker exec -u postgres -t -i pg-$ver bash -c '/usr/local/pgsql/bin/pg_ctl stop -D /var/lib/postgresql'
done
;;
run_pretest)
for ver in "${versions[@]}"; do
echo "Provisioning container pg-$ver to run tests"
docker exec -t -i pg-$ver rm -rf /home/postgres/t/
docker cp t/ pg-$ver:/home/postgres/
docker exec -t -i pg-$ver chown -R postgres:postgres /home/postgres/t/
done
;;
run_t1)
# Run test related to persistence of settings in a functional index
# during a pg_restore operation
for ver in "${versions[@]}"; do
echo "Running t1 on pg-$ver"
docker exec -u postgres -t -i pg-$ver bash /home/postgres/t/config-setting-persistence-index.sh
done
;;
run_t2)
# Run test related to compatibility of custom dump format
# between different Pg versions
#
# Create dbs on each container and dump them in their custom format.
# Copy the dumps outside the containers in a directory.
#
mkdir tmp_dmp/
for ver in "${versions[@]}"; do
echo "Creating db on pg-$ver"
docker exec -u postgres -t -i pg-$ver bash /home/postgres/t/dump-compat.sh
docker cp pg-$ver:/home/postgres/c1.dmp tmp_dmp/$ver.dmp
done
# Distribute the dumps to all containers
for ver in "${versions[@]}"; do
echo "Copying dump from pg-$ver"
docker cp tmp_dmp/ pg-$ver:/home/postgres/
done
# Now restore all the dumps on all the containers
# and get the exit code for each
for ver in "${versions[@]}"; do
echo "Running restores on pg-$ver"
docker exec -u postgres pg-$ver bash /home/postgres/t/dump-restore.sh
done
;;
run_tests)
# Run the tests on all containers
$0 run_pretest
$0 run_t1
$0 run_t2
;;
report)
exec &> >(tee "t/config-setting-persistence-index.report.txt")
echo "Report for the test config-setting-persistence-index"
echo "===================================================="
echo "Note: we're counting the lines \"prefix.persist was not set\" found in server logs"
echo "for the last pg_restore operation"
echo ""
for ver in "${versions[@]}"; do
tmpfile=$(mktemp /tmp/pg.$ver.XXXXX.log)
docker exec -t -i pg-$ver cat /var/log/postgresql/logfile | \
grep "LOG:" | \
extract_latest_pgrestore_logs > $tmpfile
cnt=$(cat $tmpfile | grep "prefix.persist was not set" | wc -l)
echo "pg-$ver,$cnt"
rm $tmpfile
done
exec &> >(tee "t/dump-compatibility.report.txt")
echo "Report for custom format dump compatibility"
echo "==========================================="
echo ""
for ver in "${versions[@]}"; do
echo "Report from $ver"
docker exec -t pg-$ver cat /home/postgres/restore-report.txt
done
;;
*)
echo "none"
;;
esac