-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbdb.sh
executable file
·124 lines (112 loc) · 4.36 KB
/
dbdb.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
#!/bin/bash
set -eu
getInstallDir() {
suffix=dbdb
if [ -z "${XDG_DATA_HOME+x}" ]; then
echo "$HOME/.local/share/$suffix"
else
echo "$XDG_DATA_HOME/$suffix"
fi
}
# Get format option
format=""
while getopts ":f:" opt; do
case ${opt} in
f)
format="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
# Define colors
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
currentDir="$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
cd "$currentDir"
installDir=$(getInstallDir)
normalOutputs=""
jsonOutputs=""
dbTypes=(mongodb mysql postgresql redis memcached)
for dbType in "${dbTypes[@]}"; do
for dbVersion in $(ls "$installDir/$dbType/versions" 2>/dev/null); do
if [ -d "$installDir/$dbType/versions/$dbVersion" ]; then
for dbServerName in $(ls "$installDir/$dbType/versions/$dbVersion/datadir" 2>/dev/null); do
if [ -d "$installDir/$dbType/versions/$dbVersion/basedir" ]; then
if [ -d "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName" ]; then
# port
dbPort="{port}"
if [ -f "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port" ]; then
dbPort=$(cat "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port")
elif [ -f "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port.last" ]; then
dbPort=$(cat "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port.last")
elif [ -f "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port.init" ]; then
dbPort=$(cat "$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.port.init")
fi
# pid
pidFile="$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/$dbType.pid"
# status
if [ -f "$pidFile" ] && pgrep -F "$pidFile" >/dev/null; then
status="running"
else
status="stopped"
fi
# change output color
if [ "$status" = "running" ]; then
normalOutputs="$normalOutputs ${GREEN}# $dbServerName (type:$dbType version:$dbVersion port:$dbPort) is ${status}.${NC}\n"
else
normalOutputs="$normalOutputs ${RED}# $dbServerName (type:$dbType version:$dbVersion port:$dbPort) is ${status}.${NC}\n"
fi
# normalOutputs
normalOutputs="$normalOutputs $currentDir/$dbType/{start|stop|restart|port|status|connect|delete}.sh $dbServerName\n"
normalOutputs="$normalOutputs $currentDir/$dbType/start.sh $dbServerName\n"
normalOutputs="$normalOutputs $currentDir/$dbType/stop.sh $dbServerName\n"
normalOutputs="$normalOutputs \n"
# confName
if [ "$dbType" = "mongodb" ];then
confName="$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/mongod.conf"
elif [ "$dbType" = "mysql" ];then
confName="$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/my.cnf"
elif [ "$dbType" = "postgresql" ];then
confName="$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/postgresql.conf"
elif [ "$dbType" = "redis" ];then
confName="$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName/redis.conf"
elif [ "$dbType" = "memcached" ];then
confName=""
fi
# jsonOutputs
availableCommands='["start.sh", "stop.sh", "restart.sh", "port.sh", "status.sh", "connect.sh", "delete.sh"]'
jsonOutputs="$jsonOutputs{
\"name\": \"$dbServerName\",
\"type\": \"$dbType\",
\"version\": \"$dbVersion\",
\"port\": \"$dbPort\",
\"status\": \"$status\",
\"commandPath\": \"$currentDir/$dbType/\",
\"availableCommands\": $availableCommands,
\"dataDir\": \"$installDir/$dbType/versions/$dbVersion/datadir/$dbServerName\",
\"confPath\": \"$confName\"
},"
fi
fi
done
fi
done
done
# Output
if [ "$format" = "json" ]; then
echo -e "[${jsonOutputs%?}]"
else
echo -e "${normalOutputs%?????}"
fi