Replace virtualbmc with sushy-emulator
[icn.git] / tools / vagrant / start_sushy.sh
1 #!/bin/bash
2 set -eu -o pipefail
3
4 listen_ip=$1
5
6 if [[ -f ${HOME}/.sushy/emulator.pid && $(ps -p $(cat ${HOME}/.sushy/emulator.pid) 2>/dev/null) ]]; then
7     echo sushy-emulator is already started
8 else
9     # Install prerequisites
10     if [[ $(which apt-get 2>/dev/null) ]]; then
11         DEBIAN_FRONTEND=noninteractive sudo apt-get install -y make apache2-utils libvirt-dev ovmf python3-pip
12     elif [[ $(which yum) ]]; then
13         # TODO OVMF doesn't include OVMF_CODE.fd
14         sudo yum install -y make httpd-tools libvirt-devel OVMF python3-pip
15     fi
16     sudo python3 -m pip install libvirt-python sushy-tools
17     # Add route to provisioning network - sushy-emulator needs to
18     # fetch ISOs over this during virtual media boot
19     dev=$(ip -o addr show to ${listen_ip} | awk '{print $2}')
20     sudo ip route add 172.22.0.0/24 dev ${dev}
21     # Configure sushy-emulator
22     mkdir -p ${HOME}/.sushy
23     openssl req -x509 -newkey rsa:4096 -keyout ${HOME}/.sushy/key.pem -out ${HOME}/.sushy/cert.pem -sha256 -days 365 -nodes -subj "/CN=${listen_ip}"
24     htpasswd -c -b -B ${HOME}/.sushy/htpasswd admin password
25     cat <<EOF >${HOME}/.sushy/emulator.conf
26 SUSHY_EMULATOR_LISTEN_IP = u'${listen_ip}'
27 SUSHY_EMULATOR_SSL_CERT = u'${HOME}/.sushy/cert.pem'
28 SUSHY_EMULATOR_SSL_KEY = u'${HOME}/.sushy/key.pem'
29 SUSHY_EMULATOR_AUTH_FILE = u'${HOME}/.sushy/htpasswd'
30 SUSHY_EMULATOR_VMEDIA_VERIFY_SSL = False
31 EOF
32     sushy-emulator --config ${HOME}/.sushy/emulator.conf 1>${HOME}/.sushy/emulator-stdout.log 2>${HOME}/.sushy/emulator-stderr.log &
33     echo $! >${HOME}/.sushy/emulator.pid
34 fi