-
Notifications
You must be signed in to change notification settings - Fork 2
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
dvalinrh
wants to merge
3
commits into
main
Choose a base branch
from
terraform_break_out
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
)There was a problem hiding this comment.
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.