-
Notifications
You must be signed in to change notification settings - Fork 5
/
d4st
executable file
·371 lines (358 loc) · 13.4 KB
/
d4st
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/bin/sh
#
# OS X shell script to run the D4ST Docker containers via
# docker-compose.
#
# Commands:
#
# start (default): Starts the services
# stop : Stops the services
# down : Stop containers and remove containers, networks, volumes, and images created by start.
# kill : Kills the services
# pause : Pauses the services
# unpause : Unpauses the services
# pull : Pull new images
# gitlab : Open GitLab in chrome
# getdemo : Get the demo content from the demo content container
#
# Requires that the docker-compose command be in the
# path.
#
# FIXME: There is a lot of redundancy here but my bash script fu is
# weak and I haven't taken the time to work out how to set up
# proper functions for the duplicated code.
D4ST_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DOTENV="$D4ST_DIR/.env"
# echo "DOTENV=\"$DOTENV\""
if [ -f $DOTENV ]; then
# Use the .env file as the basis for setting the environment variables.
# echo "Using .env file to set environmnent variables..."
while read line
do
# echo "$line"
if [[ "$line" != "#"* ]]; then
# Handle Windows \r\n line endings, e.g., .env file
# was edited with WordPad.
line=$(echo "$line" | tr -d '\015')
export "$line"
fi
done < "$DOTENV"
else
# echo "No .env file, using default environment settings."
export BASEXAPI_PORT=1984
export GITLAB_PORT=90
export GITLAB_SSH_PORT=9022
export LINKMGR_PORT=8984
export WEBDELIVERY_PORT=80
export OXYWEBAUTHOR_PORT=8080
echo "Creating docker-compose environment file \"$D4ST_DIR/.env\"..."
touch $DOTENV
echo "BASEXAPI_PORT=$BASEXAPI_PORT" >> "$DOTENV"
echo "GITLAB_PORT=$GITLAB_PORT" >> "$DOTENV"
echo "GITLAB_SSH_PORT=$GITLAB_SSH_PORT" >> "$DOTENV"
echo "LINKMGR_PORT=$LINKMGR_PORT" >> "$DOTENV"
echo "OXYWEBAUTHOR_PORT=$OXYWEBAUTHOR_PORT" >> "$DOTENV"
echo "WEBDELIVERY_PORT=$WEBDELIVERY_PORT" >> "$DOTENV"
echo "GITLAB_PRIVATE_TOKEN=${GITLAB_PRIVATE_TOKEN:-YourPrivateToken}" >> "$DOTENV"
echo "$DOTENV:"
while read line
do
echo $line
done < "$DOTENV"
fi
if [ "$OSTYPE" = "msys" ]; then
d4stBrowser=${D4ST_BROWSER:-chrome}
elif [[ "$OSTYPE" =~ "linux" ]]; then
d4stBrowser=${D4ST_BROWSER:-chrome}
else
# For OS X the browser value is the application bundle name, e.g. "Google Chrome" for Chrome.
d4stBrowser=${D4ST_BROWSER:-Safari}
browserBundleId=$(lsappinfo info -only bundleid "${d4stBrowser}" | cut -d '"' -f4)
fi
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
DOCKER_FILE=$DIR/docker-compose.yml
HOSTNAME=localhost
# macOS/Linux convention:
username="$USER"
if [ "$username" = "" ]; then
# Windows git bash convention
username="$USERNAME"
fi
ACTION=$1
if [ -z $1 ] ; then
echo "Starting D4ST Docker containers in the background. Use docker-compose commands or Kitematic to view the status."
# echo docker-compose -f ${DOCKER_FILE} up -d
docker-compose -f ${DOCKER_FILE} up -d
R=$?
# echo "Docker machine IP address is '$HOSTNAME'"
echo ""
echo "Docker containers:"
docker ps
elif [ "${ACTION}" == "start" ] ; then
echo "Starting D4ST Docker containers in the background. Use docker-compose commands or Kitematic to view the status."
# echo docker-compose -f ${DOCKER_FILE} up -d
docker-compose -f ${DOCKER_FILE} up -d
R=$?
echo ""
echo "Docker containers:"
docker ps
elif [ "${ACTION}" == "up" ] ; then
echo "Starting D4ST Docker containers in the background. Use docker-compose commands or Kitematic to view the status."
# echo docker-compose -f ${DOCKER_FILE} up -d
docker-compose -f ${DOCKER_FILE} up -d
R=$?
echo ""
echo "Docker containers:"
docker ps
elif [ "$1" = "stop" ]; then
echo "Stopping D4ST Docker containers. The containers can be started with the 'start' command."
docker-compose -f ${DOCKER_FILE} stop
R=$?
echo ""
echo "Docker containers:"
docker ps
elif [ "$1" = "kill" ]; then
echo "Killing the D4ST Docker containers. You may need to stop them first."
docker-compose -f ${DOCKER_FILE} kill
R=$?
echo ""
echo "Docker containers:"
docker ps
elif [ "$1" = "cleancontainers" ]; then
echo "Remove all containers "
docker rm -f $(docker ps -aq)
R=$?
echo ""
echo "Docker containers:"
docker ps
elif [ "$1" = "cleanimages" ]; then
echo "Remove all images"
docker rmi -f $(docker images -q)
R=$?
echo ""
echo "Docker images:"
docker images
elif [ "$1" = "cleanall" ]; then
echo "Remove all images and containers"
docker rm -f $(docker ps -aq)
docker rmi -f $(docker images -q)
R=$?
echo ""
echo "Docker containers:"
docker ps
echo ""
echo "Docker images:"
docker images
elif [ "$1" = "down" ]; then
echo "Downing the D4ST Docker containers."
docker-compose -f ${DOCKER_FILE} down
R=$?
echo ""
echo "Docker containers:"
docker ps
elif [ "$1" = "pause" ]; then
echo "Pausing the D4ST Docker containers. You may need to stop them first."
docker-compose -f ${DOCKER_FILE} pause
R=$?
elif [ "$1" = "unpause" ]; then
echo "Unpausing (resuming) the D4ST Docker containers if they were previously paused."
docker-compose -f ${DOCKER_FILE} unpause
R=$?
echo ""
echo "Docker containers:"
docker ps
elif [ "$1" = "pull" ]; then
echo "Pulling new images..."
docker-compose -f ${DOCKER_FILE} pull
R=$?
elif [ "$1" = "log-gitlab" ]; then
echo "Viewing GitLab container log. Use ctrl-c to quit logging."
docker-compose -f ${DOCKER_FILE} logs -f gitlab-d4st
R=$?
exit 0
elif [ "$1" = "gitlab" ]; then
echo "Open GitLab Web application in ${d4stBrowser}"
url="http://${HOSTNAME}:${GITLAB_PORT}"
if [ "$OSTYPE" = "msys" ]; then
start ${d4stBrowser} "${url}"
else
r=$("open" "-b" $browserBundleId "${url}")
fi
exit $?
elif [ "$1" = "linkmgr" ]; then
echo "Open D4ST Link Manager Web application in ${d4stBrowser}"
url="http://${HOSTNAME}:${LINKMGR_PORT}"
if [ "$OSTYPE" = "msys" ]; then
start ${d4stBrowser} "${url}"
else
r=$("open" "-b" $browserBundleId "${url}")
fi
exit $?
elif [ "$1" = "addhooks" ]; then
echo "Adding custom hooks to GitLab repositories..."
$(docker exec gitlab-d4st add-custom-hooks)
R=$?
elif [ "$1" = "getdemo" ]; then
targetDir=${2:-${HOME}}
echo "Copying the Thunderbird demo git repository to ${targetDir}/Thunderbird..."
if [ -d "${targetDir}/Thunderbird" ]; then
echo "Target directory \"${targetDir}/Thunderbird\" already exists, not copying the data."
exit 0
else
$(docker cp "demo-content:/opt/dita-community/data/Thunderbird" "${targetDir}")
exit $?
fi
elif [ "$1" = "make-gitlab-user" ]; then
if [ -z "${GITLAB_PRIVATE_TOKEN}" ]; then
echo "Environment variable 'GITLAB_PRIVATE_TOKEN' not set. You must set this"
echo "variable to the GitLab private token for your GitLab server. See D4ST documentation."
exit 1;
fi
# username is set to current user's name earlier.
password="password"
if [[ $# -eq 3 ]]; then
username="$2"
password="$3"
elif [[ $# -eq 2 ]]; then
username="$2"
fi
echo "Creating GitLab user \"${username}\"..."
result=$(curl -s -X POST --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "http://${HOSTNAME}:${GITLAB_PORT}/api/v3/users?username=${username}&email=${username}@example.com&password=${password}&name=${username}")
if [[ "$result" =~ "{\"message\":\"401 Unauthorized\"" ]]; then
echo "Failure: Did not authenticate using private token \"${GITLAB_PRIVATE_TOKEN}\"."
exit 1;
elif [[ "$result" =~ "404 Not Found" ]]; then
echo "Failure: Host \"${HOSTNAME}:${GITLAB_PORT}\" does not appear to have GitLab running."
exit 1;
elif [[ "$result" =~ "{\"name\":\"${username}\",\"username\":\"${username}\"" ]]; then
echo "Success: User \"${username}\" created with password \"${password}\""
exit 0;
else
echo "Failure: Doesn't look like the GitLab connection worked but the failure is not an expected one."
echo "Got response \"${result}\""
exit 1;
fi
#-------------------------
# make-gitlab-project
#-------------------------
elif [ "$1" = "make-gitlab-project" ]; then
if [ -z "${GITLAB_PRIVATE_TOKEN}" ]; then
echo "Environment variable 'GITLAB_PRIVATE_TOKEN' not set. You must set this"
echo "variable to the GitLab private token for your GitLab server. See D4ST documentation."
exit 1;
fi
project=${PWD##*/}
if [ "$2" != "" ]; then
project=$2
fi
echo "Creating GitLab project \"${project}\" for user ${username}..."
result=$(curl -s --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "http://${HOSTNAME}:${GITLAB_PORT}/api/v3/users?username=${username}")
if [ "$result" = "[]" ]; then
echo "Failure: Did not find GitLab user with name \"${username}\". Use "
echo " 'd4st make-gitlab-user' to create the GitLab user."
exit 1;
fi
userid=$([[ "$result" =~ \"id\":([0-9]+), ]] && echo ${BASH_REMATCH[1]})
if [ "$userid" = "" ]; then
echo "Failed to get GitLab user ID for user name \"${username}\"."
exit 1;
fi
result=$(curl -s -X POST --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "http://${HOSTNAME}:${GITLAB_PORT}/api/v3/projects/user/${userid}?userid=${userid}&name=${project}")
if [[ "$result" =~ "{\"message\":\"401 Unauthorized\"" ]]; then
echo "Failure: Did not authenticate using private token \"${GITLAB_PRIVATE_TOKEN}\"."
exit 1;
elif [[ "$result" =~ "404 Not Found" ]]; then
echo "Failure: Host \"${HOSTNAME}:${GITLAB_PORT}\" does not appear to have GitLab running."
exit 1;
elif [[ "$result" =~ "\"name\":[\"has already been taken\"]" ]]; then
echo "Looks like the project is already set up in GitLab."
exit 0;
elif [[ "$result" =~ "{\"name\":\"${username}\",\"username\":\"${username}\"" ]]; then
echo "Success: Project \"${project}\" created."
result=$(docker exec gitlab-d4st add-custom-hooks)
result=$(git init)
if [ "$?" -ne "0" ]; then
echo "Failed to initialize git repository"
exit 1;
fi
remotes=$(git remote | grep origin)
remote="origin"
if [[ $remotes =~ "origin" ]]; then
remote="gitlab"
fi
echo "*** remote=\"${remote}\""
# http://localhost/ekimber/project04.git
result=$(git remote add ${remote} "http://${HOSTNAME}:${GITLAB_PORT}/${username}/${project}.git")
exit $result;
else
echo "Failure: Doesn't look like the GitLab connection worked but the failure is not an expected one."
echo "Got response \"${result}\""
exit 1;
fi
elif [ "$1" = "test-gitlab" ]; then
echo "Testing GitLab connection..."
if [ -z "${GITLAB_PRIVATE_TOKEN}" ]; then
echo "Environment variable 'GITLAB_PRIVATE_TOKEN' not set. You must set this"
echo "variable to the GitLab private token for your GitLab server. See D4ST documentation."
exit 1;
fi
result=$(curl -s --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "http://${HOSTNAME}:${GITLAB_PORT}/api/v3/users?username=root")
if [[ "$result" =~ "{\"message\":\"401 Unauthorized\"" ]]; then
echo "Failure: Did not authenticate using private token \"${GITLAB_PRIVATE_TOKEN}\"."
exit 1;
elif [[ "$result" =~ "404 Not Found" ]]; then
echo "Failure: Host \"${HOSTNAME}:${GITLAB_PORT}\" does not appear to have GitLab running."
exit 1;
elif [[ "$result" =~ "[{\"name\":\"Administrator\",\"username\":\"root\"" ]]; then
echo "Success: Connected to GitLab remote API using private token."
exit 0;
else
echo "Failure: Doesn't look like the GitLab connection worked but the failure is not an expected one."
echo "Got response \"${result}\""
exit 1;
fi
else
echo "Unrecognized command \"$1\". Available commands are:"
echo " start - Start the server"
echo " stop - Stop the server"
echo " down : Stop containers and remove containers, networks, volumes, and images created by start."
echo " kill - Kill the server"
echo " cleanimages - Remove all Docker images"
echo " cleancontainers - Remove all Docker containers"
echo " cleanall - Remove all Docker containers and images"
echo " pause - Pauses the server"
echo " unpause - Resumes the server the server"
echo " pull - Pull new images"
echo " gitlab - Open GitLab Web application"
echo " log-gitlab - View the running GitLab server logs"
echo " linkmgr - Open D4ST Link Manager Web application"
echo " addhooks - Add the git hooks to the GitLab repositories"
echo " getdemo {targetDirectory} - Copy the Thunderbird demo git repository to a local directory"
echo ""
echo " make-gitlab-user [username [password]]"
echo " Create a GitLab user for the specified user and password."
echo " If username is omitted, uses the current user's name. If "
echo " password is omitted, uses value 'password'"
echo ""
echo " make-gitlab-project [projectname]"
echo " Create a GitLab project for the current user with the specifed name."
echo " If projectname is ommitted, the name of the current directory is used (e.g.,"
echo " run from within your local git repository)."
echo ""
echo " test-gitlab - Tests the GitLab remote API connection using the configured private token"
exit 1
fi
if [ $R -ne "0" ]
then
echo "Error code $R running docker-compose."
exit $R
fi
#
# End of script
#