-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.pkr.hcl
206 lines (181 loc) · 5.94 KB
/
config.pkr.hcl
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
packer {
required_plugins {
amazon = {
version = ">= 1.2.6"
source = "github.com/hashicorp/amazon"
}
digitalocean = {
version = ">= 1.1.1"
source = "github.com/hashicorp/digitalocean"
}
}
}
locals {
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
}
variable "rocketchat_version" {
type = string
default = "latest"
}
variable "aws_key_id" {
type = string
default = ""
}
variable "aws_secret_key" {
type = string
default = ""
}
variable "do_token" {
type = string
default = ""
}
variable "do_size" {
type = string
default = "s-1vcpu-1gb"
}
variable "do_region" {
type = string
default = "nyc3"
}
locals {
image_name = "rocket-chat-${var.rocketchat_version}-${local.timestamp}"
}
source "amazon-ebs" "aws-ami" {
access_key = "${var.aws_key_id}"
ami_name = "${local.image_name}"
instance_type = "t2.micro"
region = "us-east-1"
secret_key = "${var.aws_secret_key}"
source_ami = "ami-0866a3c8686eaeeba" // 24.04 lts
ssh_username = "ubuntu"
}
source "digitalocean" "do-marketplace" {
api_token = "${var.do_token}"
snapshot_name = "${local.image_name}"
size = "s-1vcpu-1gb-amd"
region = "blr1"
image = "ubuntu-20-04-x64"
ssh_username = "root"
}
build {
sources = [
"source.digitalocean.do-marketplace",
"source.amazon-ebs.aws-ami",
]
# remove old manifests if they exist
provisioner "shell-local" {
inline = [
"rm -rf manifest.json",
]
}
provisioner "shell" { // upgrade instance
pause_before = "30s"
expect_disconnect = true
inline = [
"sudo apt-get update -qqq",
"DEBIAN_FRONTEND=noninteractive sudo -E apt-get -y --allow-downgrades --allow-remove-essential --allow-change-held-packages -o Dpkg::Options::='--force-confold' -o Dpkg::Options::='--force-confdef' upgrade",
"sudo reboot",
]
}
provisioner "shell" {
inline = [
"sudo dd if=/dev/zero of=/swapfile count=512 bs=1M",
"sudo chmod 600 /swapfile",
"sudo mkswap /swapfile",
"sudo swapon /swapfile",
"echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab >/dev/null"
]
expect_disconnect = true // thanks aws . for some reason at swap creation step the connection gets frequently dropped
}
provisioner "file" {
source = "./scripts/motd.sh"
destination = "/tmp/motd.sh"
}
provisioner "shell" {
inline = [
"sudo mv -v /tmp/motd.sh /etc/update-motd.d/99-image-readme",
"sudo chmod 755 /etc/update-motd.d/99-image-readme",
"sudo sed -i 's/^PrintMotd no/PrintMotd yes/' /etc/ssh/sshd_config",
"sudo touch /etc/motd.tail",
]
}
provisioner "file" {
source = "./scripts/01-start-containers.sh"
destination = "/tmp/01-start-containers.sh"
}
provisioner "shell" {
inline = [
"sudo mv -v /tmp/01-start-containers.sh /var/lib/cloud/scripts/per-instance/01-start-containers.sh",
"sudo chmod a+x /var/lib/cloud/scripts/per-instance/01-start-containers.sh"
]
}
provisioner "shell" {
pause_before = "30s" // not needed, ideally, but sometimes auto updater hits and we hit a timeout waiting for apt to be available, FIXME: disable updater once before this step
inline = [
"DEBIAN_FRONTEND=noninteractive sudo apt-get update",
"DEBIAN_FRONTEND=noninteractive sudo apt-get install -y ufw",
"bash -c 'for allow in ssh 3000/tcp 80/tcp 443/tcp; do sudo ufw allow $allow; done'",
"sudo ufw default deny incoming",
"sudo ufw default allow outgoing",
"bash -c 'yes | sudo ufw enable'"
]
}
provisioner "shell" {
inline = [
"bash -c 'if [[ -e /etc/machine-id ]]; then sudo rm -f /etc/machine-id && sudo touch /etc/machine-id; fi'",
"bash -c 'if [[ -e /var/lib/dbus/machine-id && ! -L /var/lib/dbus/machine-id ]]; then sudo rm -f /var/lib/dbus/machine-id; fi'"
]
}
provisioner "shell" {
inline = [
"sudo curl https://get.docker.com | sh",
"sudo mkdir rocketchat",
"sudo curl -O https://raw.githubusercontent.com/RocketChat/Docker.Official.Image/six-mongo-update/compose.yml",
"bash -c 'cd rocketchat && sudo docker compose pull'"
]
}
# DigitalOcean cleanup script (fancy and fun for all builds)
provisioner "shell" {
inline = ["wget -O- https://raw.githubusercontent.com/digitalocean/marketplace-partners/master/scripts/90-cleanup.sh | sudo bash"]
}
# Things that the previous script doesn't handle
provisioner "shell" {
only = ["digitalocean.do-marketplace"]
inline = [
"sudo rm -rf /root/.ssh",
"DEBIAN_FRONTEND=noninteractive sudo apt-get purge droplet-agent -y"
]
}
provisioner "shell" {
only = ["amazon-ebs.aws-ami"]
inline = [
"sudo rm -rf /home/ubuntu/.ssh"
]
}
# Makes sure the images are clean
provisioner "shell" {
inline = ["wget -O- https://raw.githubusercontent.com/digitalocean/marketplace-partners/master/scripts/99-img-check.sh | sudo bash || true"]
}
// post-processor "manifest" {
// only = ["digitalocean.do-marketplace"]
// output = "manifest.json"
// strip_path = true
// custom_data = {
// do_size = "${var.do_size}"
// do_region = "${var.do_region}"
// image_name = "${local.image_name}"
// }
// }
// post-processor "shell-local" {
// only = ["amazon-ebs.aws-ami"]
// inline = [
// "packer build -var 'image_name=${local.image_name}' -var 'aws_secret_key=${var.aws_secret_key}' -var 'aws_key_id=${var.aws_key_id}' -only amazon-ebs.aws-ami image_test/image_test.pkr.hcl",
// ]
// }
// post-processor "shell-local" {
// only = ["digitalocean.do-marketplace"]
// inline = [
// "packer build -var 'image_name=${local.image_name}' -var \"do_image_id=$(jq -r '.builds[] | select(.builder_type== \"digitalocean\")' manifest.json | jq -r '.artifact_id' | cut -d':' -f 2 )\" -var 'do_token=${var.do_token}' -only digitalocean.do-marketplace image_test/image_test.pkr.hcl",
// ]
// }
}