-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
188 lines (166 loc) · 5.13 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
package main
import (
"errors"
"fmt"
"os"
"regexp"
"text/tabwriter"
"time"
"github.com/ardanlabs/conf/v3"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/codepipeline"
"github.com/aws/aws-sdk-go/service/s3"
)
var (
urlRe = regexp.MustCompile(`https://.*aws\.amazon\.com/s3/home\?region=[a-zA-Z]{2,3}-[a-zA-Z]+-[0-9]+#`)
revRe = regexp.MustCompile(`Amazon S3 version id: .*`)
)
type Cfg struct {
Region string `conf:"default:us-east-1"`
PipelineName string `conf:""`
Bucket string `conf:""`
Key string `conf:"default:version.zip"`
Timeout time.Duration `conf:"default:1m"`
}
func main() {
type stageDetails struct {
name string
executionId string
status string
revisionId string
versionDeployed string
releaseUrl string
commit string
}
// =========================================================================
// Configuration
var cfg Cfg
const prefix = "verdeployed"
help, err := conf.Parse(prefix, &cfg)
if err != nil {
if errors.Is(err, conf.ErrHelpWanted) {
fmt.Println(help)
os.Exit(0)
}
fmt.Printf("parsing config: %v", err)
os.Exit(1)
}
// initialize tabwriter
w := new(tabwriter.Writer)
// minwidth, tabwidth, padding, padchar, flags
w.Init(os.Stdout, 8, 8, 0, '\t', 0)
defer w.Flush()
// =========================================================================
// AWS Session
sess, err := session.NewSession(aws.NewConfig().WithRegion(cfg.Region))
if err != nil {
fmt.Printf("session error: %v", err)
os.Exit(1)
}
// =========================================================================
// Codepipeline state
pipelnsvc := codepipeline.New(sess)
pipelnStateInput := &codepipeline.GetPipelineStateInput{
Name: aws.String(cfg.PipelineName),
}
state, err := pipelnsvc.GetPipelineState(pipelnStateInput)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Printf("failed to get pipeline state: %s\n", aerr.Message())
}
} else {
fmt.Println(err.Error())
}
os.Exit(1)
}
var execId, revid string
fmt.Fprintf(w, "%s\t%s\t%s\t\t%s\t\t%s\n", "Stage", "Status", "Version", "Release URL", "ExecutionID")
fmt.Fprintf(w, "%s\t%s\t%s\t\t%s\t\t%s\n", "----", "----", "----", "----", "----")
// Get&Print every stage details
for _, stage := range state.StageStates {
// Get revision id from current pipeline execution
// This can be get for Source stage only (?)
if *stage.StageName == "Source" {
for _, astate := range stage.ActionStates {
if urlRe.MatchString(*astate.EntityUrl) {
revid = *astate.CurrentRevision.RevisionId
break
}
}
// Also
execId = *stage.LatestExecution.PipelineExecutionId
}
// save stage details
details := stageDetails{
name: *stage.StageName,
executionId: *stage.LatestExecution.PipelineExecutionId,
status: *stage.LatestExecution.Status,
}
// if stage is from current pipeline execution save revision Id
if execId == details.executionId {
details.revisionId = revid
// if stage was executed earlier - not in this run - retrieve
// revision id from that execution
} else {
pipelineExecutionInput := &codepipeline.GetPipelineExecutionInput{
PipelineExecutionId: &details.executionId,
PipelineName: &cfg.PipelineName,
}
execution, err := pipelnsvc.GetPipelineExecution(pipelineExecutionInput)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Println(aerr.Error())
}
} else {
// Prin the error, cat err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
os.Exit(1)
}
// finally, save revisionId from earlier execution
for _, revision := range execution.PipelineExecution.ArtifactRevisions {
if revRe.MatchString(*revision.RevisionSummary) {
details.revisionId = *revision.RevisionId
}
}
}
meta, err := getMetadataFromRevision(sess, cfg, details.revisionId)
if err != nil {
fmt.Printf("get metadata from file revision: %v\n", err)
os.Exit(1)
}
details.versionDeployed = *meta["Release"]
details.commit = *meta["Commit"]
details.releaseUrl = *meta["Release-Url"]
fmt.Fprintf(w, "%s\t%s\t%s\t\t%s\t\t%s\n", details.name, details.status, details.versionDeployed, details.releaseUrl, details.executionId)
}
}
func getMetadataFromRevision(s *session.Session, cfg Cfg, ver string) (map[string]*string, error) {
// =========================================================================
// S3 client
svc := s3.New(s)
input := &s3.HeadObjectInput{
Bucket: aws.String(cfg.Bucket),
Key: aws.String(cfg.Key),
VersionId: aws.String(ver),
}
result, err := svc.HeadObject(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
return make(map[string]*string), fmt.Errorf("failed to retrieve version metadata: %s", aerr.Message())
}
} else {
return make(map[string]*string), err
}
}
return result.Metadata, nil
}