forked from Patrik-Stas/indyscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indyscan.tf
207 lines (178 loc) · 7.01 KB
/
indyscan.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
resource "aws_instance" "indyscan" {
ami = var.ubuntu_ami
instance_type = var.ec2_size
availability_zone = var.availability_zone
key_name = var.keypair_name
vpc_security_group_ids = flatten([
aws_security_group.Indyscan_General.id,
aws_security_group.Indyscan_Services.id,
aws_security_group.Indyscan_IndyPool_Client.id,
aws_security_group.Indyscan_IndyPool_Node.id,
var.custom_security_group_ids
])
root_block_device {
volume_size = "20"
}
tags = {
Name = var.ec2_tag
}
}
// When using references to aws_instance in the definition of security groups, following is way how it's possible to
// add those security groups on the primary network interface of our ec2 resource. The problem is that
// if you use "vpc_security_group_ids" in aws_instance definition, you can't use these, they don't go well together
// see: https://www.terraform.io/docs/providers/aws/r/network_interface_sg_attachment.html
// If you don't specify vpc_security_group_ids at all, it will still have "default" VPC group, which I don't know what
// is here, but it might as well expose everything making our security groups defined here completely meaningless.
// TODO: How can we define security groups referring attributes of ec2 instance using them and making sure the ec2
// TODO: instance is not gonna have any other security groups such as "default"?
//resource "aws_network_interface_sg_attachment" "attach_sg_Indyscan_Services" {
// security_group_id = aws_security_group.Indyscan_Services.id
// network_interface_id = aws_instance.indyscan.primary_network_interface_id
//}
//
//resource "aws_network_interface_sg_attachment" "attach_sg_Indyscan_General" {
// security_group_id = aws_security_group.Indyscan_General.id
// network_interface_id = aws_instance.indyscan.primary_network_interface_id
//}
//
//resource "aws_network_interface_sg_attachment" "attach_sg_IndyPool_Client" {
// security_group_id = aws_security_group.Indyscan_IndyPool_Client.id
// network_interface_id = aws_instance.indyscan.primary_network_interface_id
//}
//
//resource "aws_network_interface_sg_attachment" "attach_sg_IndyPool_Node" {
// security_group_id = aws_security_group.Indyscan_IndyPool_Node.id
// network_interface_id = aws_instance.indyscan.primary_network_interface_id
//}
resource "null_resource" "assure_software" {
connection {
type = "ssh"
user = "ubuntu"
host = aws_instance.indyscan.public_ip
private_key = file(var.private_key_path)
}
provisioner "file" {
source = "${path.module}/scripts-docker"
destination = "$HOME"
}
provisioner "remote-exec" {
inline = [
"set -ex",
"chmod +x $HOME/scripts-docker/*.sh",
"$HOME/scripts-docker/assure-docker.sh ||:",
]
}
provisioner "remote-exec" {
inline = [
// it's was happening quite often I had to rerun because of unclear issue in assure-docker.sh
// (usuall it still wouldn't find docker-ce even after repo was added), so this is dirtyfix. Just rerun once more
// TODO: must be nice if we stabilize assuer-docker.sh so we could delete this remote-exec
"$HOME/scripts-docker/assure-docker.sh",
"rm -r \"$HOME/scripts-docker\"",
]
}
}
/*
Destroys all existing data
Recreate everything from scrach
TODO: Make it possible to restart ledger without destorying it. Right now that's problematic because I can't mount ledger data to named volume. There's a comment about this in start/docker-compose.yml
*/
resource "null_resource" "recreate_environment" {
connection {
type = "ssh"
user = "ubuntu"
host = aws_instance.indyscan.public_ip
private_key = file(var.private_key_path)
}
depends_on = [
null_resource.assure_software,
aws_route53_record.www
]
triggers = {
key = var.trigger_reset_environment
}
// wipe out existing files, destory existing environment
provisioner "remote-exec" {
inline = [
"set -x",
"cd $HOME/indyscan; docker-compose down --volumes ||:", // since we cannot preserve ledger data, we have to also wipe out elasticsearch data
"yes | rm -r $HOME/indyscan ||:",
]
}
// re-copy files
provisioner "file" {
source = "${path.module}/../start"
destination = "$HOME/indyscan"
}
provisioner "remote-exec" {
inline = [
"set -x",
"chmod -R +x indyscan/*.sh indyscan/**/*.sh",
]
}
// build pool image, prepare configuration
provisioner "remote-exec" {
inline = [
"set -x",
"export POOL_ADDRESS='${coalesce(var.dns_hostname, aws_instance.indyscan.public_ip)}'",
"export INDYPOOL_IMAGE_TAG=\"indypool-$POOL_ADDRESS:latest\"",
"yes | ~/indyscan/indypool/build-pool.sh",
"docker run --rm --name tmp-indypool \"$INDYPOOL_IMAGE_TAG\" cat /var/lib/indy/sandbox/pool_transactions_genesis > ~/indyscan/app-configs-daemon/genesis/INDYSCANPOOL.txn"
]
}
// start up new environment
provisioner "remote-exec" {
inline = [
"set -x",
"ls ~",
"cd ~/indyscan; docker-compose pull --ignore-pull-failures",
"export POOL_ADDRESS='${coalesce(var.dns_hostname, aws_instance.indyscan.public_ip)}'",
"export INDYPOOL_IMAGE_TAG=\"indypool-$POOL_ADDRESS:latest\"",
"echo \"INDYSCAN_INDYPOOL_IMAGE=indypool-$POOL_ADDRESS:latest\" > ~/indyscan/.env",
"echo Restarting docker-compose with following env file:",
"cat ~/indyscan/.env",
"cd ~/indyscan; docker-compose up -d",
]
}
}
//resource "null_resource" "reset-pool" {
// inline = [
// "echo 'Completely turning around Indyscan!'",
// "cd ~/indyscan/indypool; docker-compose down",
// "alias kill-all-docker-containers='docker rm -f $(docker ps -qa)'",
// "docker volume --prune --force",
// ]
//}
//
resource "null_resource" "provision_genesis_locally" {
triggers = {
key = var.trigger_reset_environment
}
depends_on = [
null_resource.recreate_environment,
]
provisioner "local-exec" {
command = "mkdir -p ${path.module}/tmp; rm ${path.module}/tmp/genesis.txn || :"
}
provisioner "local-exec" {
command = "scp -o \"StrictHostKeyChecking no\" -i ${var.private_key_path} ubuntu@${aws_instance.indyscan.public_ip}:~/indyscan/app-configs-daemon/genesis/INDYSCANPOOL.txn ${path.module}/tmp/${var.local_network_name}.txn"
}
provisioner "local-exec" {
command = "export PROVISION_POOL_DIR=\"$HOME/.indy_client/pool/${var.local_network_name}\"; mkdir -p \"$PROVISION_POOL_DIR\" || :; cp ${path.module}/tmp/${var.local_network_name}.txn \"$PROVISION_POOL_DIR/${var.local_network_name}.txn\" || :;"
}
provisioner "local-exec" {
command = "echo \"Genesis file for your AWS Indypool is located at: $HOME/.indy_client/pool/${var.local_network_name}/${var.local_network_name}.txn\""
}
}
resource "null_resource" "print_info" {
triggers = {
key = uuid()
}
depends_on = [
null_resource.recreate_environment,
null_resource.provision_genesis_locally
]
provisioner "local-exec" {
command = "./print-info.sh ${var.local_network_name} http://${aws_instance.indyscan.public_ip}:3707 http://${aws_instance.indyscan.public_ip}:3708"
}
}