Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the terraform delete and terraform list to util #111

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 3 additions & 78 deletions bin/burden
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ gl_preflight=0
# gl_top_dir: Directory we are executing from
#
gl_tf_terminate=0
gl_tf_term_list=""
gl_tf_term_list="all"
gl_tf_list=0
gl_sys_file=$value_not_set
gl_cloud_execute_tests=1
Expand Down Expand Up @@ -3102,81 +3102,6 @@ integrate_templates()
rm $tmpfile 2> /dev/null
}

#
# List the active systems created by tf
#
tf_list()
{
dirs=`find . -type d | grep terraform.tfstate.d | rev | cut -d'/' -f3- | rev | grep tf`
for tf_show in $dirs; do
pushd $tf_show > /dev/null
terraform show > tf_list_info
grep subnet tf_list_info >& /dev/null
if [ $? -eq 0 ]; then
work_dir=`pwd | rev | cut -d'/' -f 2 | rev`
grep azure tf_list_info >& /dev/null
if [ $? -eq 0 ]; then
vm_size=`grep size tf_list_info | grep -v disk | cut -d'"' -f2`
public_ip=`grep public_ip_address tf_list_info | head -1 | cut -d'"' -f2 | sort -u`
name_tag=`grep environment tf_list_info | cut -d'"' -f4 | sort -u`
printf "work_dir: %s\n" $work_dir
printf "\tfull_path: %s\n" $tf_show
printf "\tvm_size: %s\n" $vm_size
printf "\tpublic_ip: %s\n" $public_ip
printf "\tname_tag: %s\n" $name_tag
fi
grep aws tf_list_info >& /dev/null
if [ $? -eq 0 ]; then
vm_size=`grep instance_type tf_list_info | cut -d'"' -f2`
inst_state=`grep instance_state tf_list_info | cut -d'"' -f2`
public_dns=`grep public_dns tf_list_info | cut -d'"' -f2 | sort -u`
name_tag=`grep Name tf_list_info | cut -d'"' -f4 | sort -u`
printf "work_dir: %s\n" $work_dir
printf "\tfull_path: %s\n" $tf_show
printf "\tvm_size: %s\n" $vm_size
printf "\tinstance_state: %s\n" $inst_state
printf "\tpublic_dns: %s\n" $public_dns
printf "\tname_tag: %s\n" $name_tag
fi
fi
rm tf_list_info
popd > /dev/null
done
exit
}
#
# Walk all the tf directories and attempt to remove the instance.
#
tf_terminate()
{

if [[ $gl_tf_term_list == "" ]]; then
dirs=`find . -type d | grep terraform.tfstate.d | rev | cut -d'/' -f3- | rev | grep tf`
else
dirs=`echo $gl_tf_term_list | sed "s/,/ /g"`
echo $gl_tf_term_list
echo $dirs
fi
for tf_del in $dirs; do
pushd $tf_del > /dev/null
terraform plan -var-file=env.tfvars -destroy -out=destroy.tfplan
if [ $? -eq 0 ]; then
terraform apply "destroy.tfplan"
#
# Now get arid of the tf directory.
#
if [ $gl_tf_terminate -eq 0 ]; then
cd ..
rm -rf tf
fi
else
echo Warning: Unable to create the plan for $tf_del
fi
popd > /dev/null
done
exit
}

#
# If the test_def_dir has https or git in it, we will pull from
# the git repo. If not simply check that the file exists.
Expand Down Expand Up @@ -4104,14 +4029,14 @@ fi
# Does not return
#
if [ $gl_tf_terminate -eq 1 ]; then
tf_terminate
source $UTILS_DIR/terraform_ops --terminate_list $gl_tf_term_list
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be source'd in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we want burden to terminate when the script is done. We could duplicate the logic, but then would have to deal with rtcs coming back.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make more sense to have this logic as a function, then source the script as if it's a library and call the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of the idea, is these are also standalone. So you are able to call terraform_ops outside of burden.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it was truly standalone, it would be essentially a script you call, check the exit code of, and handle appropriately. As it is now, it relies on stuff within Zathras (cleanup_and_exit)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's nudge this one.

I think it would be clearer for this to be it's own standalone script that can be used elsewhere.

fi

#
# Does not return
#
if [ $gl_tf_list -eq 1 ]; then
tf_list
source $UTILS_DIR/terraform_ops --tf_list
dvalinrh marked this conversation as resolved.
Show resolved Hide resolved
fi

if [[ $gl_run_prefix == "" ]]; then
Expand Down
165 changes: 165 additions & 0 deletions bin/utils/terraform_ops
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#!/bin/bash
# License
#
# Copyright (C) 2024 David Valin [email protected]
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#

#
# Terminate the designated terraform. If "all" is passed in we will delete all
# terraform instances.
#

tf_terminate()
{
tf_term_list=$1

if [[ $tf_term_list == "all" ]]; then
dirs=`find . -type d | grep terraform.tfstate.d | rev | cut -d'/' -f3- | rev | grep tf`
else
dirs=`echo $tf_term_list | sed "s/,/ /g"`
echo $tf_term_list
echo $dirs
fi
for tf_del in $dirs; do
pushd $tf_del > /dev/null
terraform plan -var-file=env.tfvars -destroy -out=destroy.tfplan
if [ $? -eq 0 ]; then
terraform apply "destroy.tfplan"
else
echo "Warning: Unable to create the plan for $tf_del"
dvalinrh marked this conversation as resolved.
Show resolved Hide resolved
fi
#
# Get arid of the tf directory
#
cd ..
rm -rf tf
popd > /dev/null
done
exit
}

#
# List the active systems created by tf
#
tf_list_func()
{
dirs=`find . -type d | grep terraform.tfstate.d | rev | cut -d'/' -f3- | rev | grep tf`
for tf_show in $dirs; do
pushd $tf_show > /dev/null
terraform show > tf_list_info
grep subnet tf_list_info >& /dev/null
if [ $? -eq 0 ]; then
work_dir=`pwd | rev | cut -d'/' -f 2 | rev`
grep azure tf_list_info >& /dev/null
if [ $? -eq 0 ]; then
vm_size=`grep size tf_list_info | grep -v disk | cut -d'"' -f2`
public_ip=`grep public_ip_address tf_list_info | head -1 | cut -d'"' -f2 | sort -u`
name_tag=`grep environment tf_list_info | cut -d'"' -f4 | sort -u`
printf "work_dir: %s\n" $work_dir
printf "\tfull_path: %s\n" $tf_show
printf "\tvm_size: %s\n" $vm_size
printf "\tpublic_ip: %s\n" $public_ip
printf "\tname_tag: %s\n" $name_tag
fi
grep aws tf_list_info >& /dev/null
if [ $? -eq 0 ]; then
vm_size=`grep instance_type tf_list_info | cut -d'"' -f2`
inst_state=`grep instance_state tf_list_info | cut -d'"' -f2`
public_dns=`grep public_dns tf_list_info | cut -d'"' -f2 | sort -u`
name_tag=`grep Name tf_list_info | cut -d'"' -f4 | sort -u`
printf "work_dir: %s\n" $work_dir
printf "\tfull_path: %s\n" $tf_show
printf "\tvm_size: %s\n" $vm_size
printf "\tinstance_state: %s\n" $inst_state
printf "\tpublic_dns: %s\n" $public_dns
printf "\tname_tag: %s\n" $name_tag
fi
fi
rm tf_list_info
popd > /dev/null
done
}

usage()
{
echo "Usage: $0"
echo "--tf_list: List all terraform instances seen from current directory"
echo "--terminate_list <tf1,tf2...>: Terminates the given terraform instances, ie"
echo " ./tf_break/rhel/aws/r8g.large_0/tf. If \"all\" is specified, then every terraform"
echo " instance from the current directory is terminated."
echo "--usage: this usage message"
echo "-h: this usage message"
exit 0
}

ARGUMENT_LIST=(
"terminate_list"
)

NO_ARGUMENTS=(
"tf_list"
"usage"
)

# read arguments
opts=$(getopt \
--longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")" \
--longoptions "$(printf "%s," "${NO_ARGUMENTS[@]}")" \
--name "$(basename "$0")" \
--options "h" \
-- "$@"
)

tf_list=0
if [[ $? -ne 0 ]]; then
usage $0
fi

eval set --$opts

while [[ $# -gt 0 ]]; do
case "$1" in
--tf_list)
tf_list=1
shift 1
;;
--terminate_list)
tf_term_list=$2
shift 2
;;
--usage)
usage $0
;;
--h)
usage $0
;;
--)
break;
;;
*)
echo "option not found ${1}"
usage $0
;;
esac
done
if [ $tf_list -eq 1 ]; then
tf_list_func
fi
if [[ $tf_term_list != "" ]]; then
tf_terminate $tf_term_list
fi
exit 0