github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/tests/suites/spaces_ec2/util.sh (about) 1 # add_multi_nic_machine() 2 # 3 # Create a new machine, wait for it to boot and hotplug a pre-allocated 4 # network interface which has been tagged: "nic-type: hotpluggable". 5 add_multi_nic_machine() { 6 local hotplug_nic_id 7 hotplug_nic_id=$1 8 9 # Ensure machine is deployed to the same az as our nic 10 az=$(aws ec2 describe-network-interfaces --filters Name=network-interface-id,Values="$hotplug_nic_id" | jq -r ".NetworkInterfaces[0].AvailabilityZone") 11 juju add-machine --constraints zones="${az}" 12 juju_machine_id=$(juju show-machine --format json | jq -r '.["machines"] | keys[0]') 13 echo "[+] waiting for machine ${juju_machine_id} to start..." 14 15 wait_for_machine_agent_status "$juju_machine_id" "started" 16 17 # Hotplug the second network device to the machine 18 echo "[+] hotplugging second NIC with ID ${hotplug_nic_id} to machine ${juju_machine_id}..." 19 # shellcheck disable=SC2046,SC2086 20 aws ec2 attach-network-interface --device-index 1 \ 21 --network-interface-id ${hotplug_nic_id} \ 22 --instance-id $(juju show-machine --format json | jq -r ".[\"machines\"] | .[\"${juju_machine_id}\"] | .[\"instance-id\"]") 23 24 # Wait until the new NIC is UP 25 timeout=${3:-600} # default timeout: 600s = 10m 26 start_time="$(date -u +%s)" 27 while true; do 28 if juju ssh ${juju_machine_id} 'test $(ls /sys/class/net | grep "ens\|enp\|eth" | wc -l) -eq 2 && echo done' | grep "done"; then 29 echo "[+] second NIC attached." 30 break 31 fi 32 33 elapsed=$(date -u +%s)-$start_time 34 if [[ ${elapsed} -ge ${timeout} ]]; then 35 echo "[-] $(red 'timed out waiting for new NIC')" 36 exit 1 37 fi 38 39 sleep 1 40 done 41 } 42 43 # configure_multi_mic_netplan() 44 # 45 # Patch the netplan settings for the new interface, apply the new plan, 46 # restart the machine agent and wait for juju to detect the new interface 47 # before returning. 48 configure_multi_nic_netplan() { 49 local juju_machine_id hotplug_iface 50 juju_machine_id=$1 51 hotplug_iface=$2 52 53 # Add an entry to netplan and apply it so the second interface comes online 54 echo "[+] updating netplan and restarting machine agent" 55 # shellcheck disable=SC2086,SC2016 56 juju ssh ${juju_machine_id} 'sudo sh -c "sed -i \"/version:/d\" /etc/netplan/50-cloud-init.yaml"' 57 # shellcheck disable=SC2086,SC2016 58 default_route=$(juju ssh ${juju_machine_id} 'ip route | grep default | cut -d " " -f3') 59 # shellcheck disable=SC2086,SC2016 60 juju ssh ${juju_machine_id} "sudo sh -c 'echo \" routes:\n - to: default\n via: ${default_route}\n ${hotplug_iface}:\n dhcp4: true\n version: 2\n\" >> /etc/netplan/50-cloud-init.yaml'" 61 62 # shellcheck disable=SC2086,SC2016 63 echo "[+] Reconfiguring netplan:" 64 juju ssh ${juju_machine_id} 'sudo cat /etc/netplan/50-cloud-init.yaml' 65 # shellcheck disable=SC2086,SC2016 66 juju ssh ${juju_machine_id} 'sudo netplan apply' || true 67 echo "[+] Applied" 68 # shellcheck disable=SC2086,SC2016 69 juju ssh ${juju_machine_id} 'sudo systemctl restart jujud-machine-*' 70 71 # Wait for the interface to be detected by juju 72 echo "[+] waiting for juju to detect added NIC" 73 wait_for_machine_netif_count "$juju_machine_id" "3" 74 } 75 76 # assert_net_iface_for_endpoint_matches(app_name, endpoint_name, exp_if_name) 77 # 78 # Verify that the (non-fan) network adapter assigned to the specified endpoint 79 # matches the provided value. 80 assert_net_iface_for_endpoint_matches() { 81 local app_name endpoint_name exp_if_name 82 83 app_name=${1} 84 endpoint_name=${2} 85 exp_if_name=${3} 86 87 # shellcheck disable=SC2086,SC2016 88 got_if=$(juju exec -a ${app_name} "network-get ${endpoint_name}" | grep "interfacename: en" | awk '{print $2}' || echo "") 89 if [ "$got_if" != "$exp_if_name" ]; then 90 # shellcheck disable=SC2086,SC2016,SC2046 91 echo $(red "Expected network interface for ${app_name}:${endpoint_name} to be ${exp_if_name}; got ${got_if}") 92 exit 1 93 fi 94 } 95 96 # assert_endpoint_binding_matches(app_name, endpoint_name, exp_space_name) 97 # 98 # Verify that show-application shows that the specified endpoint is bound to 99 # the provided space name. 100 assert_endpoint_binding_matches() { 101 local app_name endpoint_name exp_space_name 102 103 app_name=${1} 104 endpoint_name=${2} 105 exp_space_name=${3} 106 107 # shellcheck disable=SC2086,SC2016 108 got=$(juju show-application ${app_name} --format json | jq -r ".[\"${app_name}\"] | .[\"endpoint-bindings\"] | .[\"${endpoint_name}\"]" || echo "") 109 if [ "$got" != "$exp_space_name" ]; then 110 # shellcheck disable=SC2086,SC2016,SC2046 111 echo $(red "Expected endpoint ${endpoint_name} in juju show-application ${app_name} to be ${exp_space_name}; got ${got}") 112 exit 1 113 fi 114 } 115 116 assert_machine_ip_is_in_cidrs() { 117 local machine_index cidrs 118 119 machine_index=${1} 120 cidrs=${2} 121 122 if ! which "grepcidr" >/dev/null 2>&1; then 123 sudo apt install grepcidr -y 124 fi 125 126 for cidr in $cidrs; do 127 machine_ip_in_cidr=$(juju machines --format json | jq -r ".machines[\"${machine_index}\"][\"ip-addresses\"][]" | grepcidr "${cidr}" || echo "") 128 if [ -n "${machine_ip_in_cidr}" ]; then 129 echo "${machine_ip_in_cidr}" 130 return 131 fi 132 done 133 134 # shellcheck disable=SC2086,SC2016,SC2046 135 echo $(red "machine ${machine_index} has no ips in subnet ${cidrs}") 1>&2 136 exit 1 137 } 138 139 # get_unit_index(app_name) 140 # 141 # Lookup and return the unit index for app_name. 142 get_unit_index() { 143 local app_name 144 145 app_name=${1} 146 147 index=$(juju status | grep "${app_name}/" | cut -d' ' -f1 | cut -d'/' -f2 | cut -d'*' -f1) 148 echo "$index" 149 }