-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite.pp
247 lines (219 loc) · 5.45 KB
/
site.pp
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
# my_module_dir variable define in puppet script
# Linux required -i'', not "-i ''" for inplace
os=$( uname -s )
case "${os}" in
Linux)
# Linux require -i'', not -i ' '
sed_delimer=
;;
FreeBSD)
sed_delimer=" "
;;
esac
generate_manifest()
{
cat <<EOF
class { 'profiles::db::postgresql': }
EOF
}
generate_hieradata()
{
local my_common_yaml="${my_module_dir}/common.yaml"
local databases_part_header="${my_module_dir}/databases_part_header.yaml"
local databases_part_body="${my_module_dir}/databases_part_body.yaml"
local hba_rules_part_header="${my_module_dir}/hba_rules_part_header.yaml"
local hba_rules_part_body="${my_module_dir}/hba_rules_part_body.yaml"
local roles_part_header="${my_module_dir}/roles_part_header.yaml"
local roles_part_body="${my_module_dir}/roles_part_body.yaml"
local database_grant_part_header="${my_module_dir}/database_grant_part_header.yaml"
local database_grant_part_body="${my_module_dir}/database_grant_part_body.yaml"
local _val _tpl
if [ ! -r ${databases_part_header} ]; then
echo "no such ${databases_part_header}"
exit 0
fi
if [ ! -r ${databases_part_body} ]; then
echo "no such ${databases_part_body}"
exit 0
fi
if [ ! -r ${hba_rules_part_header} ]; then
echo "no such ${hba_rules_part_header}"
exit 0
fi
if [ ! -r ${hba_rules_part_body} ]; then
echo "no such ${hba_rules_part_body}"
exit 0
fi
if [ ! -r ${roles_part_header} ]; then
echo "no such ${roles_part_header}"
exit 0
fi
if [ ! -r ${roles_part_body} ]; then
echo "no such ${roles_part_body}"
exit 0
fi
local form_add_databases=0
local form_add_hba_rule=0
local form_add_roles_rule=0
local form_add_roles=0
local form_add_database_grant=0
if [ -f "${my_common_yaml}" ]; then
local tmp_common_yaml=$( mktemp )
/bin/cp ${my_common_yaml} ${tmp_common_yaml}
for i in ${param}; do
case "${i}" in
# start with databases custom
databases_name[1-9]*)
form_add_databases=$(( form_add_databases + 1 ))
continue;
;;
# start with databases custom
hba_rules_type[1-9]*)
form_add_hba_rule=$(( form_add_hba_rule + 1 ))
continue;
;;
roles_name[1-9]*)
form_add_roles=$(( form_add_roles + 1 ))
continue
;;
database_grant_name[1-9]*)
form_add_database_grant=$(( form_add_database_grant + 1 ))
continue
;;
-*)
# delimier params
continue
;;
Expand)
# delimier params
continue
;;
esac
eval _val=\${${i}}
_tpl="#${i}#"
# Note that on Linux systems, a space after -i might cause an error
sed -i${sed_delimer}'' -Ees:"${_tpl}":"${_val}":g ${tmp_common_yaml}
done
else
for i in ${param}; do
eval _val=\${${i}}
cat <<EOF
$i: "${_val}"
EOF
done
fi
# custom databases
if [ ${form_add_databases} -ne 0 ]; then
cat ${databases_part_header} >> ${tmp_common_yaml}
for i in ${param}; do
case "${i}" in
databases_name[1-9]*)
;;
*)
continue
;;
esac
eval _val=\${${i}}
[ -z "${_val}" ] && continue
_tpl="#databases_name#"
sed -Ees/"${_tpl}"/"${_val}"/g ${databases_part_body} >> ${tmp_common_yaml}
done
fi
# custom hba rules
if [ ${form_add_hba_rule} -ne 0 ]; then
cat ${hba_rules_part_header} >> ${tmp_common_yaml}
tmpfile=$( mktemp )
cp -a ${hba_rules_part_body} ${tmpfile}
for i in ${param}; do
case "${i}" in
hba_rules_type[1-9]*)
_tpl="#hba_rules_type#"
;;
hba_rules_address[1-9]*)
_tpl="#hba_rules_address#"
;;
hba_rules_database[1-9]*)
_tpl="#hba_rules_database#"
;;
hba_rules_user[1-9]*)
_tpl="#hba_rules_user#"
;;
hba_rules_auth_method[1-9]*)
_tpl="#hba_rules_auth_method#"
;;
hba_rules_order[1-9]*)
_tpl="#hba_rules_order#"
;;
*)
continue
;;
esac
eval _val="\${${i}}"
[ -z "${_val}" ] && continue
rule_name="XXX" # concat from all field
sed -i${sed_delimer}'' -Ees@"${_tpl}"@"${_val}"@g ${tmpfile}
done
cat ${tmpfile} >> ${tmp_common_yaml}
rm -f ${tmpfile}
fi
# custom roles rules
if [ ${form_add_roles} -ne 0 ]; then
cat ${roles_part_header} >> ${tmp_common_yaml}
tmpfile=$( mktemp )
cp -a ${roles_part_body} ${tmpfile}
for i in ${param}; do
case "${i}" in
roles_name[1-9]*)
_tpl="#roles_name#"
;;
roles_password_hash[1-9]*)
_tpl="#roles_password_hash#"
;;
roles_superuser[1-9]*)
_tpl="#roles_superuser#"
;;
*)
continue
;;
esac
eval _val="\${${i}}"
[ -z "${_val}" ] && continue
rule_name="XXX" # concat from all field
sed -i${sed_delimer}'' -Ees@"${_tpl}"@"${_val}"@g ${tmpfile}
done
cat ${tmpfile} >> ${tmp_common_yaml}
rm -f ${tmpfile}
fi
# custom database_grant rules
if [ ${form_add_database_grant} -ne 0 ]; then
cat ${database_grant_part_header} >> ${tmp_common_yaml}
tmpfile=$( mktemp )
cp -a ${database_grant_part_body} ${tmpfile}
for i in ${param}; do
case "${i}" in
database_grant_name[1-9]*)
_tpl="#database_grant_name#"
;;
database_grant_privilege[1-9]*)
_tpl="#database_grant_privilege#"
;;
database_grant_db[1-9]*)
_tpl="#database_grant_db#"
;;
database_grant_role[1-9]*)
_tpl="#database_grant_role#"
;;
*)
continue
;;
esac
eval _val="\${${i}}"
[ -z "${_val}" ] && continue
rule_name="XXX" # concat from all field
sed -i${sed_delimer}'' -Ees@"${_tpl}"@"${_val}"@g ${tmpfile}
done
cat ${tmpfile} >> ${tmp_common_yaml}
rm -f ${tmpfile}
fi
cat ${tmp_common_yaml}
}