-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path011_configure_vm.tf
More file actions
229 lines (193 loc) · 8.48 KB
/
Copy path011_configure_vm.tf
File metadata and controls
229 lines (193 loc) · 8.48 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* NOTE
July 28/25: This was originally a monolithic resource, with all Bash commands stored within for convenience.
While useful from an administrative view, had coarse runtime granularity -- you could not easily see
what command was executing and stack traces from failed executions.
Accepting repetitive boilerplate in return for finer granularity and more visibility.
*/
## ------------------------------------------------------------------------------------
## SSH Connectivity Check
## ------------------------------------------------------------------------------------
resource "null_resource" "ssh_connectivity_check" {
count = var.flag_vm_copy_files_to_instance == true ? 1 : 0
triggers = { always_run = "${timestamp()}" }
depends_on = [null_resource.allow_file_copy_to_start]
provisioner "local-exec" {
quiet = true
command = <<-EOT
set -e
echo "[$(date)] Starting SSH connectivity check for ${var.app_name}"
counter=0
until ssh -T ${var.app_name} || [ $counter -gt 60 ]; do
echo "[$(date)] Waiting for SSH connection to be available."
sleep 5
counter=$((counter+5))
done
echo "[$(date)] SSH connectivity established successfully"
EOT
interpreter = ["/bin/bash", "-c"]
}
}
## ------------------------------------------------------------------------------------
## File Transfer
## ------------------------------------------------------------------------------------
resource "null_resource" "file_transfer" {
count = var.flag_vm_copy_files_to_instance == true ? 1 : 0
triggers = { always_run = "${timestamp()}" }
depends_on = [null_resource.ssh_connectivity_check]
provisioner "local-exec" {
quiet = true
command = <<-EOT
set -e
echo "[$(date)] Starting file transfer to ${var.app_name}"
echo "[$(date)] Purging old target folder on remote VM"
ssh -T ${var.app_name} 'cd /home/ec2-user && rm -rf target || true'
echo "[$(date)] Transferring new target folder"
scp -r assets/target ${var.app_name}:/home/ec2-user/target
echo "[$(date)] File transfer completed successfully"
EOT
interpreter = ["/bin/bash", "-c"]
}
}
## ------------------------------------------------------------------------------------
## Host Configuration
## ------------------------------------------------------------------------------------
resource "null_resource" "host_configuration" {
count = var.flag_vm_copy_files_to_instance == true ? 1 : 0
triggers = { always_run = "${timestamp()}" }
depends_on = [null_resource.file_transfer]
provisioner "local-exec" {
quiet = true
command = <<-EOT
set -e
echo "[$(date)] Starting host configuration for ${var.app_name}"
ssh -T ${var.app_name} '/bin/bash /home/ec2-user/target/bash/remote/cleanse_and_configure_host.sh'
echo "[$(date)] Host configuration completed successfully"
EOT
interpreter = ["/bin/bash", "-c"]
}
}
## ------------------------------------------------------------------------------------
## Ansible Setup
## ------------------------------------------------------------------------------------
resource "null_resource" "ansible_setup" {
count = var.flag_vm_copy_files_to_instance == true ? 1 : 0
triggers = { always_run = "${timestamp()}" }
depends_on = [null_resource.host_configuration]
provisioner "local-exec" {
quiet = true
command = <<-EOT
set -e
echo "[$(date)] Waiting for Ansible to be ready on ${var.app_name}"
ssh -T ${var.app_name} 'cd ${local.playbook_dir} && chmod u+x 00_wait_for_ansible.sh && ./00_wait_for_ansible.sh'
echo "[$(date)] Ansible setup completed successfully"
EOT
interpreter = ["/bin/bash", "-c"]
}
}
## ------------------------------------------------------------------------------------
## System Packages Installation
## ------------------------------------------------------------------------------------
resource "null_resource" "system_packages" {
count = var.flag_vm_copy_files_to_instance == true ? 1 : 0
triggers = { always_run = "${timestamp()}" }
depends_on = [null_resource.ansible_setup]
provisioner "local-exec" {
quiet = true
command = <<-EOT
set -e
echo "[$(date)] Loading System Packages on ${var.app_name}"
ssh -T ${var.app_name} 'set -e && cd ${local.playbook_dir} && ansible-playbook -i inventory.ini 01_load_system_packages.yml'
echo "[$(date)] System packages installation completed successfully"
EOT
interpreter = ["/bin/bash", "-c"]
}
}
## ------------------------------------------------------------------------------------
## Update Configuration Files
## ------------------------------------------------------------------------------------
resource "null_resource" "update_configuration_files" {
count = var.flag_vm_copy_files_to_instance == true ? 1 : 0
triggers = { always_run = "${timestamp()}" }
depends_on = [null_resource.system_packages]
provisioner "local-exec" {
quiet = true
command = <<-EOT
set -e
echo "[$(date)] Updating Configuration Files on ${var.app_name}"
ssh -T ${var.app_name} 'cd ${local.playbook_dir} && ansible-playbook -i inventory.ini 02_update_file_configurations.yml'
echo "[$(date)] Configuration files updated successfully"
EOT
interpreter = ["/bin/bash", "-c"]
}
}
## ------------------------------------------------------------------------------------
## Pull Containers and Run Tower
## ------------------------------------------------------------------------------------
resource "null_resource" "pull_containers_run_tower" {
count = var.flag_vm_copy_files_to_instance == true ? 1 : 0
triggers = { always_run = "${timestamp()}" }
depends_on = [null_resource.update_configuration_files]
provisioner "local-exec" {
quiet = true
command = <<-EOT
set -e
echo "[$(date)] Pulling containers and running Tower on ${var.app_name}"
ssh -T ${var.app_name} 'cd ${local.playbook_dir} && ansible-playbook -i inventory.ini 03_pull_containers_and_run_tower.yml'
echo "[$(date)] Containers pulled and Tower started successfully"
EOT
interpreter = ["/bin/bash", "-c"]
}
}
## ------------------------------------------------------------------------------------
## Wait for Tower Containers
## ------------------------------------------------------------------------------------
resource "null_resource" "wait_for_tower" {
count = var.flag_vm_copy_files_to_instance == true ? 1 : 0
triggers = { always_run = "${timestamp()}" }
depends_on = [null_resource.pull_containers_run_tower]
provisioner "local-exec" {
quiet = true
command = <<-EOT
set -e
echo "[$(date)] Waiting for Tower containers to be ready on ${var.app_name}"
ssh -T ${var.app_name} 'cd ${local.playbook_dir} && ansible-playbook -i inventory.ini 04_wait_for_tower.yml'
echo "[$(date)] Tower containers are ready successfully"
EOT
interpreter = ["/bin/bash", "-c"]
}
}
## ------------------------------------------------------------------------------------
## Patch Groundswell
## ------------------------------------------------------------------------------------
resource "null_resource" "patch_groundswell" {
count = var.flag_vm_copy_files_to_instance == true ? 1 : 0
triggers = { always_run = "${timestamp()}" }
depends_on = [null_resource.wait_for_tower]
provisioner "local-exec" {
quiet = true
command = <<-EOT
set -e
echo "[$(date)] Patching Groundswell (if necessary) on ${var.app_name}"
ssh -T ${var.app_name} 'cd ${local.playbook_dir} && ansible-playbook -i inventory.ini 05_patch_groundswell.yml'
echo "[$(date)] Groundswell patching completed successfully"
EOT
interpreter = ["/bin/bash", "-c"]
}
}
## ------------------------------------------------------------------------------------
## Run Seqerakit (if allowed)
## ------------------------------------------------------------------------------------
resource "null_resource" "run_seqerkit" {
count = var.flag_vm_copy_files_to_instance == true && var.flag_run_seqerakit == true ? 1 : 0
triggers = { always_run = "${timestamp()}" }
depends_on = [null_resource.patch_groundswell]
provisioner "local-exec" {
quiet = true
command = <<-EOT
set -e
echo "Running Seqerakit"
ssh -T ${var.app_name} 'cd ${local.playbook_dir} && ansible-playbook -i inventory.ini 06_run_seqerakit.yml'
EOT
interpreter = ["/bin/bash", "-c"]
}
}