-
Notifications
You must be signed in to change notification settings - Fork 7
/
aws.go
131 lines (117 loc) · 3.4 KB
/
aws.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
package main
import (
"bytes"
"fmt"
"log"
"regexp"
"strings"
"text/template"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/lambda"
)
type ECRClient struct {
client *ecr.ECR
}
type ImageTagVars struct {
Branch string
Phase string
}
func (self ImageTagVars) Parse(s string) (string, error) {
b := bytes.NewBuffer([]byte(""))
tmpl, err := template.New("").Parse(s)
if err != nil {
return "", err
}
err = tmpl.Execute(b, self)
return b.String(), err
}
func CreateECRInstance() (ECRClient, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("ap-northeast-1")},
)
if err != nil {
return ECRClient{}, err
}
return ECRClient{client: ecr.New(sess, aws.NewConfig().WithRegion("ap-northeast-1"))}, nil
}
func (e ECRClient) FindImageTagByRegexp(registryId string, repo string, rawFilterRegexp string, rawTargetRegexp string, vars ImageTagVars) (string, error) {
vars.Branch = strings.Replace(vars.Branch, "/", "_", -1)
filterRegexp, err := vars.Parse(rawFilterRegexp)
if err != nil {
return "", fmt.Errorf("[ERROR] filterRegexp cannot be parsed: %s", rawFilterRegexp)
}
targetRegexp, err := vars.Parse(rawTargetRegexp)
if err != nil {
return "", fmt.Errorf("[ERROR] targetRegexp cannot be parsed: %s", rawTargetRegexp)
}
arr := e.describeImages(®istryId, &repo, nil)
for _, v := range arr {
for _, vv1 := range v.ImageTags {
if regexp.MustCompile(filterRegexp).FindStringSubmatch(*vv1) == nil {
continue
}
for _, vv2 := range v.ImageTags {
if regexp.MustCompile(targetRegexp).FindStringSubmatch(*vv2) != nil {
return *vv2, nil
}
}
}
}
return "", fmt.Errorf("[ERROR] NotFound specified image tag")
}
func (e ECRClient) describeImages(registryId *string, repo *string, nextToken *string) []*ecr.ImageDetail {
input := &ecr.DescribeImagesInput{
RegistryId: registryId,
RepositoryName: repo,
NextToken: nextToken,
}
outputs, err := e.client.DescribeImages(input)
if err != nil {
log.Printf("Failed to describe images: %v", err)
return []*ecr.ImageDetail{}
}
if outputs.NextToken != nil {
return append(outputs.ImageDetails, e.describeImages(registryId, repo, outputs.NextToken)...)
}
return outputs.ImageDetails
}
type LambdaClient struct {
client *lambda.Lambda
}
func CreateLambdaInstance() (LambdaClient, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("ap-northeast-1")},
)
if err != nil {
return LambdaClient{}, err
}
return LambdaClient{client: lambda.New(sess)}, nil
}
func (self LambdaClient) Invoke(funcName string, payload string) (*lambda.InvokeOutput, error) {
input := &lambda.InvokeInput{
FunctionName: &funcName,
Payload: []byte(payload),
InvocationType: aws.String("RequestResponse"),
}
res, err := self.client.Invoke(input)
return res, err
}
type ECSClient struct {
client *ecs.ECS
}
func CreateECSInstance() (ECSClient, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("ap-northeast-1")},
)
if err != nil {
return ECSClient{}, err
}
return ECSClient{client: ecs.New(sess)}, nil
}
func (self ECSClient) DescribeTaskDefinition(arn string) (*ecs.TaskDefinition, error) {
o, err := self.client.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{TaskDefinition: &arn})
return o.TaskDefinition, err
}