From bd63279e5921f64cf72d0edf0c701f2f2d16a5f2 Mon Sep 17 00:00:00 2001 From: Gianluca Arbezzano Date: Thu, 10 Sep 2020 16:37:32 +0200 Subject: [PATCH] this is the right version of the terraform code I copied a wrong version of the terraform code. Sorry Signed-off-by: Gianluca Arbezzano --- deploy/terraform/README.md | 1 - .../{terraform => }/hardware_data.tpl | 0 deploy/terraform/input.tf | 17 --- deploy/terraform/install_package.sh | 4 +- deploy/terraform/main.tf | 118 ++++++++++++--- deploy/terraform/{terraform => }/outputs.tf | 0 deploy/terraform/terraform/install_package.sh | 66 -------- deploy/terraform/terraform/main.tf | 141 ------------------ deploy/terraform/{terraform => }/variables.tf | 0 deploy/terraform/{terraform => }/versions.tf | 0 10 files changed, 99 insertions(+), 248 deletions(-) delete mode 100644 deploy/terraform/README.md rename deploy/terraform/{terraform => }/hardware_data.tpl (100%) delete mode 100644 deploy/terraform/input.tf rename deploy/terraform/{terraform => }/outputs.tf (100%) delete mode 100644 deploy/terraform/terraform/install_package.sh delete mode 100644 deploy/terraform/terraform/main.tf rename deploy/terraform/{terraform => }/variables.tf (100%) rename deploy/terraform/{terraform => }/versions.tf (100%) diff --git a/deploy/terraform/README.md b/deploy/terraform/README.md deleted file mode 100644 index 5801874..0000000 --- a/deploy/terraform/README.md +++ /dev/null @@ -1 +0,0 @@ -https://tinkerbell.org/docs/setup/packet-with-terraform/ diff --git a/deploy/terraform/terraform/hardware_data.tpl b/deploy/terraform/hardware_data.tpl similarity index 100% rename from deploy/terraform/terraform/hardware_data.tpl rename to deploy/terraform/hardware_data.tpl diff --git a/deploy/terraform/input.tf b/deploy/terraform/input.tf deleted file mode 100644 index 958888c..0000000 --- a/deploy/terraform/input.tf +++ /dev/null @@ -1,17 +0,0 @@ -variable "packet_api_token" { - description = "Packet user api token" -} - -variable "project_id" { - description = "Project ID" -} - -variable "facility" { - description = "Packet facility to provision in" - default = "sjc1" -} - -variable "device_type" { - description = "Type of device to provision" - default = "c3.small.x86" -} diff --git a/deploy/terraform/install_package.sh b/deploy/terraform/install_package.sh index 9992160..95e86fd 100644 --- a/deploy/terraform/install_package.sh +++ b/deploy/terraform/install_package.sh @@ -13,9 +13,10 @@ declare -a YUM_LIST=("https://download.docker.com/linux/centos/7/x86_64/stable/P "docker-ce" "docker-ce-cli" "epel-release" + "pass" "python3") declare -a APT_LIST=("docker" - "docker-compose") + "docker-compose" "pass") add_yum_repo() ( $YUM_CONFIG_MGR --add-repo https://download.docker.com/linux/centos/docker-ce.repo @@ -34,7 +35,6 @@ update_apt() ( restart_docker_service() ( service docker restart ) - install_yum_packages() ( $YUM_INSTALL "${YUM_LIST[@]}" -y ) diff --git a/deploy/terraform/main.tf b/deploy/terraform/main.tf index 3fbe9d3..9cd4f12 100644 --- a/deploy/terraform/main.tf +++ b/deploy/terraform/main.tf @@ -1,33 +1,83 @@ # Configure the Packet Provider. +terraform { + required_providers { + packet = { + source = "packethost/packet" + version = "~> 3.0.1" + } + null = { + source = "hashicorp/null" + } + } +} + provider "packet" { auth_token = var.packet_api_token - version = "~> 2.9" } # Create a new VLAN in datacenter "ewr1" -resource "packet_vlan" "provisioning-vlan" { - description = "provisioning-vlan" +resource "packet_vlan" "provisioning_vlan" { + description = "provisioning_vlan" facility = var.facility project_id = var.project_id } # Create a device and add it to tf_project_1 -resource "packet_device" "tink-provisioner" { +resource "packet_device" "tink_provisioner" { hostname = "tink-provisioner" plan = var.device_type facilities = [var.facility] operating_system = "ubuntu_18_04" billing_cycle = "hourly" project_id = var.project_id - network_type = "hybrid" - user_data = "${file("install_package.sh")}" + 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" + ] + } +} + +resource "packet_device_network_type" "tink_provisioner_network_type" { + device_id = packet_device.tink_provisioner.id + type = "hybrid" } # Create a device and add it to tf_project_1 -resource "packet_device" "tink-worker" { - hostname = "tink-worker" +resource "packet_device" "tink_worker" { + count = var.worker_count + + hostname = "tink-worker-${count.index}" plan = var.device_type facilities = [var.facility] operating_system = "custom_ipxe" @@ -35,31 +85,57 @@ resource "packet_device" "tink-worker" { always_pxe = "true" billing_cycle = "hourly" project_id = var.project_id - network_type = "layer2-individual" +} + +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" } # Attach VLAN to provisioner resource "packet_port_vlan_attachment" "provisioner" { - device_id = packet_device.tink-provisioner.id - port_name = "eth1" - vlan_vnid = packet_vlan.provisioning-vlan.vxlan + 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 } # Attach VLAN to worker resource "packet_port_vlan_attachment" "worker" { - device_id = packet_device.tink-worker.id + count = var.worker_count + depends_on = [packet_device_network_type.tink_worker_network_type] + + device_id = packet_device.tink_worker[count.index].id port_name = "eth0" - vlan_vnid = packet_vlan.provisioning-vlan.vxlan + vlan_vnid = packet_vlan.provisioning_vlan.vxlan } -output "provisioner_dns_name" { - value = "${split("-", packet_device.tink-provisioner.id)[0]}.packethost.net" +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 + } } -output "provisioner_ip" { - value = "${packet_device.tink-provisioner.network[0].address}" -} +resource "null_resource" "hardware_data" { + count = var.worker_count + depends_on = [null_resource.tink_directory] -output "worker_mac_addr" { - value = "${packet_device.tink-worker.ports[1].mac}" + connection { + type = "ssh" + user = var.ssh_user + host = packet_device.tink_provisioner.network[0].address + } + + provisioner "file" { + content = data.template_file.worker_hardware_data[count.index].rendered + destination = "/root/tink/deploy/hardware-data-${count.index}.json" + } } diff --git a/deploy/terraform/terraform/outputs.tf b/deploy/terraform/outputs.tf similarity index 100% rename from deploy/terraform/terraform/outputs.tf rename to deploy/terraform/outputs.tf diff --git a/deploy/terraform/terraform/install_package.sh b/deploy/terraform/terraform/install_package.sh deleted file mode 100644 index 95e86fd..0000000 --- a/deploy/terraform/terraform/install_package.sh +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env bash - -YUM="yum" -APT="apt" -PIP3="pip3" -YUM_CONFIG_MGR="yum-config-manager" -WHICH_YUM=$(command -v $YUM) -WHICH_APT=$(command -v $APT) -YUM_INSTALL="$YUM install" -APT_INSTALL="$APT install" -PIP3_INSTALL="$PIP3 install" -declare -a YUM_LIST=("https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm" - "docker-ce" - "docker-ce-cli" - "epel-release" - "pass" - "python3") -declare -a APT_LIST=("docker" - "docker-compose" "pass") - -add_yum_repo() ( - $YUM_CONFIG_MGR --add-repo https://download.docker.com/linux/centos/docker-ce.repo -) - -update_yum() ( - $YUM_INSTALL -y yum-utils - add_yum_repo -) - -update_apt() ( - $APT update - DEBIAN_FRONTEND=noninteractive $APT --yes --force-yes -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade -) - -restart_docker_service() ( - service docker restart -) -install_yum_packages() ( - $YUM_INSTALL "${YUM_LIST[@]}" -y -) - -install_pip3_packages() ( - $PIP3_INSTALL docker-compose -) - -install_apt_packages() ( - $APT_INSTALL "${APT_LIST[@]}" -y -) - -main() ( - if [[ -n $WHICH_YUM ]]; then - update_yum - install_yum_packages - install_pip3_packages - restart_docker_service - elif [[ -n $WHICH_APT ]]; then - update_apt - install_apt_packages - restart_docker_service - else - echo "Unknown platform. Error while installing the required packages" - exit 1 - fi -) - -main diff --git a/deploy/terraform/terraform/main.tf b/deploy/terraform/terraform/main.tf deleted file mode 100644 index 9cd4f12..0000000 --- a/deploy/terraform/terraform/main.tf +++ /dev/null @@ -1,141 +0,0 @@ -# Configure the Packet Provider. -terraform { - required_providers { - packet = { - source = "packethost/packet" - version = "~> 3.0.1" - } - null = { - source = "hashicorp/null" - } - } -} - -provider "packet" { - auth_token = var.packet_api_token -} - -# Create a new VLAN in datacenter "ewr1" -resource "packet_vlan" "provisioning_vlan" { - description = "provisioning_vlan" - facility = var.facility - project_id = var.project_id -} - -# Create a device and add it to tf_project_1 -resource "packet_device" "tink_provisioner" { - 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" - ] - } -} - -resource "packet_device_network_type" "tink_provisioner_network_type" { - device_id = packet_device.tink_provisioner.id - type = "hybrid" -} - -# Create a device and add it to tf_project_1 -resource "packet_device" "tink_worker" { - count = var.worker_count - - hostname = "tink-worker-${count.index}" - 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" -} - -# 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 -} - -# 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 - port_name = "eth0" - vlan_vnid = packet_vlan.provisioning_vlan.vxlan -} - -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 - } -} - -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 - } - - provisioner "file" { - content = data.template_file.worker_hardware_data[count.index].rendered - destination = "/root/tink/deploy/hardware-data-${count.index}.json" - } -} diff --git a/deploy/terraform/terraform/variables.tf b/deploy/terraform/variables.tf similarity index 100% rename from deploy/terraform/terraform/variables.tf rename to deploy/terraform/variables.tf diff --git a/deploy/terraform/terraform/versions.tf b/deploy/terraform/versions.tf similarity index 100% rename from deploy/terraform/terraform/versions.tf rename to deploy/terraform/versions.tf