/
create.go
122 lines (106 loc) · 4.15 KB
/
create.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
package model
import (
"errors"
"regexp"
"github.com/auto-staging/builder/helper"
"github.com/auto-staging/builder/types"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/codebuild"
yaml "gopkg.in/yaml.v2"
)
// CreateCodeBuildJob creates the CodebuildJob via AWS SDK with the configuration defined for the Environment.
// If an error occurs the error gets logged and the returned.
func (CodeBuildModel *CodeBuildModel) CreateCodeBuildJob(event types.Event) error {
// Adapt branch name to only contain allowed characters for CodeBuild name
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
if err != nil {
helper.Logger.Log(err, map[string]string{"module": "model/CreateCodeBuildJob", "operation": "regex/compile"}, 0)
return err
}
branchName := reg.ReplaceAllString(event.Branch, "-")
var envVars []*codebuild.EnvironmentVariable
// Set default variables
envVars = append(envVars, &codebuild.EnvironmentVariable{
Name: aws.String("TF_VAR_branch_raw"),
Type: aws.String("PLAINTEXT"),
Value: aws.String(event.Branch),
})
envVars = append(envVars, &codebuild.EnvironmentVariable{
Name: aws.String("TF_VAR_branch"),
Type: aws.String("PLAINTEXT"),
Value: aws.String(branchName),
})
envVars = append(envVars, &codebuild.EnvironmentVariable{
Name: aws.String("TF_VAR_repository"),
Type: aws.String("PLAINTEXT"),
Value: aws.String(event.Repository),
})
envVars = append(envVars, &codebuild.EnvironmentVariable{
Name: aws.String("TF_VAR_random"),
Type: aws.String("PLAINTEXT"),
Value: aws.String(getRandomValueForBranch(event.Branch)),
})
for _, environmentVariable := range event.EnvironmentVariables {
envVars = append(envVars, &codebuild.EnvironmentVariable{
Name: aws.String(environmentVariable.Name),
Type: aws.String(environmentVariable.Type),
Value: aws.String(environmentVariable.Value),
})
}
buildspec := types.Buildspec{
Version: "0.2",
Phases: types.Phases{
Build: types.Build{
Commands: []string{
"make auto-staging-init",
"make auto-staging-apply",
},
Finally: []string{
"aws lambda invoke --function-name auto-staging-builder --invocation-type Event --payload '{ \"operation\": \"RESULT_CREATE\", \"success\": '${CODEBUILD_BUILD_SUCCEEDING}', \"repository\": \"" + event.Repository + "\", \"branch\": \"" + event.Branch + "\" }' /dev/null",
},
},
},
}
res, err := yaml.Marshal(buildspec)
if err != nil {
helper.Logger.Log(err, map[string]string{"module": "model/CreateCodeBuildJob", "operation": "yaml/marshal"}, 0)
return err
}
helper.Logger.Log(errors.New(string(res)), map[string]string{"module": "model/CreateCodeBuildJob", "operation": "buildspec"}, 4)
createInput := codebuild.CreateProjectInput{
Name: aws.String("auto-staging-" + event.Repository + "-" + branchName),
Description: aws.String("Managed by auto-staging"),
ServiceRole: aws.String(event.CodeBuildRoleARN),
Environment: &codebuild.ProjectEnvironment{
ComputeType: aws.String("BUILD_GENERAL1_SMALL"),
Image: aws.String("autostaging/auto-staging-codebuild"),
Type: aws.String("LINUX_CONTAINER"),
EnvironmentVariables: envVars,
},
Source: &codebuild.ProjectSource{
Type: aws.String("GITHUB"),
Location: aws.String(event.InfrastructureRepoURL),
Buildspec: aws.String(string(res)),
},
Artifacts: &codebuild.ProjectArtifacts{
Type: aws.String("NO_ARTIFACTS"),
},
}
client := CodeBuildModel.CodeBuildAPI
_, err = client.CreateProject(&createInput)
if err != nil {
helper.Logger.Log(err, map[string]string{"module": "model/CreateCodeBuildJob", "operation": "codebuild/create"}, 0)
return err
}
return err
}
// SetStatusAfterCreation checks the success variable in the event struct, which gets set in the CodeBuild Job. If success euqals 1 then the status
// gets set to "running" otherwise it gets set to "initating failed".
// If an error occurs the error gets logged and the returned.
func (DynamoDBModel *DynamoDBModel) SetStatusAfterCreation(event types.Event) error {
status := "initiating failed"
if event.Success == 1 {
status = "running"
}
return DynamoDBModel.SetStatusForEnvironment(event, status)
}