-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommands.tf
141 lines (119 loc) · 3.96 KB
/
commands.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
resource "null_resource" "provision_openvpn" {
triggers = {
user = var.ssh_user
port = var.ssh_port
private_key = var.private_key
host = aws_eip.this.public_ip
}
depends_on = [aws_instance.this, aws_eip.this, aws_security_group.this]
connection {
type = "ssh"
user = self.triggers.user
port = self.triggers.port
private_key = file(self.triggers.private_key)
host = self.triggers.host
}
provisioner "remote-exec" {
inline = [
"sudo apt-get -y update",
"sudo apt-get install -y curl vim git libltdl7 python3 python3-pip python software-properties-common unattended-upgrades",
"touch ~/provisioned", # Troll
]
}
}
# provision core templates
resource "null_resource" "provision_core" {
triggers = {
user = var.ssh_user
port = var.ssh_port
private_key = var.private_key
host = aws_eip.this.public_ip
}
depends_on = [null_resource.provision_openvpn]
connection {
type = "ssh"
user = self.triggers.user
port = self.triggers.port
private_key = file(self.triggers.private_key)
host = self.triggers.host
}
provisioner "remote-exec" {
inline = [
format("%s %s", "rm -rf ", local.templates_path),
format("%s %s", "mkdir -p ", local.templates_path),
]
}
}
#installing openvpn through third-party openvpn script by dumrauf you can check it out here: https://github.com/dumrauf
resource "null_resource" "openvpn_install" {
triggers = {
user = var.ssh_user
port = var.ssh_port
private_key = var.private_key
host = aws_eip.this.public_ip
}
depends_on = [null_resource.provision_core]
connection {
type = "ssh"
user = self.triggers.user
port = self.triggers.port
private_key = file(self.triggers.private_key)
host = self.triggers.host
}
provisioner "file" {
destination = local.templates.vpn.install.file
content = templatefile(local.templates.vpn.install.template, local.templates.vpn.install.vars)
}
provisioner "remote-exec" {
inline = [
format("%s %s", "sudo chmod a+x", local.templates.vpn.install.file),
format("%s %s", "sudo ", local.templates.vpn.install.file),
]
}
}
#adding user to connect to vpn through third party script by dumrauf you can check it out here: https://github.com/dumrauf
resource "null_resource" "openvpn_adduser" {
triggers = {
user = var.ssh_user
port = var.ssh_port
private_key = var.private_key
host = aws_eip.this.public_ip
}
depends_on = [null_resource.openvpn_install]
connection {
type = "ssh"
user = self.triggers.user
port = self.triggers.port
private_key = file(self.triggers.private_key)
host = self.triggers.host
}
provisioner "file" {
destination = local.templates.vpn.update_user.file
content = templatefile(local.templates.vpn.update_user.template, local.templates.vpn.update_user.vars)
}
provisioner "remote-exec" {
inline = [
format("%s %s", "sudo chmod a+x", local.templates.vpn.update_user.file),
format("%s %s", "sudo ", local.templates.vpn.update_user.file),
]
}
}
#download ovpn configurations to use with openvpn client
resource "null_resource" "openvpn_download_configurations" {
count = var.is_test ? 0 : 1
triggers = {
user = var.ssh_user
port = var.ssh_port
private_key = var.private_key
host = aws_eip.this.public_ip
}
depends_on = [null_resource.openvpn_adduser, aws_eip.this]
provisioner "local-exec" {
command = <<EOT
mkdir -p ${var.storage_path}/${self.triggers.host};
scp -o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-i ${self.triggers.private_key} ${self.triggers.user}@${self.triggers.host}:/home/${self.triggers.user}/*.ovpn ${var.storage_path}/${self.triggers.host}/
EOT
}
}