Skip to content

Commit

Permalink
Reindenting + sections
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoraboeuf committed Nov 9, 2016
1 parent 24882b5 commit 747979b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 77 deletions.
156 changes: 87 additions & 69 deletions swarm.tf
Original file line number Diff line number Diff line change
@@ -1,86 +1,104 @@
##################################################################################################################
# SSH Key
##################################################################################################################

resource "digitalocean_ssh_key" "docker_swarm_ssh_key" {
name = "${var.do_swarm_name}-ssh-key"
public_key = "${file(var.do_ssh_key_public)}"
name = "${var.do_swarm_name}-ssh-key"
public_key = "${file(var.do_ssh_key_public)}"
}

resource "digitalocean_droplet" "docker_swarm_master_initial" {
count = 1
name = "${format("${var.do_swarm_name}-master-%02d", count.index)}"

image = "docker"
size = "${var.do_agent_size}"
region = "${var.do_region}"
private_networking = true

user_data = "#cloud-config\n\nssh_authorized_keys:\n - \"${file("${var.do_ssh_key_public}")}\"\n"
ssh_keys = [ "${digitalocean_ssh_key.docker_swarm_ssh_key.id}" ]

connection {
user = "root"
private_key = "${file(var.do_ssh_key_private)}"
agent = false
}

provisioner "remote-exec" {
inline = [
"docker swarm init --advertise-addr ${self.ipv4_address}",
"docker swarm join-token --quiet worker > /var/lib/docker/worker.token",
"docker swarm join-token --quiet manager > /var/lib/docker/manager.token"
]
}
##################################################################################################################
# Initial master node
##################################################################################################################

provisioner "local-exec" {
command = "scp -o StrictHostKeyChecking=no -o NoHostAuthenticationForLocalhost=yes -o UserKnownHostsFile=/dev/null -i ${var.do_ssh_key_private} root@${self.ipv4_address}:/var/lib/docker/worker.token ."
}

provisioner "local-exec" {
command = "scp -o StrictHostKeyChecking=no -o NoHostAuthenticationForLocalhost=yes -o UserKnownHostsFile=/dev/null -i ${var.do_ssh_key_private} root@${self.ipv4_address}:/var/lib/docker/manager.token ."
}

# TODO Visualizer
resource "digitalocean_droplet" "docker_swarm_master_initial" {
count = 1
name = "${format("${var.do_swarm_name}-master-%02d", count.index)}"

image = "docker"
size = "${var.do_agent_size}"
region = "${var.do_region}"
private_networking = true

user_data = "#cloud-config\n\nssh_authorized_keys:\n - \"${file("${var.do_ssh_key_public}")}\"\n"
ssh_keys = [
"${digitalocean_ssh_key.docker_swarm_ssh_key.id}"]

connection {
user = "root"
private_key = "${file(var.do_ssh_key_private)}"
agent = false
}

provisioner "remote-exec" {
inline = [
"docker swarm init --advertise-addr ${self.ipv4_address}",
"docker swarm join-token --quiet worker > /var/lib/docker/worker.token",
"docker swarm join-token --quiet manager > /var/lib/docker/manager.token"
]
}

provisioner "local-exec" {
command = "scp -o StrictHostKeyChecking=no -o NoHostAuthenticationForLocalhost=yes -o UserKnownHostsFile=/dev/null -i ${var.do_ssh_key_private} root@${self.ipv4_address}:/var/lib/docker/worker.token ."
}

provisioner "local-exec" {
command = "scp -o StrictHostKeyChecking=no -o NoHostAuthenticationForLocalhost=yes -o UserKnownHostsFile=/dev/null -i ${var.do_ssh_key_private} root@${self.ipv4_address}:/var/lib/docker/manager.token ."
}

}

##################################################################################################################
# Floating IP / DNS entry
##################################################################################################################

resource "digitalocean_floating_ip" "docker_swarm_floating_ip" {
droplet_id = "${digitalocean_droplet.docker_swarm_master_initial.id}"
region = "${digitalocean_droplet.docker_swarm_master_initial.region}"
droplet_id = "${digitalocean_droplet.docker_swarm_master_initial.id}"
region = "${digitalocean_droplet.docker_swarm_master_initial.region}"
}

resource "digitalocean_record" "docker_swarm_dns_record" {
domain = "${var.docker_swarm_domain}"
type = "A"
name = "${var.docker_swarm_domain_name}"
value = "${digitalocean_floating_ip.docker_swarm_floating_ip.ip_address}"
domain = "${var.docker_swarm_domain}"
type = "A"
name = "${var.docker_swarm_domain_name}"
value = "${digitalocean_floating_ip.docker_swarm_floating_ip.ip_address}"
}

##################################################################################################################
# TODO Other masters
##################################################################################################################

##################################################################################################################
# Swarm agents
##################################################################################################################

resource "digitalocean_droplet" "docker_swarm_agent" {
count = "${var.do_swarm_agent_count}"
name = "${format("${var.do_swarm_name}-agent-%02d", count.index)}"

image = "docker"
size = "${var.do_agent_size}"
region = "${var.do_region}"
private_networking = true

user_data = "#cloud-config\n\nssh_authorized_keys:\n - \"${file("${var.do_ssh_key_public}")}\"\n"
ssh_keys = [ "${digitalocean_ssh_key.docker_swarm_ssh_key.id}" ]

connection {
user = "root"
private_key = "${file(var.do_ssh_key_private)}"
agent = false
}

provisioner "file" {
source = "worker.token"
destination = "/var/lib/docker/worker.token"
}

provisioner "remote-exec" {
inline = [
"docker swarm join --token $(cat /var/lib/docker/worker.token) ${digitalocean_droplet.docker_swarm_master_initial.ipv4_address}:2377"
]
}
count = "${var.do_swarm_agent_count}"
name = "${format("${var.do_swarm_name}-agent-%02d", count.index)}"

image = "docker"
size = "${var.do_agent_size}"
region = "${var.do_region}"
private_networking = true

user_data = "#cloud-config\n\nssh_authorized_keys:\n - \"${file("${var.do_ssh_key_public}")}\"\n"
ssh_keys = [
"${digitalocean_ssh_key.docker_swarm_ssh_key.id}"]

connection {
user = "root"
private_key = "${file(var.do_ssh_key_private)}"
agent = false
}

provisioner "file" {
source = "worker.token"
destination = "/var/lib/docker/worker.token"
}

provisioner "remote-exec" {
inline = [
"docker swarm join --token $(cat /var/lib/docker/worker.token) ${digitalocean_droplet.docker_swarm_master_initial.ipv4_address}:2377"
]
}
}
16 changes: 8 additions & 8 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ variable "do_token" {
}

variable "docker_swarm_domain" {
description = "Name of the DNS domain for the swarm"
default = "nemerosa.net"
description = "Name of the DNS domain for the swarm"
default = "nemerosa.net"
}

variable "docker_swarm_domain_name" {
description = "Name of the swarm in the DNS domain"
default = "swarm"
description = "Name of the swarm in the DNS domain"
default = "swarm"
}

variable "do_region" {
Expand All @@ -23,13 +23,13 @@ variable "do_agent_size" {
}

variable "do_ssh_key_public" {
description = "Path to the SSH public key"
default = "./do-key.pub"
description = "Path to the SSH public key"
default = "./do-key.pub"
}

variable "do_ssh_key_private" {
description = "Path to the SSH private key"
default = "./do-key"
description = "Path to the SSH private key"
default = "./do-key"
}

variable "do_swarm_name" {
Expand Down

0 comments on commit 747979b

Please sign in to comment.