-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform.mx
130 lines (108 loc) · 3.59 KB
/
terraform.mx
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
function terraform-get {
# Download and install a preferred version of Terraform. Params: [0-9]+\.[0-9]+\.[0-9]+
trypipe {
$ARGS[1] -> set ver
getfile https://releases.hashicorp.com/terraform/$ver/terraform_${$ver}_${os}_${cpuarch}.zip ->> $DEVOPSVERSIONED/deleteme.zip
unzip $DEVOPSVERSIONED/deleteme.zip -d $DEVOPSVERSIONED
mv -i $DEVOPSVERSIONED/terraform $DEVOPSVERSIONED/terraform_$ver
rm $DEVOPSVERSIONED/deleteme.zip
#ls -l @{g $DEVOPSVERSIONED/terraform_*}
terraform-switch $ver
}
catch {
err "Something went wrong. You haven't been switched to $ARGS[1]"
}
}
function terraform-switch {
# Switches Terraform binary versions (used when switching between older and newer Terraform states/scripts)
test define version {
"StdoutRegex": "[0-9]+\.[0-9]+\\.[0-9]+",
"ExitNum": 0
}
test define tfverexists {
"ExitNum": 0
}
test define tfexists {
"ExitNum": 0
}
test define symlink {
"StderrRegex": "^$",
"ExitNum": 0
}
test define unlink {
"ExitNum": 0
}
trypipe {
$ARGS -> [ <test_version> 1 ] -> set ver
if {g: <test_tfverexists> $DEVOPSVERSIONED/terraform_$ver} then {
if {g: <test_tfexists> $DEVOPSBIN/terraform} then { unlink <!null> <test_unlink> $DEVOPSBIN/terraform }
ln <test_symlink> -sv $DEVOPSVERSIONED/terraform_$ver $DEVOPSBIN/terraform
global: TF_VERSION=$ver
murex-update-exe-list
} else {
err "That version doesn't exist. If version number is not a typo then please use `terraform-get` to download it."
}
}
}
autocomplete set terraform-switch { [{
"Dynamic": ({ g $DEVOPSVERSIONED/terraform_* -> regexp 'f/terraform_([0-9]+\.[0-9]+\.[0-9]+)$/' -> format str -> sort -Vr })
}] }
#function workspace {
# # Initializes the Terraform workspace
# try {
# out "{YELLOW}Checking AWS session token...{RESET}"
# and { $AWS_SESSION_TOKEN } { $AWS_SESSION_EXPIRATION } { = ((${date -d $AWS_SESSION_EXPIRATION +%s}-${date +%s})/60) > 10 }
# #err "Please rerun `awscon`"
# #return 1
#
# out "{YELLOW}Checking SSH agent...{RESET}"
# if { $SSH_AGENT_PID } else {
# agent
# }
#
# out "{YELLOW}Initializing Terraform...{RESET}"
# terraform: init
#
# out "{YELLOW}Selecting workspace '$USER'...{RESET}"
# if { terraform: workspace new $USER } else {
# terraform: workspace select $USER
# }
#
# }; catch {
# err "Failed to initialize you workspace"
# }
#}
function env-tf {
# Sets some lazy shortcut functions for working with Terraform
function init {
# Remove existing .terraform directory and run `terraform init`
rm -rf .terraform
terraform init
}
function plan {
# Runs a Terraform plan. -c / --clean to remove .terraform directory and plan from a clean init
try {
args args {
"AllowAdditional": false,
"Flags": {
"--clean": "bool",
"-c": "--clean"
}
}
if { $args[Flags]->[--clean] } then {
init
} else {
terraform get
}
terraform plan
}
}
autocomplete set plan { [{
"Flags": [ "--clean" ]
}] }
function apply {
# Runs a Terraform get to sync modules before applying the config
terraform get
terraform apply
}
}