-
Notifications
You must be signed in to change notification settings - Fork 3
/
servers.tf
154 lines (128 loc) · 3.76 KB
/
servers.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
provider "tls" {
version = "~> 2.1"
}
resource "tls_private_key" "ssh_key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "generated_key" {
key_name = var.ami_key_pair_name
public_key = tls_private_key.ssh_key.public_key_openssh
}
data "aws_ami" "ami" {
most_recent = true
owners = [
"099720109477"]
# Canonical
filter {
name = "name"
values = [
"ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = [
"hvm"]
}
}
resource "aws_instance" "grocy-ec2-instance" {
ami = data.aws_ami.ami.image_id
instance_type = "t2.micro"
key_name = var.ami_key_pair_name
security_groups = [
aws_security_group.ingress-all-grocy.id]
subnet_id = aws_subnet.subnet-grocy.id
associate_public_ip_address = true
tags = {
Name = "grocy server"
}
// Create the dbx dir if does not exist
provisioner "local-exec" {
command = "mkdir -p ~/.config/dbxcli"
}
provisioner "remote-exec" {
inline = [
"mkdir -p dns",
"mkdir -p docker-compose-letsencrypt-nginx-proxy-companion",
]
}
// put in the correct domain name in .env
provisioner "file" {
destination = "~/.env"
content = templatefile("${path.cwd}/../docker/.env.tpl", {
grocy_domain_name = "${var.grocy_duckdns_domain}.duckdns.org"
bbuddy_domain_name = "${var.grocy_bbuddy_duckdns_domain}.duckdns.org"
})
}
// put in the correct domain name in dns script
provisioner "file" {
destination = "~/dns/register-duckdns.sh"
content = templatefile("${path.cwd}/../dns/register-duckdns.sh.tpl", {
grocy_domain_name = var.grocy_duckdns_domain
bbuddy_domain_name = var.grocy_bbuddy_duckdns_domain
duckdns_token = var.duckdns_token
})
}
// put in the correct domain name in dns script to wait for grocy server domain
provisioner "file" {
destination = "~/dns/wait-for-dns.sh"
content = templatefile("${path.cwd}/../dns/wait-for-dns.sh.tpl", {
duckdns_domain = var.grocy_duckdns_domain
duckdns_token = var.duckdns_token
})
}
// put in the correct domain name in dns script to wait for bbuddy server domain
provisioner "file" {
destination = "~/dns/wait-for-dns2.sh"
content = templatefile("${path.cwd}/../dns/wait-for-dns2.sh.tpl", {
duckdns_domain = var.grocy_bbuddy_duckdns_domain
duckdns_token = var.duckdns_token
})
}
// Copy over dns scripts
provisioner "file" {
source = "${path.cwd}/../dns/"
destination = "~/dns"
}
// Copy over docker scripts
provisioner "file" {
source = "${path.cwd}/../docker/"
destination = "~/"
}
// Copy over letsencrypt-nginx-proxy-companion files
provisioner "file" {
source = "${path.cwd}/../docker-compose-letsencrypt-nginx-proxy-companion/"
destination = "~/docker-compose-letsencrypt-nginx-proxy-companion"
}
// Copy over backup scripts
provisioner "file" {
source = "${path.cwd}/../backup/"
destination = "~/"
}
// Copy over Dropbox configs
provisioner "remote-exec" {
inline = [ "mkdir -p .config",
]
}
provisioner "file" {
source = "~/.config/dbxcli"
destination = "~/.config"
}
connection {
user = "ubuntu"
type = "ssh"
host = aws_instance.grocy-ec2-instance.public_ip
private_key = tls_private_key.ssh_key.private_key_pem
}
provisioner "remote-exec" {
inline = [
"sudo chmod a+x -R ./*.sh",
"sudo ./install-docker.sh",
"sudo ./install-docker-compose.sh",
"cd dns && sudo chmod a+x -R ./*.sh && sudo ./schedule-duckdns.sh",
"cd ../docker-compose-letsencrypt-nginx-proxy-companion && sudo chmod a+x -R ./*.sh && sudo ./start.sh",
"cd .. && sudo docker-compose up -d"
//"sudo ./schedule-backup.sh",
]
}
}