-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
431 lines (389 loc) · 14.7 KB
/
main.go
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
//go:generate go run $GOPATH/src/github.com/mjibson/esc/main.go -o ./resources/RESOURCES.go -pkg resources ./resources/source
package main
import (
"encoding/json"
"fmt"
"github.com/Sirupsen/logrus"
"github.com/asaskevich/govalidator"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
gocf "github.com/crewjam/go-cloudformation"
sparta "github.com/mweagle/Sparta"
spartaAWS "github.com/mweagle/Sparta/aws"
spartaCF "github.com/mweagle/Sparta/aws/cloudformation"
spartaIAM "github.com/mweagle/Sparta/aws/iam"
"github.com/mweagle/SpartaCICD/concourse"
"github.com/mweagle/SpartaCICD/resources"
"github.com/spf13/cobra"
"net/http"
"os"
"strings"
"time"
)
const (
databaseMasterUsername = "concourse"
// The parameter MasterUserPassword is not a valid password because it is shorter than 8 characters.
databaseMasterPassword = "picard1337"
)
// Additional command line options used for both the provision
// and CLI commands
type optionsStruct struct {
Username string `valid:"required,match(\\w+)"`
Password string `valid:"required,match(\\w+)"`
SSHKeyName string `valid:"-"`
}
var options optionsStruct
// Nothing super interesting to do here. In a real system you might
// actually define the lambda functions in the same codebase.
func ciCDConfigurator(event *json.RawMessage,
context *sparta.LambdaContext,
w http.ResponseWriter,
logger *logrus.Logger) {
configuration, _ := sparta.Discover()
logger.WithFields(logrus.Fields{
"Discovery": configuration,
}).Info("Custom resource request")
fmt.Fprint(w, "Hello World")
}
// Given a slice of EC2 images, return the most recently Created one.
func mostRecentImageID(images []*ec2.Image) (string, error) {
if len(images) <= 0 {
return "", fmt.Errorf("No images to search")
}
var mostRecentImage *ec2.Image
for _, eachImage := range images {
if nil == mostRecentImage {
mostRecentImage = eachImage
} else {
curTime, curTimeErr := time.Parse(time.RFC3339, *mostRecentImage.CreationDate)
if nil != curTimeErr {
return "", curTimeErr
}
testTime, testTimeErr := time.Parse(time.RFC3339, *eachImage.CreationDate)
if nil != testTimeErr {
return "", testTimeErr
}
if (testTime.Unix() - curTime.Unix()) > 0 {
mostRecentImage = eachImage
}
}
}
return *mostRecentImage.ImageId, nil
}
// Lambda CustomResource function that looks up the latest Ubuntu AMI ID for the
// current region and returns a map with the latest AMI IDs via the resource's
// outputs. For this example we're going to look for the latest Ubuntu 16.04
// release.
// Ref: https://help.ubuntu.com/community/EC2StartersGuide#Official_Ubuntu_Cloud_Guest_Amazon_Machine_Images_.28AMIs.29
func ubuntuAMICustomResource(requestType string,
stackID string,
properties map[string]interface{},
logger *logrus.Logger) (map[string]interface{}, error) {
if requestType != "Create" {
return map[string]interface{}{}, nil
}
// Setup the common filters
describeImageFilters := []*ec2.Filter{}
describeImageFilters = append(describeImageFilters, &ec2.Filter{
Name: aws.String("name"),
Values: []*string{aws.String("*hvm-ssd/ubuntu-xenial-16.04-amd64-server*")},
})
describeImageFilters = append(describeImageFilters, &ec2.Filter{
Name: aws.String("root-device-type"),
Values: []*string{aws.String("ebs")},
})
describeImageFilters = append(describeImageFilters, &ec2.Filter{
Name: aws.String("architecture"),
Values: []*string{aws.String("x86_64")},
})
describeImageFilters = append(describeImageFilters, &ec2.Filter{
Name: aws.String("virtualization-type"),
Values: []*string{aws.String("hvm")},
})
// Get the HVM AMIs
params := &ec2.DescribeImagesInput{
Filters: describeImageFilters,
Owners: []*string{aws.String("099720109477")},
}
logger.Level = logrus.DebugLevel
ec2Svc := ec2.New(spartaAWS.NewSession(logger))
describeImagesOutput, describeImagesOutputErr := ec2Svc.DescribeImages(params)
if nil != describeImagesOutputErr {
return nil, describeImagesOutputErr
}
logger.WithFields(logrus.Fields{
"DescribeImagesOutput": describeImagesOutput,
"DescribeImagesOutputErr": describeImagesOutputErr,
}).Info("Results")
amiID, amiIDErr := mostRecentImageID(describeImagesOutput.Images)
if nil != amiIDErr {
return nil, amiIDErr
}
// Set the HVM type
outputProps := map[string]interface{}{
"HVM": amiID,
}
logger.WithFields(logrus.Fields{
"Outputs": outputProps,
}).Info("CustomResource outputs")
return outputProps, nil
}
// The CloudFormation template decorator that inserts all the other
// AWS components we need to support this deployment...
func ciCDLambdaDecorator(customResourceAMILookupName string,
ec2SecurityGroupResourceName string) sparta.TemplateDecorator {
return func(serviceName string,
lambdaResourceName string,
lambdaResource gocf.LambdaFunction,
resourceMetadata map[string]interface{},
S3Bucket string,
S3Key string,
template *gocf.Template,
logger *logrus.Logger) error {
// Create the launch configuration with Metadata to download the ZIP file, unzip it & launch the
// golang binary...
dbSecurityGroupName := sparta.CloudFormationResourceName("ConcoursePostgresqlSG",
"ConcoursePostgresqlSG")
dbInstanceName := sparta.CloudFormationResourceName("ConcoursePostgresql",
"ConcoursePostgresql")
asgLaunchConfigurationName := sparta.CloudFormationResourceName("ConcourseCIASGLaunchConfig",
"ConcourseCIASGLaunchConfig")
asgResourceName := sparta.CloudFormationResourceName("ConcourseCIASG",
"ConcourseCIASG")
ec2InstanceRoleName := sparta.CloudFormationResourceName("ConcourseCIEC2InstanceRole",
"ConcourseCIEC2InstanceRole")
ec2InstanceProfileName := sparta.CloudFormationResourceName("ConcourseCIEC2InstanceProfile",
"ConcourseCIEC2InstanceProfile")
//////////////////////////////////////////////////////////////////////////////
// 1 - Create the security group for the Concourse EC2 instance
ec2SecurityGroup := &gocf.EC2SecurityGroup{
GroupDescription: gocf.String("Concourse CI/CD Group"),
SecurityGroupIngress: &gocf.EC2SecurityGroupRuleList{
gocf.EC2SecurityGroupRule{
CidrIp: gocf.String("0.0.0.0/0"),
IpProtocol: gocf.String("tcp"),
FromPort: gocf.Integer(8080),
ToPort: gocf.Integer(8080),
},
gocf.EC2SecurityGroupRule{
CidrIp: gocf.String("0.0.0.0/0"),
IpProtocol: gocf.String("tcp"),
FromPort: gocf.Integer(22),
ToPort: gocf.Integer(22),
},
},
}
// TODO - when deploying to a VPC, remove this when
// https://forums.aws.amazon.com/thread.jspa?messageID=713978
// is fixed
/*ec2SecurityGroupResource := */ template.AddResource(ec2SecurityGroupResourceName, ec2SecurityGroup)
//ec2SecurityGroupResource.DeletionPolicy = "Retain"
//////////////////////////////////////////////////////////////////////////////
// 2 - Create the DB Instance, enable access from the EC2 instance...
dbSecurityGroup := &gocf.RDSDBSecurityGroup{
GroupDescription: gocf.String("Allow access from Concourse CI server"),
DBSecurityGroupIngress: &gocf.RDSSecurityGroupRuleList{
gocf.RDSSecurityGroupRule{
EC2SecurityGroupName: gocf.Ref(ec2SecurityGroupResourceName).String(),
EC2SecurityGroupOwnerId: gocf.Ref("AWS::AccountId").String(),
},
},
}
template.AddResource(dbSecurityGroupName, dbSecurityGroup)
// Create the DB instance
dbInstance := &gocf.RDSDBInstance{
DBName: gocf.String("atc"),
Engine: gocf.String("postgres"),
AllocatedStorage: gocf.String("10"),
DBInstanceClass: gocf.String("db.t2.micro"),
MasterUsername: gocf.String(databaseMasterUsername),
MasterUserPassword: gocf.String(databaseMasterPassword),
DBSecurityGroups: gocf.StringList(
gocf.Ref(dbSecurityGroupName),
),
}
dbCFResource := template.AddResource(dbInstanceName, dbInstance)
dbCFResource.DependsOn = append(dbCFResource.DependsOn, ec2SecurityGroupResourceName)
//////////////////////////////////////////////////////////////////////////////
// 3 - Create the ASG and associate the userdata with the EC2 init
// EC2 Instance Role...
statements := sparta.CommonIAMStatements.Core
statements = append(statements, spartaIAM.PolicyStatement{
Effect: "Allow",
Action: []string{"*"},
Resource: gocf.String("*"),
})
statements = append(statements, spartaIAM.PolicyStatement{
Effect: "Allow",
Action: []string{"s3:GetObject"},
Resource: gocf.String(fmt.Sprintf("arn:aws:s3:::%s/%s", S3Bucket, S3Key)),
})
// Enable all access to the S3 bucket s.t. the Version SemVer resource
// can manipulate objects. This could be more tightly scoped.
statements = append(statements, spartaIAM.PolicyStatement{
Effect: "Allow",
Action: []string{"s3:*"},
Resource: gocf.String(fmt.Sprintf("arn:aws:s3:::%s", S3Bucket)),
})
iamPolicyList := gocf.IAMPoliciesList{}
iamPolicyList = append(iamPolicyList,
gocf.IAMPolicies{
PolicyDocument: sparta.ArbitraryJSONObject{
"Version": "2012-10-17",
"Statement": statements,
},
PolicyName: gocf.String("EC2Policy"),
},
)
ec2InstanceRole := &gocf.IAMRole{
AssumeRolePolicyDocument: sparta.AssumePolicyDocument,
Policies: &iamPolicyList,
}
template.AddResource(ec2InstanceRoleName, ec2InstanceRole)
// Create the instance profile
ec2InstanceProfile := &gocf.IAMInstanceProfile{
Path: gocf.String("/"),
Roles: []gocf.Stringable{gocf.Ref(ec2InstanceRoleName).String()},
}
template.AddResource(ec2InstanceProfileName, ec2InstanceProfile)
//Now setup the properties map, expand the userdata, and attach it...
userDataProps := map[string]interface{}{
"S3Bucket": S3Bucket,
"S3Key": S3Key,
"ServiceName": serviceName,
"DBInstanceResourceName": dbInstanceName,
"DBInstanceUser": databaseMasterUsername,
"DBInstancePassword": databaseMasterPassword,
"DBInstanceDatabaseName": "atc",
"Username": options.Username,
"Password": options.Password,
}
userDataTemplateInput, userDataTemplateInputErr := resources.FSString(false, "/resources/source/userdata.sh")
if nil != userDataTemplateInputErr {
return userDataTemplateInputErr
}
userDataExpression, userDataExpressionErr := spartaCF.ConvertToTemplateExpression(strings.NewReader(userDataTemplateInput), userDataProps)
if nil != userDataExpressionErr {
return userDataExpressionErr
}
logger.WithFields(logrus.Fields{
"Parameters": userDataProps,
"Expanded": userDataExpression,
}).Debug("Expanded userdata")
asgLaunchConfigurationResource := &gocf.AutoScalingLaunchConfiguration{
ImageId: gocf.GetAtt(customResourceAMILookupName, "HVM"),
InstanceType: gocf.String("t2.micro"),
KeyName: gocf.String(options.SSHKeyName),
IamInstanceProfile: gocf.Ref(ec2InstanceProfileName).String(),
UserData: gocf.Base64(userDataExpression),
SecurityGroups: gocf.StringList(gocf.GetAtt(ec2SecurityGroupResourceName, "GroupId")),
}
launchConfigResource := template.AddResource(asgLaunchConfigurationName,
asgLaunchConfigurationResource)
launchConfigResource.DependsOn = append(launchConfigResource.DependsOn,
customResourceAMILookupName)
// Create the ASG
asgResource := &gocf.AutoScalingAutoScalingGroup{
// Empty Region is equivalent to all region AZs
// Ref: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getavailabilityzones.html
AvailabilityZones: gocf.GetAZs(gocf.String("")),
LaunchConfigurationName: gocf.Ref(asgLaunchConfigurationName).String(),
MaxSize: gocf.String("1"),
MinSize: gocf.String("1"),
}
template.AddResource(asgResourceName, asgResource)
return nil
}
}
func registerSpartaCICDFlags(command *cobra.Command) {
command.Flags().StringVarP(&options.Username,
"username",
"u",
"",
"Concourse HTTP Basic Auth username")
command.Flags().StringVarP(&options.Password,
"password",
"p",
"",
"Concourse HTTP Basic Auth password")
}
////////////////////////////////////////////////////////////////////////////////
// Main
func main() {
// Custom command to handle rebuilding pipelines
// Add the custom command to run the sync loop
syncCommand := &cobra.Command{
Use: "sync",
Short: "Periodically rebuild Concourse pipelines",
Long: `Periodically scan the repo for pipelines and update them with ec2metadata credentials`,
RunE: func(cmd *cobra.Command, args []string) error {
return concourse.SyncIt(options.Username, options.Password, sparta.OptionsGlobal.Logger)
},
}
// Include the basic auth flags for the sync command
registerSpartaCICDFlags(syncCommand)
sparta.CommandLineOptions.Root.AddCommand(syncCommand)
// Add them to the standard provision command
registerSpartaCICDFlags(sparta.CommandLineOptions.Provision)
// And add the SSHKeyName option
sparta.CommandLineOptions.Provision.Flags().StringVarP(&options.SSHKeyName,
"key",
"k",
"",
"SSH Key Name to use for EC2 instances")
// Define a validation hook s.t. we can verify the subnetIDs command line
// option is acceptable...
validationHook := func(command *cobra.Command) error {
if command.Name() == "provision" && len(options.SSHKeyName) <= 0 {
return fmt.Errorf("SSHKeyName option is required")
}
fmt.Printf("Command: %s\n", command.Name())
switch command.Name() {
case "provision",
"sync":
_, validationErr := govalidator.ValidateStruct(options)
return validationErr
default:
return nil
}
}
// What are the subnets?
parseErr := sparta.ParseOptions(validationHook)
if nil != parseErr {
os.Exit(3)
}
ec2SecurityGroupName := sparta.CloudFormationResourceName("ConcourseSecurityGroup",
"ConcourseSecurityGroup")
// The primary lambda function
lambdaFn := sparta.NewLambda(sparta.IAMRoleDefinition{},
ciCDConfigurator,
nil)
// Lambda custom resource to lookup the latest Ubuntu AMIs
iamRoleCustomResource := sparta.IAMRoleDefinition{}
iamRoleCustomResource.Privileges = append(iamRoleCustomResource.Privileges,
sparta.IAMRolePrivilege{
Actions: []string{"ec2:DescribeImages"},
Resource: "*",
})
customResourceLambdaOptions := sparta.LambdaFunctionOptions{
MemorySize: 128,
Timeout: 30,
}
amiIDCustomResourceName, _ := lambdaFn.RequireCustomResource(iamRoleCustomResource,
ubuntuAMICustomResource,
&customResourceLambdaOptions,
nil)
// Get the resource name and pass it to the decorator
lambdaFn.Decorator = ciCDLambdaDecorator(amiIDCustomResourceName,
ec2SecurityGroupName)
var lambdaFunctions []*sparta.LambdaAWSInfo
lambdaFunctions = append(lambdaFunctions, lambdaFn)
err := sparta.Main("SpartaCICD",
fmt.Sprintf("Provision a Concourse CICD system"),
lambdaFunctions,
nil,
nil)
if err != nil {
os.Exit(1)
}
}