-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
190 lines (163 loc) · 4.64 KB
/
Copy pathmain.tf
File metadata and controls
190 lines (163 loc) · 4.64 KB
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
terraform {
required_version = ">= 1.0.0, < 2.0.0"
required_providers {
null = {
source = "hashicorp/null"
version = "3.2.2"
}
docker = {
source = "kreuzwerker/docker"
version = "3.0.2"
}
}
}
provider "docker" {
host = "unix:///var/run/docker.sock"
}
resource "null_resource" "manage_ssh" {
triggers = {
always_run = timestamp()
}
provisioner "local-exec" {
when = create
command = <<-EOT
ssh-keygen -t rsa -b 2048 -f id_rsa -N ''
chmod 600 id_rsa.pub
[ -f $HOME/.ssh/config ] || mkdir --parents $HOME/.ssh && touch $HOME/.ssh/config
echo "### Terraform autogenerated content start ###" >> $HOME/.ssh/config
EOT
}
provisioner "local-exec" {
when = destroy
command = <<-EOT
rm id_rsa id_rsa.pub
sed --in-place \
"/### Terraform autogenerated content start ###/,\
/### Terraform autogenerated content end ###/d" \
$HOME/.ssh/config
EOT
}
}
resource "docker_network" "nodes_network" {
name = "${var.node_prefix}-network"
ipam_config {
subnet = var.subnet
gateway = var.gateway
}
labels {
label = "app"
value = "superset-cluster"
}
}
resource "docker_image" "node_image" {
name = "${var.node_prefix}:${var.node_version}"
triggers = {
always_run = timestamp()
}
build {
context = "."
labels = {
label = "app"
value = "superset-cluster"
}
}
depends_on = [
null_resource.manage_ssh
]
}
resource "docker_container" "nodes" {
count = "5"
name = "${var.node_prefix}-${count.index}"
hostname = "${var.node_prefix}-${count.index}"
image = docker_image.node_image.name
privileged = true # nodes containers are treated as standalone virtual machines
ulimit {
name = "nofile"
soft = 1024
hard = 65535
}
networks_advanced {
name = docker_network.nodes_network.name
ipv4_address = cidrhost(var.subnet, 2 + count.index)
}
labels {
label = "app"
value = "superset-cluster"
}
provisioner "local-exec" {
command = <<-EOT
docker cp ../../src $HOSTNAME:/opt/superset-testing
docker cp ../../services/mysql-mgmt/interfaces.py $HOSTNAME:/opt/superset-testing
docker cp ../testsuite/roles/testing/files/. $HOSTNAME:/opt/superset-testing
docker exec $HOSTNAME /bin/bash -c " \
wget --directory-prefix=/tmp --quiet https://bootstrap.pypa.io/get-pip.py && \
python3 /tmp/get-pip.py > /dev/null 2>&1 && \
python3 -m pip install --quiet --no-cache-dir --user --requirement /opt/superset-testing/requirements.txt"
docker exec --user=root $HOSTNAME /bin/bash -c " \
service ssh start && \
mkdir -p /etc/docker && \
echo '{\"storage-driver\": \"vfs\"}' > /etc/docker/daemon.json && \
service docker start && \
echo -e 'nameserver 8.8.8.8\nnameserver 8.8.4.4' >> /etc/resolv.conf && \
usermod --append --groups docker superset && \
chown --recursive superset:superset /opt/superset-testing"
echo "Host $HOSTNAME
Hostname $IP_ADDRESS
StrictHostKeyChecking no
IdentityFile $(pwd)/id_rsa
IdentitiesOnly yes" >> $HOME/.ssh/config
EOT
environment = {
HOSTNAME = "${var.node_prefix}-${count.index}"
IP_ADDRESS = cidrhost(var.subnet, 2 + count.index)
}
}
depends_on = [
docker_image.node_image,
docker_network.nodes_network,
null_resource.manage_ssh
]
}
resource "null_resource" "generate_ansible_group_vars" {
triggers = {
always_run = timestamp()
}
provisioner "local-exec" {
when = create
command = <<-EOT
mkdir $(dirname $GROUP_VARS_FILE)
{
echo "virtual_network_interface: eth0"
echo "virtual_ip_address: \"$VIRTUAL_IP_ADDRESS\""
echo "virtual_network_mask: \"$VIRTUAL_NETWORK_MASK\""
echo "node_prefix: \"$NODE_PREFIX\""
} > $GROUP_VARS_FILE
EOT
environment = {
GROUP_VARS_FILE = "../testsuite/group_vars/testing.yml"
NODE_PREFIX = var.node_prefix
VIRTUAL_IP_ADDRESS = cidrhost(var.subnet, 10)
VIRTUAL_NETWORK_MASK = cidrnetmask(var.subnet)
}
}
provisioner "local-exec" {
when = destroy
command = "rm --recursive $(dirname $GROUP_VARS_FILE)"
environment = {
GROUP_VARS_FILE = "../testsuite/group_vars/testing.yml"
}
}
}
resource "null_resource" "finish_configuration" {
triggers = {
always_run = timestamp()
}
provisioner "local-exec" {
command = <<-EOT
echo "### Terraform autogenerated content end ###" >> $HOME/.ssh/config
EOT
}
depends_on = [
docker_container.nodes
]
}