Tinkerbell.Sandbox/deploy/terraform/main.tf

142 lines
3.7 KiB
Terraform
Raw Normal View History

2020-08-20 11:53:27 +00:00
# Configure the Packet Provider.
terraform {
required_providers {
packet = {
source = "packethost/packet"
version = "~> 3.0.1"
}
null = {
source = "hashicorp/null"
}
}
}
2020-08-20 11:53:27 +00:00
provider "packet" {
auth_token = var.packet_api_token
}
# Create a new VLAN in datacenter "ewr1"
resource "packet_vlan" "provisioning_vlan" {
description = "provisioning_vlan"
2020-08-20 11:53:27 +00:00
facility = var.facility
project_id = var.project_id
}
# Create a device and add it to tf_project_1
resource "packet_device" "tink_provisioner" {
2020-08-20 11:53:27 +00:00
hostname = "tink-provisioner"
plan = var.device_type
facilities = [var.facility]
operating_system = "ubuntu_18_04"
billing_cycle = "hourly"
project_id = var.project_id
user_data = file("install_package.sh")
}
resource "null_resource" "tink_directory" {
connection {
type = "ssh"
user = var.ssh_user
host = packet_device.tink_provisioner.network[0].address
}
provisioner "remote-exec" {
inline = [
"mkdir -p /root/tink/deploy"
]
}
provisioner "file" {
source = "../../setup.sh"
destination = "/root/tink/setup.sh"
}
provisioner "file" {
source = "../../generate-envrc.sh"
destination = "/root/tink/generate-envrc.sh"
}
provisioner "file" {
source = "../../deploy"
destination = "/root/tink"
}
provisioner "remote-exec" {
inline = [
"chmod +x /root/tink/*.sh /root/tink/deploy/tls/*.sh"
]
}
2020-08-20 11:53:27 +00:00
}
resource "packet_device_network_type" "tink_provisioner_network_type" {
device_id = packet_device.tink_provisioner.id
type = "hybrid"
2020-08-20 11:53:27 +00:00
}
# Create a device and add it to tf_project_1
resource "packet_device" "tink_worker" {
count = var.worker_count
hostname = "tink-worker-${count.index}"
2020-08-20 11:53:27 +00:00
plan = var.device_type
facilities = [var.facility]
operating_system = "custom_ipxe"
ipxe_script_url = "https://boot.netboot.xyz"
always_pxe = "true"
billing_cycle = "hourly"
project_id = var.project_id
}
resource "packet_device_network_type" "tink_worker_network_type" {
count = var.worker_count
device_id = packet_device.tink_worker[count.index].id
type = "layer2-individual"
2020-08-20 11:53:27 +00:00
}
# Attach VLAN to provisioner
resource "packet_port_vlan_attachment" "provisioner" {
depends_on = [packet_device_network_type.tink_provisioner_network_type]
device_id = packet_device.tink_provisioner.id
port_name = "eth1"
vlan_vnid = packet_vlan.provisioning_vlan.vxlan
2020-08-20 11:53:27 +00:00
}
# Attach VLAN to worker
resource "packet_port_vlan_attachment" "worker" {
count = var.worker_count
depends_on = [packet_device_network_type.tink_worker_network_type]
device_id = packet_device.tink_worker[count.index].id
2020-08-20 11:53:27 +00:00
port_name = "eth0"
vlan_vnid = packet_vlan.provisioning_vlan.vxlan
2020-08-20 11:53:27 +00:00
}
data "template_file" "worker_hardware_data" {
count = var.worker_count
template = file("${path.module}/hardware_data.tpl")
vars = {
id = packet_device.tink_worker[count.index].id
facility_code = packet_device.tink_worker[count.index].deployed_facility
plan_slug = packet_device.tink_worker[count.index].plan
address = "192.168.1.${count.index + 5}"
mac = packet_device.tink_worker[count.index].ports[1].mac
}
2020-08-20 11:53:27 +00:00
}
resource "null_resource" "hardware_data" {
count = var.worker_count
depends_on = [null_resource.tink_directory]
connection {
type = "ssh"
user = var.ssh_user
host = packet_device.tink_provisioner.network[0].address
}
2020-08-20 11:53:27 +00:00
provisioner "file" {
content = data.template_file.worker_hardware_data[count.index].rendered
destination = "/root/tink/deploy/hardware-data-${count.index}.json"
}
2020-08-20 11:53:27 +00:00
}