-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcotls.sh
executable file
·443 lines (321 loc) · 9.2 KB
/
cotls.sh
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
#!/usr/bin/env bash
# Copyright (c) 2014 Jan Tezner under the MIT license
# Condensed Tools for (Web) Developers
#
# INSTALL:
# - put script somewhere (home directory)
# - add execute permissoon
# chmod +x ~/cotls.sh
# - make alias in your .bashrc
# alias cotls="~/cotls.sh"
###############################################################################
# MAIN VARIABLES
COTLS_ALIAS="cotls"
COTLS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
COTLS_VERSION="2015-09-07"
COTLS_HASH=
CONFIG_SUFFIX=
CONFIG_PATH=
ACTION_DIR="${COTLS_DIR}/actions/"
ACTION_SUFFIX=".sh"
DEFAULT_CONFIG_NAME=".cotls"
ARGUMENTS=("$@")
ARGUMENTS_COUNT=${#ARGUMENTS[@]}
PROJECT_CMS=
PROJECT_SETTINGS_FILE=
SYSTEM_COMPLETION_DIR="/etc/bash_completion.d/"
SKIP_CONFIG=0
DRY_RUN=false
###############################################################################
# HELPER FUNCTIONS
version() {
# check if is GIT root
if [[ -d "${COTLS_DIR}/.git" ]]
then
cd ${COTLS_DIR}
COTLS_HASH=$(git rev-parse --short HEAD)
fi
if [ -n "${COTLS_HASH}" ]
then
COTLS_HASH=" #${COTLS_HASH}"
fi
echo "${COTLS_VERSION}${COTLS_HASH}"
}
# help message
usage() {
# colors definition
RST="\e[0m" # reset
TIT="\e[32m" # title
VAR="\e[33m" # variable
VAL="\e[36m" # value
HGL="\e[31m" # highlight
echo -e "Cotls - Condensed Tools [ ${COTLS_VERSION} ]"
echo -e ""
echo -e "${TIT}Actions:${RST}"
echo -e "${VAR}batch${RST} - call cotls command sets"
echo -e "${VAR}dumpdown${RST} - dump database from remote host to localhost"
echo -e "${VAR}syncdown${RST} - rsync data from remote host to localhost"
echo -e "${VAR}import${RST} - import dump file into database"
echo -e " Example: cotls import dump.sql dabatase_name database_user"
echo -e "${VAR}fulldrop${RST} - drop all tables for selected database in local mysql"
echo -e "${VAR}deploy${RST} - make a GIT deploy on remote server"
echo -e "${VAR}rechmod${RST} - apply file mode to defined directories/files"
echo -e "${VAR}self-update${RST} - update COTLS it self from GIT"
echo -e ""
echo -e "${TIT}Arguments:${RST}"
# -c=<config-suffix>
echo -e "${VAR}-c=${RST}${VAL}<config-suffix>${RST}"
echo -e " Custom config file suffix, default blank (config file has name \"${DEFAULT_CONFIG_NAME}\") "
echo -e " E.g. value \"dev\" supposed to config file has name \"${DEFAULT_CONFIG_NAME}.dev\")"
# -cp=<path-to-config-file>
echo -e "${VAR}-cp=${RST}${VAL}<path-to-config-file>${RST}"
echo -e " Path can be relative or absolute, default value is current directory."
# -prdb
echo -e "${VAR}-prdb${RST}"
echo -e " Prompt user for password for remote DB"
# -tf
echo -e "${VAR}-tf=${RST}${VAL}<target-filename>${RST}"
echo -e " Target filename, can contain relative or absolute path"
echo -e " Placeholders:"
echo -e " ${VAL}#date${RST} - current date in mysql format (e.g. 2015-01-01)"
echo -e " ${VAL}#time${RST} - current time in hours and minutes (e.g. 1000)"
echo -e " ${VAL}#name${RST} - database name"
echo -e " ${VAL}#suffix${RST} - config suffix if defined"
echo -e ""
}
log() {
# colors definition
RST="\e[0m" # reset
SCS="\e[32m" # title
DEF="\e[33m" # default
ERR="\e[31m" # highlight
DEP="\e[31m" # deprecated
MSG=
if [ -n "$1" ]
then
IN="$1"
else
read IN # This reads a string from stdin and stores it in a variable called IN
fi
if [ -z "${2+x}" ]
then
COLOR=${DEF}
else
case "$2" in
deprecated)
COLOR=${DEP}
MSG=${DEF}
;;
error)
COLOR=${ERR}
;;
success)
COLOR=${SCS}
;;
*)
COLOR=${DEF}
;;
esac
fi
DATETIME=$(date +"%Y-%m-%d %H:%M:%S")
echo -e "${COLOR}[${DATETIME}]${RST} ${MSG}${IN}"
}
loge() {
log "$1" "error"
if [ -z "${2+x}" ]
then
echo ""
exit 1
fi
}
logs() {
log "$1" "success"
}
logd() {
log "$1" "deprecated"
}
strindex() {
x="${1%%$2*}"
if [[ $x = $1 ]]
then
POS=-1
else
POS=${#x}
fi
echo $POS
}
# http://stackoverflow.com/a/14367368
# usage array_contains ARRAY $SEEK
array_contains() {
local array="$1[@]"
local seeking=$2
local in=0
for element in "${!array}"; do
if [[ $element == $seeking ]]; then
in=1
break
fi
done
return $in
}
prepareAction() {
local ACTION=$1
# chose a behavior depends on action name
case ${ACTION} in
dumpdown|syncdown|deploy|rechmod)
# actions without defined routines or validations
;;
fulldrop)
# TODO pass user, pass, name from CLI
;;
batch)
if [[ $ARGUMENTS_COUNT != 2 ]]; then
loge "Batch name not specified!"
fi
BATCH_NAME=${ARGUMENTS[1]}
;;
ssh)
if [[ $ARGUMENTS_COUNT != 2 ]]; then
loge "SSH remote command not specified!"
fi
SSH_COMMAND=${ARGUMENTS[1]}
;;
self-update)
SKIP_CONFIG=1
;;
import)
if [[ $ARGUMENTS_COUNT < 2 ]]; then
loge "File to import not specified!"
fi
FILE_TO_IMPORT=${ARGUMENTS[1]}
DB_LOCAL_NAME=${ARGUMENTS[2]}
DB_LOCAL_USER=${ARGUMENTS[3]}
;;
*)
loge "Unknown command!"
;;
esac
}
callAction() {
local ACTION=$1
ACTION_FILE="${ACTION_DIR}${ACTION}${ACTION_SUFFIX}"
# check if action file exists
if [ ! -f ${ACTION_FILE} ]
then
loge "Action file \"${ACTION_FILE}\" not found!"
exit 1
else
source ${ACTION_FILE}
# run selected action
$ACTION
# get return content from action function
local RETURN=$?
# TODO resolve RETURN to success or error
fi
return $RETURN
}
checkSSHAccess() {
# main variables validation
if [ -z "${SSH_USER}" ] || [ -z "${SSH_SERVER}" ]
then
loge "SSH user or server not defined, check your COTLS configuration file"
fi
# check if ssh agent is running and has a loaded key
ssh-add -l >/dev/null 2>&1
if [ $? = 2 ]
then
# exit-status 2 = couldn't connect to ssh-agent
loge "SSH-AGENT not active, please start SSH-AGENT before continue!"
fi
# check if has access to remote server
ssh -q ${SSH_USER}@${SSH_SERVER} exit
if [ $? -ne 0 ]
then
loge "User don't have access to remote server over SSH"
fi
}
###############################################################################
# ARGUMENTS CONTROL + VALIDATION
# prepare arguments
for i in "${ARGUMENTS[@]}"
do
case $i in
# allowed arguments
-c=*)
CUSTOM_CONFIG="${i#*=}"
shift
;;
-cp=*)
CONFIG_PATH="${i#*=}"
;;
-tf=*)
CUSTOM_TARGET_FILENAME="${i#*=}"
;;
-prdb)
# TODO overide predefined value from config file
read -s -p "Enter Password for Remote DB: " DB_REMOTE_PASS
echo ""
shift
;;
-n)
DRY_RUN=true
;;
-v)
version
exit
;;
*)
# unknown option
;;
esac
done
###############################################################################
# ACTION CONTROL + VALIDATION
# check arguments
if [ $ARGUMENTS_COUNT -le 0 ]
then
usage
exit 1
fi
CLI_ACTION="${ARGUMENTS[0]}"
prepareAction ${CLI_ACTION}
###############################################################################
# CONFIG FILE ROUTINE
if [ ${SKIP_CONFIG} -eq 0 ]
then
# prepare config file name
CONFIG_FILE="${CONFIG_PATH}${DEFAULT_CONFIG_NAME}"
# check if custom config name
if [ ! -z "${CUSTOM_CONFIG+x}" ] && [ ! -z "${CUSTOM_CONFIG}" ]
then
CONFIG_SUFFIX=".${CUSTOM_CONFIG}"
CONFIG_FILE+=${CONFIG_SUFFIX}
log "Using custom config file \"${DEFAULT_CONFIG_NAME}.${CUSTOM_CONFIG}\""
fi
# check if config exists
if [ ! -f ${CONFIG_FILE} ]
then
loge "Config file \"${CONFIG_FILE}\" not found!"
exit 1
fi
# load current config file data
source ${CONFIG_FILE}
fi
###############################################################################
# DEPRECATIONS
# TODO 2015-02-01 remove
if [ ! -z "${RSYNC_LOCAL_ROOT_PATH+x}" ]
then
logd "DEPRECATED usage of \"RSYNC_LOCAL_ROOT_PATH\" in your config, please change variable name to \"PROJECT_LOCAL_ROOT\" (old will be removed on end of january)"
if [ -z "${PROJECT_LOCAL_ROOT}" ]
then
logd "\"PROJECT_LOCAL_ROOT\" not defined, using value of \"RSYNC_LOCAL_ROOT_PATH\""
PROJECT_LOCAL_ROOT="${RSYNC_LOCAL_ROOT_PATH}"
fi
fi
###############################################################################
# MAIN ACTION CODE
callAction ${CLI_ACTION}
RETURN_VALUE=$?
# TODO resolve RETURN_VALUE to success or error
echo ""