forked from wckr/wocker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wocker
executable file
·495 lines (433 loc) · 14.8 KB
/
wocker
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#!/bin/bash -e
WOCKER_VERSION='1.1.3'
WORDPRESS_VERSION='4.6.1'
RED=31
USER=wocker
DOCROOT=/var/www/wordpress
image='wocker/wocker:latest'
cname=''
ports=''
cid=''
cids=''
dirname=''
containers=''
force=false
running=''
confirmation=''
theme_test_data_url=''
wocker_usage() {
echo ""
echo "Usage: wocker COMMAND [arg...]"
echo ""
echo "Commands:"
echo " attach Attach to a running container (Docker alias)"
echo " build Build an image from a Dockerfile (Docker alias)"
echo " commit Create a new image from a container's changes (Docker alias)"
echo " cp Copy files/folders between a container and the local filesystem (Docker alias)"
echo " create Create a new container (Docker alias)"
echo " destroy Remove all containers and local related files if '--files' argument is set"
echo " diff Inspect changes on a container's filesystem (Docker alias)"
echo " events Get real time events from the server (Docker alias)"
echo " exec Run a command in a running container (Docker alias)"
echo " export Stream the contents of a container as a tar archive (Docker alias)"
echo " help Show this help"
echo " --help"
echo " -h"
echo " history Show the history of an image (Docker alias)"
echo " images List images (Docker alias)"
echo " import Import the contents from a tarball to create a filesystem image (Docker alias)"
echo " info Display system-wide information (Docker alias)"
echo " inspect Return low-level information on a container or image (Docker alias)"
echo " kill Kill a running container (Docker alias)"
echo " load Load an image from a tar archive or STDIN (Docker alias)"
echo " login Register or log in to a Docker registry (Docker alias)"
echo " logout Log out from a Docker registry (Docker alias)"
echo " logs Fetch the logs of a container (Docker alias)"
echo " network Manage Docker networks (Docker alias)"
echo " pause Pause all processes within a container (Docker alias)"
echo " port List port mappings or a specific mapping for the CONTAINER (Docker alias)"
echo " ps List containers (Docker alias)"
echo " pull Pull an image or a repository from a registry (Docker alias)"
echo " push Push an image or a repository to a registry (Docker alias)"
echo " rename Rename a container (Docker alias)"
echo " restart Restart a container (Docker alias)"
echo " rm Remove one or more containers"
echo " rmi Remove one or more images (Docker alias)"
echo " run Run a command in a new container"
echo " save Save an image(s) to a tar archive (Docker alias)"
echo " search Search the Docker Hub for images (Docker alias)"
echo " start Start a stopped container (Docker alias)"
echo " stats Display a live stream of container(s) resource usage statistics (Docker alias)"
echo " stop Stop the running container"
echo " switch Stop the running container then start a stopped container"
echo " tag Tag an image into a repository (Docker alias)"
echo " theme-test Import theme unit test data in to a running container"
echo " top Display the running processes of a container (Docker alias)"
echo " unpause Unpause all processes within a container (Docker alias)"
echo " update Update Wocker to the latest version"
echo " version Show the Wocker version information"
echo " --version"
echo " -v"
echo " volume Manage Docker volumes"
echo " wait Block until a container stops, then print its exit code (Docker alias)"
echo ""
echo "Run 'wocker COMMAND --help' for more information on a command."
}
wocker_run_usage() {
echo ''
echo 'Usage: wocker run [--name=""] [IMAGE[:TAG]]'
echo ''
echo 'Run a new container'
echo ''
echo ' --name="" Assign a name to the container. If omitted, it will be a random name.'
echo ' IMAGE[:TAG] Docker image. If omitted, it will be wocker/wocker:latest.'
}
wocker_stop_usage() {
echo ''
echo 'Usage: wocker stop [OPTIONS] [CONTAINER...]'
echo ''
echo 'Stop the running container.'
echo 'Sending SIGTERM and then SIGKILL after a grace period'
echo ''
echo ' --help=false Print usage'
echo ' -t, --time=10 Seconds to wait for stop before killing it'
echo ' CONTAINER If omitted, all running containers will be stopped.'
}
wocker_switch_usage() {
echo ''
echo 'Usage: wocker switch CONTAINER'
echo ''
echo 'Stop the running container then start a stopped container'
echo ''
echo ' --help=false Print usage'
}
wocker_rm_usage() {
echo ''
echo 'Usage: wocker rm [--force=false] CONTAINER [CONTAINER...]'
echo ''
echo 'Remove one or more containers'
echo ''
echo ' -f, --force=false Force the removal of a running container (uses SIGKILL)'
}
wocker_update_usage() {
echo ''
echo 'Usage: wocker update'
echo ''
echo 'Update the command line and the Docker image of Wocker.'
}
wocker_version_usage() {
echo ''
echo 'Usage: wocker version | --version | -v'
echo ''
echo 'Show the Wocker version information'
}
wocker_version_info() {
echo "Wocker version: $WOCKER_VERSION"
echo "WordPress $WORDPRESS_VERSION"
}
wocker_destroy_usage() {
echo ''
echo 'Usage: wocker destroy [--files=false]'
echo ''
echo "Remove all containers and local related files if '--files' argument is set"
}
wocker_wp_usage() {
echo ''
echo 'Usage: wocker wp COMMAND SUBCOMMAND arg...'
echo ''
echo 'Execute WP-CLI commands in the running container'
echo 'See: http://wp-cli.org/'
echo ''
echo 'Commands:'
echo ' cache Manage the object cache.'
echo ' cap Manage user capabilities.'
echo ' cli Get information about WP-CLI itself.'
echo ' comment Manage comments.'
echo ' core Download, install, update and otherwise manage WordPress proper.'
echo ' cron Manage WP-Cron events and schedules.'
echo ' db Perform basic database operations.'
echo ' eval Execute arbitrary PHP code after loading WordPress.'
echo ' eval-file Load and execute a PHP file after loading WordPress.'
echo ' export Export content to a WXR file.'
echo ' help Get help on a certain command.'
echo ' import Import content from a WXR file.'
echo ' media Manage attachments.'
echo ' menu List, create, assign, and delete menus'
echo ' network -'
echo ' option Manage options.'
echo ' plugin Manage plugins.'
echo ' post Manage posts.'
echo ' rewrite Manage rewrite rules.'
echo ' role Manage user roles.'
echo ' scaffold Generate code for post types, taxonomies, etc.'
echo ' search-replace Search/replace strings in the database.'
echo ' shell Interactive PHP console.'
echo ' sidebar Manage sidebars.'
echo ' site Perform site-wide operations.'
echo ' super-admin List, add, and remove super admins from a network.'
echo ' term Manage terms.'
echo ' theme Manage themes.'
echo ' transient Manage transients.'
echo ' user Manage users.'
echo ' widget Manage sidebar widgets.'
}
wocker_theme_test_usage() {
echo ''
echo 'Usage: wocker theme-test [LOCALE]'
echo ''
echo 'Import theme unit test data'
echo ''
echo ' LOCALE Select which language you want to import. `en` and `ja` are available. If omitted, `en` will be imported.'
}
wocker_wordmove_usage() {
echo ''
echo 'Usage: wocker wordmove COMMAND arg'
echo ''
echo 'Execute wordmove commands in the running container'
echo 'See http://welaika.github.io/wordmove/'
echo ''
echo 'Commands:'
echo ' help Describe available tasks or one specific task'
echo ' init Generates a brand new Movefile'
echo ' pull Pulls WP data from remote host to the local machine'
echo ' push Pushes WP data from local machine to remote host'
}
case "$1" in
#
# wocker run
#
'run' )
if [[ "$2" = '--help' ]]; then
wocker_run_usage
else
if [[ "$2" = '--name' ]]; then
cname="$3"
image=${4:-$image}
elif [[ "$2" =~ ^--name=(.*)$ ]]; then
cname="${BASH_REMATCH[1]}"
image=${3:-$image}
else
cname=""
image=${2:-$image}
fi
if [[ $(docker ps -q) ]]; then
ports=$(docker inspect --format='{{.NetworkSettings.Ports}}' $(docker ps -q))
fi
if [[ $ports =~ "0.0.0.0 80" ]]; then
echo -e "\033[${RED}mCannot create container $cname: Bind for 0.0.0.0:80 failed: port is already allocated\033[m"
# Use existing WordPress files to run a container
elif [[ $cname && -d ~/data/${cname} ]]; then
docker run -d --name $cname -p 80:80 -p 3306:3306 -v ~/data/${cname}:${DOCROOT}:rw $image
# Or copy WordPress files from the image to run a container
else
if [[ $cname ]]; then
docker run -d --name $cname $image
else
docker run -d $image
fi
cid=$(docker inspect --format='{{.Id}}' $(docker ps -l -q)) && \
cid=${cid:0:12} && \
dirname=$(docker inspect --format='{{.Name}}' $(docker ps -l -q)) && \
dirname=${dirname#*/} && \
cname=$dirname && \
docker cp $(docker ps -l -q):${DOCROOT} ~/data/${dirname} && \
docker rm -f $(docker ps -l -q) && \
docker run -d --name $cname -p 80:80 -p 3306:3306 -v ~/data/${dirname}:${DOCROOT}:rw $image
fi
fi
;;
#
# wocker stop
#
'stop' )
if [[ "$2" = '--help' ]]; then
wocker_stop_usage
else
if [[ "$2" ]]; then
docker $@
elif [[ $(docker ps -q) ]]; then
docker stop $(docker ps --format='{{.Names}}')
fi
fi
;;
#
# wocker switch
#
'switch' )
if [[ "$2" = '--help' ]]; then
wocker_switch_usage
else
if [[ "$2" ]]; then
if [[ $(docker ps -q) ]]; then
docker stop $(docker ps --format='{{.Names}}')
fi
docker start $2
else
wocker_switch_usage
fi
fi
;;
#
# wocker rm
#
'rm' )
if [[ "$2" = '--help' ]]; then
wocker_rm_usage
else
case "$2" in
'-f' | '--force' | '--force=true' )
force=true
containers=${@:3}
;;
* )
force=false
containers=${@:2}
;;
esac
cids=$(docker inspect --format='{{.Id}}' $containers)
for cid in $cids; do
running=$(docker inspect --format='{{.State.Running}}' $cid)
dirname=$(docker inspect --format='{{.Name}}' $cid)
dirname=${dirname#*/}
docker rm --force=${force} $cid
if [[ $force = true || $running = false ]]; then
rm -rf ~/data/${dirname}
fi
done
fi
;;
#
# wocker update
#
'update' )
if [[ "$2" = '--help' ]]; then
wocker_update_usage
else
sudo wget -q -O /opt/bin/wocker https://raw.githubusercontent.com/wckr/wocker-cli/master/wocker && sudo chmod +x /opt/bin/wocker
docker pull wocker/wocker:latest
/opt/bin/wocker version
fi
;;
#
# wocker --help | wocker -h
#
'help' | '--help' | '-h' )
wocker_usage
;;
#
# wocker version | wocker --version | wocker -v
#
'version' | '--version' | '-v' )
if [[ "$2" = '--help' ]]; then
wocker_version_usage
else
wocker_version_info
fi
;;
#
# wocker wp
#
'wp' )
if [[ "$2" = '--help' ]]; then
wocker_wp_usage
else
if [[ $(docker ps -q) ]]; then
cid=$(docker ps -q)
if [[ ! $cid =~ $'\n' ]]; then
if [[ "$2" = 'shell' ]]; then
docker exec -u $USER -it $cid wp ${@:2}
else
docker exec -u $USER $cid wp ${@:2}
fi
fi
fi
fi
;;
#
# wocker wp
#
'wordmove' )
if [[ "$2" = '--help' ]]; then
wocker_wordmove_usage
else
if [[ $(docker ps -q) ]]; then
cid=$(docker ps -q)
if [[ ! $cid =~ $'\n' ]]; then
if [[ "$2" = 'shell' ]]; then
docker exec -u $USER -it $cid wordmove ${@:2}
else
docker exec -u $USER $cid wordmove ${@:2}
fi
fi
fi
fi
;;
#
# wocker theme-test
#
'theme-test' )
if [[ "$2" = '--help' ]]; then
wocker_theme_test_usage
else
case "$2" in
'ja' )
theme_test_data_url='https://raw.githubusercontent.com/jawordpressorg/theme-test-data-ja/master/wordpress-theme-test-date-ja.xml'
;;
'en' | * )
theme_test_data_url='https://wpcom-themes.svn.automattic.com/demo/theme-unit-test-data.xml'
;;
esac
if [[ $(docker ps -q) ]]; then
cid=$(docker ps -q)
if [[ ! $cid =~ $'\n' ]]; then
docker exec -u $USER $cid curl -OL $theme_test_data_url \
&& docker exec -u $USER $cid wp plugin install wordpress-importer --activate \
&& docker exec -u $USER $cid wp import ${theme_test_data_url##*/} --authors=create \
&& docker exec -u $USER $cid rm ${theme_test_data_url##*/}
fi
fi
fi
;;
#
# wocker destroy
#
'destroy' )
if [[ "$2" = '--help' ]]; then
wocker_destroy_usage
else
if [[ $(docker ps -a -q) ]]; then
echo 'Are you sure you want to remove all containers and related files? [y/N]'
read confirmation
case $confirmation in
'y' )
if [[ "$2" = '--files' || "$2" = '--files=true' ]]; then
for cid in $(docker ps -a -q); do
dirname=$(docker inspect --format='{{.Name}}' $cid)
dirname=${dirname#*/}
rm -rf ~/data/${dirname}
done
fi
docker rm -f $(docker ps -a -q)
;;
* )
echo 'Containers and file will not be removed, since the confirmation was declined.'
;;
esac
else
echo 'Nothing to destroy.'
fi
fi
;;
#
# Other Docker commands
#
'daemon' | 'attach' | 'build' | 'commit' | 'cp' | 'create' | 'diff' | 'events' | 'exec' | 'export' | 'history' | 'images' | 'import' | 'info' | 'inspect' | 'kill' | 'load' | 'login' | 'logout' | 'logs' | 'network' | 'pause' | 'port' | 'ps' | 'pull' | 'push' | 'rename' | 'restart' | 'rm' | 'rmi' | 'run' | 'save' | 'search' | 'start' | 'stats' | 'stop' | 'tag' | 'top' | 'unpause' | 'version' | 'volume' | 'wait' )
docker $@
;;
#
# Show Wocker usage
#
* )
wocker_usage
;;
esac