#!/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 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_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}" " tap: to generate" 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}" # create volume # create qemu } function stop_vm { DEFINE_string 'vm_name' '-' 'VM NAME' 'm' DEFINE_string 'subnet_name' '-' 'Subnet NAME' 's' DEFINE_boolean 'dryrun' false 'Enable dry-run mode' 'd' FLAGS "$@" || exit $? eval set -- "${FLAGS_ARGV}" # delete qemu # check how many qemu in subnet # if qemu number = 0 delete_subnet "${FLAGS_subnet_name}" # check subnet number in vpc # if subnet number =0 # delete_vpc "${FLAGS_vpc_name}" } function main { fonction="${1}" shift 1 case "${fonction}" in "StartVm") start_vm "$@" ;; "StopVm") stop_vm "$@" ;; *) echo "action : ${fonction} not known !" echo "avalable action :" echo " -> StartVm" ;; esac } main "$@"