-
Notifications
You must be signed in to change notification settings - Fork 3
/
supermarket-cluster.tf
283 lines (240 loc) · 8.68 KB
/
supermarket-cluster.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
provider "aws" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
}
# Create security group for servers in this cluster
resource "aws_security_group" "allow-ssh" {
name = "allow-ssh"
tags {
Name = "Allow All SSH"
}
}
resource "aws_security_group_rule" "allow-ssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.allow-ssh.id}"
}
resource "aws_security_group" "allow-443" {
name = "allow-443"
tags {
Name = "Allow connections over 443"
}
}
resource "aws_security_group_rule" "allow-443" {
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.allow-443.id}"
}
resource "aws_security_group_rule" "allow_all_egress" {
type = "egress"
from_port = 0
to_port = 65535
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.allow-ssh.id}"
}
# This will create the users and organization
resource "template_file" "chef_bootstrap" {
template = "${file("chef_bootstrap.tpl")}"
vars {
chef-server-user = "${var.chef-server-user}"
chef-server-user-full-name = "${var.chef-server-user-full-name}"
chef-server-user-email = "${var.chef-server-user-email}"
chef-server-user-password = "${var.chef-server-user-password}"
chef-server-org-name = "${var.chef-server-org-name}"
chef-server-org-full-name = "${var.chef-server-org-full-name}"
}
}
# Setup chef-server
resource "aws_instance" "chef_server" {
ami = "${var.ami}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
tags {
Name = "test-chef-server"
}
security_groups = ["${aws_security_group.allow-ssh.name}", "${aws_security_group.allow-443.name}"]
# Uploads all cookbooks needed to install Chef server
provisioner "file" {
source = "cookbooks"
destination = "/tmp"
connection {
type = "ssh"
user = "ubuntu"
private_key = "${file(\"${var.private_ssh_key_path}\")}"
}
}
provisioner "remote-exec" {
inline = [
"sudo service iptables stop",
"sudo chkconfig iptables off",
"curl -LO https://www.chef.io/chef/install.sh && sudo bash ./install.sh -P chefdk -n && rm install.sh",
"cd /tmp; sudo chef exec chef-client -z -o chef-server",
"echo '${template_file.chef_bootstrap.rendered}' > /tmp/bootstrap-chef-server.sh",
"chmod +x /tmp/bootstrap-chef-server.sh",
"sudo sh /tmp/bootstrap-chef-server.sh",
"sudo sed -i 's/api_fqdn.*$/api_fqdn \"${self.public_ip}\"/' /etc/opscode/chef-server.rb",
"sudo chown ubuntu /etc/opscode/chef-server.rb",
"sudo chef-server-ctl reconfigure",
]
connection {
type = "ssh"
user = "ubuntu"
private_key = "${file(\"${var.private_ssh_key_path}\")}"
}
}
# Make .chef directory
provisioner "local-exec" {
command = "mkdir -p .chef"
}
# This will download the chef user pem to your local workstation
provisioner "local-exec" {
command = "scp -oStrictHostKeyChecking=no -i ${var.private_ssh_key_path} ubuntu@${self.public_ip}:${var.chef-server-user}.pem .chef/${var.chef-server-user}.pem"
}
}
# Template to render knife.rb
resource "template_file" "knife_rb" {
template = "${file("knife_rb.tpl")}"
vars {
chef-server-user = "${var.chef-server-user}"
chef-server-fqdn = "${aws_instance.chef_server.public_ip}"
organization = "${var.chef-server-org-name}"
}
# Make .chef/knife.rb file
provisioner "local-exec" {
command = "echo '${template_file.knife_rb.rendered}' > .chef/knife.rb"
}
# Fetch Chef Server Certificate
provisioner "local-exec" {
command = "knife ssl fetch"
}
# Upload cookbooks to the Chef Server
provisioner "local-exec" {
command = "knife cookbook upload --all --cookbook-path cookbooks"
}
}
# Sets up VM for Supermarket
resource "aws_instance" "supermarket_server" {
depends_on = ["aws_instance.chef_server"]
ami = "${var.ami}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
tags {
Name = "test-supermarket"
}
security_groups = ["${aws_security_group.allow-ssh.name}", "${aws_security_group.allow-443.name}"]
}
# Template for the Supermarket Databag
resource "template_file" "supermarket_databag" {
depends_on = ["aws_instance.supermarket_server"]
template = "${file("supermarket_databag.tpl")}"
vars {
chef-server-url = "${aws_instance.chef_server.public_ip}"
fqdn = "${aws_instance.supermarket_server.public_ip}"
}
}
resource "null_resource" "supermarket-chef-setup" {
depends_on = ["aws_instance.supermarket_server"]
# Forces 60 second wait to allow Supermarket server to become available, then bootstraps Supermarket VM with Chef
provisioner "local-exec" {
command = "sleep 60 && knife bootstrap ${aws_instance.supermarket_server.public_ip} -N supermarket-node -x ubuntu --sudo"
}
# Make a data bags directory
provisioner "local-exec" {
command = "mkdir -p databags/apps"
}
# Make json file for supermarket data bag item
provisioner "local-exec" {
command = "echo '${template_file.supermarket_databag.rendered}' > databags/apps/supermarket.json"
}
# Create the apps data bag on the Chef server
provisioner "local-exec" {
command = "knife data bag create apps"
}
# Create supermarket data bag item on the Chef server
provisioner "local-exec" {
command = "knife data bag from file apps databags/apps/supermarket.json"
}
}
# Template to add Supermarket as an oc_id application to the Chef Server
resource "template_file" "oc-id" {
depends_on = ["aws_instance.supermarket_server"]
template = "${file("oc_id.tpl")}"
vars {
supermarket-ip = "${aws_instance.supermarket_server.public_ip}"
}
provisioner "local-exec" {
command = "echo '${template_file.oc-id.rendered}' > oc-id.txt"
}
provisioner "local-exec" {
command = "scp oc-id.txt ubuntu@${aws_instance.chef_server.public_ip}:."
}
provisioner "local-exec" {
command = "ssh ubuntu@${aws_instance.chef_server.public_ip} 'sudo cat oc-id.txt >> /etc/opscode/chef-server.rb'"
}
provisioner "local-exec" {
command = "ssh ubuntu@${aws_instance.chef_server.public_ip} 'sudo chef-server-ctl reconfigure'"
}
}
resource "null_resource" "update-supermarket-databag" {
depends_on = ["aws_instance.supermarket_server", "template_file.oc-id", "null_resource.supermarket-chef-setup"]
# Changes ownership of /etc/opscode/oc-id-applications/supermarket.json on the Chef Server
# So it can be pulled down to the local workstation using the ubuntu user
provisioner "local-exec" {
command = "ssh ubuntu@${aws_instance.chef_server.public_ip} 'sudo chown ubuntu /etc/opscode/oc-id-applications/supermarket.json'"
}
provisioner "local-exec" {
command = "scp ubuntu@${aws_instance.chef_server.public_ip}:/etc/opscode/oc-id-applications/supermarket.json ."
}
# Add comma to the end of the chef_server_url line
provisioner "local-exec" {
command = "perl -pi -e 's/(\"chef_server_url\".*)$/$1,/' databags/apps/supermarket.json"
}
# Remove ending bracket from data bag
provisioner "local-exec" {
command = "sed -i s/}// databags/apps/supermarket.json"
}
# Extract uid from supermarket.json (NOTE the comma at the end of the regex)
provisioner "local-exec" {
command = "grep -Po '\"uid\".*?[^\\\\]\",' supermarket.json >> uid.txt"
}
# Add uid to supermarket databag
provisioner "local-exec" {
command = "cat uid.txt >> databags/apps/supermarket.json"
}
# Extract secret from supermarket.json
provisioner "local-exec" {
command = "grep -Po '\"secret\".*?[^\\\\]\"(?=,)' supermarket.json > secret.txt"
}
# Add secret to supermarket databag
provisioner "local-exec" {
command = "cat secret.txt >> databags/apps/supermarket.json"
}
# Adds a closing bracket to the end of the data bag file
provisioner "local-exec" {
command = "echo \"}\" >> databags/apps/supermarket.json"
}
# Uploads the new Supermarket data bag
provisioner "local-exec" {
command = "knife data bag from file apps databags/apps/supermarket.json"
}
}
resource "null_resource" "configure-supermarket-node-run-list" {
depends_on = ["aws_instance.supermarket_server", "null_resource.update-supermarket-databag"]
provisioner "local-exec" {
command = "knife node run_list add supermarket-node 'recipe[supermarket-wrapper::default]'"
}
}
resource "null_resource" "supermarket-node-client" {
depends_on = ["aws_instance.supermarket_server", "null_resource.configure-supermarket-node-run-list"]
provisioner "local-exec" {
command = "ssh ubuntu@${aws_instance.supermarket_server.public_ip} 'sudo chef-client'"
}
}