Skip to content

Instantly share code, notes, and snippets.

@youanswer
Last active August 29, 2015 14:26
Show Gist options
  • Save youanswer/bc1ca37773df968038a8 to your computer and use it in GitHub Desktop.
Save youanswer/bc1ca37773df968038a8 to your computer and use it in GitHub Desktop.
provider "digitalocean" {
token = "${var.do_token}"
}
resource "digitalocean_droplet" "server" {
count = "${var.servers}"
image = "ubuntu-14-04-x64"
name = "${var.tagName}-${count.index}"
region = "${var.region}"
size = "${var.size}"
private_networking = true
ssh_keys = ["${var.ssh_fingerprint}"]
connection {
user = "root"
key_file = "${var.key_path}"
}
provisioner "file" {
source = "${path.module}/scripts/ubuntu/upstart.conf"
destination = "/tmp/upstart.conf"
}
provisioner "file" {
source = "${path.module}/scripts/ubuntu/upstart-script.sh"
destination = "/tmp/upstart-script.sh"
}
provisioner "remote-exec" {
inline = [
"echo ${var.servers} > /tmp/consul-server-count",
"echo ${digitalocean_droplet.server.0.ipv4_address} > /tmp/consul-server-addr"
]
}
provisioner "remote-exec" {
scripts = [
"${path.module}/scripts/ubuntu/install.sh",
]
}
}
#hack for doing some manipulation when servers are already created
resource "null_resource" "docker_provisioner" {
count = "${var.servers}"
depends_on = ["digitalocean_droplet.server"]
connection {
user = "root"
host = "${element(digitalocean_droplet.server.*.name, count.index)}.${var.domain_name}"
key_file = "${var.key_path}"
}
# generate cert for docker locally
provisioner "local-exec" {
command = "ruby ${path.module}/certgen/certgen.rb ${element(digitalocean_droplet.server.*.name, count.index)}.${var.domain_name}"
}
# copy certs to remote
provisioner "file" {
source = "/Users/user/Documents/local/docker-certs/${element(digitalocean_droplet.server.*.name, count.index)}.${var.domain_name}"
destination = "/root/.docker/"
}
# run docker daemon
provisioner "remote-exec" {
scripts = [
"${path.module}/scripts/ubuntu/docker-daemon.sh",
"${path.module}/scripts/ubuntu/service.sh",
]
}
}
#Create DNS
resource "digitalocean_record" "server" {
depends_on = ["digitalocean_droplet.server"]
count = "${var.servers}"
domain = "${var.domain_name}"
type = "A"
name = "${element(digitalocean_droplet.server.*.name, count.index)}"
value = "${element(digitalocean_droplet.server.*.ipv4_address, count.index)}"
}
#This docker module inside /docker/docker.tf
provider "docker" {
host = "tcp://${var.domain_name}:4243/"
cert_path = "${var.cert_path}${var.domain_name}"
}
resource "docker_container" "nginx" {
image = "progrium/nginx"
name = "nginx"
ports = {
internal = 80
external = 8000
}
ports = {
internal = 443
external = 443
}
must_run = true
}
#main root file, I run 'terraform apply' from here
module "do" {
source = "./do"
#variables here
key_path = "~/.ssh/terraform"
ssh_fingerprint = "123456"
do_token = "do_token"
domain_name = "example.com"
}
module "server_0" {
source = "./docker"
domain_name = "${module.do.domain_0}"
}
module "server_1" {
source = "./docker"
domain_name = "${module.do.domain_1}"
}
module "server_2" {
source = "./docker"
domain_name = "${module.do.domain_2}"
}
# /do/outputs.tf outputs of consul.tf
output "server_0" {
value = "${digitalocean_droplet.server.0.ipv4_address}"
}
output "server_1" {
value = "${digitalocean_droplet.server.1.ipv4_address}"
}
output "server_2" {
value = "${digitalocean_droplet.server.2.ipv4_address}"
}
output "domain_0" {
value = "${digitalocean_record.server.0.name}.${var.domain_name}"
}
output "domain_1" {
value = "${digitalocean_record.server.1.name}.${var.domain_name}"
}
output "domain_2" {
value = "${digitalocean_record.server.2.name}.${var.domain_name}"
}
# /docker/variables.tf Variables of docker module
variable "domain_name" {
description = "Domain of the droplet"
}
variable "cert_path" {
description = "Cert path for docker tcp connection over tls"
default = "/Users/user/Documents/local/docker-certs/"
}
# /do/variables.tf
variable "platform" {
default = "ubuntu"
description = "The OS of Droplet"
}
variable "user" {
default = "ubuntu"
decription = "User for droplet"
}
variable "ssh_fingerprint" {
description = "Fingerprint of your ssh key"
}
variable "key_path" {
default = "~/.ssh/terraform"
description = "Path to the private key specified by key_name."
}
variable "region" {
default = "nyc3"
description = "The region of Droplet."
}
variable "size" {
default = "512mb"
description = "Size of droplet"
}
variable "servers" {
default = "3"
description = "The number of Consul servers to launch."
}
variable "tagName" {
default = "consul"
description = "Name tag for the servers"
}
variable "do_token" {
description = "Digital Ocean api token"
}
variable "domain_name" {
description = "Domain name to be based on"
}
variable "cert_path" {
description = "Cert path for docker tcp connection over tls"
default = "/Users/user/Documents/local/docker-certs/"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment