kevydotvinu / openshift-network-playground

The OpenShift Network Playground is built for advanced OpenShift users who needs to quickly build and test different OpenShift network scenarios.
https://kevy.gitbook.io/onp
MIT License
3 stars 1 forks source link

Add `rhcos` and `fcos` `Make` target #48

Closed kevydotvinu closed 1 year ago

kevydotvinu commented 1 year ago
#!/bin/bash
# The script only needs the butane file as an arg

set -eou pipefail

BUTANE=$(readlink -f ${1})
IGNITION_NAME=$(basename -- ${BUTANE})
IGNITION_NAME=${IGNITION_NAME%.*}
RELEASE=$(grep 4[.][0-9] ${BUTANE} | awk '{print $2}')
RELEASE_IMAGE=$(curl -s https://mirror.openshift.com/pub/openshift-v4/clients/ocp/${RELEASE}/release.txt | grep 'Pull From: quay.io' | awk -F ' ' '{print $3}')
CMD=openshift-baremetal-install
PULLSECRET=${HOME}/openshift-network-playground/pullsecret
DIRECTORY=/opt/openshift-network-playground/libvirt/rhcos

sudo podman run --interactive --rm quay.io/coreos/butane:release --raw --strict < ${BUTANE} > ${DIRECTORY}/${IGNITION_NAME}.ign

echo "Downloading oc binary ..."
if [ ! -f "${DIRECTORY}/oc" ] || [ "$(${DIRECTORY}/oc version --client -o json | jq -r .releaseClientVersion)" != "${RELEASE}" ]; then
curl -s https://mirror.openshift.com/pub/openshift-v4/clients/ocp/${RELEASE}/openshift-client-linux.tar.gz | tar zxf - -C ${DIRECTORY} oc
fi
echo "✔ Downloaded!"

echo "Downloading openshift-install binary ..."
if [ ! -f "${DIRECTORY}/openshift-baremetal-install" ] || [ "$(${DIRECTORY}/openshift-baremetal-install version | head -n 1 | awk '{print $2}')" != "${RELEASE}" ]; then
${DIRECTORY}/oc adm release extract --registry-config "${PULLSECRET}" --command=${CMD} --to "${DIRECTORY}" ${RELEASE_IMAGE}
fi
echo "✔ Downloaded!"

RHCOS_QEMU_URI=$(${DIRECTORY}/openshift-baremetal-install coreos print-stream-json | jq -r --arg ARCH "$(arch)" '.architectures[$ARCH].artifacts.qemu.formats["qcow2.gz"].disk.location')
RHCOS_QEMU_NAME=${RHCOS_QEMU_URI##*/}
echo "Downloading image ..."

if [ ! -f "${DIRECTORY}/${RHCOS_QEMU_NAME}" ]; then
curl -#L ${RHCOS_QEMU_URI} -o ${DIRECTORY}/${RHCOS_QEMU_NAME}
fi
echo "✔ Downloaded!"

echo "Extracting image ..."
gunzip -c ${DIRECTORY}/${RHCOS_QEMU_NAME} > ${DIRECTORY}/${RHCOS_QEMU_NAME%.*}
cp ${DIRECTORY}/${RHCOS_QEMU_NAME%.*} ${DIRECTORY}/rhcos.qcow2
echo "✔ Extracted!"

DISK=${DIRECTORY}/rhcos.qcow2
VM_NAME=$(basename -- ${DISK})
VM_NAME=${VM_NAME%.*}

sudo virsh -q destroy ${VM_NAME} > /dev/null || true
sudo virsh -q undefine ${VM_NAME} > /dev/null || true
sudo virt-install --name ${VM_NAME} \
                  --vcpus 2 \
                  --ram 2048 \
                  --os-variant fedora-coreos-stable \
                  --import --network bridge=baremetal \
                  --qemu-commandline="-fw_cfg name=opt/com.coreos/config,file=${DIRECTORY}/${IGNITION_NAME}.ign" \
                  --disk ${DISK} \
                  --graphics spice,listen=0.0.0.0 \
                  --video virtio \
                  --channel spicevmc \
                  --console pty \
                  --serial pty \
                  --noautoconsole
sudo virsh console ${VM_NAME}