-
Notifications
You must be signed in to change notification settings - Fork 0
/
artifact.go
121 lines (87 loc) · 2.63 KB
/
artifact.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
package deployer
import (
"archive/zip"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/papabearsoftware/eks-lambda-deployer/internal/util"
)
type DeploymentJSON struct {
Cluster string `json:"cluster"`
RollbackOnFail bool `json:"rollback_on_fail"`
Deployment string `json:"deployment"`
Namespace string `json:"namespace"`
Containers []ContainerDefinitions `json:"containers"`
}
type ContainerDefinitions struct {
ContainerName string `json:"container_name"`
Tag string `json:"tag"`
}
func retrieveS3Artifact(bucket string, key string) error {
f, err := os.Create(fmt.Sprintf("/tmp/artifact-%s.zip", jobID))
if err != nil {
util.LogError(fmt.Sprintf("Error creating '/tmp/artifact-%s.zip'", jobID), err.Error())
return err
}
defer f.Close()
downloader := s3manager.NewDownloader(sess)
util.LogInfo(fmt.Sprintf("Retrieving input artifact from %s/%s", bucket, key))
_, err = downloader.Download(f,
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
util.LogError(fmt.Sprintf("Error downloading %s/%s", bucket, key), err.Error())
return err
}
util.LogDebug("Downloaded input artifact")
return nil
}
func parseArtifact() error {
util.LogDebug("Starting artifact parsing")
r, err := zip.OpenReader(fmt.Sprintf("/tmp/artifact-%s.zip", jobID))
if err != nil {
util.LogError(fmt.Sprintf("Error opening '/tmp/artifact-%s.zip'", jobID), err.Error())
return err
}
defer r.Close()
util.LogDebug("Created zip reader")
configExists := false
for _, f := range r.File {
if f.Name == "deployer_config.json" {
util.LogDebug("Iterating through files in archive")
configExists = true
rc, err := f.Open()
util.LogDebug("Opened deployer_config.json")
if err != nil {
util.LogError("Error opening config file", err.Error())
return err
}
body, err := ioutil.ReadAll(rc)
util.LogDebug("Read file body into []byte")
if err != nil {
util.LogError("Error reading config file", err.Error())
return err
}
err = json.Unmarshal(body, &deploymentJSON)
if err != nil {
util.LogError("Error umarshaling config file contents into struct", err.Error())
return err
}
util.LogDebug("Unmarshalled successfully")
rc.Close()
}
}
if !configExists {
util.LogError("Config file not found!", "")
return errors.New("Config file not found")
}
util.LogDebug("Returning nil error")
return nil
}