This repository has been archived by the owner on May 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
bashrc
388 lines (338 loc) · 12.7 KB
/
bashrc
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
wocker_usage() {
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 from a container's filesystem to the host path (Docker alias)"
echo " create Create a new container (Docker alias)"
echo " destroy Force remove all containers and local related files"
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 Create a new filesystem image from the contents of a tarball (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 (Docker alias)"
echo " login Register or log in to a Docker registry server (Docker alias)"
echo " logout Log out from a Docker registry server (Docker alias)"
echo " logs Fetch the logs of a container (Docker alias)"
echo " port Lookup the public-facing port that is NAT-ed to PRIVATE_PORT (Docker alias)"
echo " pause Pause all processes within a container (Docker alias)"
echo " ps List containers (Docker alias)"
echo " pull Pull an image or a repository from a Docker registry server (Docker alias)"
echo " push Push an image or a repository to a Docker registry server (Docker alias)"
echo " rename Rename an existing container (Docker alias)"
echo " restart Restart a running 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 to a tar archive (Docker alias)"
echo " search Search for an image on the Docker Hub (Docker alias)"
echo " start Start a stopped container (Docker alias)"
echo " stats Display a live stream of one or more containers' resource usage statistics (Docker alias)"
echo " stop Stop a running container (Docker alias)"
echo " tag Tag an image into a repository (Docker alias)"
echo " theme-test Import theme unit test data"
echo " top Lookup the running processes of a container (Docker alias)"
echo " unpause Unpause a paused container (Docker alias)"
echo " update Update Wocker to the latest version"
echo " version Show the Wocker version information"
echo " --version"
echo " -v"
echo " wait Block until a container stops, then print its exit code (Docker alias)"
}
wocker_run_usage() {
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_rm_usage() {
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 'Usage: wocker update'
echo ''
echo 'Update the command line and the Docker image of Wocker.'
}
wocker_version_usage() {
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 'Usage: wocker destroy'
echo ''
echo 'Force remove all containers and local related files'
}
wocker_wp_usage() {
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 '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() {
local WOCKER_VERSION='0.9.2'
local WORDPRESS_VERSION='4.4'
local RED=31
local image='wocker/wocker:latest'
local cname
local ports
local cid
local cids
local dirname
local containers
local force
local running
local confirmation
local theme_test_data_url
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 =~ "HostIp:0.0.0.0 HostPort:80" ]]; then
echo -e "\033[${RED}mCannot start 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}:/var/www/wordpress: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 && \
mkdir ~/data/${cid}
docker cp $(docker ps -l -q):/var/www/wordpress ~/data/${cid} && \
mv ~/data/${cid}/wordpress ~/data/${dirname} && \
rm -rf ~/data/${cid} && \
docker rm -f $(docker ps -l -q) && \
docker run -d --name $cname -p 80:80 -p 3306:3306 -v ~/data/${dirname}:/var/www/wordpress:rw $image
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
curl -OL https://raw.githubusercontent.com/wckr/wocker-bashrc/master/bashrc && mv -f bashrc ~/.bashrc && source ~/.bashrc
docker pull wocker/wocker:latest
wocker_version_info
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' )
for cid in $(docker ps -a -q); do
dirname=$(docker inspect --format='{{.Name}}' $cid)
dirname=${dirname#*/}
rm -rf ~/data/${dirname}
done
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
;;
#
# $ 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 -it $cid wp --allow-root ${@:2}
else
docker exec $cid wp --allow-root ${@: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 $cid curl -OL $theme_test_data_url \
&& docker exec $cid wp --allow-root plugin install wordpress-importer --activate \
&& docker exec $cid wp --allow-root import ${theme_test_data_url##*/} --authors=create \
&& docker exec $cid rm ${theme_test_data_url##*/}
fi
fi
fi
;;
#
# Other Docker commands
#
'attach' | 'build' | 'commit' | 'cp' | 'create' | 'diff' | 'events' | 'exec' | 'export' | 'history' | 'images' | 'import' | 'info' | 'inspect' | 'kill' | 'load' | 'login' | 'logout' | 'logs' | 'port' | 'pause' | 'ps' | 'pull' | 'push' | 'rename' | 'restart' | 'rmi' | 'save' | 'search' | 'start' | 'stop' | 'tag' | 'top' | 'unpause' | 'wait' )
docker $@
;;
#
# Show Wocker usage
#
* )
wocker_usage
;;
esac
}