-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun
executable file
·133 lines (107 loc) · 3.48 KB
/
run
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
#!/bin/bash
# ./run config command
# ./run xnat setup | run setup - if already configured, prompt to re-run setup
# ./run xnat ssh | ssh into the 'xnat' VM
# ./run xnat start | launch the 'xnat' config after it has been set up
# ./run xnat stop | stop (halt) the 'xnat' VM
# ./run xnat destroy | delete the VM and associated files
CFG=$1
#CMD=start
# Exit with error status
die() {
echo >&2 "$@"
exit -1
}
listdirs(){
set -- $1*/
printf "%s\n" "${@%/}"
}
# create local folders
[[ ! -d .work ]] && mkdir -p .work
[[ ! -d local ]] && \
mkdir -p \
local/downloads \
local/resources/captures \
local/resources/data \
local/resources/docker
if [[ -e .work/config ]]; then
# use an underscore as the only argument to run the
# last config used (stored in the '.work/config' file)
[[ $1 == _ ]] && CFG=$(<.work/config)
# use a double-underscore to just show the config name and exit
[[ $1 == __ ]] && die $(<.work/config)
fi
#######################
# RESOLVE THE CONFIG #
#######################
# if first [config] argument is not passed,
# list the 'configs' sub-folder names
[[ -z $CFG ]] && {
echo "Configuration not specified. Please choose one of the following:"
echo
INDEX=1
cd ./configs
CONFIGS=(`listdirs`)
cd ../
# look in 'configs' folder
for CONFIG in "${CONFIGS[@]}"; do
echo ${INDEX} - ${CONFIG%%/}
INDEX=$(expr ${INDEX} + 1)
done
echo
read -p "Select: " SELECT
echo
CHOICE=$(expr ${SELECT} - 1)
CFG=${CONFIGS[${CHOICE}]}
}
# make sure the config folder exists
# you can type the full path or the name of a folder in the 'configs' folder
[[ ! -d $CFG ]] && { [[ -d configs/$CFG ]] && CFG=configs/$CFG; }
# check the ./local and ./local/configs folders too
[[ ! -d $CFG ]] && { [[ -d local/$CFG ]] && CFG=local/$CFG; }
[[ ! -d $CFG ]] && { [[ -d local/configs/$CFG ]] && CFG=local/configs/$CFG; }
# give up if we haven't found the config folder by now
[[ ! -d $CFG ]] && die "Configuration not found."
# remove trailing slash
CFG=${CFG%%/}
# save the config we're using as a reference
printf $CFG > .work/config
# archive it for posterity, or don't
#timestamp=$(date +%s);
#dateTime=$(date +%Y%m%d-%H%M%S);
#configArchiveFile="config-$dateTime.txt"
#printf $CFG > .work/$configArchiveFile
#######################
# CD TO CONFIG FOLDER #
#######################
cd $CFG
# create a .work folder there too
mkdir -p .work
#######################
# RESOLVE THE COMMAND #
#######################
# second argument: setup, ssh, start, stop/halt, reload, destroy
[[ ! -z $2 ]] && CMD=$2
# if no CMD argument is passed, and there's a 'startup' file,
# use that as CMD (should be 'reload' if setup has completed)
[[ -z $2 ]] && [[ -f .work/startup ]] && CMD=$(<.work/startup)
# alias 'start' to 'reload', which is how
# the VM needs to be started after setup
[[ $CMD == start ]] && CMD=reload
# alias 'stop' to 'halt'
[[ $CMD == stop ]] && CMD=halt
if [[ $CMD == setup ]]; then
echo
echo "Starting XNAT build using '${CFG}' config..."
bash ./setup.sh && STATUS=$?
exit
elif [[ ${CMD} =~ (up|ssh|halt|reload|destroy) ]]; then
(vagrant ${CMD}) && STATUS=$?
else
die "Command '${CMD}' not found."
fi
# delete files if doing 'destroy'
[[ $STATUS == 0 && $CMD == destroy ]] && rm -R .work/*
## after setup is complete, specify 'reload' as the startup command
# (this is actually done in the 'war-deploy.sh' script)
#[[ $STATUS == 0 ]] && printf "reload" > .work/startup