#!/bin/bash # red delete # orange check-no-delete # orange check-no-create # green create [[ -f ./libs/shflags ]] && . ./libs/shflags || eval "$(curl --silent https://git.g3e.fr/H6N/tools/raw/branch/main/libs/shflags)" . ./lib/vpc.sh . ./lib/subnet.sh . ./lib/colors.sh . ./lib/qemu.sh . ./lib/metadata.sh DRY_RUN="1" SIMULATION="1" function start_vm { DEFINE_string 'interface' 'eno1' 'KVM Interface' 'i' DEFINE_string 'vxlan_id' '-' 'VXLAN ID' 'v' DEFINE_string 'gateway_ip' '-' 'Gatewat IP' 'g' DEFINE_string 'network' '-' 'Network IP' 'n' DEFINE_string 'netmask' '-' 'Network Mask' 'e' DEFINE_string 'vpc_name' '-' 'VPC NAME' 'c' DEFINE_string 'subnet_name' '-' 'SUBNET NAME' 's' DEFINE_string 'vm_name' '-' 'VM NAME' 'm' DEFINE_string 'vm_ip' '-' 'VM CIDR' 'p' DEFINE_string 'volume_id' '-' 'Volume backend file' 'o' DEFINE_string 'ssh_key' '-' 'pub ssh Key' 'k' DEFINE_boolean 'dryrun' false 'Enable dry-run mode' 'd' FLAGS "$@" || exit $? eval set -- "${FLAGS_ARGV}" #bash agent.sh StartVm -c vpc-00000 -s subnet-00000 -v 12 -g 240.0.0.1 -n 240.0.0.0 -e 16 -m i-00000 -p 240.0.1.1 local local_ip=$(ip a show dev ${FLAGS_interface} | grep 'inet ' | sed -e 's/ */ /g' |cut -d\ -f 3|cut -d\/ -f1) local gateway_ip="${FLAGS_gateway_ip}/32" local subnet="${FLAGS_network}/${FLAGS_netmask}" local vm_ip="${FLAGS_vm_ip}/${FLAGS_netmask}" print_in_color "${COLOR_SYAN}" "#############################################" print_in_color "${COLOR_SYAN}" "Create vm" print_in_color "${COLOR_SYAN}" "kvm:" print_in_color "${COLOR_SYAN}" " interface name: ${FLAGS_interface}" print_in_color "${COLOR_SYAN}" " interface ip: ${local_ip}" print_in_color "${COLOR_SYAN}" "subnet:" print_in_color "${COLOR_SYAN}" " vpc: ${FLAGS_vpc_name}" print_in_color "${COLOR_SYAN}" " vxlan: ${FLAGS_vxlan_id}" print_in_color "${COLOR_SYAN}" " name: ${FLAGS_subnet_name}" print_in_color "${COLOR_SYAN}" " gateway: ${gateway_ip}" print_in_color "${COLOR_SYAN}" " network: ${subnet}" print_in_color "${COLOR_SYAN}" "vm:" print_in_color "${COLOR_SYAN}" " name: ${FLAGS_vm_name}" print_in_color "${COLOR_SYAN}" " ip: ${vm_ip}" print_in_color "${COLOR_SYAN}" " volume backing file: ${FLAGS_volume_id}" print_in_color "${COLOR_SYAN}" " sshkey: ${FLAGS_ssh_key}" print_in_color "${COLOR_SYAN}" "#############################################" echo echo create_vpc "${FLAGS_vpc_name}" create_subnet "${FLAGS_vpc_name}" "${FLAGS_subnet_name}" "${FLAGS_vxlan_id}" "${local_ip}" "${gateway_ip}" "${subnet}" qemu_start_vm "${FLAGS_vm_ip}" "${FLAGS_subnet_name}" "${FLAGS_vpc_name}" "${FLAGS_vm_name}" "${FLAGS_volume_id}" "${FLAGS_ssh_key}" "${FLAGS_gateway_ip}" } function stop_vm { DEFINE_string 'vm_name' '-' 'VM NAME' 'm' DEFINE_boolean 'dryrun' false 'Enable dry-run mode' 'd' FLAGS "$@" || exit $? eval set -- "${FLAGS_ARGV}" qemu_stop_vm "${FLAGS_vm_name}" } function create_volume { DEFINE_string 'volume_name' '-' 'Volume Name' 'v' DEFINE_string 'backind_volume' '-' 'Volume Back' 'b' DEFINE_string 'volume_size' '-' 'Volume Size' 's' DEFINE_boolean 'dryrun' false 'Enable dry-run mode' 'd' FLAGS "$@" || exit $? eval set -- "${FLAGS_ARGV}" qemu-img create \ -f qcow2 -b "${FLAGS_backind_volume}" \ -F qcow2 "${FLAGS_volume_name}" \ "${FLAGS_volume_size}" } function delete_volume { DEFINE_string 'volume_name' '-' 'Volume Name' 'v' DEFINE_boolean 'dryrun' false 'Enable dry-run mode' 'd' FLAGS "$@" || exit $? eval set -- "${FLAGS_ARGV}" rm -rf "${FLAGS_backind_volume}" } function main { fonction="${1}" shift 1 case "${fonction}" in "StartVm") start_vm "$@" ;; "StopVm") stop_vm "$@" ;; "CreateVolume") create_volume "$@" ;; "DeleteVolume") delete_volume "$@" ;; "Config") metadata_service ;; *) echo "action : ${fonction} not known !" echo "avalable action :" echo " -> StartVm" echo " -> StopVm" echo " -> CreateVolume" echo " -> DeleteVolume" ;; esac } main "$@"