-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrist.pkr.hcl
197 lines (170 loc) · 4.75 KB
/
grist.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
# Building an Ubuntu image with Grist for both AWS and DigitalOcean
packer {
required_plugins {
digitalocean = {
version = ">= 1"
source = "github.com/digitalocean/digitalocean"
}
amazon = {
version = ">= 1"
source = "github.com/hashicorp/amazon"
}
azure = {
source = "github.com/hashicorp/azure"
version = "~> 2"
}
}
}
variable "aws_access_key" {
type = string
default = ""
}
variable "aws_secret_key" {
type = string
default = ""
}
variable "do_token" {
type = string
default = ""
}
variable "azure_tenant_id" {
type = string
default = ""
}
variable "azure_subscription_id" {
type = string
default = ""
}
variable "azure_client_id" {
type = string
default = ""
}
variable "azure_client_secret" {
type = string
default = ""
}
variable "azure_resource_group" {
type = string
default = "Grist"
}
variable "do_image" {
type = string
default = "ubuntu-24-04-x64"
}
variable "aws_image_filter" {
type = string
default = "ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*"
}
locals {
timestamp = formatdate("YYYY-MM-DD-hhmm", timestamp())
enabled_sources = flatten([
var.aws_access_key != "" && var.aws_secret_key != "" ? ["source.amazon-ebs.ubuntu"] : [],
var.do_token != "" ? ["source.digitalocean.ubuntu"] : [],
var.azure_tenant_id != ""
&& var.azure_subscription_id != ""
&& var.azure_client_id != ""
&& var.azure_client_secret != "" ? ["source.azure-arm.ubuntu"] : []
])
}
source "amazon-ebs" "ubuntu" {
ami_name = "grist-marketplace-${local.timestamp}"
instance_type = "t2.micro"
region = "us-east-1"
source_ami_filter {
filters = {
name = var.aws_image_filter
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["099720109477"] # Canonical's official Ubuntu AMIs
}
ssh_username = "ubuntu"
access_key = var.aws_access_key
secret_key = var.aws_secret_key
user_data_file = ""
ssh_clear_authorized_keys = true
}
source "digitalocean" "ubuntu" {
image = var.do_image
region = "nyc3"
size = "s-1vcpu-1gb"
api_token = var.do_token
ssh_username = "root"
snapshot_name = "grist-marketplace-${local.timestamp}"
ssh_clear_authorized_keys = true
}
source "azure-arm" "ubuntu" {
subscription_id = var.azure_subscription_id
client_id = var.azure_client_id
client_secret = var.azure_client_secret
tenant_id = var.azure_tenant_id
location = "Central US"
vm_size = "Standard_B2s"
image_publisher = "Canonical"
image_offer = "ubuntu-24_04-lts"
image_sku = "server"
managed_image_resource_group_name = var.azure_resource_group
managed_image_name = "grist-marketplace-${local.timestamp}"
os_type = "Linux"
communicator = "ssh"
ssh_username = "packer"
}
build {
sources = local.enabled_sources
# Pack up and send the dist
provisioner "shell-local" {
inline = [
"tar --transform 's/^dist/grist-dist/' --exclude dist/persist -czvf grist-dist.tar.gz dist/"
]
}
provisioner "file" {
source = "grist-dist.tar.gz"
destination = "/tmp/"
generated = true
}
provisioner "shell" {
inline = [
"cd /tmp/",
"tar xvf grist-dist.tar.gz",
"rm grist-dist.tar.gz"
]
}
provisioner "shell" {
# Wait for startup to finish (ignore error code)
inline = ["cloud-init status --wait || true"]
}
provisioner "shell" {
# Because the default AWS image doesn't run as root but DO does,
# easier to just sudo in both. So change the default
# execute_command
execute_command = "sudo bash -xc '{{ .Vars }} {{ .Path }}'"
scripts = [
"scripts/install-docker",
"scripts/setup-grist-dist",
"scripts/setup-ufw",
"scripts/setup-systemd",
"scripts/setup-login-user",
"scripts/cleanup",
]
}
provisioner "shell" {
script = "scripts/digitalocean-img-check"
only = ["digitalocean.ubuntu_do"]
}
provisioner "shell" {
execute_command = "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'"
inline = [
"/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync"
]
inline_shebang = "/bin/sh -x"
only = ["azure-arm.ubuntu"]
}
# Need to figure out the story for making it easy to mount volumes for AWS
# and DO. They each handle it differently.
# We can use this to keep track of what images have been built,
# helps to keep track in case we want to clean them up.
post-processor "manifest" {
output = "manifest.json"
}
}