-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsuitecrm-cfg2env
executable file
·219 lines (173 loc) · 4.71 KB
/
suitecrm-cfg2env
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
#!/usr/bin/env bash
# This script comes with ABSOLUTELY NO WARRANTY, use at own risk
# Copyright (C) 2024 Osiris Alejandro Gomez <[email protected]>
# Copyright (C) 2024 Osiris Alejandro Gomez <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# shellcheck disable=SC2034
function usage()
{
cat << EOF
## \`$BIN\` Overwrite environment database credentials in ENV with values from CFG
Usage:
~~~bash
$BIN [CFG] [ENV]
~~~
### Example
~~~bash
$BIN
## SUITECRM ENVIRONMENT CONFIGURATION
COMPARE /home/osiris/git/suitecrm8.x-conf-dev/public/legacy/config_override.php
WITH /home/osiris/git/suitecrm8.x-conf-dev/.env
STATUS CONFIG PHP VS NG
----------------------------------------------------------------------------
MATCH DB_PORT 3306 = 3306
MATCH DB_USER dev = dev
MATCH DB_HOST sqldev01 = sqldev01
MATCH DB_NAME suitecrm = suitecrm
MATCH DB_PASS D*******p = D*******p
~~~
EOF
exit 0
}
function stderror()
{
echo >&2 "$1"
}
function die()
{
stderror "$1"
exit 1
}
function cfg_get_db_var()
{
local VAR
VAR="${1,,}"
grep -E "dbconfig.*${VAR}" "$CFG" | cut -d= -f2 | tr -d "'; "
}
function env_gen_db_url()
{
printf "DATABASE_URL='mysql://%s:%s@%s:%s/%s'" \
"${ENV_URL[$DB_USER]}" \
"${ENV_URL[$DB_PASS]}" \
"${ENV_URL[$DB_HOST]}" \
"${ENV_URL[$DB_PORT]}" \
"${ENV_URL[$DB_NAME]}"
}
function env_get_db_url()
{
grep '^DATABASE_URL=' "$ENV" \
| cut -d '/' -f3- \
| tr -d '"' \
| tr ':@/' ' '
}
function normal()
{
printf '%s%s' $'\033' '[22m'
}
function error()
{
printf '%s%s' $'\033' '[31m'
}
function defaults()
{
declare -gA CFG_DB_KEY=([DB_USER]=db_user_name [DB_PASS]=db_password [DB_HOST]=db_host_name [DB_PORT]=db_port [DB_NAME]=db_name)
readonly DB_USER=0
readonly DB_PASS=1
readonly DB_HOST=2
readonly DB_PORT=3
readonly DB_NAME=4
[[ -n "$BIN" ]] || BIN="$(basename "$0")"
[[ -n "$DIR" ]] || DIR="$PWD"
[[ -n "$ENV" ]] || ENV="$DIR/.env"
[[ -n "$CFG" ]] || CFG="$DIR/public/legacy/config_override.php"
[[ -n "$HLP" ]] || HLP='^[-]+(h|help)'
}
function params()
{
[[ "$1" =~ $HLP ]] && usage
[[ -e "$1" ]] && CFG="$1"
[[ -z "$CFG" ]] && CFG="$DIR/public/legacy/$1"
[[ -e "$2" ]] && ENV="$2"
[[ -z "$ENV" ]] && ENV="$DIR/$2"
[[ -s "$CFG" ]] || die "NOT FOUND CFG FILE $CFG"
[[ -e "$ENV" ]] || die "NOT FOUND ENV FILE $ENV"
CFG_TYPE="$(file -b --mime-type "$CFG")"
ENV_TYPE="$(file -b --mime-type "$ENV")"
[[ "$CFG_TYPE" = 'text/x-php' ]] || die "INVALID CFG FILE $CFG"
if [[ "$ENV_TYPE" != 'inode/x-empty' ]]
then
[[ "$ENV_TYPE" = 'text/plain' ]] || die "INVALID ENV FILE $ENV"
fi
}
function tbl_hdr()
{
printf "\\n## SUITECRM ENVIRONTMENT CONFIGURATION\\n"
printf "\\nCOMPARE %s\\nWITH %s\\n\\n" "$CFG" "$ENV"
}
function tbl_tit()
{
printf "%8s %6s %40s %2s %-40s\\n" \
'STATUS' \
'CONFIG ' \
'PHP' \
'VS' \
'NG'
}
function tbl_div()
{
tbl_tit | sed 's/[^|]/-/g'
}
function hide_pass()
{
local PASS_MAX PASS_MED PASS_PAD PASS_VAL
PASS_VAL="$1"
PASS_MAX="${#PASS_VAL}"
PASS_MED="$((PASS_MAX-2))"
printf -v PASS_PAD "%*s" "$PASS_MED" ' '
PASS_PAD="${PASS_PAD// /*}"
printf "%s%s%s" \
"${PASS_VAL:0:1}" \
"$PASS_PAD" \
"${PASS_VAL: -1}"
}
function cfg2env()
{
declare -gA ENV_URL
for CFG_VAR in "${!CFG_DB_KEY[@]}"
do
CFG_KEY="${CFG_DB_KEY[$CFG_VAR]}"
CFG_VAL="$(cfg_get_db_var "$CFG_KEY")"
ENV_URL[${!CFG_VAR}]="$CFG_VAL"
done
DATABASE_URL="$(env_gen_db_url)"
if grep -q '^DATABASE_URL=.*' "$ENV" 2>/dev/null
then
DATABASE_URL="${DATABASE_URL//\//\\/}"
sed -i "s/^DATABASE_URL.*/$DATABASE_URL/g" "$ENV"
else
echo "$DATABASE_URL" >> "$ENV"
fi
}
function main()
{
defaults
params "$@"
cfg2env
if command -v suitecrm-cfg-chk >/dev/null
then
suitecrm-cfg-chk "$CFG" "$ENV"
fi
}
main "$@"