-
Notifications
You must be signed in to change notification settings - Fork 0
/
bhyve-api
executable file
·612 lines (535 loc) · 16.9 KB
/
bhyve-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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#!/usr/local/bin/cbsd
#v14.0.0
DIST_MODULE_PATH="${distmoduledir}/api.d"
. ${DIST_MODULE_PATH}/share/bhyve.conf
MYOPTARG="imgsize jname image ram cpus pubkey host_hostname extras ci_user_pw_user ci_user_pw_root bhyve_vnc_tcp_bind vm_os_type vm_os_profile"
MYARG="mode"
MYDESC="Operate with bhyve 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
dryrun=0
checkip=1
gold=0
develop=0
runasap=0
ip4_gw=
readconf bhyve-default-default.conf
readconf bhyve.conf
# ssh host
ssh_host=
readconf api.conf
readconf bhyve-api.conf
[ -n "${ip4_gw}" ] && oip4_gw="${ip4_gw}"
set -e
. ${distmoduledir}/api.d/api.subr
set +e
MY_LOG="${cbsd_api_logdir}/bhyve-api.log"
log "wakeup: [workdir: ${workdir}, cbsdworkdir: ${cbsd_workdir}, api_postcreate_hook: ${api_postcreate_hook}, cbsd_api_dbdir: ${cbsd_api_dbdir}]: $*"
ip4_addr=
vm_os_type=
vm_os_profile=
ip4_addr=
ip4_gw=
vm_os_type=
vm_os_profile=
. ${cbsdinit}
. ${subrdir}/system.subr
. ${subrdir}/strings.subr
[ -z "${ip4_gw}" -a -n "${oip4_gw}" ] && ip4_gw="${oip4_gw}"
create_env()
{
local _profile_file=
ssh_user=
ssh_port=
if [ "${image}" = "bhyve" -o -z "${image}" ]; then
[ -z "${vm_os_type}" ] && err 1 "empty image and/or vm_os_type"
[ -z "${vm_os_profile}" ] && err 1 "empty image and/or vm_os_profile"
if [ -r "${workdir}/etc/vm-${vm_os_type}-${vm_os_profile}.conf" ]; then
_profile_file="${workdir}/etc/vm-${vm_os_type}-${vm_os_profile}.conf"
elif [ -r "${workdir}/etc/defaults/vm-${vm_os_type}-${vm_os_profile}.conf" ]; then
_profile_file="${workdir}/etc/defaults/vm-${vm_os_type}-${vm_os_profile}.conf"
fi
[ -z "${_profile_file}" ] && err 1 "no such profile for type/profile: ${vm_os_type}-${vm_os_profile}"
eval $( ${GREP_CMD} ^myb_image= ${_profile_file} 2>/dev/null )
[ -z "${myb_image}" ] && err 1 "no such myb_image vars for type/profile: ${vm_os_type}-${vm_os_profile}"
image="${myb_image}"
else
eval vm_os_type="\$${image}_vm_os_type"
eval vm_os_profile="\$${image}_vm_os_profile"
fi
eval ssh_user="\$${image}_ssh_user"
[ -z "${ssh_host}" ] && ssh_host="${nodeip}"
[ -z "${vm_os_type}" ] && err 1 "vm_os_type empty? check img/mapping in bhyve-api.conf for: ${image}"
[ -z "${vm_os_profile}" ] && err 1 "vm_os_profile empty? check img/mapping in bhyve-api.conf for: ${image}"
[ -z "${ssh_user}" ] && err 1 "ssh_user empty? check img/mapping in bhyve-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
ip4_addr="${v4_addr}"
# for i in ${v4_addr} ${v6_addr}; do
# if [ -z "${ip4_addr}" ]; then
# ip4_addr="${i}"
# else
# ip4_addr="${ip4_addr},${i}"
# fi
# done
#ip4_addr=$( dhcpd ip4pool=${ip4pool} )
[ -z "${ip4_gw}" ] && ip4_gw="10.0.0.1"
ci_jname="${jname}"
if [ -n "${host_hostname}" ]; then
ci_fqdn="${host_hostname}"
else
ci_fqdn="${jname}.my.domain"
fi
ci_ip4_addr="${ip4_addr}"
ci_gw4="${ip4_gw}"
[ -z "${vm_os_type}" ] && err 1 "no vm_os_type"
[ -z "${vm_os_profile}" ] && err 1 "no vm_os_profile"
# 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 "${ci_gw42}" ]; then
str="cbsd bcreate jname=${jname} runasap=1 vm_cpus=${cpus} vm_ram=${ram} imgsize=${imgsize} vm_os_type=\"${vm_os_type}\" vm_os_profile=\"${vm_os_profile}\" ci_jname=\"${jname}\" ci_fqdn=\"${ci_fqdn}\" ci_ip4_addr=\"${ip4_addr}\" ci_gw4=\"${ip4_gw}\" ip4_addr=\"${ip4_addr}\" ci_user_pubkey=\"${cbsd_api_dbdir}/${cid}/.ssh/authorized_keys\" ci_jname=\"${ci_fqdn}\" ci_ip4_addr2=\"${v6_addr}\" ci_user_pw_root=\"cbsd\""
else
str="cbsd bcreate jname=${jname} runasap=1 vm_cpus=${cpus} vm_ram=${ram} imgsize=${imgsize} vm_os_type=\"${vm_os_type}\" vm_os_profile=\"${vm_os_profile}\" ci_jname=\"${jname}\" ci_fqdn=\"${ci_fqdn}\" ci_ip4_addr=\"${ip4_addr}\" ci_gw4=\"${ip4_gw}\" ip4_addr=\"${ip4_addr}\" ci_user_pubkey=\"${cbsd_api_dbdir}/${cid}/.ssh/authorized_keys\" ci_jname=\"${ci_fqdn}\""
fi
[ -n "${bhyve_vnc_tcp_bind}" ] && str="${str} bhyve_vnc_tcp_bind=\"${bhyve_vnc_tcp_bind}\""
[ -n "${ci_user_pw_user}" ] && str="${str} ci_user_pw_user=\"${ci_user_pw_user}\""
[ -n "${ci_user_pw_root}" ] && str="${str} ci_user_pw_root=\"${ci_user_pw_root}\""
log "${str}"
${str}
max_retry=20
retry=0
# inherit workdir from CBSD/API?
. /etc/rc.conf
while [ ${retry} -lt ${max_retry} ]; do
if [ -r ${cbsd_workdir}/vm/${jname}/vnc_port ]; then
vnc_file_stat=$( ${STAT_CMD} -f "%z" ${cbsd_workdir}/vm/${jname}/vnc_port 2>/dev/null )
[ ${vnc_file_stat} -gt 0 ] && break
fi
sleep 1
retry=$(( retry + 1 ))
done
if [ ${retry} -gt ${max_retry} ]; then
log "unable to read vnc port file: ${cbsd_workdir}/vm/${jname}/vnc_port"
err 1 "unable to read vnc port file: ${cbsd_workdir}/vm/${jname}/vnc_port"
fi
# for slow hdd/vm startup
sleep 5
if [ "${nodeip_expose}" = "0" ]; then
port=22
ssh_host="${ip4_addr}"
else
vnc_port=$( ${HEAD_CMD} -n1 ${cbsd_workdir}/vm/${jname}/vnc_port | ${AWK_CMD} '{printf $1}' )
port=$(( vnc_port + 3000 ))
vnc_redir=$(( vnc_port + 4000 ))
# to config: VDI flags
expose mode=add jname=${jname} in=${vnc_redir} out=${vnc_port} outaddr=127.0.0.1 >> ${MY_LOG}
if [ "${vm_os_type}" = "windows" ]; then
log "expose mode=add jname=${jname} in=${port} out=3389"
expose mode=add jname=${jname} in=${port} out=3389 >> ${MY_LOG}
elif [ "${image}" = "ubuntu22_vdi" ]; then
log "expose mode=add jname=${jname} in=${port} out=3389"
expose mode=add jname=${jname} in=${port} out=3389 >> ${MY_LOG}
else
log "expose mode=add jname=${jname} in=${port} out=22"
expose mode=add jname=${jname} in=${port} out=22 >> ${MY_LOG}
fi
fi
if [ -n "${api_ip4_pool}" ]; then
my_extv4=$( /usr/local/bin/cbsd dhcpd ip4pool=${api_ip4_pool} )
fi
# for test only
if [ -n "${my_extv4}" ]; then
cat > /usr/jails/jails-system/${jname}/master_poststart.d/v4 <<EOF
#!/bin/sh
/sbin/ifconfig ix1 add ${my_extv4}/28
/usr/local/bin/cbsd expose mode=add jname=${jname} inaddr=${my_extv4}
EOF
/sbin/ifconfig ix1 add ${my_extv4}/28
/usr/local/bin/cbsd expose mode=add jname=${jname} inaddr=${my_extv4}
cat > /usr/jails/jails-system/${jname}/master_poststop.d/v4 <<EOF
#!/bin/sh
/sbin/ifconfig ix1 delete ${my_extv4}
/usr/local/bin/cbsd expose mode=delete jname=${jname} inaddr=${my_extv4}
EOF
chmod +x /usr/jails/jails-system/${jname}/master_poststop.d/v4 /usr/jails/jails-system/${jname}/master_poststart.d/v4
ssh4_host="${my_extv4}"
fi
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
if conv2human "${ram_bytes}"; then
ram="${convval}"
fi
if conv2human "${imgsize_bytes}"; then
imgsize="${convval}"
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
#echo "${host_hostname} (${vm_os_type}:${vm_os_profile}) ${cpus}/${ram}/${imgsize} ssh ${ssh_user}@${ssh_host} -p${port}" > ${cbsd_api_dbdir}/${cid}/vms/${jname}
if [ -z "${mq_tube}" ]; then
hostname=$( hostname )
else
hostname="${mq_tube}"
fi
case "${vm_os_type}" in
windows)
port4=3389
port6=3389
rdp_host="${ssh_host}"
rdp_port="${port}"
rdp4_host="${ssh4_host}"
rdp6_host="${ssh6_host}"
rdp4_port="${port4}"
rdp6_port="${port6}"
[ -z "${rdp6_port}" ] && rdp6_port="${rdp4_port}"
ssh_user=
ssh_host=
ssh4_host=
ssh6_host=
ssh_port=
ssh4_port=
ssh6_port=
rdp_user="Admin"
rdp_password="cix2022"
;;
*)
rdp_user=
rdp_password=
rdp_host=
rdp_port=
;;
esac
if [ "${image}" = "ubuntu22_vdi" ]; then
port4=3389
port6=3389
rdp_host="${ssh_host}"
rdp_port="${port}"
rdp4_host="${ssh4_host}"
rdp6_host="${ssh6_host}"
rdp4_port="${port4}"
rdp6_port="${port6}"
[ -z "${rdp6_port}" ] && rdp6_port="${rdp4_port}"
ssh_user=
ssh_host=
ssh4_host=
ssh6_host=
ssh_port=
ssh4_port=
ssh6_port=
rdp_user="user"
rdp_password="password"
fi
echo "${hostname}" > ${cbsd_api_dbdir}/${cid}/${jname}.node
${CAT_CMD} > ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
{
"id": "${host_hostname}",
"type": "vm",
"emulator": "bhyve",
"is_power_on": true,
"created": "${create_time}",
"cpus": ${cpus},
"ram": "${ram}",
"ram_bytes": ${ram_bytes},
"ram_human": "${ram}",
"imgsize_bytes": ${imgsize_bytes},
"imgsize": "${imgsize}",
"vm_os_type": "${vm_os_type}",
"vm_os_profile": "${vm_os_profile}",
"image": "${image}",
EOF
if [ -n "${ssh_user}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"ssh_user": "${ssh_user}",
EOF
fi
if [ -n "${ssh_host}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"ssh_host": "${ssh_host}",
EOF
fi
if [ -n "${ssh4_user}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"ssh4_host": "${ssh4_host}",
EOF
fi
if [ -n "${ssh6_host}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"ssh6_host": "${ssh6_host}",
EOF
fi
if [ -n "${ssh_port}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"ssh_port": ${ssh_port},
EOF
fi
if [ -n "${ssh4_port}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"ssh4_port": ${ssh4_port},
EOF
fi
if [ -n "${ssh6_port}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"ssh6_port": ${ssh6_port},
EOF
fi
if [ -n "${rdp_user}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"rdp_user": "${rdp_user}",
EOF
fi
if [ -n "${rdp_password}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"rdp_password": "${rdp_password}",
EOF
fi
if [ -n "${rdp_host}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"rdp_host": "${rdp_host}",
EOF
fi
if [ -n "${rdp4_host}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"rdp4_host": "${rdp4_host}",
EOF
fi
if [ -n "${rdp4_port}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"rdp4_port": ${rdp4_port},
EOF
fi
if [ -n "${rdp6_host}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"rdp6_host": "${rdp6_host}",
EOF
fi
if [ -n "${rdp6_port}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"rdp6_port": ${rdp6_port},
EOF
fi
if [ -n "${rdp_port}" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"rdp_port": ${rdp_port},
EOF
fi
## VNC REDIR
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"vnc_port": ${vnc_redir},
"vnc_password": "cbsd",
"vnc_host": "209.127.24.102",
EOF
case "${vm_os_type}" in
windows)
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"rdp_string": "xfreerdp +clipboard +fonts /rfx /size:1920x1200 /u:Admin /p:cix2022 /v:${rdp_host}:${rdp_port}",
"rdp4_string": "xfreerdp +clipboard +fonts /rfx /size:1920x1200 /u:Admin /p:cix2022 /v:${rdp4_host}:${port4}",
"rdp6_string": "xfreerdp +clipboard +fonts /rfx /size:1920x1200 /u:Admin /p:cix2022 /v:[${rdp6_host}]:${port6}"
}
EOF
;;
*)
if [ "${image}" = "ubuntu22_vdi" ]; then
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"rdp_string": "xfreerdp +clipboard +fonts /rfx /size:1920x1200 /u:user /p:password /v:${rdp_host}:${rdp_port}",
"rdp4_string": "xfreerdp +clipboard +fonts /rfx /size:1920x1200 /u:user /p:password /v:${rdp4_host}:${port4}",
"rdp6_string": "xfreerdp +clipboard +fonts /rfx /size:1920x1200 /u:user /p:password /v:[${rdp6_host}]:${port6}"
}
EOF
else
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.ssh <<EOF
"ssh_port": ${port},
"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
fi
;;
esac
# dup to param-values
${CAT_CMD} > ${cbsd_api_dbdir}/${cid}/vms/${jname} <<EOF
host_hostname="${host_hostname}"
gid="${jname}"
hostname="${hostname}"
type="vm"
emulator="bhyve"
is_power_on="true"
created="${create_time}"
cpus="${cpus}"
ram_bytes="${ram_bytes}"
ram="${ram}"
imgsize_bytes="${imgsize_bytes}"
imgsize="${imgsize}"
vm_os_type="${vm_os_type}"
vm_os_profile="${vm_os_profile}"
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}"
EOF
case "${vm_os_type}" in
windows)
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/vms/${jname} <<EOF
ssh_string="xfreerdp +clipboard +fonts /rfx /size:1920x1200 /u:Admin /v:${ssh_host}:${port}"
ssh4_string="xfreerdp +clipboard +fonts /rfx /size:1920x1200 /u:Admin /v:${ssh4_host}:${port4}"
ssh6_string="xfreerdp +clipboard +fonts /rfx /size:1920x1200 /u:Admin /v:${ssh6_host}:${port6}"
EOF
;;
*)
${CAT_CMD} >> ${cbsd_api_dbdir}/${cid}/vms/${jname} <<EOF
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
;;
esac
${CHOWN_CMD} ${cbsduser}:${cbsduser} ${cbsd_api_dbdir}/${cid}/vms/${jname} ${cbsd_api_dbdir}/${cid}/${jname}.node ${cbsd_api_dbdir}/${cid}/${jname}-bhyve.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