Created
March 5, 2023 22:19
-
-
Save ilobmirt/8ce058b00bd80d67d2da6b70fafee714 to your computer and use it in GitHub Desktop.
Brings down and up the target vagrant environment, then re-establishes the ssh connection host_ids
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#==================================================================================================# | |
#refresh_vagrant.sh | |
#----------------------- | |
#by: ilobmirt 2023 MAR 05 | |
# | |
#Brings down and up the target vagrant environment, then re-establishes the ssh connection host_ids | |
#==================================================================================================# | |
#Source from public github shared library, download only once in a temporary spot | |
lib_dir="/tmp/gists/ilobmirt" | |
lib_file="shared_functs.lib" | |
lib_source="https://gist.githubusercontent.com/ilobmirt/4852e33aaf0fbb064c2a3b6141ed5172/raw" | |
if [ ! -d "${lib_dir}" ]; then | |
mkdir -p $lib_dir | |
fi | |
if [ ! -f "${lib_dir}/${lib_file}" ]; then | |
wget -q --output-document="${lib_dir}/${lib_file}" $lib_source | |
fi | |
source "${lib_dir}/${lib_file}" | |
function down_project(){ | |
local func_input="$(l2s "$@")" | |
local target_user='' | |
local target_host='' | |
local target_project='' | |
local project_dir='' | |
local out_result=${return_code[SUCCESS]} | |
fill_params --input "${func_input}" --var target_user --search user --default 'root' > /dev/null | |
fill_params --input "${func_input}" --var target_host --search host --default '127.0.0.1' > /dev/null | |
fill_params --input "${func_input}" --var target_project --search project --default 'default_project' > /dev/null | |
fill_params --input "${func_input}" --var project_dir --search base_dir --default "/home/${target_user}/Vagrant" > /dev/null | |
local out_txt=$(cat <<EOF | |
╭─────────────────────────────────╮ | |
│Bringing down the Vagrant project│ | |
╰─────────────────────────────────╯ | |
EOF | |
) | |
printf "%s\n\n" "${out_txt}" | |
rem_ssh_cmd --user $target_user --host $target_host --cmd \"vagrant destroy -f\" --dir "${project_dir}/${target_project}" > /dev/null | |
return $out_result | |
} | |
function up_project(){ | |
local func_input="$(l2s "$@")" | |
local target_user='' | |
local target_host='' | |
local target_project='' | |
local project_dir='' | |
local out_result=${return_code[SUCCESS]} | |
fill_params --input "${func_input}" --var target_user --search user --default 'root' > /dev/null | |
fill_params --input "${func_input}" --var target_host --search host --default '127.0.0.1' > /dev/null | |
fill_params --input "${func_input}" --var target_project --search project --default 'default_project' > /dev/null | |
fill_params --input "${func_input}" --var project_dir --search base_dir --default "/home/${target_user}/Vagrant" > /dev/null | |
local out_txt=$(cat <<EOF | |
╭───────────────────────────────╮ | |
│Bringing up the Vagrant project│ | |
╰───────────────────────────────╯ | |
EOF | |
) | |
printf "%s\n\n" "${out_txt}" | |
rem_ssh_cmd --user $target_user --host $target_host --cmd \"vagrant up\" --dir "${project_dir}/${target_project}" > /dev/null | |
return $out_result | |
} | |
function down_ssh_id(){ | |
local func_input="$(l2s "$@")" | |
local target_ip='' | |
local max_tries_input='' | |
local max_tries=0 | |
local current_try=1 | |
local eval_status='not empty' | |
fill_params --input "${func_input}" --var target_ip --search target --default '127.0.0.1' > /dev/null | |
fill_params --input "${func_input}" --var max_tries_input --search max_tries --default '10' > /dev/null | |
max_tries=$((max_tries_input)) | |
local out_txt=$(cat <<EOF | |
◶┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄◵ | |
┊Removing ssh id of host┊ | |
◷┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄◴ | |
▻ ${target_ip} | |
EOF | |
) | |
printf "%s\n" "${out_txt}" | |
while : ; do | |
sleep 1 | |
printf "\tTRY #${current_try}...\n" | |
ssh-keygen -q -R $target_ip > /dev/null 2>&1 | |
eval_status=$(ssh-keygen -F $target_ip) | |
if [ "${eval_status}" = '' ] || [ $current_try -ge $max_tries ] ; then | |
break | |
fi | |
current_try=$(( current_try + 1 )) | |
done | |
if [ "${eval_status}" = '' ] ; then | |
printf "\tREMOVED!!!\n\n" | |
else | |
printf "\tFAILED TO REMOVE AFTER ${current_try} TRIES!!!\n\n" | |
fi | |
} | |
function up_ssh_id(){ | |
local func_input="$(l2s "$@")" | |
local target_ip='' | |
local max_tries_input='' | |
local max_tries=0 | |
local current_try=1 | |
local eval_status='not empty' | |
fill_params --input "${func_input}" --var target_ip --search target --default '127.0.0.1' > /dev/null | |
fill_params --input "${func_input}" --var max_tries_input --search max_tries --default '10' > /dev/null | |
max_tries=$((max_tries_input)) | |
local out_txt=$(cat <<EOF | |
◶┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄◵ | |
┊Adding ssh id of host┊ | |
◷┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄◴ | |
▻ ${target_ip} | |
EOF | |
) | |
printf "%s\n" "${out_txt}" | |
while : ; do | |
sleep 1 | |
printf "\tTRY #${current_try}...\n" | |
ssh-keyscan -H $target_ip >> ~/.ssh/known_hosts 2> /dev/null | |
eval_status=$(ssh-keygen -F $target_ip) | |
if [ "${eval_status}" != '' ] || [ $current_try -ge $max_tries ] ; then | |
break | |
fi | |
current_try=$(( current_try + 1 )) | |
done | |
if [ "${eval_status}" != '' ] ; then | |
printf "\tADDED ID TO KNOWN HOSTS!!!\n\n" | |
else | |
printf "\tFAILED TO ADD AFTER ${current_try} TRIES!!!\n\n" | |
fi | |
} | |
function main(){ | |
local func_input="$(l2s "$@")" | |
local target_user='' | |
local target_host='' | |
local target_project='' | |
local project_dir='' | |
local vagrant_net='' | |
local vagrant_start_input='' | |
local vagrant_start=0 | |
local vagrant_end_input='' | |
local vagrant_end=0 | |
local out_result=${return_code[SUCCESS]} | |
fill_params --input "${func_input}" --var target_user --search user --default 'root' > /dev/null | |
fill_params --input "${func_input}" --var target_host --search host --default '127.0.0.1' > /dev/null | |
fill_params --input "${func_input}" --var target_project --search project --default 'default_project' > /dev/null | |
fill_params --input "${func_input}" --var project_dir --search base_dir --default "/home/${target_user}/Vagrant" > /dev/null | |
fill_params --input "${func_input}" --var vagrant_net --search net --default '192.168.0' > /dev/null | |
fill_params --input "${func_input}" --var vagrant_start_input --search start --default '3' > /dev/null | |
vagrant_start=$((vagrant_start_input)) | |
fill_params --input "${func_input}" --var vagrant_end_input --search end --default '7' > /dev/null | |
vagrant_end=$((vagrant_end_input)) | |
local out_txt=$(cat <<EOF | |
╔═════════════════════════════╗ | |
║We have passed the following:║ | |
╚═════════════════════════════╝ | |
▻ VAGRANT TARGET: ${target_user}@${target_host} | |
▔▔▔▔▔▔▔ ▔▔▔▔▔▔▔ | |
▻ PROJECT: ${project_dir}/${target_project} | |
▔▔▔▔▔▔▔▔ | |
▻ NODE START: ${vagrant_net}.${vagrant_start} | |
▔▔▔▔ ▔▔▔▔▔▔ | |
▻ NODE END: ${vagrant_net}.${vagrant_end} | |
▔▔▔▔ ▔▔▔▔ | |
EOF | |
) | |
printf "%s\n\n\n" "${out_txt}" | |
down_project --user $target_user --host $target_host --project $target_project --base_dir $project_dir | |
sleep 10 | |
for curr_host in $(seq $vagrant_start $vagrant_end ) ; do | |
down_ssh_id --target "${vagrant_net}.${curr_host}" --max_tries 30 | |
done | |
up_project --user $target_user --host $target_host --project $target_project --base_dir $project_dir | |
sleep 10 | |
for curr_host in $(seq $vagrant_start $vagrant_end ) ; do | |
up_ssh_id --target "${vagrant_net}.${curr_host}" --max_tries 30 | |
done | |
out_txt=$(cat <<EOF | |
╔══════════════╗ | |
║End of Script:║ | |
╚══════════════╝ | |
The Vagrant project should now have been remade in a fresh state. | |
The ids associated with those old virtual machines are disposed of | |
and new host ids are stored in known_hosts. | |
EOF | |
) | |
printf "%s\n\n\n" "${out_txt}" | |
return $out_result | |
} | |
main $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment