-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunner
executable file
·139 lines (112 loc) · 3.27 KB
/
runner
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
usage () {
if [ -f README.md ] ; then
exec >&2
echo
echo "USAGE:"
sed '/## Usage/,/##/!d; /##/d' README.md
else
echo "Please see README.md for usage" >&2
fi
exit 1
}
do_export () {
# Export from BigIP using iControl REST api from python
# This builds a skeleton terraform file called all.tf
# which can be used to do import commands from
if [ ! -f login.json ] ; then
cat <<EOF >&2
There are no login details. Please provide login.json:
---
{
"bigip": "172.16.17.18",
"user": "someuser",
"password": "the-password"
}
---
The user may be a read only user (audit user).
EOF
exit 1
fi
echo "** Dumping all VIPs, used pools and used nodes"
./dump.py "$@" >import.tf
}
do_import () {
# After the skeleton all.tf has been generated we can run the
# terraform commands to populate the state file and then use
# "terraform show" to generate a new all.tf with actual resource
# definitions
cp -v import.tf terraformer
newname=import-$(date '+%F-%R').tf
mv import.tf $newname
cd terraformer
echo "** Initializing for terraform import"
if [ ! -f secrets.tf ] ; then
echo "* No secrets.tf provided. Seeding from login.json"
cat <<EOF >secrets.tf
variable "hostname" { default = $(jq .bigip ../login.json) }
variable "username" { default = $(jq .user ../login.json) }
variable "password" { default = $(jq .password ../login.json) }
EOF
else
echo "* There is already a secrets.tf here. Using."
fi
if [ "$clear" = yes ] ; then
# Only apropriate to remove state if starting from scratch
echo "* Clearing terraform state and resources"
rm -rf .terraform* terraform.tfstate* resources.tf*
fi
echo "* Initializing terraform"
terraform init
echo "** Doing a terraform import"
# This issues one terraform command pr. resource
<import.tf grep '^#import#' | cut -d' ' -f2- | bash -e
<import.tf grep "^#sed#" | cut -d' ' -f2- > new.sed
terraform show -no-color | grep -vw id >../new.tf
sed -i.bak -f new.sed ../new.tf
rm new.sed
if [ ! -f resources.tf ] ; then
: > resources.tf
fi
(echo "# Imported $only_vip $(date)"; echo; cat ../new.tf) >> resources.tf
rm import.tf
cd ..
echo
echo "** Done"
echo "* Newly imported resources are found in terraformer/resources.tf"
echo "* The skeleton terraform file is in ./$newname"
echo " In this file you can find lists of unreferenced resources if"
echo " you would like to remove them from your BigIP."
}
# Option defaults
clear=yes
really_clear=''
undefs_only=''
only_vip="complete config"
while getopts "v:uch" opt; do
case $opt in
v) clear=''
undefs_only=''
only_vip="with filter $OPTARG"
;;
u) undefs_only='yes';;
c) really_clear=yes;;
h) usage;;
*) usage;;
esac
done
if [ "$really_clear" = "yes" ] ; then
clear=yes
fi
# Exit on all errors from now on
set -e
do_export "$@"
if [ -z $undefs_only ] ; then
do_import
else
newname="unref-$(date '+%F-%R').tf"
mv import.tf $newname
echo
echo "** Done"
echo "* List of unreferenced resources is in $newname"
fi