-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-dbox
executable file
·299 lines (225 loc) · 7.68 KB
/
run-dbox
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
#!/bin/bash
INTERACTIVE=
NAME=dbox
BASE_DIR=dropbox-containers
DEFAULT_CMD=
ATTACH_CMD=bash
ATTACH_MODE=
RESTART_POLICY=always
FULL_PATH=
QUIET=
DEBUG=
VALID_INITIAL="a-zA-Z0-9_."
VALID_NAME="$VALID_INITIAL-"
VALID_FILENAME=" /$VALID_NAME"
ERROR_MSG=
DO_NOTHING=
IMAGE_TAG=latest
SHORT_HELP="
Generate and execute a 'docker run' command to run a sonysantos/dropbox container.
Usage: $0 [options] [NAME]
NAME is the container name and the base directory for dropbox
account, normally under \$HOME/BASE_DIR/. Default name is $NAME.
Use -h or --help option for more details.
Options:
-i, --interactive Interactive mode (default: daemon mode)
-b, --base-dir=BASE_DIR Parent directory of NAME under \$HOME
--full-path=FULL_PATH Path to be used instead of \$HOME/BASE_DIR/NAME
-c, --command=CMD Set alternative command for the container
-a, --attach[=CMD] Attach CMD (default: bash) to a running container
--restart=RESTART_POLICY Set restart policy for daemon mode
-t, --tag=TAG Use sonysantos/dropbox:TAG image
-h, --help Display detailed help
-n, --do-nothing Do not execute; just show the what would be done
-q, --quiet Show no info at all
--debug Show additional info for debugging this script
"
LONG_HELP="Script to generate and excecute docker command to run a dropbox container.
Usage: $0 [options] [NAME]
NAME is a nickname for the Dropbox account. It is used both as the container name and as the base directory for Dropbox account, which contais subdirectories like Dropbox, .dropbox and .dropbox-dist.
The NAME directory is a subdirectory of BASE_DIR:
BASE_DIR # base directory; see also --base-dir option below
+- NAME # nickname for this Dropbox account and container
| +- Dropbox # synced documents in this Dropbox account
| +- .dropbox # local metadata for this Dropbox account
+- ANOTHER_NAME # nickname for another Dropbox account, etc.
...
That way you can have several different Dropbox accounts running in paralell in the same machine, each with a different name.
If omitted, NAME defaults to $NAME.
BASE_DIR is a subdirectory of \$HOME and it defaults to $BASE_DIR. You can alter it with the --base-dir option (see below).
You can also use the --full-path option to set the full path of the Dropbox account, if it is not composed by \$HOME/BASE_DIR/NAME. See details below.
Options are:
-i, --interactive
Run in interactive mode; useful to link dropbox account. By default it runs as a daemon, unless if Dropbox subdirectories don't exist under \$HOME/BASE_DIR/NAME. In this case they will be created.
-b, --base-dir=BASE_DIR
BASE_DIR is a subdirectory of \$HOME and it's a base for several dropbox accounts, each one under a different NAME under BASE_DIR. If omitted, default value is $BASE_DIR.
--full-path=FULL_PATH
If the path of the dropbox account is not of the form \$HOME/BASE_DIR/NAME, you can set the full path with this option. It must exist. If it doesn't start with a slash (/) it will be considered as a subdirectory of \$HOME. Besides it will not be followed by /NAME, NAME still will be used as the container name. This option overrides the --base-dir option above.
-c, --command=CMD
Override default command with CMD (e.g. bash). At this moment arguments for CMD are not supported.
-a, --attach[=CMD]
Attach CMD to a running container. If omitted, the default CMD is $ATTACH_CMD in this option. This option is always interactive. Other options are ignored.
--restart=RESTART_POLICY
Set the restart policy for the container. Applies only to daemon mode. Default is 'always'. See docker documentation for details.
-t, --tag=TAG
Use a specific tag for the image, that is, sonysantos/dropbox:TAG. Default is 'latest'.
-h, --help
Display this message.
-n, --do-nothing
Do not execute; just show the 'docker run' command generated.
-q, --quiet
Show no info at all.
--debug
Show additional info for debugging this script, and do not execute."
# 0 argument message
[[ "$#" == "0" ]] && echo "Using default directories. Use -h or --help option for details."
# check existence of directory supplied by user
check_user_supplied_dir () {
if [[ "$arg" != /* ]]; then
arg="$HOME/$arg"
fi
if [[ ! -d "$arg" ]]; then
echo "Path supplied via --fullpath doesn't exist: $arg" >&2
exit 1
fi
}
for i in "$@"; do
# test input
eqpos=`expr index "$i" "="`
if [[ $eqpos > 0 ]]; then
opt="${i:0:$eqpos}"
arg="${i:$eqpos}"
if [[ "$arg" =~ ^[$VALID_INITIAL][$VALID_NAME]*$ ]]; then
opt+="NAME"
elif [[ "$arg" =~ ^/?[$VALID_INITIAL][$VALID_FILENAME]*$ ]]; then
opt+="FILENAME"
arg=$(sed 's:/\+$::' <<<$arg) # remove last bars
else
opt+="OTHER_ARGUMENT"
fi
[[ "$opt" == *NAME && ( "$arg" == *..* || "$arg" == *//* ) ]] && \
opt="INVALID_FILENAME"
elif [[ "$i" == -* ]]; then
opt=$i
arg=
elif [[ "$i" =~ ^[$VALID_INITIAL][$VALID_NAME]*$ ]]; then
opt="NAME"
arg=$i
else
opt="INVALID_NAME"
arg=$i
fi
[[ -n "$DEBUG" ]] && echo "Option: $opt ($arg)"
case $opt in
-i|--interactive)
INTERACTIVE=1
;;
-b=*NAME|--base-dir=*NAME)
BASE_DIR=${arg#/} # remove eventual first bar
;;
--full-path=*NAME)
check_user_supplied_dir
FULL_PATH=$arg
;;
-c=*NAME|--command=*NAME)
DEFAULT_CMD=$arg
;;
-t=*NAME|--tag=*NAME)
IMAGE_TAG=$arg
;;
-a|--attach)
ATTACH_MODE=1
;;
-a=*NAME|--attach=*NAME)
ATTACH_MODE=1
ATTACH_CMD=$arg
;;
--restart=*)
if [[ "$arg" =~ ^(always|no|unless-stopped|on-failure(:[0-9]+)?)$ ]]; then
RESTART_POLICY=$arg
else
ERROR_MSG="Invalid restart policy: $arg"
fi
;;
-h|--help)
# help | add blank lines | format | remove added blank lines
echo "$LONG_HELP" | sed G | fmt -c | sed -e '$d' -e '/^$/ N; s/\n//'
exit 0
;;
-n|--do-nothing)
DO_NOTHING=1
;;
-q|--quiet)
QUIET=1
;;
--debug)
DEBUG=1
;;
NAME)
NAME=$arg
;;
INVALID_NAME)
ERROR_MSG="Invalid name: $arg"
;;
INVALID_FILENAME)
ERROR_MSG="Name and path cannot contain two adjacent dots or slashes: $arg"
;;
-*=OTHER_ARGUMENT)
ERROR_MSG="Invalid argument: $arg"
;;
-*)
ERROR_MSG="Invalid option: $opt"
;;
*)
ERROR_MSG="Unknown error for option $opt and argument $arg"
;;
esac
# check for errors
if [[ -n "$ERROR_MSG" ]]; then
echo "$ERROR_MSG" >&2
echo "$SHORT_HELP"
exit 1
fi
shift
done
# set fullpath
if [[ -z $FULL_PATH ]]; then
FULL_PATH=$HOME/$BASE_DIR/$NAME
if [[ ! -d "$FULL_PATH" ]]; then
[[ -z "$QUIET" ]] && echo "Creating directory '$1'..."
mkdir -p "$FULL_PATH" || {
echo "Could not create $1" >&2
exit 1
}
fi
fi
# if dropbox is empty set INTERACTIVE mode
[[ ! -d "$FULL_PATH/.dropbox" || ! "$(ls -A $FULL_PATH/.dropbox)" ]] && \
INTERACTIVE=1
# attach mode
if [[ -n "$ATTACH_MODE" ]]; then
DOCKER_CMD="docker exec -it $NAME $ATTACH_CMD"
else
if [[ -n "$INTERACTIVE" ]]; then
DOCKER_OPT="-it --rm"
else
DOCKER_OPT="-d --restart=$RESTART_POLICY"
fi
VOLUME="-v $FULL_PATH:/dbox"
ENV_DBOX="-e DBOX_UID=`id -u` -e DBOX_GID=`id -g`"
IMAGE="sonysantos/dropbox:$IMAGE_TAG"
DOCKER_CMD="docker run $DOCKER_OPT
--name $NAME
$VOLUME
$ENV_DBOX
$IMAGE
$DEFAULT_CMD"
fi
[[ -n "$DEBUG" ]] && DO_NOTHING=1
[[ -n "$DO_NOTHING" ]] && QUIET=
[[ -z "$QUIET" ]] && \
echo -e "\nGenerated Docker command:\n$DOCKER_CMD"
[[ -z "$DO_NOTHING" ]] && {
[[ -z "$QUIET" ]] && echo -e "\nExecuting Docker Command..."
$DOCKER_CMD
}