imfreedom/bamboo-terraform

Initial revision
draft
2019-02-06, Gary Kramlich
21774683eab9
Parents
Children d3cb1067774e
Initial revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore Wed Feb 06 19:53:43 2019 -0600
@@ -0,0 +1,9 @@
+syntax: glob
+*.tfstate.backup
+*.tfplan
+*.tfstate
+
+syntax: regexp
+\.terraform\/
+nodes\/
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile Wed Feb 06 19:53:43 2019 -0600
@@ -0,0 +1,31 @@
+NODE=
+PLAN_LIMIT=$(LIMIT:%= -target=%)
+TFARGS = -var-file nodes/$(NODE).tfvars
+
+all: plan
+
+plan: _require_node
+ terraform plan -parallelism=1 -out $(NODE).tfplan $(TFARGS) $(PLAN_LIMIT)
+
+destroy: _require_node
+ terraform plan -destroy -out $(NODE).tfplan $(TFARGS)
+
+apply: _require_node
+ terraform apply -input=true $(NODE).tfplan
+ rm $(NODE).tfplan
+
+showplan: _require_node
+ terraform show $(NODE).tfplan
+
+_require_node: nodes/$(NODE).tfvars
+ $(warning *)
+ $(warning *)
+ $(warning Executing on host $(NODE))
+ $(warning *)
+ $(warning *)
+
+nodes/$(NODE).tfvars:
+ @echo unknown node \"$(NODE)\"
+ @exit 1
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md Wed Feb 06 19:53:43 2019 -0600
@@ -0,0 +1,36 @@
+# Pidgin Bamboo Terraform
+
+# Requirements
+
+terraform and terraform-libvirt provider
+
+# Usage
+
+You need to have a `node.tfvars` in the `nodes/` directory which has contents
+like the following.
+
+```
+libvirt_uri = "qemu:///system"
+admin_ssh_pubkey = "ssh pubkey"
+
+worker_count = 2
+```
+
+`libvirt_uri` is the connect string for libvirt and `admin_ssh_pubkey` is the
+SSH pubkey that should be accepted for the root user.
+
+`worker_count` is the number of Bamboo agents to provision.
+
+Once that file is created you provision it via:
+
+```
+make plan NODE=node1
+make apply NODE=node1
+```
+
+
+# Terraform Host
+
+mkisofs is needed on the machine where terraform is running from to be able to
+create cloudinit isos.
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/instance-registry-cache.tf Wed Feb 06 19:53:43 2019 -0600
@@ -0,0 +1,80 @@
+variable "registry_cache_disk_size" { default = "53687091200" } # 50gb
+variable "registry_cache_memory" { default="1024" } # 1gb
+variable "registry_cache_cpu" { default="1" }
+
+variable "admin_ssh_pubkey" {}
+
+resource "libvirt_volume" "registry_cache" {
+ name = "registry-cache-root"
+ base_volume_id = "${libvirt_volume.debian_base.id}"
+ format = "qcow2"
+ size = "${var.registry_cache_disk_size}"
+}
+
+data "template_file" "registry_cache_user_data" {
+ template = <<EOF
+#cloud-config
+users:
+ - name: root
+ ssh_authorized_keys:
+ - $${admin_ssh_pubkey}
+growpart:
+ mode: auto
+ devices: ['/']
+ ignore_growroot_disabled: false
+EOF
+
+ vars {
+ admin_ssh_pubkey = "${var.admin_ssh_pubkey}"
+ }
+}
+
+resource "libvirt_cloudinit_disk" "registry_cache" {
+ name = "registry-cache-init.iso"
+ user_data = "${data.template_file.registry_cache_user_data.rendered}"
+}
+
+resource "libvirt_domain" "registry_cache" {
+ name = "registry-cache"
+ memory = "${var.registry_cache_memory}"
+ vcpu = "${var.registry_cache_cpu}"
+ autostart = true
+
+ cloudinit = "${libvirt_cloudinit_disk.registry_cache.id}"
+
+ console {
+ type = "pty"
+ target_port = "0"
+ target_type = "serial"
+ }
+
+# console {
+# type = "pty"
+# target_port = "1"
+# target_type = "virtio"
+# }
+
+ disk {
+ volume_id = "${libvirt_volume.registry_cache.id}"
+ }
+
+# graphics {
+# type = "spice"
+# listen_type = "address"
+# autoport = true
+# }
+
+ network_interface {
+ network_name = "default"
+ wait_for_lease = true
+ }
+
+ boot_device {
+ dev = ["hd"]
+ }
+}
+
+output "registry_cache_ip" {
+ value = "${libvirt_domain.registry_cache.network_interface.0.addresses}"
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/instance-worker.tf Wed Feb 06 19:53:43 2019 -0600
@@ -0,0 +1,89 @@
+variable "worker_count" { default = "1" }
+variable "worker_disk_size" { default = "21474836480" } # 20gb
+variable "worker_memory" { default="1024" } # 1gb
+variable "worker_cpu" { default="1" }
+
+resource "libvirt_volume" "worker" {
+ count = "${var.worker_count}"
+ name = "worker-${count.index}-root"
+
+ format = "qcow2"
+ base_volume_id = "${libvirt_volume.debian_base.id}"
+ size = "${var.worker_disk_size}"
+}
+
+data "template_file" "worker_user_data" {
+ count = "${var.worker_count > 0 ? 1 : 0}"
+
+ template = <<EOF
+#cloud-config
+fqdn: worker-$${index}
+users:
+ - name: root
+ ssh_authorized_keys:
+ - $${admin_ssh_pubkey}
+growpart:
+ mode: auto
+ devices: ['/']
+ ignore_growroot_disabled: false
+EOF
+
+ vars {
+ admin_ssh_pubkey = "${var.admin_ssh_pubkey}"
+ index = "${count.index}"
+ }
+}
+
+resource "libvirt_cloudinit_disk" "worker" {
+ count = "${var.worker_count}"
+
+ name = "worker-${count.index}-init.iso"
+ user_data = "${data.template_file.worker_user_data.rendered}"
+}
+
+resource "libvirt_domain" "worker" {
+ count = "${var.worker_count}"
+ name = "worker-${count.index}"
+
+ memory = "${var.worker_memory}"
+ vcpu = "${var.worker_cpu}"
+ autostart = true
+
+ cloudinit = "${element(libvirt_cloudinit_disk.worker.*.id, count.index)}"
+
+# console {
+# type = "pty"
+# target_port = "0"
+# target_type = "serial"
+# }
+
+# console {
+# type = "pty"
+# target_port = "1"
+# target_type = "virtio"
+# }
+
+ disk {
+ volume_id = "${element(libvirt_volume.worker.*.id, count.index)}"
+ }
+
+# graphics {
+# type = "spice"
+# listen_type = "address"
+# autoport = true
+# }
+
+ network_interface {
+ network_name = "default"
+ wait_for_lease = true
+ }
+
+ boot_device {
+ dev = ["hd"]
+ }
+}
+
+output "worker_ips" {
+ value = "${libvirt_domain.worker.*.network_interface.0.addresses}"
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/provider-libvirt.tf Wed Feb 06 19:53:43 2019 -0600
@@ -0,0 +1,6 @@
+variable "libvirt_uri" { default = "qemu:///system" }
+
+provider "libvirt" {
+ uri = "${var.libvirt_uri}"
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/volume-base.tf Wed Feb 06 19:53:43 2019 -0600
@@ -0,0 +1,7 @@
+resource "libvirt_volume" "debian_base" {
+ name = "debian-base"
+ pool = "default"
+ source = "https://cdimage.debian.org/cdimage/openstack/9.6.2-20181228/debian-9.6.2-20181228-openstack-amd64.qcow2"
+ format = "qcow2"
+}
+