This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
config.py
709 lines (610 loc) · 24.5 KB
/
config.py
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
import json
import logging
import os
import sys
import textwrap
from troposphere import Base64, FindInMap, GetAZs, GetAtt, Join, Output, Ref, Tags, Template
from troposphere.autoscaling import AutoScalingGroup, BlockDeviceMapping, \
EBSBlockDevice, LaunchConfiguration, Tag
from troposphere.ec2 import InternetGateway, Route, RouteTable, SecurityGroup, \
SecurityGroupIngress, Subnet, SubnetRouteTableAssociation, VPC, \
VPCGatewayAttachment
from troposphere.elasticloadbalancing import ConnectionDrainingPolicy, \
HealthCheck, LoadBalancer, Policy
from troposphere.iam import InstanceProfile, PolicyType, Role
from troposphere.rds import DBInstance, DBSubnetGroup
from troposphere.route53 import AliasTarget, RecordSet, RecordSetGroup
from troposphere.s3 import Bucket, BucketPolicy
import yaml
from bootstrap_cfn import errors, mime_packer, utils
class ProjectConfig:
config = None
def __init__(self, config, environment, passwords=None):
try:
self.config = self.load_yaml(config)[environment]
except KeyError:
raise errors.BootstrapCfnError("Environment " + environment + " not found")
if passwords:
passwords_dict = self.load_yaml(passwords)[environment]
self.config = utils.dict_merge(self.config, passwords_dict)
@staticmethod
def load_yaml(fp):
if os.path.exists(fp):
return yaml.load(open(fp).read())
class ConfigParser:
config = {}
def __init__(self, data, stack_name, environment=None, application=None):
self.stack_name = stack_name
self.data = data
# Some things possibly used in user data templates
self.environment = environment
self.application = application
def process(self):
template = self.base_template()
vpc = self.vpc()
map(template.add_resource, vpc)
iam = self.iam()
map(template.add_resource, iam)
ec2 = self.ec2()
map(template.add_resource, ec2)
if 'elb' in self.data:
elbs, sgs = self.elb()
map(template.add_resource, elbs)
map(template.add_resource, sgs)
template = self._attach_elbs(template)
if 'rds' in self.data:
rds = self.rds()
map(template.add_resource, rds)
template.add_output(Output(
"dbhost",
Description="RDS Hostname",
Value=GetAtt("RDSInstance", "Endpoint.Address")
))
template.add_output(Output(
"dbport",
Description="RDS Port",
Value=GetAtt("RDSInstance", "Endpoint.Port")
))
if 's3' in self.data:
s3 = self.s3()
map(template.add_resource, s3)
template = json.loads(template.to_json())
if 'includes' in self.data:
for inc_path in self.data['includes']:
inc = json.load(open(inc_path))
template = utils.dict_merge(template, inc)
return json.dumps(
template, sort_keys=True, indent=4, separators=(',', ': '))
def base_template(self):
t = Template()
t.add_mapping("AWSRegion2AMI", {
"eu-west-1": {"AMI": "ami-f0b11187"},
})
if 'vpc' in self.data:
t.add_mapping("SubnetConfig", {
"VPC": self.data['vpc']
})
else:
t.add_mapping("SubnetConfig", {
"VPC": {
"CIDR": "10.0.0.0/16",
"SubnetA": "10.0.0.0/20",
"SubnetB": "10.0.16.0/20",
"SubnetC": "10.0.32.0/20"
}
})
return t
def vpc(self):
vpc = VPC(
"VPC",
InstanceTenancy="default",
EnableDnsSupport="true",
CidrBlock=FindInMap("SubnetConfig", "VPC", "CIDR"),
EnableDnsHostnames="true",
)
subnet_a = Subnet(
"SubnetA",
VpcId=Ref(vpc),
AvailabilityZone="eu-west-1a",
CidrBlock=FindInMap("SubnetConfig", "VPC", "SubnetA"),
Tags=Tags(
Application=Ref("AWS::StackId"),
Network="Public",
),
)
subnet_b = Subnet(
"SubnetB",
VpcId=Ref(vpc),
AvailabilityZone="eu-west-1b",
CidrBlock=FindInMap("SubnetConfig", "VPC", "SubnetB"),
Tags=Tags(
Application=Ref("AWS::StackId"),
Network="Public",
),
)
subnet_c = Subnet(
"SubnetC",
VpcId=Ref(vpc),
AvailabilityZone="eu-west-1c",
CidrBlock=FindInMap("SubnetConfig", "VPC", "SubnetC"),
Tags=Tags(
Application=Ref("AWS::StackId"),
Network="Public",
),
)
igw = InternetGateway(
"InternetGateway",
Tags=Tags(
Application=Ref("AWS::StackId"),
Network="Public",
),
)
gw_attachment = VPCGatewayAttachment(
"AttachGateway",
VpcId=Ref(vpc),
InternetGatewayId=Ref(igw),
)
route_table = RouteTable(
"PublicRouteTable",
VpcId=Ref(vpc),
Tags=Tags(
Application=Ref("AWS::StackId"),
Network="Public",
),
)
public_route = Route(
"PublicRoute",
GatewayId=Ref(igw),
DestinationCidrBlock="0.0.0.0/0",
RouteTableId=Ref(route_table),
DependsOn=gw_attachment.title
)
subnet_a_route_assoc = SubnetRouteTableAssociation(
"SubnetRouteTableAssociationA",
SubnetId=Ref(subnet_a),
RouteTableId=Ref(route_table),
)
subnet_b_route_assoc = SubnetRouteTableAssociation(
"SubnetRouteTableAssociationB",
SubnetId=Ref(subnet_b),
RouteTableId=Ref(route_table),
)
subnet_c_route_assoc = SubnetRouteTableAssociation(
"SubnetRouteTableAssociationC",
SubnetId=Ref(subnet_c),
RouteTableId=Ref(route_table),
)
resources = [vpc, subnet_a, subnet_b, subnet_c, igw, gw_attachment,
public_route, route_table, subnet_a_route_assoc,
subnet_b_route_assoc, subnet_c_route_assoc]
# Hack until we return troposphere objects directly
# return json.loads(json.dumps(dict((r.title, r) for r in resources), cls=awsencode))
return resources
def iam(self):
role = Role(
"BaseHostRole",
Path="/",
AssumeRolePolicyDocument={
"Statement": [{
"Action": ["sts:AssumeRole"],
"Effect": "Allow",
"Principal": {"Service": ["ec2.amazonaws.com"]}
}]
},
)
role_policies = PolicyType(
"RolePolicies",
PolicyName="BaseHost",
PolicyDocument={"Statement": [
{"Action": ["autoscaling:Describe*"], "Resource": "*", "Effect": "Allow"},
{"Action": ["ec2:Describe*"], "Resource": "*", "Effect": "Allow"},
{"Action": ["rds:Describe*"], "Resource": "*", "Effect": "Allow"},
{"Action": ["elasticloadbalancing:Describe*"], "Resource": "*", "Effect": "Allow"},
{"Action": ["elasticache:Describe*"], "Resource": "*", "Effect": "Allow"},
{"Action": ["cloudformation:Describe*"], "Resource": "*", "Effect": "Allow"},
{"Action": ["s3:List*"], "Resource": "*", "Effect": "Allow"}
]},
Roles=[Ref(role)],
)
instance_profile = InstanceProfile(
"InstanceProfile",
Path="/",
Roles=[Ref(role)],
)
resources = [role, role_policies, instance_profile]
# Hack until we return troposphere objects directly
# return json.loads(json.dumps(dict((r.title, r) for r in resources), cls=awsencode))
return resources
def s3(self):
# REQUIRED FIELDS AND MAPPING
required_fields = {
'static-bucket-name': 'BucketName'
}
# TEST FOR REQUIRED FIELDS AND EXIT IF MISSING ANY
present_keys = self.data['s3'].keys()
for i in required_fields.keys():
if i not in present_keys:
print "\n\n[ERROR] Missing S3 fields [%s]" % i
sys.exit(1)
bucket = Bucket(
"StaticBucket",
AccessControl="BucketOwnerFullControl",
BucketName=self.data['s3']['static-bucket-name'],
)
if 'policy' in present_keys:
policy = json.loads(open(self.data['s3']['policy']).read())
else:
arn = 'arn:aws:s3:::%s/*' % self.data['s3']['static-bucket-name']
policy = {
'Action': ['s3:GetObject'],
'Resource': arn,
'Effect': 'Allow',
'Principal': '*'}
bucket_policy = BucketPolicy(
"StaticBucketPolicy",
Bucket=Ref(bucket),
PolicyDocument={"Statement": [policy]},
)
resources = [bucket, bucket_policy]
# Hack until we return troposphere objects directly
return resources
def ssl(self):
return self.data['ssl']
def rds(self):
# REQUIRED FIELDS MAPPING
required_fields = {
'db-name': 'DBName',
'storage': 'AllocatedStorage',
'storage-type': 'StorageType',
'backup-retention-period': 'BackupRetentionPeriod',
'db-master-username': 'MasterUsername',
'db-master-password': 'MasterUserPassword',
'identifier': 'DBInstanceIdentifier',
'db-engine': 'Engine',
'db-engine-version': 'EngineVersion',
'instance-class': 'DBInstanceClass',
'multi-az': 'MultiAZ'
}
optional_fields = {
'storage-encrypted': 'StorageEncrypted',
}
# LOAD STACK TEMPLATE
resources = []
rds_subnet_group = DBSubnetGroup(
"RDSSubnetGroup",
SubnetIds=[Ref("SubnetA"), Ref("SubnetB"), Ref("SubnetC")],
DBSubnetGroupDescription="VPC Subnets"
)
resources.append(rds_subnet_group)
database_sg = SecurityGroup(
"DatabaseSG",
SecurityGroupIngress=[
{"ToPort": 5432,
"FromPort": 5432,
"IpProtocol": "tcp",
"CidrIp": FindInMap("SubnetConfig", "VPC", "CIDR")},
{"ToPort": 3306,
"FromPort": 3306,
"IpProtocol": "tcp",
"CidrIp": FindInMap("SubnetConfig", "VPC", "CIDR")}
],
VpcId=Ref("VPC"),
GroupDescription="SG for EC2 Access to RDS",
)
resources.append(database_sg)
rds_instance = DBInstance(
"RDSInstance",
PubliclyAccessible=False,
AllowMajorVersionUpgrade=False,
AutoMinorVersionUpgrade=False,
VPCSecurityGroups=[GetAtt(database_sg, "GroupId")],
DBSubnetGroupName=Ref(rds_subnet_group),
StorageEncrypted=False,
DependsOn=database_sg.title
)
resources.append(rds_instance)
# TEST FOR REQUIRED FIELDS AND EXIT IF MISSING ANY
for yaml_key, rds_prop in required_fields.iteritems():
if yaml_key not in self.data['rds']:
print "\n\n[ERROR] Missing RDS fields [%s]" % yaml_key
sys.exit(1)
else:
rds_instance.__setattr__(rds_prop, self.data['rds'][yaml_key])
for yaml_key, rds_prop in optional_fields.iteritems():
if yaml_key in self.data['rds']:
rds_instance.__setattr__(rds_prop, self.data['rds'][yaml_key])
return resources
def elb(self):
# REQUIRED FIELDS AND MAPPING
required_fields = {
'listeners': 'Listeners',
'scheme': 'Scheme',
'name': 'LoadBalancerName',
'hosted_zone': 'HostedZoneName'
}
elb_list = []
elb_sgs = []
# COULD HAVE MULTIPLE ELB'S (PUBLIC / PRIVATE etc)
for elb in self.data['elb']:
safe_name = elb['name'].replace('-', '').replace('.', '').replace('_', '')
# TEST FOR REQUIRED FIELDS AND EXIT IF MISSING ANY
for i in required_fields.keys():
if i not in elb.keys():
print "\n\n[ERROR] Missing ELB fields [%s]" % i
sys.exit(1)
load_balancer = LoadBalancer(
"ELB" + safe_name,
Subnets=[Ref("SubnetA"), Ref("SubnetB"), Ref("SubnetC")],
Listeners=elb['listeners'],
Scheme=elb['scheme'],
LoadBalancerName=self._get_elb_canonical_name(elb['name']),
ConnectionDrainingPolicy=ConnectionDrainingPolicy(
Enabled=True,
Timeout=120,
),
Policies=[
Policy(
Attributes=[{'Name': "Reference-Security-Policy", 'Value': "ELBSecurityPolicy-2015-05"}],
PolicyType='SSLNegotiationPolicyType',
PolicyName='PinDownSSLNegotiationPolicy201505'
)
]
)
if "health_check" in elb:
load_balancer.HealthCheck = HealthCheck(**elb['health_check'])
for listener in load_balancer.Listeners:
if listener['Protocol'] == 'HTTPS':
try:
cert_name = elb['certificate_name']
except KeyError:
raise errors.CfnConfigError(
"HTTPS listener but no certificate_name specified")
try:
self.ssl()[cert_name]['cert']
self.ssl()[cert_name]['key']
except KeyError:
raise errors.CfnConfigError(
"Couldn't find ssl cert {0} in config file".format(cert_name))
listener["SSLCertificateId"] = Join("", [
"arn:aws:iam::",
Ref("AWS::AccountId"),
":server-certificate/",
"{0}-{1}".format(cert_name, self.stack_name)]
)
# if not present, add the default cipher policy
if 'PolicyNames' not in listener:
logging.debug(
"ELB Listener for port 443 has no SSL Policy. " +
"Using default ELBSecurityPolicy-2015-05")
listener['PolicyNames'] = ['PinDownSSLNegotiationPolicy201505']
elb_list.append(load_balancer)
dns_record = RecordSetGroup(
"DNS" + safe_name,
HostedZoneName=elb['hosted_zone'],
Comment="Zone apex alias targeted to ElasticLoadBalancer.",
RecordSets=[
RecordSet(
"TitleIsIgnoredForThisResource",
Name="%s.%s" % (elb['name'], elb['hosted_zone']),
Type="A",
AliasTarget=AliasTarget(
GetAtt(load_balancer, "CanonicalHostedZoneNameID"),
GetAtt(load_balancer, "DNSName"),
),
),
]
)
elb_list.append(dns_record)
elb_role_policies = PolicyType(
"Policy" + safe_name,
PolicyName=safe_name+"BaseHost",
PolicyDocument={"Statement": [{
"Action": [
"elasticloadbalancing:DeregisterInstancesFromLoadBalancer",
"elasticloadbalancing:RegisterInstancesWithLoadBalancer"
],
"Resource": [
Join("", [
"arn:aws:elasticloadbalancing:",
Ref("AWS::Region"),
":",
Ref("AWS::AccountId"),
':loadbalancer/%s' % load_balancer.LoadBalancerName
])
],
"Effect": "Allow"}
]},
Roles=[Ref("BaseHostRole")],
)
elb_list.append(elb_role_policies)
if "security_groups" in elb:
load_balancer.SecurityGroups = []
for sg_name, sg_rules in elb['security_groups'].items():
sg = SecurityGroup(
sg_name,
GroupDescription=sg_name,
SecurityGroupIngress=sg_rules,
VpcId=Ref("VPC")
)
load_balancer.SecurityGroups.append(Ref(sg))
elb_sgs.append(sg)
else:
sg = SecurityGroup(
"DefaultSG" + safe_name,
GroupDescription="DefaultELBSecurityGroup",
SecurityGroupIngress=[
{
"IpProtocol": "tcp",
"FromPort": 443,
"ToPort": 443,
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "tcp",
"FromPort": 80,
"ToPort": 80,
"CidrIp": "0.0.0.0/0"
}
],
VpcId=Ref("VPC")
)
load_balancer.SecurityGroups = [Ref(sg)]
elb_sgs.append(sg)
return elb_list, elb_sgs
def _convert_ref_dict_to_objects(self, o):
"""
Some troposphere objects need troposphere.Ref objects instead of a
plain dict of {"Ref": "x" }. This helper function will do such
transformations and return a new dict
"""
def ref_fixup(x):
if isinstance(x, dict) and "Ref" in x:
return Ref(x["Ref"])
else:
return x
return dict([(k, ref_fixup(v)) for k, v in o.items()])
def get_ec2_userdata(self):
data = self.data['ec2']
parts = []
boothook = self.get_hostname_boothook(data)
if boothook:
parts.append(boothook)
if "cloud_config" in data:
parts.append({
'content': yaml.dump(data['cloud_config']),
'mime_type': 'text/cloud-config'
})
if len(parts):
return parts
HOSTNAME_BOOTHOOK_TEMPLATE = textwrap.dedent("""\
#!/bin/sh
[ -e /etc/cloud/cloud.cfg.d/99_hostname.cfg ] || echo "hostname: {hostname}" > /etc/cloud/cloud.cfg.d/99_hostname.cfg
""")
DEFAULT_HOSTNAME_PATTERN = "{instance_id}.{environment}.{application}"
def get_hostname_boothook(self, data):
"""
Return a boothook part that will set the hostname of instances on boot.
The pattern comes from the ``hostname_pattern`` pattern of data dict,
with a default of "{instance_id}.{environment}.{application}". To
disable this functionality explicitly pass None in this field.
"""
hostname_pattern = data.get('hostname_pattern', self.DEFAULT_HOSTNAME_PATTERN)
if hostname_pattern is None:
return None
interploations = {
# This gets interploated by cloud-init at run time.
'instance_id': '${INSTANCE_ID}',
'tags': data['tags'],
'environment': self.environment,
'application': self.application,
'stack_name': self.stack_name,
}
try:
hostname = hostname_pattern.format(**interploations)
except KeyError as e:
raise errors.CfnHostnamePatternError("Error interpolating hostname_pattern '{pattern}' - {key} is not a valid interpolation".format(
pattern=hostname_pattern,
key=e.args[0]))
return {
'mime_type': 'text/cloud-boothook',
'content': self.HOSTNAME_BOOTHOOK_TEMPLATE.format(hostname=hostname)
}
def ec2(self):
# LOAD STACK TEMPLATE
data = self.data['ec2']
resources = []
sgs = []
for sg_name, ingress in data['security_groups'].items():
sg = SecurityGroup(
sg_name,
VpcId=Ref("VPC"),
GroupDescription="BaseHost Security Group",
)
sgs.append(sg)
resources.append(sg)
# Because we want to be able to add ingress rules to a security
# group that referes to itself (for example allow all instances in
# the sg to speak to each other on 9300 for Elasticsearch
# clustering) we create the SG in one resource and rules as other
# resources
#
# The yaml for this case is:
#
# security_groups:
# EScluster:
# - FromPort: 9300
# - ToPort: 9300
# - SourceSecurityGroupId: { Ref: EScluster }
for idx, rule in enumerate(ingress):
# Convert { Ref: "x"} to Ref("x")
rule = self._convert_ref_dict_to_objects(rule)
ingress = SecurityGroupIngress(
"{}Rule{}".format(sg_name, idx),
GroupId=Ref(sg),
**rule)
resources.append(ingress)
devices = []
try:
for i in data['block_devices']:
devices.append(BlockDeviceMapping(
DeviceName=i['DeviceName'],
Ebs=EBSBlockDevice(VolumeSize=i['VolumeSize']),
))
except KeyError:
devices.append(BlockDeviceMapping(
DeviceName="/dev/sda1",
Ebs=EBSBlockDevice(VolumeSize=20),
))
launch_config = LaunchConfiguration(
"BaseHostLaunchConfig",
KeyName=data['parameters']['KeyName'],
SecurityGroups=[Ref(g) for g in sgs],
InstanceType=data['parameters']['InstanceType'],
AssociatePublicIpAddress=True,
IamInstanceProfile=Ref("InstanceProfile"),
ImageId=FindInMap("AWSRegion2AMI", Ref("AWS::Region"), "AMI"),
BlockDeviceMappings=devices,
)
user_data = self.get_ec2_userdata()
if user_data:
user_data = mime_packer.pack(user_data)
launch_config.UserData = Base64(user_data)
resources.append(launch_config)
# Allow deprecation of tags
ec2_tags = []
deprecated_tags = ["Env"]
for k, v in data['tags'].items():
if k not in deprecated_tags:
ec2_tags.append(Tag(k, v, True))
else:
logging.warning("config: Tag '%s' is deprecated.."
% (k))
scaling_group = AutoScalingGroup(
"ScalingGroup",
VPCZoneIdentifier=[Ref("SubnetA"), Ref("SubnetB"), Ref("SubnetC")],
MinSize=data['auto_scaling']['min'],
MaxSize=data['auto_scaling']['max'],
DesiredCapacity=data['auto_scaling']['desired'],
AvailabilityZones=GetAZs(),
Tags=ec2_tags,
LaunchConfigurationName=Ref(launch_config),
)
resources.append(scaling_group)
return resources
@classmethod
def _find_resources(cls, template, resource_type):
f = lambda x: x.resource_type == resource_type
return filter(f, template.resources.values())
@classmethod
def _get_elb_canonical_name(cls, elb_yaml_name):
return 'ELB-{}'.format(elb_yaml_name.replace('.', ''))
def _attach_elbs(self, template):
if 'elb' not in self.data:
return template
asgs = self._find_resources(template,
'AWS::AutoScaling::AutoScalingGroup')
elbs = self._find_resources(template,
'AWS::ElasticLoadBalancing::LoadBalancer')
asgs[0].LoadBalancerNames = [x.LoadBalancerName for x in elbs]
template.resources[asgs[0].title] = asgs[0]
return template