diff --git a/examples/opnsense/scripts/common b/examples/opnsense/scripts/common new file mode 100644 index 0000000..1a1f884 --- /dev/null +++ b/examples/opnsense/scripts/common @@ -0,0 +1,54 @@ +#! /bin/bash + +_warn(){ >&2 echo "WARNING: $*" ; } + +_fatal(){ + >&2 echo "FATAL ERROR: $*" + >&2 echo stopping... + exit 1 +} + +is_string_empty(){ + if [ "${*:-}" != "" ]; then + return 0 + else + return 1 + fi +} + +is_debian_family()( + is_string_empty "$(apt --version 2> /dev/null )" +) + +has_ip(){ + is_string_empty "$(ip -V 2> /dev/null)" +} + +has_virsh(){ + is_string_empty "$(virsh --version 2> /dev/null)" +} + +has_virt_customize(){ + is_string_empty "$(virt-customize --version 2> /dev/null)" +} + +has_curl(){ + is_string_empty "$(curl --version 2> /dev/null)" +} +has_wget(){ + is_string_empty "$(wget --version 2> /dev/null)" +} + +install_kvm(){ + sudo apt install -y --no-install-recommends qemu-system libvirt-clients libvirt-daemon-system + sudo adduser "$USER" libvirt +} + +install_virt_customize(){ + sudo apt install -y libguestfs-tools +} + +install_wget(){ + sudo apt install -y wget +} + diff --git a/examples/opnsense/scripts/devops b/examples/opnsense/scripts/devops new file mode 100755 index 0000000..d35a220 --- /dev/null +++ b/examples/opnsense/scripts/devops @@ -0,0 +1,118 @@ +#! /bin/bash + +devops()( + + set -eu + + [ "${1:-}" != "-d" ] || { set -x ; shift ; } + trap '[ "$?" = "0" ] || >&2 echo ABNORMAL TERMINATION' EXIT + + BASE_DIR=$(readlink -f "$(dirname "${BASH_SOURCE}")/..") + + _short_help(){ + + cat <<-EOM + + NAME + devops -- utility script for this example ($(basename ${BASE_DIR})) + + SYNOPSYS + + devops [GLOBAL_OPTIONS] COMMAND [OPTIONS] + + devops up [-f] # Ensure that everything needed by the example is ready + devops down # Destroy everything + + devops check-dependencies # Check that dependencies are installed + devops install-dependencies # Check that dependencies are installed + devops setup-network # Prepare the network + devops + +EOM + + } + + _extra_help(){ + + cat <<-EOM + + GLOBAL_OPTIONS + + -d Debug mode. + + WARNINGS + + This script is experimetal. Use with caution. +EOM + + } + + + _check_dependencies(){ + . "${BASE_DIR}/scripts/common" + missing=0 + NEED_IP=false + NEED_KVM=false + NEED_VIRT_CUSTOMIZE=false + NEED_WGET=false + is_debian_family || _fatal only debian based version is supported + has_ip || { + missing=$(( missing + 1)); + _warn "ip command is missing"; + NEED_IP=true + } + has_virsh ||{ + missing=$(( missing + 1)); + _warn "virsh command is missing"; + NEED_KVM=true + } + has_virt_customize || { + missing=$(( missing + 1)); + _warn "virt-customize command is missing"; + NEED_VIRT_CUSTOMIZE=true + } + has_wget || has_curl || { + missing=$(( missing + 1)); + _warn "wget and curl commands are missing"; + NEED_WGET=true + + } + + } + + _install_dependencies(){ + + [ "$NEED_KVM" != "true" ] || install_kvm + [ "$NEED_VIRT_CUSTOMIZE" != "true" ] || install_virt_customize + [ "$NEED_WGET" != "true" ] || install_wget + } + + +case "${1:-}" in + + -h|--help|"") + _short_help + _extra_help + ;; + check-dependencies) + _check_dependencies + if [ "$missing" -gt 0 ]; then + exit 1 + fi + _warn No missing dependencies + ;; + install-dependencies) + _check_dependencies + _install_dependencies + ;; + *) + echo "Unknown COMMAND '$1'" + exit 1 + ;; +esac + +) + + +[ "$0" != "${BASH_SOURCE}" ] || devops "${@}" + diff --git a/examples/opnsense/setup b/examples/opnsense/setup new file mode 100644 index 0000000..74767e3 --- /dev/null +++ b/examples/opnsense/setup @@ -0,0 +1,4 @@ +#! /bin/bash + + +export PATH=$(readlink -f "$(dirname "${BASH_SOURCE}")/scripts"):"${PATH}"