github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/e2e/terraform/provision-nomad/main.tf (about) 1 locals { 2 provision_script = var.platform == "windows_amd64" ? "C:/opt/provision.ps1" : "/opt/provision.sh" 3 4 config_path = dirname("${path.root}/config/") 5 6 config_files = compact(setunion( 7 fileset(local.config_path, "**"), 8 )) 9 10 update_config_command = var.platform == "windows_amd64" ? "if (test-path /opt/config) { Remove-Item -Path /opt/config -Force -Recurse }; cp -r /tmp/config /opt/config" : "sudo rm -rf /opt/config; sudo mv /tmp/config /opt/config" 11 12 # abstract-away platform-specific parameter expectations 13 _arg = var.platform == "windows_amd64" ? "-" : "--" 14 } 15 16 resource "null_resource" "provision_nomad" { 17 18 depends_on = [ 19 null_resource.upload_configs, 20 null_resource.upload_nomad_binary 21 ] 22 23 # no need to re-run if nothing changes 24 triggers = { 25 script = data.template_file.provision_script.rendered 26 } 27 28 # Run the provisioner as a local-exec'd ssh command as a workaround for 29 # Windows remote-exec zero-byte scripts bug: 30 # https://github.com/hashicorp/terraform/issues/25634 31 # https://github.com/hashicorp/terraform/blob/master/CHANGELOG.md#0150-unreleased 32 # 33 # The retry behavior and explicit PasswordAuthenticaiton flag here are to 34 # workaround a race with the Windows userdata script that installs the 35 # authorized_key. Unfortunately this still results in a bunch of "permission 36 # denied" errors while waiting for those keys to be configured. 37 provisioner "local-exec" { 38 command = "until ssh -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no -o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ${var.connection.private_key} -p ${var.connection.port} ${var.connection.user}@${var.connection.host} ${data.template_file.provision_script.rendered}; do sleep 5; done" 39 } 40 41 } 42 43 data "template_file" "provision_script" { 44 template = "${local.provision_script}${data.template_file.arg_nomad_sha.rendered}${data.template_file.arg_nomad_version.rendered}${data.template_file.arg_nomad_binary.rendered}${data.template_file.arg_nomad_enterprise.rendered}${data.template_file.arg_nomad_acls.rendered}${data.template_file.arg_profile.rendered}${data.template_file.arg_role.rendered}${data.template_file.arg_index.rendered}${data.template_file.autojoin_tag.rendered}" 45 } 46 47 data "template_file" "arg_nomad_sha" { 48 template = var.nomad_sha != "" && var.nomad_local_binary == "" ? " ${local._arg}nomad_sha ${var.nomad_sha}" : "" 49 } 50 51 data "template_file" "arg_nomad_version" { 52 template = var.nomad_version != "" && var.nomad_sha == "" && var.nomad_local_binary == "" ? " ${local._arg}nomad_version ${var.nomad_version}" : "" 53 } 54 55 data "template_file" "arg_nomad_binary" { 56 template = var.nomad_local_binary != "" ? " ${local._arg}nomad_binary /tmp/nomad" : "" 57 } 58 59 data "template_file" "arg_nomad_enterprise" { 60 template = var.nomad_enterprise ? " ${local._arg}enterprise" : "" 61 } 62 63 data "template_file" "arg_nomad_acls" { 64 template = var.nomad_acls ? " ${local._arg}nomad_acls" : "" 65 } 66 67 data "template_file" "arg_profile" { 68 template = var.profile != "" ? " ${local._arg}config_profile ${var.profile}" : "" 69 } 70 71 data "template_file" "arg_role" { 72 template = var.role != "" ? " ${local._arg}role ${var.role}" : "" 73 } 74 75 data "template_file" "arg_index" { 76 template = var.index != "" ? " ${local._arg}index ${var.index}" : "" 77 } 78 79 data "template_file" "autojoin_tag" { 80 template = var.cluster_name != "" ? " ${local._arg}autojoin auto-join-${var.cluster_name}" : "" 81 } 82 83 resource "null_resource" "upload_nomad_binary" { 84 85 count = var.nomad_local_binary != "" ? 1 : 0 86 depends_on = [null_resource.upload_configs] 87 triggers = { 88 nomad_binary_sha = filemd5(var.nomad_local_binary) 89 } 90 91 connection { 92 type = "ssh" 93 user = var.connection.user 94 host = var.connection.host 95 port = var.connection.port 96 private_key = file(var.connection.private_key) 97 timeout = "15m" 98 } 99 100 provisioner "file" { 101 source = var.nomad_local_binary 102 destination = "/tmp/nomad" 103 } 104 } 105 106 resource "null_resource" "upload_configs" { 107 108 triggers = { 109 hashes = join(",", [for file in local.config_files : filemd5("${local.config_path}/${file}")]) 110 } 111 112 connection { 113 type = "ssh" 114 user = var.connection.user 115 host = var.connection.host 116 port = var.connection.port 117 private_key = file(var.connection.private_key) 118 timeout = "15m" 119 } 120 121 provisioner "file" { 122 source = local.config_path 123 destination = "/tmp/" 124 } 125 126 provisioner "local-exec" { 127 command = "until ssh -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no -o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ${var.connection.private_key} -p ${var.connection.port} ${var.connection.user}@${var.connection.host} '${local.update_config_command}'; do sleep 5; done" 128 } 129 130 }