| 1 |
local amis_debian = import '../amis/debian.libsonnet'; |
| 2 |
local apt = import '../cmd/apt.libsonnet'; |
| 3 |
local cmd = import '../cmd/cmd.libsonnet'; |
| 4 |
local base = import 'base.libsonnet'; |
| 5 |
|
| 6 |
{ |
| 7 |
Credentials:: { |
| 8 |
kind: 'Amazon', |
| 9 |
accessKey: error "Amazon credentials must have 'accessKey'", |
| 10 |
secretKey: error "Amazon credentials must have 'secretKey'", |
| 11 |
region: 'us-east-1', |
| 12 |
}, |
| 13 |
|
| 14 |
|
| 15 |
Image:: { |
| 16 |
sourceAmi: error "Amazon AMI must have 'sourceAmi'", |
| 17 |
instanceType: 't2.small', |
| 18 |
sshUser: error "Amazon AMI must have 'sshUser'", |
| 19 |
cmds: [], |
| 20 |
}, |
| 21 |
|
| 22 |
|
| 23 |
DebianImage:: $.Image + apt.Mixin + apt.PipMixin { |
| 24 |
sourceAmi: amis_debian.wheezy.amd64['20150128']['us-west-1'], |
| 25 |
sshUser: 'admin', |
| 26 |
}, |
| 27 |
|
| 28 |
|
| 29 |
StandardInstance:: { |
| 30 |
local instance = self, |
| 31 |
instance_type: 't2.small', |
| 32 |
ami: instance.StandardRootImage, |
| 33 |
associate_public_ip_address: true, |
| 34 |
cmds: [], |
| 35 |
bootCmds: [], |
| 36 |
tags: {}, |
| 37 |
// TODO(dcunnin): Figure out an equivalent here. |
| 38 |
supportsLogging:: false, |
| 39 |
supportsMonitoring:: false, |
| 40 |
supportsJmxMonitoring:: false, |
| 41 |
enableLogging:: false, |
| 42 |
enableMonitoring:: false, |
| 43 |
enableJmxMonitoring:: false, |
| 44 |
MonitoringLoggingImageMixin:: { |
| 45 |
}, |
| 46 |
StandardRootImage:: $.DebianImage + instance.MonitoringLoggingImageMixin, |
| 47 |
}, |
| 48 |
|
| 49 |
|
| 50 |
Service(parent, name):: base.Service(parent, name) { |
| 51 |
// TODO: Automatic registration of domain names for instances and addresses. |
| 52 |
}, |
| 53 |
|
| 54 |
|
| 55 |
Network(parent, name):: $.Service(parent, name) { |
| 56 |
local service = self, |
| 57 |
idRef:: '${aws_vpc.%s.id}' % service.fullName, |
| 58 |
nameRef:: '${aws_vpc.%s.name}' % service.fullName, |
| 59 |
subnetIdRef(zone):: '${aws_subnet.%s-%s.id}' % [service.fullName, zone], |
| 60 |
ipv4Range:: '10.0.0.0/16', |
| 61 |
infrastructure: { |
| 62 |
aws_vpc: { |
| 63 |
[service.fullName]: { |
| 64 |
cidr_block: service.ipv4Range, |
| 65 |
}, |
| 66 |
}, |
| 67 |
aws_internet_gateway: { |
| 68 |
[service.fullName]: { |
| 69 |
vpc_id: '${aws_vpc.%s.id}' % service.fullName, |
| 70 |
}, |
| 71 |
}, |
| 72 |
aws_route_table: { |
| 73 |
[service.fullName]: { |
| 74 |
vpc_id: '${aws_vpc.%s.id}' % service.fullName, |
| 75 |
route: { |
| 76 |
cidr_block: '0.0.0.0/0', |
| 77 |
gateway_id: '${aws_internet_gateway.%s.id}' % service.fullName, |
| 78 |
}, |
| 79 |
}, |
| 80 |
}, |
| 81 |
aws_subnet: { |
| 82 |
[service.prefixName(zone)]: { |
| 83 |
vpc_id: '${aws_vpc.%s.id}' % service.fullName, |
| 84 |
cidr_block: service.subnets[zone], |
| 85 |
availability_zone: zone, |
| 86 |
} |
| 87 |
for zone in std.objectFields(service.subnets) |
| 88 |
}, |
| 89 |
aws_route_table_association: { |
| 90 |
[service.prefixName(zone)]: { |
| 91 |
subnet_id: '${aws_subnet.%s.id}' % service.prefixName(zone), |
| 92 |
route_table_id: '${aws_route_table.%s.id}' % service.fullName, |
| 93 |
} |
| 94 |
for zone in std.objectFields(service.subnets) |
| 95 |
}, |
| 96 |
}, |
| 97 |
// Maps zone to CIDR. |
| 98 |
subnets:: {}, |
| 99 |
}, |
| 100 |
|
| 101 |
|
| 102 |
InstanceBasedService(parent, name): $.Service(parent, name) { |
| 103 |
local service = self, |
| 104 |
fwTcpPorts:: [22], |
| 105 |
fwUdpPorts:: [], |
| 106 |
networkName:: null, |
| 107 |
keyName:: error 'InstanceBasedService needs keyName', |
| 108 |
|
| 109 |
Mixin:: ( |
| 110 |
if service.networkName != null then { |
| 111 |
vpc_security_group_ids: ['${aws_security_group.%s.id}' % service.fullName], |
| 112 |
subnet_id: $.Network.subnetIdRef(self.availability_zone), |
| 113 |
} else { |
| 114 |
security_groups: ['${aws_security_group.%s.name}' % service.fullName], |
| 115 |
} |
| 116 |
) + { |
| 117 |
[if service.keyName != null then 'key_name']: service.keyName, |
| 118 |
}, |
| 119 |
|
| 120 |
Instance:: $.StandardInstance + service.Mixin, |
| 121 |
|
| 122 |
infrastructure+: { |
| 123 |
aws_security_group: { |
| 124 |
[service.fullName]: { |
| 125 |
name: service.fullName, |
| 126 |
|
| 127 |
local IngressRule(p, protocol) = { |
| 128 |
from_port: p, |
| 129 |
to_port: p, |
| 130 |
protocol: protocol, |
| 131 |
cidr_blocks: ['0.0.0.0/0'], |
| 132 |
}, |
| 133 |
ingress: [IngressRule(p, 'tcp') for p in service.fwTcpPorts] |
| 134 |
+ [IngressRule(p, 'udp') for p in service.fwUdpPorts], |
| 135 |
|
| 136 |
[if service.networkName != null then 'vpc_id']: $.Network.idRef, |
| 137 |
}, |
| 138 |
}, |
| 139 |
aws_instance: error 'InstanceBasedService should define some instances.', |
| 140 |
}, |
| 141 |
}, |
| 142 |
|
| 143 |
|
| 144 |
Cluster3(parent, name): $.InstanceBasedService(parent, name) { |
| 145 |
local service = self, |
| 146 |
lbTcpPorts:: [], |
| 147 |
lbUdpPorts:: [], |
| 148 |
fwTcpPorts:: [22], |
| 149 |
fwUdpPorts:: [], |
| 150 |
httpHealthCheckPort:: 80, |
| 151 |
zones:: error 'Cluster3 version (or service) needs an array of zones.', |
| 152 |
|
| 153 |
// In this service, the 'instance' is really an instance template used to create |
| 154 |
// a pool of instances. |
| 155 |
Mixin:: { |
| 156 |
zones:: service.zones, |
| 157 |
}, |
| 158 |
versions:: {}, |
| 159 |
deployment:: {}, |
| 160 |
local merge(objs) = std.foldl(function(a, b) a + b, objs, {}), |
| 161 |
local instances = merge([ |
| 162 |
{ |
| 163 |
[service.prefixName('%s-%d' % [vname, i])]: |
| 164 |
if std.objectHas(service.versions, vname) then |
| 165 |
service.versions[vname] { |
| 166 |
availability_zone: self.zones[i % std.length(self.zones)], |
| 167 |
tags+: { |
| 168 |
version: vname, |
| 169 |
index: i, |
| 170 |
}, |
| 171 |
} |
| 172 |
else |
| 173 |
error 'Undefined version: %s' % vname |
| 174 |
for i in std.set(service.deployment[vname].deployed) |
| 175 |
} |
| 176 |
for vname in std.objectFields(service.deployment) |
| 177 |
]), |
| 178 |
local attached_instances = std.join([], [ |
| 179 |
local attached = std.set(service.deployment[vname].attached); |
| 180 |
local deployed = std.set(service.deployment[vname].deployed); |
| 181 |
[service.prefixName('%s-%d' % [vname, i]) for i in std.setInter(attached, deployed)] |
| 182 |
for vname in std.objectFields(service.deployment) |
| 183 |
]), |
| 184 |
|
| 185 |
addressRef:: '${aws_elb.%s.dns_name}' % service.fullName, |
| 186 |
|
| 187 |
infrastructure+: { |
| 188 |
aws_instance: instances, |
| 189 |
|
| 190 |
aws_elb: { |
| 191 |
[service.fullName]: { |
| 192 |
name: service.fullName, |
| 193 |
|
| 194 |
availability_zones: service.zones, |
| 195 |
|
| 196 |
listener: [{ |
| 197 |
instance_port: p, |
| 198 |
instance_protocol: 'tcp', |
| 199 |
lb_port: p, |
| 200 |
lb_protocol: 'tcp', |
| 201 |
} for p in service.lbTcpPorts], |
| 202 |
|
| 203 |
health_check: { |
| 204 |
healthy_threshold: 2, |
| 205 |
unhealthy_threshold: 2, |
| 206 |
timeout: 3, |
| 207 |
target: 'HTTP:%d/' % service.httpHealthCheckPort, |
| 208 |
interval: 5, |
| 209 |
}, |
| 210 |
|
| 211 |
instances: ['${aws_instance.%s.id}' % iname for iname in attached_instances], |
| 212 |
cross_zone_load_balancing: true, |
| 213 |
idle_timeout: 60, |
| 214 |
connection_draining: true, |
| 215 |
connection_draining_timeout: 60, |
| 216 |
}, |
| 217 |
}, |
| 218 |
aws_security_group: { |
| 219 |
[service.prefixName('elb')]: { |
| 220 |
name: service.prefixName('elb'), |
| 221 |
|
| 222 |
local IngressRule(p, protocol) = { |
| 223 |
from_port: p, |
| 224 |
to_port: p, |
| 225 |
protocol: protocol, |
| 226 |
cidr_blocks: ['0.0.0.0/0'], |
| 227 |
}, |
| 228 |
ingress: [IngressRule(p, 'tcp') for p in service.lbTcpPorts] |
| 229 |
+ [IngressRule(p, 'udp') for p in service.lbUdpPorts], |
| 230 |
|
| 231 |
//[if service.networkName != null then "vpc_id"]: $.Network.idRef, |
| 232 |
}, |
| 233 |
}, |
| 234 |
}, |
| 235 |
}, |
| 236 |
|
| 237 |
|
| 238 |
SingleInstance(parent, name): $.InstanceBasedService(parent, name) { |
| 239 |
local service = self, |
| 240 |
zone:: error 'SingleInstance needs a zone.', |
| 241 |
|
| 242 |
addressRef:: '${aws_instance.%s.public_ip}' % service.fullName, |
| 243 |
|
| 244 |
infrastructure+: { |
| 245 |
aws_instance: { |
| 246 |
[service.fullName]: service.Instance { |
| 247 |
availability_zone: service.zone, |
| 248 |
}, |
| 249 |
}, |
| 250 |
}, |
| 251 |
}, |
| 252 |
|
| 253 |
DnsZone(parent, name):: self.Service(parent, name) { |
| 254 |
local service = self, |
| 255 |
dnsName:: error 'DnsZone must have dnsName, e.g. example.com', |
| 256 |
nameRef:: '${aws_route53_zone.%s.zone_id}' % service.fullName, |
| 257 |
description:: 'Zone for ' + self.dnsName, |
| 258 |
infrastructure+: { |
| 259 |
aws_route53_zone: { |
| 260 |
[service.fullName]: { |
| 261 |
dns_name: service.dnsName, |
| 262 |
comment: service.description, |
| 263 |
}, |
| 264 |
}, |
| 265 |
aws_dns_record: { |
| 266 |
}, |
| 267 |
}, |
| 268 |
outputs+: { |
| 269 |
[service.prefixName('name_servers')]: |
| 270 |
'${google_dns_managed_zone.%s.name_servers.0}' % service.fullName, |
| 271 |
}, |
| 272 |
}, |
| 273 |
|
| 274 |
} |