-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlinux.tf
315 lines (277 loc) · 12.4 KB
/
linux.tf
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
data cloudinit_config user_data {
gzip = false
base64_encode = false
part {
content = templatefile("${path.root}/../cloudinit/cloud-config-post-generation.yaml",
{
user_name = var.user_name
})
content_type = "text/cloud-config"
merge_type = "list(append)+dict(recurse_array)+str()"
}
dynamic "part" {
for_each = range(var.install_tools ? 1 : 0)
content {
content = templatefile("${path.root}/../cloudinit/cloud-config-tools.yaml",
{
outbound_ip = var.outbound_ip_address
subnet_id = var.subnet_id
virtual_network_id = local.virtual_network_id
})
content_type = "text/cloud-config"
merge_type = "list(append)+dict(recurse_array)+str()"
}
}
dynamic "part" {
for_each = range(var.deploy_files_share ? 1 : 0)
content {
content = templatefile("${path.root}/../cloudinit/cloud-config-files-share.yaml",
{
diagnostics_directory = "/var/opt/pipelines-agent/diag"
smb_mount_point = var.diagnostics_smb_share_mount_point
smb_share = var.diagnostics_smb_share
storage_account_key = data.azurerm_storage_account.files.0.primary_access_key
storage_account_name = data.azurerm_storage_account.files.0.name
user = var.user_name
})
content_type = "text/cloud-config"
merge_type = "list(append)+dict(recurse_array)+str()"
}
}
part {
content = templatefile("${path.root}/../cloudinit/cloud-config-userdata.yaml",
{
user_name = var.user_name
environment = var.environment_variables
})
content_type = "text/cloud-config"
merge_type = "list(append)+dict(recurse_array)+str()"
}
# Azure Log Analytics VM extension fails on https://github.com/actions/runner-images
dynamic "part" {
for_each = range(var.deploy_non_essential_vm_extensions ? 1 : 0)
content {
content = templatefile("${path.root}/../cloudinit/cloud-config-log-analytics.yaml",
{
workspace_id = data.azurerm_log_analytics_workspace.monitor.workspace_id
workspace_key = data.azurerm_log_analytics_workspace.monitor.primary_shared_key
})
content_type = "text/cloud-config"
merge_type = "list(append)+dict(recurse_array)+str()"
}
}
dynamic "part" {
for_each = range(var.deploy_agent ? 1 : 0)
content {
content = templatefile("${path.root}/../cloudinit/cloud-config-agent.yaml",
{
agent_name = var.azdo_pipeline_agent_name
agent_pool = var.azdo_pipeline_agent_pool != null ? var.azdo_pipeline_agent_pool : ""
agent_version_id = var.azdo_pipeline_agent_version_id
deployment_group = var.azdo_deployment_group_name != null ? var.azdo_deployment_group_name : ""
environment = var.azdo_environment_name != null ? var.azdo_environment_name : ""
install_agent_script_b64= filebase64("${path.root}/../scripts/host/install_agent.sh")
project = var.azdo_project
org = var.azdo_org
pat = var.azdo_pat
user = var.user_name
})
content_type = "text/cloud-config"
merge_type = "list(append)+dict(recurse_array)+str()"
}
}
count = var.deploy_agent || var.prepare_host ? 1 : 0
}
resource azurerm_public_ip linux_pip {
name = "${var.name}-pip"
location = var.location
resource_group_name = var.resource_group_name
allocation_method = "Static"
sku = "Standard"
tags = var.tags
}
resource azurerm_network_interface linux_nic {
name = "${var.name}-nic"
location = var.location
resource_group_name = var.resource_group_name
dynamic "ip_configuration" {
for_each = range(var.create_public_ip_address ? 1 : 0)
content {
name = "ipconfig"
subnet_id = var.subnet_id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.linux_pip.id
}
}
dynamic "ip_configuration" {
for_each = range(var.create_public_ip_address ? 0 : 1)
content {
name = "ipconfig"
subnet_id = var.subnet_id
private_ip_address_allocation = "Dynamic"
}
}
accelerated_networking_enabled = var.vm_accelerated_networking
tags = var.tags
}
resource azurerm_network_security_rule admin_ssh {
name = "AdminSSH"
priority = 201
direction = "Inbound"
access = var.enable_public_access ? "Allow" : "Deny"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefixes = var.admin_cidr_ranges
destination_address_prefixes = [
azurerm_public_ip.linux_pip.ip_address,
azurerm_network_interface.linux_nic.ip_configuration.0.private_ip_address
]
resource_group_name = azurerm_network_security_group.nsg.resource_group_name
network_security_group_name = azurerm_network_security_group.nsg.name
}
resource azurerm_network_interface_security_group_association linux_nic_nsg {
network_interface_id = azurerm_network_interface.linux_nic.id
network_security_group_id = azurerm_network_security_group.nsg.id
}
resource azurerm_linux_virtual_machine linux_agent {
name = var.name
computer_name = var.computer_name
location = var.location
resource_group_name = var.resource_group_name
size = var.vm_size
admin_username = var.user_name
admin_password = var.user_password
allow_extension_operations = var.deploy_non_essential_vm_extensions || var.deploy_agent || var.prepare_host
custom_data = var.deploy_agent || var.prepare_host ? base64encode(data.cloudinit_config.user_data.0.rendered) : null
disable_password_authentication = false
network_interface_ids = [azurerm_network_interface.linux_nic.id]
provision_vm_agent = var.deploy_non_essential_vm_extensions || var.deploy_agent || var.prepare_host
admin_ssh_key {
username = var.user_name
public_key = file(var.ssh_public_key)
}
boot_diagnostics {
storage_account_uri = null # Managed Storage Account
}
identity {
type = "SystemAssigned, UserAssigned"
identity_ids = [var.user_assigned_identity_id]
}
os_disk {
name = "${var.name}-osdisk"
caching = "ReadWrite"
storage_account_type = var.storage_type
}
source_image_id = var.os_image_id
dynamic "source_image_reference" {
for_each = range(var.os_image_id == null || var.os_image_id == "" ? 1 : 0)
content {
publisher = var.os_publisher
offer = var.os_offer
sku = var.os_sku
version = var.os_version
}
}
lifecycle {
ignore_changes = [
custom_data,
source_image_id,
source_image_reference.0.version,
]
}
tags = var.tags
depends_on = [azurerm_network_interface_security_group_association.linux_nic_nsg]
# Terraform azurerm does not allow disk access configuration of OS disk
# BUG: https://github.com/Azure/azure-cli/issues/19455
# So use disk_access_name instead of disk_access_id
provisioner local-exec {
command = "az disk update --name ${var.name}-osdisk --resource-group ${self.resource_group_name} --disk-access ${var.disk_access_name} --network-access-policy AllowPrivate --query 'networkAccessPolicy'"
}
}
resource azurerm_virtual_machine_extension cloud_config_status {
name = "CloudConfigStatusScript"
virtual_machine_id = azurerm_linux_virtual_machine.linux_agent.id
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.1"
auto_upgrade_minor_version = true
settings = jsonencode({
"commandToExecute" = "/usr/bin/cloud-init status --long --wait ; systemctl status cloud-final.service --full --no-pager --wait"
})
tags = var.tags
timeouts {
create = "60m"
}
}
resource azurerm_virtual_machine_extension linux_monitor {
name = "AzureMonitorLinuxAgent"
virtual_machine_id = azurerm_linux_virtual_machine.linux_agent.id
publisher = "Microsoft.Azure.Monitor"
type = "AzureMonitorLinuxAgent"
type_handler_version = "1.33"
auto_upgrade_minor_version = true
count = var.deploy_non_essential_vm_extensions ? 1 : 0
tags = var.tags
depends_on = [azurerm_virtual_machine_extension.cloud_config_status]
}
resource azurerm_virtual_machine_extension linux_dependency_monitor {
name = "DAExtension"
virtual_machine_id = azurerm_linux_virtual_machine.linux_agent.id
publisher = "Microsoft.Azure.Monitoring.DependencyAgent"
type = "DependencyAgentLinux"
type_handler_version = "9.5"
auto_upgrade_minor_version = true
settings = jsonencode({
"workspaceId" = data.azurerm_log_analytics_workspace.monitor.workspace_id
})
protected_settings = jsonencode({
"workspaceKey" = data.azurerm_log_analytics_workspace.monitor.primary_shared_key
})
tags = var.tags
count = var.deploy_non_essential_vm_extensions ? 1 : 0
depends_on = [
azurerm_virtual_machine_extension.cloud_config_status,
azurerm_virtual_machine_extension.linux_monitor
]
}
resource azurerm_virtual_machine_extension linux_watcher {
name = "AzureNetworkWatcherExtension"
virtual_machine_id = azurerm_linux_virtual_machine.linux_agent.id
publisher = "Microsoft.Azure.NetworkWatcher"
type = "NetworkWatcherAgentLinux"
type_handler_version = "1.4"
auto_upgrade_minor_version = true
tags = var.tags
count = var.deploy_non_essential_vm_extensions ? 1 : 0
depends_on = [
azurerm_virtual_machine_extension.cloud_config_status,
azurerm_virtual_machine_extension.linux_monitor
]
}
resource azurerm_virtual_machine_extension policy {
name = "AzurePolicyforLinux"
virtual_machine_id = azurerm_linux_virtual_machine.linux_agent.id
publisher = "Microsoft.GuestConfiguration"
type = "ConfigurationforLinux"
type_handler_version = "1.0"
auto_upgrade_minor_version = true
tags = var.tags
count = var.deploy_non_essential_vm_extensions ? 1 : 0
depends_on = [
azurerm_virtual_machine_extension.cloud_config_status,
azurerm_virtual_machine_extension.linux_monitor
]
}
resource azurerm_dev_test_global_vm_shutdown_schedule auto_shutdown {
virtual_machine_id = azurerm_linux_virtual_machine.linux_agent.id
location = azurerm_linux_virtual_machine.linux_agent.location
enabled = true
daily_recurrence_time = replace(var.shutdown_time,":","")
timezone = var.timezone
notification_settings {
enabled = false
}
tags = var.tags
count = var.shutdown_time != null && var.shutdown_time != "" ? 1 : 0
}