-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
167 lines (125 loc) · 5.76 KB
/
main.tf
File metadata and controls
167 lines (125 loc) · 5.76 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
module "chef-server" {
source = "github.com/NetworkBytes/tf-module-aws-vm"
config = "${local.config}"
}
resource "null_resource" "chef-server_configure" {
triggers {
chef-server_instance_ids = "${join(",", module.chef-server.id)}"
}
connection {
host = "${local.public_ip}"
user = "${local.user}"
private_key = "${file(pathexpand(local.key_file_private))}"
}
provisioner "local-exec" {
command = <<-EOF
set -e
echo "** Gererate encrypted_data_bag_secret"
rm -f .chef/encrypted_data_bag_secret
openssl rand -base64 512 | tr -d '\r\n' > .chef/encrypted_data_bag_secret
echo "** Generating ssl certificate"
openssl req -x509 -newkey rsa:4096 \
-keyout .chef/${var.instance["hostname"]}.${var.instance["domain"]}.key \
-out .chef/${var.instance["hostname"]}.${var.instance["domain"]}.pem \
-days 1000 -nodes -subj "/CN=${var.instance["hostname"]}.${var.instance["domain"]}"
EOF
}
provisioner "remote-exec" {
inline = "[[ -d .chef ]] || mkdir -p .chef; echo Created .chef directory"
}
## Put certificate key
provisioner "file" {
source = "${local.chef_ssl_private_key}"
destination = ".chef/${var.instance["hostname"]}.${var.instance["domain"]}.key"
}
# Put certificate
provisioner "file" {
source = "${local.chef_ssl_cert}"
destination = ".chef/${var.instance["hostname"]}.${var.instance["domain"]}.pem"
}
# Upload Attributes file .chef/dna.json for the chef-solo run
provisioner "file" {
content = "${data.template_file.attributes-json.rendered}"
destination = ".chef/dna.json"
}
# install and configure
provisioner "remote-exec" {
inline = <<-EOF
set -e
#TODO centos aws image doesnt have any FW enabled
#echo ** Disabling firewall
#sudo systemctl disable firewalld
#sudo systemctl stop firewalld
echo "** Installing Chef client version ${var.chef_versions["client"]}"
curl -L https://omnitruck.chef.io/install.sh | sudo bash -s -- -v ${var.chef_versions["client"]}
#curl -L https://omnitruck.chef.io/install.sh | sudo bash
echo "** Preparing directories"
for DIR in cookbooks cache ssl; do sudo rm -rf /var/chef/$DIR; sudo mkdir -vp /var/chef/$DIR; done
echo "** Copying certs /${var.instance["hostname"]}.${var.instance["domain"]} to /var/chef/ssl"
sudo cp -f .chef/${var.instance["hostname"]}.${var.instance["domain"]}.* /var/chef/ssl/
echo "** Downloading cookbooks"
for DEP in chef-client chef-server chef-ingredient; do
echo " - downloading cookbook $DEP"
curl -sL https://supermarket.chef.io/cookbooks/$DEP/download | sudo tar --warning=no-unknown-keyword -xzC /var/chef/cookbooks;
done
echo "** Running chef-solo to install Chef server"
#sudo chef-solo -j .chef/dna.json -o 'recipe[system::default],recipe[chef-server::default],recipe[chef-server::addons]'
sudo chef-solo -j ~${local.user}/.chef/dna.json
echo "** Creating Chef user and org"
sudo chef-server-ctl org-list |grep -q ${var.chef_org["short"]} && sudo chef-server-ctl org-delete ${var.chef_org["short"]} -y
sudo chef-server-ctl user-list |grep -q ${var.chef_user["username"]} && sudo chef-server-ctl user-delete ${var.chef_user["username"]} -y
sudo chef-server-ctl user-create ${var.chef_user["username"]} ${var.chef_user["first"]} ${var.chef_user["last"]} ${var.chef_user["email"]} ${base64sha256(self.id)} -f .chef/${var.chef_user["username"]}.pem
sudo chef-server-ctl org-create ${var.chef_org["short"]} '${var.chef_org["long"]}' --association_user ${var.chef_user["username"]} --filename .chef/${var.chef_org["short"]}-validator.pem
echo "** Correct ownership on .chef so we can scp the files"
sudo chown -R ${local.user} .chef
echo "** Berkshelf Gem"
sudo yum install -y gcc
sudo /opt/chef/embedded/bin/gem install berkshelf
EOF
}
# Copy things back to local
provisioner "local-exec" {
command = <<-EOF
set -e
echo "** Copy .chef files to local terraform directory"
scp -r -o stricthostkeychecking=no -i ${local.key_file_private} ${local.user}@${local.public_ip}:.chef/* .chef/
echo "** Replace local .chef/user.pem file with generated one"
cp -f .chef/${var.chef_user["username"]}.pem .chef/user.pem
echo "** Generate knife.rb"
cat > .chef/knife.rb <<-EOK
${data.template_file.knife-rb.rendered}
EOK
echo Write generated template file
cat > .chef/chef-server.creds <<-EOC
${data.template_file.chef-server-creds.rendered}
EOC
EOF
}
# Upload knife.rb to chef server
provisioner "file" {
source = ".chef/knife.rb"
destination = ".chef/knife.rb"
}
provisioner "remote-exec" {
inline = <<-EOF
set -e
echo "** Fetching the ssl cert from the server"
knife ssl fetch
echo "** Uploading cookbooks into the Chef Server"
sudo knife cookbook upload -a -c .chef/knife.rb --cookbook-path /var/chef/cookbooks
sudo rm -rf /var/chef/cookbooks
EOF
}
# Provision with Chef
provisioner "chef" {
attributes_json = "${data.template_file.attributes-json.rendered}"
environment = "_default"
log_to_file = "${var.chef_log}"
node_name = "${local.public_dns}"
run_list = ["recipe[chef-client::default]"] #,"recipe[system::default]","recipe[chef-client::config]","recipe[chef-client::cron]","recipe[chef-client::delete_validation]","recipe[chef-server::default]","recipe[chef-server::addons]"]
server_url = "https://${local.public_dns}/organizations/${var.chef_org["short"]}"
skip_install = true
user_name = "${var.chef_user["username"]}"
user_key = "${file(".chef/user.pem")}"
}
}