-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpdns_dev
executable file
·212 lines (175 loc) · 5.58 KB
/
pdns_dev
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
#!/bin/bash
PDNS_PATH_LOCATION="./data/internal/PDNS_PATH"
function commit_container(){
docker commit $(docker ps -lq) $1
}
function remove_container(){
docker rm $(docker ps -lq)
}
function read_pdns_location(){
if [[ ! -e "$PDNS_PATH_LOCATION" ]]; then
PDNS_LOCATION=""
return 1
fi
PDNS_LOCATION=$(cat $PDNS_PATH_LOCATION)
PDNS_LOCATION=${PDNS_LOCATION%/}
}
function check_pdns_repo(){
if [[ ! -e "$PDNS_PATH_LOCATION" ]]; then
return 1
elif [[ ! -e "$PDNS_LOCATION/bootstrap" ]]; then
>&2 echo "Invalid pdns location: File 'bootstrap' not found!"
return 2
elif [[ ! -e "$PDNS_LOCATION/configure" ]]; then
>&2 echo "Invalid pdns location: File 'configure' not found!"
return 3
fi
return 0
}
function init(){
# Delete images created
docker images | grep pdns_ | grep -v pdns_base | awk '{ print $1 }' | xargs -r docker rmi >/dev/null 2>&1
# Delete the old base image
docker rmi "pdns_base" >/dev/null 2>&1
# Rebuild the base image
docker build -t pdns_base ./data/internal
}
function check_build_type(){
if [ "$1" == "server" ]; then
return 0
elif [ "$1" == "test" ]; then
return 0
else
echo "Usage: pdns_dev $2 {server|test}"
exit -1
fi
}
function get_module(){
if [ "$1" == "server" ]; then
if [ ! -z "$2" ]; then
module="$2"
else
module="gmysql"
fi
if [[ ! -e "./data/backends/$module" ]]; then
echo "Directory 'data/backends/$module' not found!"
exit -1
elif [[ ! -e "./data/backends/$module/build/" ]]; then
echo "Directory 'data/backends/$module/build' not found!"
exit -1
elif [[ ! -e "./data/backends/$module/build/Dockerfile" ]]; then
echo "Dockerfile 'data/backends/$module/build/Dockerfile' not found!"
exit -1
elif [[ ! -e "./data/backends/$module/conf" ]]; then
echo "Directory 'data/backends/$module/conf' not found!"
exit -1
fi
elif [ "$1" == "test" ]; then
module="gsqlite3"
fi
echo $module
}
function get_image(){
if [ "$1" == "server" ]; then
echo "pdns_server_$(get_module $1 $2)"
elif [ "$1" == "test" ]; then
echo "pdns_test"
fi
}
function configure(){
check_build_type "$1" "configure"
module=$(get_module $1 $2)
image=$(get_image $1 $2)
# Cleanup
docker rmi $image >/dev/null 2>&1
docker rmi $(docker images -q -f dangling=true) >/dev/null 2>&1
if [ "$1" == "server" ]; then
# Check if commands-file exists
if [[ -e "data/backends/$module/commands" ]]; then
# Fill additional variable with given commands e.g. apt-get install whatever-db
additional="$(cat data/backends/$module/commands)"
additional="$additional;"
fi
fi
# Execute "additional" commands and bootstrap/configure for pdns
docker run -v "$PDNS_LOCATION:/usr/src/pdns/" pdns_base /bin/bash -c "$additional cd /usr/src/pdns/; ./bootstrap; ./configure --with-modules=\"$module\" --without-lua"
commit_container $image
remove_container
}
function make_pdns(){
check_build_type "$1" "make"
module=$(get_module $1 $2)
image=$(get_image $1 $2)
if [ "$1" == "server" ]; then
docker run --name "pdns_make" $image /bin/bash -c "mkdir -p /etc/powerdns/pdns.d/; rm -f /etc/powerdns/pdns.d/*.conf"
docker cp data/internal/pdns.conf pdns_make:/etc/powerdns/
for conf_file in "data/backends/$module/conf/*.conf"; do
docker cp $conf_file pdns_make:/etc/powerdns/pdns.d/
done
commit_container $image
remove_container
fi
docker run -v "$PDNS_LOCATION:/usr/src/pdns/" $image /bin/bash -c "make -j5; make install"
commit_container $image
remove_container
}
function run_pdns(){
check_build_type "$1" "run"
module=$(get_module $1 $2)
if [ "$1" == "server" ]; then
cd data/internal/compose
echo "Starting server with backend '$module'..."
# Link the selected backend
rm -f pdns_backend
ln -s "../../backends/$module/build/" pdns_backend
# Set the environment variable for docker-compose
export MODULE=$module
# Check if backend provides an environment file and link it
rm -f environment
if [[ -e "../../backends/$module/environment" ]]; then
echo "Linking environment file."
ln "../../backends/$module/environment" environment
else
echo "No environment file provided."
echo "" > environment
fi
# Substitutes the enviroment variables in template.yml and writes to docker-compose.yml
# The .yml is generated for each backend, so the databases won't always be rebuilt
envsubst < "template.yml" > "docker-compose.yml"
# Start the containers
docker-compose up
# Remove the server container (otherwise the image can't be removed afterwards)
docker-compose rm -f pdns
elif [ "$1" == "test" ]; then
docker run -v "$PDNS_LOCATION:/usr/src/pdns/" pdns_test /bin/bash -c "cd regression-tests.api; ./runtests authoritative"
remove_container
fi
}
function terminal_pdns(){
docker exec -it compose_pdns_1 /bin/bash
}
read_pdns_location
while ! check_pdns_repo; do
read -e -p "Please enter the path to your pdns repository: " path
echo $path > $PDNS_PATH_LOCATION
read_pdns_location
done
if [ "$1" == "init" ]; then
init
elif [ "$1" == "configure" ]; then
configure $2 $3
elif [ "$1" == "make" ]; then
make_pdns $2 $3
elif [ "$1" == "run" ]; then
run_pdns $2 $3
elif [ "$1" == "terminal" ]; then
terminal_pdns
else
echo "Usage: pdns_dev [Command] [Type] [Backend]" && echo
echo "Commands:"
echo " init Inits the base image with installed dependencies."
echo " configure Configures the compiler for the given build type. Types: {server|test}"
echo " make Compiles pdns with the given build type. Types: {server|test}"
echo " run Runs pdns with the given build type. Types: {server|test}"
echo " terminal Opens a terminal in the active \"pdns_dev run server\""
fi