# CBSDfile sample for 'cbsd up' and 'cbsd destroy' command
# cbsd [up|destroy] <name>     - to deploy only <name> env
#
# Uncommend CLOUD_\* vars ro work remote API provider
# See https://www.convectix.com/en/cbsdfile.html for details
#CLOUD_URL="https://us-sw.api.my.domain"
#CLOUD_KEY="ssh-ed25519 XXX your@comment"

# in postcreate_\* function regular CBSD commands can be used, 
# the 'jname' argument can be omitted, e.g:
# postcreate_jail1()
# {
#     expose mode=apply in=1234 out=22
# }


# run create/destroy in parallel?
# 0 - disabled
# any positive integer  - max simulative threads
PARALLEL=10

# verbose output ?
quiet=0

# jail_XXX and bhyve_XXX function only for params definition
# If you need any action upon create/destroy, please use
# postcreate_XXX and postdestroy_XXX function

# via globals() you can set same parameters once.
globals()
{
	domain="example.com"
	bhyve_vnc_tcp_bind="0.0.0.0"
}

# CentOS 7
bhyve_vm1()
{
	vm_ram="1g"
	vm_cpus="1"
	imgsize="6g"
	vm_os_type="linux"
	vm_os_profile="cloud-CentOS-7-x86_64"
	interface="auto"		# can be VALE, e.g: interface="vale1"
	ci_fqdn="${jname}.example.com"

	ci_ip4_addr=$( dhcpd )		# use CBSD 'dhcp' script to get free IP from CBSD pool
					# or use static IP, e.g: ci_ip4_addr="192.168.0.50"
	ci_gw4="192.168.0.50"		# IPv4 gateway, can set globally via globals or ~cbsd/etc/bhyve-default-default.conf
	ci_interface_mtu="1500"		# MTU, can set globally via globals or ~cbsd/etc/bhyve-default-default.conf
	# vm_vnc_port=6000		# by default (when vm_vnc_port not set), internal CBSD function:
					#   "get-next-tcp-port address=${_vnc_bind} start_port=5900 end_port=6900" (see virtual.subr)
					# used to determine and assing free TCP VNC port. But you can specify a fixed port.
}

# CentOS 9
bhyve_vm2()
{
	vm_ram="1g"
	vm_cpus="1"
	imgsize="6g"
	vm_os_type="linux"
	vm_os_profile="cloud-CentOS-stream-9-x86_64"
	interface="auto"		# can be VALE, e.g: interface="vale1"
	ci_fqdn="${jname}.example.com"

	ci_ip4_addr=$( dhcpd )		# use CBSD 'dhcp' script to get free IP from CBSD pool
					# or use static IP, e.g: ci_ip4_addr="192.168.0.50"
	ci_gw4="192.168.0.50"		# IPv4 gateway
	# vm_vnc_port=6000		# by default (when vm_vnc_port not set), internal CBSD function:
					#   "get-next-tcp-port address=${_vnc_bind} start_port=5900 end_port=6900" (see virtual.subr)
					# used to determine and assing free TCP VNC port. But you can specify a fixed port.
}

# Debian 10
bhyve_vm3()
{
	vm_ram="1g"
	vm_cpus="1"
	imgsize="6g"
	vm_os_type="linux"
	vm_os_profile="cloud-Debian-x86-12"
	interface="auto"		# can be VALE, e.g: interface="vale1"
	ci_fqdn="${jname}.example.com"

	ci_ip4_addr=$( dhcpd )		# use CBSD 'dhcp' script to get free IP from CBSD pool
					# or use static IP, e.g: ci_ip4_addr="192.168.0.50"
	ci_gw4="192.168.0.50"		# IPv4 gateway
	ci_interface_mtu="1500"		# MTU
	# vm_vnc_port=6000		# by default (when vm_vnc_port not set), internal CBSD function:
					#   "get-next-tcp-port address=${_vnc_bind} start_port=5900 end_port=6900" (see virtual.subr)
					# used to determine and assing free TCP VNC port. But you can specify a fixed port.
}

# Ubuntu 20
bhyve_vm4()
{
	vm_ram="1g"
	vm_cpus="1"
	imgsize="6g"
	vm_os_type="linux"
	vm_os_profile="cloud-ubuntuserver-amd64-20.04"
	interface="auto"		# can be VALE, e.g: interface="vale1"
	ci_fqdn="${jname}.example.com"

	ci_ip4_addr=$( dhcpd )		# use CBSD 'dhcp' script to get free IP from CBSD pool
					# or use static IP, e.g: ci_ip4_addr="192.168.0.50"
	ci_gw4="192.168.0.50"		# IPv4 gateway
	ci_interface_mtu="1500"		# MTU
	# vm_vnc_port=6000		# by default (when vm_vnc_port not set), internal CBSD function:
					#   "get-next-tcp-port address=${_vnc_bind} start_port=5900 end_port=6900" (see virtual.subr)
					# used to determine and assing free TCP VNC port. But you can specify a fixed port.
}

# FreeBSD 12 UFS
bhyve_vm5()
{
	vm_ram="1g"
	vm_cpus="1"
	imgsize="6g"
	vm_os_type="freebsd"
	vm_os_profile="cloud-FreeBSD-ufs-x64-12.1"
	interface="auto"		# can be VALE, e.g: interface="vale1"
	ci_fqdn="${jname}.example.com"

	ci_ip4_addr=$( dhcpd )		# use CBSD 'dhcp' script to get free IP from CBSD pool
					# or use static IP, e.g: ci_ip4_addr="192.168.0.50"
	ci_gw4="192.168.0.50"		# IPv4 gateway
	ci_interface_mtu="1500"		# MTU
	# vm_vnc_port=6000		# by default (when vm_vnc_port not set), internal CBSD function:
					#   "get-next-tcp-port address=${_vnc_bind} start_port=5900 end_port=6900" (see virtual.subr)
					# used to determine and assing free TCP VNC port. But you can specify a fixed port.
}


# alternative env
#bhyve_vmX()
#{
#	cbsd_workdir="/env2"		# set alternative workdir
#
#	vm_ram="4g"
#	vm_cpus="2"
#	imgsize="10g"
#	vm_os_type="linux"
#	vm_os_profile="cloud-CentOS-7-x86_64"
#}

jail_test1()
{
	ip4_addr="DHCP"
	host_hostname="${jname}.example.com"
	pkg_bootstrap="0"
	sysrc="syslogd_enable=NO cron_enable=NO"
}

# postcreate_ function has facts/variables from export_jail_data_for_external_hook, e.g.:
#  $ipv4_first_public $ipv4_first_private $ipv4_first $ipv6_first_public $ipv6_first_private $ipv6_first
postcreate_test1()
{
	# turn off syslogd/sendmail/cron services
	sysrc \
		syslogd_enable="NO" \
		syslogd_flags="-ss" \
		sendmail_enable="NO" \
		sendmail_submit_enable="NO" \
		sendmail_outbound_enable="NO" \
		sendmail_msp_queue_enable="NO" \
		cron_enable="NO"

	# copy file from host system into
	jscp /COPYRIGHT ${jname}:/root/COPYRIGHT

	# run bunch of command inside env
	jexec <<EOF

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin
set -o errexit

date
hostname
EOF

}

jail_test2()
{
	ip4_addr="DHCP"
	host_hostname="test2.my.domain"
	pkg_bootstrap="0"
	sysrc="syslogd_enable=NO cron_enable=NO"

	# LIMIT/QUOTAs:
	fsquota="10g"		# set ZFS dataset quota
	cpu="1"			# only one vCPU allowed!
	vmemoryuse="512m"	# only 512MB RAM allowed!
}

postcreate_test2()
{
	# generate temp script and copy them to test2 jail

	file=$( mktemp )
	sysrc syslogd_flags="-ss"

	date >> ${file}

	jscp ${file} ${jname}:/root/readme.txt
	jexec cat /root/readme.txt
	${RM_CMD} -f ${file}
}

jail_bash()
{
	ip4_addr="DHCP"
	host_hostname="test2.my.domain"
	pkg_bootstrap="1"
	# install bash and mc
	pkglist="shells/bash misc/mc"
}


bhyve_rmq1()
{
	vm_ram="4g"
	vm_cpus="2"
	imgsize="6g"
	vm_os_type="linux"
	vm_os_profile="cloud-CentOS-7-x86_64"

	ip4_addr="10.132.32.106"
	ip4_gw="10.132.32.254"
	fqdn="${jname}.test.inf.dev"

	# interface="vale1"
	interface="auto"
	ci_jname="${jname}"
	ci_fqdn="${fqdn}"
	ci_ip4_addr="${ip4_addr}"
	ci_gw4="${ip4_gw}"
	ci_interface_mtu="${ci_interface_mtu}"
	#imgtype="md"
	imgtype="zvol"
	runasap=1

	# wait for SSH ready
	ssh_wait=1
}

postcreate_rmq1()
{
	#bexec sudo yum update -y

	# install puppet
	bexec <<EOF
sudo yum -y install https://yum.puppet.com/puppet6-release-el-7.noarch.rpm
sudo yum install -y puppet-agent
sudo yum clean -y all
EOF
	# copy script and execute it to attach env to puppetserver ( just sample )
	#bscp prepare_pup.sh ${jname}:prepare_pup.sh
	#bexec sudo /home/centos/prepare_pup.sh
	#echo "SIGN Puppet"
	# re-run puppet agent
	# bexec sudo /opt/puppetlabs/bin/puppet agent -t
}
