| 1 |
local apt = import '../cmd/apt.libsonnet'; |
| 2 |
local cmd = import '../cmd/cmd.libsonnet'; |
| 3 |
local base = import 'base.libsonnet'; |
| 4 |
|
| 5 |
{ |
| 6 |
// A helper for defining Micromange credentials for a Google environment. |
| 7 |
Credentials:: { |
| 8 |
kind: 'Google', |
| 9 |
project: error "Google credentials must have 'project'", |
| 10 |
// A path to a file on disk where the service account can be found. |
| 11 |
serviceAccount: error "Google credentials must have 'serviceAccount'", |
| 12 |
// Packer uses ssh to run commands on the instance during an image build. |
| 13 |
sshUser: error "Google credentials must have 'sshUser'", |
| 14 |
// Unused. |
| 15 |
region: 'us-central1', |
| 16 |
}, |
| 17 |
|
| 18 |
|
| 19 |
// A helper for building a Google image with Packer. |
| 20 |
Image:: { |
| 21 |
|
| 22 |
// The image from which we derive a new image. |
| 23 |
source: error "Google Image must have 'source'", |
| 24 |
|
| 25 |
// The kind of instance used to build the new image. |
| 26 |
machineType: 'n1-standard-1', |
| 27 |
|
| 28 |
// The zone in which to deploy the instance that builds the image. |
| 29 |
zone: 'us-central1-f', |
| 30 |
|
| 31 |
// The commands that are executed to build the new image. |
| 32 |
cmds: [], |
| 33 |
}, |
| 34 |
|
| 35 |
|
| 36 |
// A higher level image config that lets you add Apt and Pip packages. |
| 37 |
DebianImage:: $.Image + apt.Mixin + apt.PipMixin { |
| 38 |
source: 'debian-9-stretch-v20191121', |
| 39 |
}, |
| 40 |
|
| 41 |
|
| 42 |
// A base instance config for a GCE instance resource in Terraform. It sets up: |
| 43 |
// - Default service account scopes. |
| 44 |
// - Default values for the network and machine type. |
| 45 |
// - A default image |
| 46 |
// - Basic system-level monitoring / logging |
| 47 |
// Instances are intended to be created in services, so the service must be provided as an |
| 48 |
// argument here. |
| 49 |
StandardInstance(service):: { |
| 50 |
local instance = self, |
| 51 |
machine_type: 'f1-micro', |
| 52 |
|
| 53 |
// Either the string 'default' or create a hard dependency on a custom network, use |
| 54 |
// somenetwork.nameRef, where somenetwork evaluates to a service derived from $.Network. |
| 55 |
networkName:: 'default', |
| 56 |
|
| 57 |
// Commands that are run when the instance is booted for the first time. |
| 58 |
// This is useful for deploying configuration files and other artefacts. |
| 59 |
cmds: [], |
| 60 |
|
| 61 |
// Commands that are run every boot. This is useful for running processes. |
| 62 |
bootCmds: [], |
| 63 |
|
| 64 |
// The default network interface has a generated external ip. |
| 65 |
network_interface: { |
| 66 |
network: instance.networkName, |
| 67 |
access_config: { |
| 68 |
}, |
| 69 |
}, |
| 70 |
|
| 71 |
// The capabiltiies of the service account whose credentials are available within the instance. |
| 72 |
scopes:: ['cloud-platform'], |
| 73 |
|
| 74 |
// Use the 'scopes' field as an easier way to override the default service account scopes. |
| 75 |
service_account: [ |
| 76 |
{ |
| 77 |
scopes: ['https://www.googleapis.com/auth/' + s for s in instance.scopes], |
| 78 |
}, |
| 79 |
], |
| 80 |
|
| 81 |
// By default, the root disk is built at VM creation time from the StandardRootImage |
| 82 |
// which can be overridden at the top level to e.g. add commands or packages. |
| 83 |
boot_disk: { |
| 84 |
initialize_params: { |
| 85 |
image: instance.StandardRootImage, |
| 86 |
}, |
| 87 |
}, |
| 88 |
|
| 89 |
// By default the OS is debian and has support for monitoring and logging agents. |
| 90 |
StandardRootImage:: $.DebianImage + instance.MonitoringLoggingImageMixin, |
| 91 |
|
| 92 |
supportsLogging:: true, |
| 93 |
supportsMonitoring:: true, |
| 94 |
supportsJmxMonitoring:: true, |
| 95 |
enableLogging:: false, |
| 96 |
enableMonitoring:: false, |
| 97 |
enableJmxMonitoring:: false, |
| 98 |
jmxHost:: 'localhost', |
| 99 |
jmxPort:: 9012, |
| 100 |
jmxHotspotConfig:: { |
| 101 |
host: instance.jmxHost, |
| 102 |
port: instance.jmxPort, |
| 103 |
numQueryThreads: 2, |
| 104 |
SdQuery:: { |
| 105 |
outputWriters: [ |
| 106 |
{ |
| 107 |
'@class': 'com.googlecode.jmxtrans.model.output.StackdriverWriter', |
| 108 |
settings: { |
| 109 |
token: 'STACKDRIVER_API_KEY', |
| 110 |
detectInstance: 'GCE', |
| 111 |
url: 'https://jmx-gateway.google.stackdriver.com/v1/custom', |
| 112 |
}, |
| 113 |
}, |
| 114 |
], |
| 115 |
}, |
| 116 |
queries: [ |
| 117 |
self.SdQuery { |
| 118 |
resultAlias: 'jvm.localhost.Threading', |
| 119 |
obj: 'java.lang:type=Threading', |
| 120 |
attr: ['DaemonThreadCount', 'ThreadCount', 'PeakThreadCount'], |
| 121 |
}, |
| 122 |
self.SdQuery { |
| 123 |
resultAlias: 'jvm.localhost.Memory', |
| 124 |
obj: 'java.lang:type=Memory', |
| 125 |
attr: ['HeapMemoryUsage', 'NonHeapMemoryUsage'], |
| 126 |
}, |
| 127 |
self.SdQuery { |
| 128 |
resultAlias: 'jvm.localhost.Runtime', |
| 129 |
obj: 'java.lang:type=Runtime', |
| 130 |
attr: ['Uptime'], |
| 131 |
}, |
| 132 |
self.SdQuery { |
| 133 |
resultAlias: 'jvm.localhost.os', |
| 134 |
obj: 'java.lang:type=OperatingSystem', |
| 135 |
attr: [ |
| 136 |
'CommittedVirtualMemorySize', |
| 137 |
'FreePhysicalMemorySize', |
| 138 |
'FreeSwapSpaceSize', |
| 139 |
'OpenFileDescriptorCount', |
| 140 |
'ProcessCpuTime', |
| 141 |
'SystemLoadAverage', |
| 142 |
], |
| 143 |
}, |
| 144 |
self.SdQuery { |
| 145 |
resultAlias: 'jvm.localhost.gc', |
| 146 |
obj: 'java.lang:type=GarbageCollector,name=*', |
| 147 |
attr: ['CollectionCount', 'CollectionTime'], |
| 148 |
}, |
| 149 |
], |
| 150 |
}, |
| 151 |
jmxLocalhostConfig:: instance.jmxHotspotConfig, |
| 152 |
jmxConfig:: { |
| 153 |
servers: [ |
| 154 |
instance.jmxLocalhostConfig, |
| 155 |
], |
| 156 |
}, |
| 157 |
|
| 158 |
MonitoringLoggingImageMixin:: { |
| 159 |
local monitoring = |
| 160 |
if instance.enableMonitoring then [ |
| 161 |
'curl -s https://dl.google.com/cloudagents/install-monitoring-agent.sh | bash', |
| 162 |
] else [], |
| 163 |
local jmx = |
| 164 |
if instance.enableJmxMonitoring then [ |
| 165 |
'mkdir -p /opt/jmxtrans/{conf,log}', |
| 166 |
'curl https://repo.stackdriver.com/jmxtrans/jmxtrans-all.jar -o /opt/jmxtrans/jmxtrans-all.jar', |
| 167 |
cmd.LiteralFile { |
| 168 |
to: '/etc/cron.d/jmxtrans', |
| 169 |
content: '@reboot root /usr/bin/java -Djmxtrans.log.dir=/opt/jmxtrans/log -jar /opt/jmxtrans/jmxtrans-all.jar -j /opt/jmxtrans/conf/ &\n', |
| 170 |
filePermissions: '700', |
| 171 |
}, |
| 172 |
cmd.LiteralFile { |
| 173 |
to: '/opt/jmxtrans/conf/jmx.json', |
| 174 |
content: std.toString(instance.jmxConfig), |
| 175 |
}, |
| 176 |
] else [], |
| 177 |
local logging = |
| 178 |
if instance.enableLogging then [ |
| 179 |
'curl -s https://dl.google.com/cloudagents/install-logging-agent.sh | bash', |
| 180 |
] else [], |
| 181 |
cmds+: monitoring + jmx + logging, |
| 182 |
}, |
| 183 |
|
| 184 |
// This Terraform policy allows changing some properties by stopping the instance. |
| 185 |
allow_stopping_for_update: true, |
| 186 |
|
| 187 |
// By default, the only tag is the name of the service owning the instance. |
| 188 |
tags: [service.fullName], |
| 189 |
|
| 190 |
// By default there is no metadata (startup scripts are managed by micromanage so do not appear |
| 191 |
// here. |
| 192 |
metadata: {}, |
| 193 |
}, |
| 194 |
|
| 195 |
|
| 196 |
// The base class of all Google services. This provides support for automatic but optional |
| 197 |
// creation of DNS names for any instances or address resources with external IPs. |
| 198 |
Service(outer, name): base.Service(outer, name) { |
| 199 |
local service = self, |
| 200 |
infrastructure+: if self.dnsZone == null then { |
| 201 |
// Don't do anything if we have no DNS zone. |
| 202 |
} else { |
| 203 |
local infra = self, |
| 204 |
local DnsRecord = { |
| 205 |
managed_zone: service.dnsZone.nameRef, |
| 206 |
type: 'A', |
| 207 |
ttl: 300, |
| 208 |
}, |
| 209 |
// Ensure the fields exist, since we access them below. |
| 210 |
google_compute_instance+: { }, |
| 211 |
google_compute_address+: { }, |
| 212 |
// Add a record for every address and instance |
| 213 |
google_dns_record_set+: { |
| 214 |
[name]: DnsRecord { |
| 215 |
name: '${google_compute_instance.' + name + '.name}.' + service.dnsZone.dnsName, |
| 216 |
rrdatas: [ |
| 217 |
'${google_compute_instance.' + name + '.network_interface.0.access_config.0.nat_ip}', |
| 218 |
], |
| 219 |
// Use a short TTL for instance addresses, because instances churn a fair amount and the |
| 220 |
// majority of traffic does not go to specific instances but load balancer frontends. |
| 221 |
ttl: 5, |
| 222 |
} |
| 223 |
for name in std.objectFields(infra.google_compute_instance) |
| 224 |
} + { |
| 225 |
// Note that if an address and an instance has the same name, the domain will go to the |
| 226 |
// address's IP, not the instance's IP. |
| 227 |
[name]: DnsRecord { |
| 228 |
name: '${google_compute_address.' + name + '.name}.' + service.dnsZone.dnsName, |
| 229 |
rrdatas: ['${google_compute_address.' + name + '.address}'], |
| 230 |
} |
| 231 |
for name in std.objectFields(infra.google_compute_address) |
| 232 |
}, |
| 233 |
}, |
| 234 |
|
| 235 |
// A reference to a service which is derived from $.DnsZone, or null. |
| 236 |
dnsZone:: null, |
| 237 |
}, |
| 238 |
|
| 239 |
|
| 240 |
// A custom network into which other services can be deployed. To put a resource in this network |
| 241 |
// use the .nameRef field, which tells Terraform to create the network first. |
| 242 |
Network(outer, name):: $.Service(outer, name) { |
| 243 |
local service = self, |
| 244 |
nameRef:: '${google_compute_network.%s.name}' % service.fullName, |
| 245 |
infrastructure: { |
| 246 |
google_compute_network: { |
| 247 |
[service.fullName]: { |
| 248 |
name: service.fullName, |
| 249 |
}, |
| 250 |
}, |
| 251 |
}, |
| 252 |
}, |
| 253 |
|
| 254 |
|
| 255 |
// A base class for a service whose compute facility is provided by instances. This supports: |
| 256 |
// - Optional automatic creation of firewall rules that assume the instances in this service |
| 257 |
// have the service's full name as a tag (by default, this is the case). |
| 258 |
// - Creation of a reserved address for the front end of the service, which might be a load |
| 259 |
// balancer or a VM. |
| 260 |
InstanceBasedService(outer, name): $.Service(outer, name) { |
| 261 |
local service = self, |
| 262 |
|
| 263 |
// The firewall rule generation can be turned off here. This is useful if your firewalls |
| 264 |
// are centrally managed using tags. |
| 265 |
perServiceFirewalls:: true, |
| 266 |
// Ports to allow through the firewall. |
| 267 |
fwTcpPorts:: [22], |
| 268 |
fwUdpPorts:: [], |
| 269 |
|
| 270 |
// Either the string 'default' or create a hard dependency on a custom network, use |
| 271 |
// somenetwork.nameRef, where somenetwork evaluates to a service derived from $.Network. |
| 272 |
networkName:: 'default', |
| 273 |
|
| 274 |
// A base class for the instances that will be used in this service. This can be extended |
| 275 |
// to customise the instances. The networkName is conveniently populated since it must |
| 276 |
// be the same for all instances in the service. |
| 277 |
// |
| 278 |
// Note that InstanceBasedService does not actually create any instances, it's up to sub-classes |
| 279 |
// to do that. But they can use (and extend) this onfiguration when they do so. |
| 280 |
Instance:: $.StandardInstance(service) { |
| 281 |
networkName: service.networkName, |
| 282 |
}, |
| 283 |
|
| 284 |
// Use this when you depend on this service to access the generated IP address. This creates |
| 285 |
// an ordering dependency in Terraform because the IP address is not known until deployment |
| 286 |
// of this service has begun. |
| 287 |
addressRef:: '${google_compute_address.%s.address}' % service.fullName, |
| 288 |
|
| 289 |
infrastructure+: { |
| 290 |
google_compute_address: { |
| 291 |
// The single frontend IP for this service. |
| 292 |
[service.fullName]: { name: service.fullName }, |
| 293 |
}, |
| 294 |
google_compute_firewall: if !service.perServiceFirewalls then {} else { |
| 295 |
[service.fullName]: { |
| 296 |
name: service.fullName, |
| 297 |
source_ranges: ['0.0.0.0/0'], |
| 298 |
network: service.networkName, |
| 299 |
allow: [{ protocol: 'tcp', ports: [std.toString(p) for p in service.fwTcpPorts] }], |
| 300 |
target_tags: [service.fullName], |
| 301 |
}, |
| 302 |
}, |
| 303 |
google_compute_instance: error 'InstanceBasedService should define some instances.', |
| 304 |
}, |
| 305 |
}, |
| 306 |
|
| 307 |
|
| 308 |
// A service that is provided by many instances behind a network load balancer. This supports: |
| 309 |
// - Automatic creation of the loadbalancer resources on whatever ports you need and bound to the |
| 310 |
// reserved IP address from $.InstanceBasedService (referencable with .addressRef). |
| 311 |
// - Health checking for the network load balancer. |
| 312 |
// - Management of a set of VMs across a range of zones (round robin). |
| 313 |
// - The facility to "Drain" VMs on demand by removing them from the load balancer. |
| 314 |
// - Canarying new instance configurations or blue / green deployment strategies. |
| 315 |
Cluster3(outer, name): self.InstanceBasedService(outer, name) { |
| 316 |
local service = self, |
| 317 |
lbTcpPorts:: [], |
| 318 |
lbUdpPorts:: [], |
| 319 |
httpHealthCheckPort:: 80, |
| 320 |
zones:: error 'Cluster3 version (or service) needs an array of zones.', |
| 321 |
|
| 322 |
Instance+: { |
| 323 |
// Creating a layer of indirection through the instance |
| 324 |
zones:: service.zones, |
| 325 |
}, |
| 326 |
|
| 327 |
// This is a map from version code to instance configuration. These are the versions that may |
| 328 |
// be deployed. |
| 329 |
versions:: {}, |
| 330 |
|
| 331 |
// Which versions are deployed. This is a map from version code to an object with fields |
| 332 |
// 'deployed' and 'attached'. Those must hold arrays of integers, indicating which VM indexes |
| 333 |
// within that version are deployed or attached. The instance names are generated as so: |
| 334 |
// ${service_name}-${version_name}-${index} |
| 335 |
deployment:: {}, |
| 336 |
|
| 337 |
local instances = std.foldl(function(a, b) a + b, [ |
| 338 |
{ |
| 339 |
[service.prefixName('%s-%d' % [vname, i])]: |
| 340 |
if std.objectHas(service.versions, vname) then |
| 341 |
service.versions[vname] { |
| 342 |
name: service.prefixName('%s-%d' % [vname, i]), |
| 343 |
zone: self.zones[i % std.length(self.zones)], |
| 344 |
tags+: [vname, 'index-%d' % i], |
| 345 |
} |
| 346 |
else |
| 347 |
error 'Undefined version: %s' % vname |
| 348 |
for i in std.set(service.deployment[vname].deployed) |
| 349 |
} |
| 350 |
for vname in std.objectFields(service.deployment) |
| 351 |
], {}), |
| 352 |
|
| 353 |
local attached_instances = std.join( |
| 354 |
[], [ |
| 355 |
local attached = std.set(service.deployment[vname].attached); |
| 356 |
local deployed = std.set(service.deployment[vname].deployed); |
| 357 |
[service.prefixName('%s-%d' % [vname, i]) for i in std.setInter(attached, deployed)] |
| 358 |
for vname in std.objectFields(service.deployment) |
| 359 |
] |
| 360 |
), |
| 361 |
|
| 362 |
infrastructure+: { |
| 363 |
google_compute_http_health_check: { |
| 364 |
[service.fullName]: { |
| 365 |
name: service.fullName, |
| 366 |
port: service.httpHealthCheckPort, |
| 367 |
}, |
| 368 |
}, |
| 369 |
google_compute_target_pool: { |
| 370 |
[service.fullName]: { |
| 371 |
name: service.fullName, |
| 372 |
depends_on: ['google_compute_http_health_check.' + service.fullName], |
| 373 |
health_checks: [service.fullName], |
| 374 |
instances: [ |
| 375 |
'%s/%s' % [instances[iname].zone, iname] |
| 376 |
for iname in attached_instances |
| 377 |
], |
| 378 |
}, |
| 379 |
}, |
| 380 |
google_compute_forwarding_rule: { |
| 381 |
[service.prefixName(port)]: { |
| 382 |
name: service.prefixName(port), |
| 383 |
ip_address: '${google_compute_address.%s.address}' % service.fullName, |
| 384 |
target: '${google_compute_target_pool.%s.self_link}' % service.fullName, |
| 385 |
port_range: port, |
| 386 |
} |
| 387 |
for port in [std.toString(p) for p in service.lbTcpPorts] |
| 388 |
}, |
| 389 |
google_compute_instance: instances, |
| 390 |
}, |
| 391 |
|
| 392 |
outputs+: { |
| 393 |
[service.prefixName('addr')]: service.addressRef, |
| 394 |
}, |
| 395 |
}, |
| 396 |
|
| 397 |
|
| 398 |
// A service that is provided by a single instance. This is less reliable than $.Cluster3 but is |
| 399 |
// useful for testing or other non-highly available services. It supports: |
| 400 |
// - Management of the single instance on the frontend address created by $.InstanceBasedService. |
| 401 |
// This IP does not change when the instance is rebuilt. |
| 402 |
SingleInstance(outer, name): self.InstanceBasedService(outer, name) { |
| 403 |
local service = self, |
| 404 |
// Control the zone at the top-level. |
| 405 |
zone:: error 'SingleInstance needs a zone.', |
| 406 |
|
| 407 |
Instance+: { |
| 408 |
zone: service.zone, |
| 409 |
network_interface+: { |
| 410 |
access_config: { |
| 411 |
// Use the service's predefined IP address. |
| 412 |
nat_ip: '${google_compute_address.%s.address}' % service.fullName, |
| 413 |
}, |
| 414 |
}, |
| 415 |
}, |
| 416 |
|
| 417 |
infrastructure+: { |
| 418 |
google_compute_instance: { |
| 419 |
[service.fullName]: service.Instance { name: service.fullName, }, |
| 420 |
}, |
| 421 |
}, |
| 422 |
|
| 423 |
outputs+: { |
| 424 |
[service.prefixName('addr')]: service.addressRef, |
| 425 |
}, |
| 426 |
}, |
| 427 |
|
| 428 |
|
| 429 |
// A service that manages a DNS zone, which can then be used by other services to expose their |
| 430 |
// addresses via DNS. The DNS zone should be recreated rarely, because its domain name servers |
| 431 |
// will change and then upstream NS record will need to be updated. However, the records within |
| 432 |
// the zone can change at any time as the set of mapped IP addresses changes. |
| 433 |
DnsZone(outer, name):: self.Service(outer, name) { |
| 434 |
local service = self, |
| 435 |
// The suffix that some authority will delegate to the generated nameserves via a NS record. |
| 436 |
// All domain names managed by this zone will have the same suffix. |
| 437 |
dnsName:: error 'DnsZone must have dnsName, e.g. example.com', |
| 438 |
|
| 439 |
// Use this reference to ensure Terraform deploys the DNS zone before services that need to |
| 440 |
// register domain names in it. |
| 441 |
nameRef:: '${google_dns_managed_zone.%s.name}' % self.fullName, |
| 442 |
|
| 443 |
// A convenient field to override the description in the underlying resource. |
| 444 |
description:: 'Zone for ' + self.dnsName, |
| 445 |
|
| 446 |
infrastructure+: { |
| 447 |
google_dns_managed_zone: { |
| 448 |
[service.fullName]: { |
| 449 |
name: service.fullName, |
| 450 |
dns_name: service.dnsName, |
| 451 |
description: service.description, |
| 452 |
}, |
| 453 |
}, |
| 454 |
}, |
| 455 |
|
| 456 |
outputs+: { |
| 457 |
// The first of the set of nameservers, which can be used to configure the upstream authority |
| 458 |
// to delegate to the new DNS zone via an NS record. |
| 459 |
[service.prefixName('name_servers')]: |
| 460 |
'${google_dns_managed_zone.%s.name_servers.0}' % service.fullName, |
| 461 |
}, |
| 462 |
}, |
| 463 |
|
| 464 |
|
| 465 |
// A simple service that creates a domain name alias from www.foo to target.foo, where target is |
| 466 |
// the front end IP of some other service. This allows designating the front end of your |
| 467 |
// application, and allowing it to be accessed via www.foo instead of servicename.foo. |
| 468 |
DnsRecordWww(outer, name):: self.Service(outer, name) { |
| 469 |
local service = self, |
| 470 |
// A service derived from $.DnsZone. |
| 471 |
zone:: error 'DnsRecordWww requires zone.', |
| 472 |
|
| 473 |
// The name of the service within the zone, e.g. 'servicename' without the '.foo' suffix. |
| 474 |
target:: error 'DnsRecordWww requires target.', |
| 475 |
|
| 476 |
infrastructure+: { |
| 477 |
google_dns_record_set: { |
| 478 |
[service.fullName]: { |
| 479 |
managed_zone: service.zone.nameRef, |
| 480 |
name: 'www.' + service.zone.dnsName, |
| 481 |
type: 'CNAME', |
| 482 |
ttl: 300, |
| 483 |
rrdatas: [service.target + '.' + service.zone.dnsName], |
| 484 |
}, |
| 485 |
}, |
| 486 |
}, |
| 487 |
}, |
| 488 |
} |