-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjail-api
executable file
·343 lines (297 loc) · 8.56 KB
/
jail-api
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
#!/usr/local/bin/cbsd
#v13.0.1
DIST_MODULE_PATH="${distmoduledir}/api.d"
MYOPTARG="imgsize jname img ram cpus pubkey host_hostname ver extras pkglist"
MYARG="mode"
MYDESC="Operate with jail via API"
CBSDMODULE="sys"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:
Extra helper for jail creation via API
${H3_COLOR}General Options${N0_COLOR}:
${H3_COLOR}Examples${N0_COLOR}:
${H3_COLOR}See also${N0_COLOR}:
"
EXTHELP="wf_bhyve"
MODULE_PATH="${dbdir}/bhyve"
. ${subrdir}/nc.subr
ip4_addr=
cpus=
ram=
mq_tube=
. ${cbsdinit}
. ${subrdir}/system.subr
. ${subrdir}/strings.subr
readconf api.conf
readconf jail-api.conf
[ -n "${ip4_gw}" ] && oip4_gw="${ip4_gw}"
[ -z "${ver}" ] && ver="native"
set -e
. ${distmoduledir}/api.d/api.subr
set +e
MY_LOG="${cbsd_api_logdir}/jail-api.log"
log "wakeup: [workdir: ${workdir}, cbsdworkdir: ${cbsd_workdir}, api_postcreate_hook: ${api_postcreate_hook}, cbsd_api_dbdir: ${cbsd_api_dbdir}]: $*"
create_env()
{
local ssh_user="root" # always root for jail
local ssh_port=
local ssh_host=
[ -z "${ssh_host}" ] && ssh_host="${nodeip}"
[ -z "${ssh_user}" ] && err 1 "ssh_user empty? check img/mapping in vm-api.conf ({jail,bhyve,xen,qemu,virtualbox}-api.conf) for: ${image}"
# extras area
v6_addr=
v4_addr=
primary_ip=
ip4_addr=
ossh_port=
port=
if [ -n "${extras}" ]; then
# strip special: must be performed on the API first
extras=$( echo ${extras} | ${TR_CMD} -dc '[:alnum:]:,\n\r' | ${TR_CMD} '[:upper:]' '[:lower:]' )
log "EXTRAS [${extras}]"
fi
exec_extras=
if [ -n "${extras}" ]; then
for i in ${jail_known_extras}; do
unset ${i}
done
OIFS="${IFS}"
IFS=","
for i in ${extras}; do
IFS="${OIFS}"
extras_name=$( echo ${i} | ${CUT_CMD} -d ":" -f 1 )
known=0
for x in ${jail_known_extras}; do
log "extras check [${x}][${extras_name}]"
if [ "${x}" = "${extras_name}" ]; then
known=1
fi
done
if [ ${known} -eq 1 ]; then
#extras_val=$( echo ${i} | ${CUT_CMD} -d ":" -f 2 )
_arg_len=$( strlen ${i} )
strpos --str="${i}" --search=":"
_eqpos=$?
_eqpos=$(( _eqpos + 2 ))
extras_val=$( substr --pos=${_eqpos} --len=${_arg_len} --str="${i}" )
log "known extras found: ${extras_name}, val: [${extras_val}]"
eval "${extras_name}=\"${extras_val}\""
if [ -z "${exec_extras}" ]; then
exec_extras="${extras_name}"
else
exec_extras="${exec_extras} ${extras_name}"
fi
else
log "Unknown extras: ${extras_name}"
fi
IFS=","
done
IFS="${OIFS}"
if [ "${ipv6}" = "external" ]; then
v6_addr=$( dhcpdv6 )
primary_ip="${v6_addr}"
ssh_host="${primary_ip}"
ossh_port="22"
v4_addr=$( dhcpd ip4pool=${ip4pool} )
else
v4_addr=$( dhcpd ip4pool=${ip4pool} )
primary_ip="${v4_addr}"
fi
else
v4_addr=$( dhcpd ip4pool=${ip4pool} )
primary_ip="${v4_addr}"
fi
if [ -n "${ci_gw42}" ]; then
v6_addr=$( dhcpdv6 )
fi
log "exec extras: ${exec_extras}"
for i in ${exec_extras}; do
log "looking for helper : ${i}"
#helper_var="extras_helper_${i}"
#log "looking for helper : ${helper_var}"
#eval helper="\$helper_var"
eval helper="\$extras_helper_$i"
if [ -x "${helper}" ]; then
log "HELPER FOUND for ${i}: [${helper}]"
letsencrypt_args=
[ -n "${letsencrypt}" ] && letsencrypt_args="-l true"
log "EXEC HELPER: ${helper} -m create -n \"${lb}\" -a \"${v4_addr}\" -b \"${v6_addr}\" ${letsencrypt_args}"
${helper} -m create -n "${lb}" -a "${v4_addr}" -b "${v6_addr}" ${letsencrypt_args} >> ${MY_LOG} 2>&1
ret=$?
# todo: notify for error
else
log "HELPER for ${i} not found: ${helper}!"
fi
done
for i in ${v4_addr} ${v6_addr}; do
if [ -z "${ip4_addr}" ]; then
ip4_addr="${i}"
else
ip4_addr="${ip4_addr},${i}"
fi
done
# additional check for profile/type?
cid=$( ${miscdir}/cbsd_md5 "${pubkey}" )
if [ ! -d ${cbsd_api_dbdir}/${cid} ]; then
${MKDIR_CMD} -m 0770 -p ${cbsd_api_dbdir}/${cid}
${CHOWN_CMD} ${cbsduser}:${cbsduser} ${cbsd_api_dbdir}/${cid}
fi
if [ ! -d ${cbsd_api_dbdir}/${cid}/.ssh ]; then
${MKDIR_CMD} -m 0770 -p ${cbsd_api_dbdir}/${cid}/.ssh
${CHOWN_CMD} ${cbsduser}:${cbsduser} ${cbsd_api_dbdir}/${cid}/.ssh
fi
if [ ! -r ${cbsd_api_dbdir}/${cid}/.ssh/authorized_keys ]; then
echo "${pubkey}" > ${cbsd_api_dbdir}/${cid}/.ssh/authorized_keys
${CHOWN_CMD} ${cbsduser}:${cbsduser} ${cbsd_api_dbdir}/${cid}/.ssh/authorized_keys
sync
#fflush?
sleep 1
fi
if [ ! -d ${cbsd_api_dbdir}/${cid}/vms ]; then
${MKDIR_CMD} -m 0770 -p ${cbsd_api_dbdir}/${cid}/vms
${CHOWN_CMD} ${cbsduser}:${cbsduser} ${cbsd_api_dbdir}/${cid}/vms
fi
if [ -n "${pkglist}" ]; then
pkg_bootstrap="1"
else
pkg_bootstrap="0"
fi
[ -z "${host_hostname}" ] && host_hostname="${jname}.my.domain"
if [ -z "${cpus}" -o "${cpus}" = "0" ]; then
cpu_args=
else
cpu_args="cpu=\"${cpus}\""
fi
if [ -z "${ram}" -o "${ram}" = "0" ]; then
ram_args=
else
ram_args="vmemoryuse=\"${ram}\""
fi
str="cbsd jcreate \
jname=${jname} \
host_hostname=\"${host_hostname}\" \
runasap=1 \
ip4_addr=\"${ip4_addr}\" \
ver=\"${ver}\" \
fsquota=\"${imgsize}\" \
ci_user_pubkey=\"${cbsd_api_dbdir}/${cid}/.ssh/authorized_keys\" \
pkg_bootstrap="${pkg_bootstrap}" \
allow_raw_sockets=1 \
${cpu_args} \
${ram_args} \
pkglist=\"${pkglist}\" \
"
log "exec [${str}]"
${str}
if [ "${nodeip_expose}" = "0" ]; then
port=22
ssh_host="${primary_ip}"
else
if [ -n "${ossh_port}" ]; then
port=22
else
jname_id=$( echo ${jname} | ${TR_CMD} -d 'env' )
port=$(( jname_id + 4000 ))
log "expose mode=add jname=${jname} in=${port} out=22 outaddr=${v4_addr}"
expose mode=add jname=${jname} in=${port} out=22 outaddr=${v4_addr} >> ${MY_LOG}
fi
fi
if [ -z "${mq_tube}" ]; then
hostname=$( hostname )
else
hostname="${mq_tube}"
fi
echo "${hostname}" > ${cbsd_api_dbdir}/${cid}/${jname}.node
if is_number "${imgsize}"; then
if conv2bytes ${imgsize}; then
imgsize_bytes="${convval}"
else
imgsize_bytes="0"
fi
else
# already on bytes?
imgsize_bytes="${imgsize}"
fi
if is_number "${ram}"; then
if conv2bytes ${ram}; then
ram_bytes="${convval}"
else
ram_bytes="0"
fi
else
# already on bytes?
ram_bytes="${ram}"
fi
create_time=$( ${DATE_CMD} -u "+%Y-%m-%dT%H:%M:%S" )
[ -z "${port4}" ] && port4="${port}"
[ -z "${port6}" ] && port6="22"
[ -z "${ssh4_host}" ] && ssh4_host="${v4_addr}"
[ -z "${ssh6_host}" ] && ssh6_host="${v6_addr}"
if [ "${port4_real}" = "1" ]; then
port4="22"
fi
${CAT_CMD} > ${cbsd_api_dbdir}/${cid}/${jname}-vm.ssh <<EOF
{
"instanceid": "${host_hostname}",
"type": "container",
"emulator": "jail",
"is_power_on": true,
"created": "${create_time}",
"cpus": ${cpus},
"ram_bytes": ${ram_bytes},
"ram_human": "${ram}",
"imgsize_bytes": ${imgsize_bytes},
"imgsize_human": "${imgsize}",
"ssh_user": "${ssh_user}",
"ssh_host": "${ssh_host}",
"ssh4_host": "${ssh4_host}",
"ssh6_host": "${ssh6_host}",
"ssh_port": ${port},
"ssh4_port": ${port4},
"ssh6_port": ${port6},
"ssh_string": "ssh ${ssh_user}@${ssh_host} -p${port}",
"ssh4_string": "ssh ${ssh_user}@${ssh4_host} -p${port4}",
"ssh6_string": "ssh ${ssh_user}@${ssh6_host} -p${port6}"
}
EOF
# dup to param-values
${CAT_CMD} > ${cbsd_api_dbdir}/${cid}/vms/${jname} <<EOF
host_hostname="${host_hostname}"
gid="${jname}"
hostname="${hostname}"
ver="${ver}"
type="container"
emulator="jail"
is_power_on="true"
created="${create_time}"
cpus="${cpus}"
ram_bytes="${ram_bytes}"
ram_human="${ram}"
imgsize_bytes="${imgsize_bytes}"
imgsize_human="${imgsize}"
ssh_user="${ssh_user}"
ssh_host="${ssh_host}"
ssh4_host="${ssh4_host}"
ssh6_host="${ssh6_host}"
ssh_port="${port}"
ssh_port4="${port4}"
ssh_port6="${port6}"
ssh_string="ssh ${ssh_user}@${ssh_host} -p${port}"
ssh4_string="ssh ${ssh_user}@${ssh4_host} -p${port4}"
ssh6_string="ssh ${ssh_user}@${ssh6_host} -p${port6}"
EOF
# echo "${host_hostname} (jail:${ver}) ${cpus}/${ram}/${imgsize} ssh ${ssh_user}@${ssh_host} -p${port}" > ${cbsd_api_dbdir}/${cid}/vms/${jname}
#echo -n "ssh ${ssh_user}@${ssh_host} -p${port}" > ${cbsd_api_dbdir}/${cid}/${jname}-vm.ssh
${CHOWN_CMD} ${cbsduser}:${cbsduser} ${cbsd_api_dbdir}/${cid}/vms/${jname} ${cbsd_api_dbdir}/${cid}/${jname}.node ${cbsd_api_dbdir}/${cid}/${jname}-vm.ssh
[ -n "${api_postcreate_hook}" -a -x "${api_postcreate_hook}" ] && ${api_postcreate_hook} -c "${cid}" -j "${jname}" -m create -r "${cbsd_api_dbdir}" -w "${cbsd_workdir}"
}
## MAIN
case "${mode}" in
create)
create_env
;;
esac
# temporary hack/mock for standalone no-DB API version
[ -x /root/bin/tubestat.sh ] && /root/bin/tubestat.sh > /dev/null 2>&1
[ -x /root/bin/update_cluster_status.sh ] && /root/bin/update_cluster_status.sh > /dev/null 2>&1
exit 0