-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
197 lines (169 loc) · 5.16 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
package main
import (
"errors"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"os"
"os/exec"
"path/filepath"
"strings"
)
var (
pluginVersion = "1.0.0"
)
func main() {
app := cli.NewApp()
app.Name = "drone-s3-upload-publish"
app.Usage = "Drone plugin to upload file/directories to AWS S3 Bucket and display the bucket url under 'Executions > Artifacts' tab"
app.Action = run
app.Version = pluginVersion
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "aws-access-key",
Usage: "AWS Access Key ID",
EnvVar: "PLUGIN_AWS_ACCESS_KEY_ID",
},
cli.StringFlag{
Name: "aws-secret-key",
Usage: "AWS Secret Access Key",
EnvVar: "PLUGIN_AWS_SECRET_ACCESS_KEY",
},
cli.StringFlag{
Name: "aws-default-region",
Usage: "AWS Default Region",
EnvVar: "PLUGIN_AWS_DEFAULT_REGION",
},
cli.StringFlag{
Name: "aws-bucket",
Usage: "AWS S3 Bucket",
EnvVar: "PLUGIN_AWS_BUCKET",
},
cli.StringFlag{
Name: "source",
Usage: "Source",
EnvVar: "PLUGIN_SOURCE",
},
cli.StringFlag{
Name: "target-path",
Usage: "target",
EnvVar: "PLUGIN_TARGET",
},
cli.StringFlag{
Name: "artifact-file",
Usage: "Artifact file",
EnvVar: "PLUGIN_ARTIFACT_FILE",
},
cli.StringFlag{
Name: "glob",
Usage: "Include file patterns int ant style glob style",
EnvVar: "PLUGIN_GLOB",
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
var execCommand = exec.Command
func run(c *cli.Context) error {
awsAccessKey := c.String("aws-access-key")
awsSecretKey := c.String("aws-secret-key")
awsDefaultRegion := c.String("aws-default-region")
awsBucket := c.String("aws-bucket")
source := c.String("source")
target := c.String("target-path")
newFolder := filepath.Base(source)
artifactFilePath := c.String("artifact-file")
includeFilesGlobStr := c.String("glob")
if strings.ContainsAny(source, "*") {
log.Fatal("Glob pattern not allowed!")
}
// AWS config commands to set ACCESS_KEY_ID and SECRET_ACCESS_KEY
execCommand("aws", "configure", "set", "aws_access_key_id", awsAccessKey).Run()
execCommand("aws", "configure", "set", "aws_secret_access_key", awsSecretKey).Run()
urls := ""
urlsList := []string{}
var err error
var urlArtifactFiles []File
switch {
case includeFilesGlobStr != "": // Glob copy
urlsList, err = CopyFilesToS3WithGlobIncludes(awsDefaultRegion, awsBucket, source, target, includeFilesGlobStr)
if err != nil {
log.Println("Error copying files to S3: ", err.Error())
return err
}
for _, url := range urlsList {
urlArtifactFiles = append(urlArtifactFiles, File{Name: artifactFilePath, URL: url})
}
default: // Single file or directory copy
urls, err = CopyToS3(source, target, newFolder, awsBucket, awsDefaultRegion)
if err != nil {
log.Println("Error copying files to S3: ", err.Error())
return err
}
urlArtifactFiles = append(urlArtifactFiles, File{Name: artifactFilePath, URL: urls})
}
return writeArtifactFile(urlArtifactFiles, artifactFilePath)
}
func CopyToS3(source, target, newFolder, awsBucket, awsDefaultRegion string) (string, error) {
fileType, err := os.Stat(source)
if err != nil {
log.Fatal(err)
}
isDir := fileType.IsDir()
s3Path, _, urls := GetPathsAndURLs(target, newFolder, awsBucket, awsDefaultRegion, isDir)
UploadCmd := RunS3CliCopyCmd(source, s3Path, awsDefaultRegion, isDir)
out, err := UploadCmd.Output()
if err != nil {
log.Println("Error uploading to S3: ", err.Error())
return urls, err
}
log.Printf("Output: %s\n", out)
// End of S3 upload operation
return urls, nil
}
func GetPathsAndURLs(target, newFolder, awsBucket, awsDefaultRegion string, isDir bool) (string, string, string) {
urls := ""
prefixPath := awsBucket
if target != "" {
prefixPath += "/" + target
}
s3Path := "s3://" + prefixPath
s3Path += "/" + newFolder
if isDir {
urls = baseURL + "buckets/" + awsBucket + "?region=" + awsDefaultRegion + "&prefix=" + prefixPath + "/" + newFolder + "/&showversions=false"
} else {
urls = baseURL + "object/" + awsBucket + "?region=" + awsDefaultRegion + "&prefix=" + prefixPath + "/" + newFolder
}
return s3Path, prefixPath, urls
}
func RunS3CliCopyCmd(source, s3Path, awsDefaultRegion string, isDir bool) *exec.Cmd {
cliArgs := []string{"s3", "cp", source, s3Path, "--region", awsDefaultRegion}
if isDir {
cliArgs = append(cliArgs, "--recursive")
}
log.Println("aws ", strings.Join(cliArgs, " "))
uploadCmd := execCommand("aws", cliArgs...)
return uploadCmd
}
func CopyFilesToS3WithGlobIncludes(defaultRegion, s3Bucket, source, targetPath,
includesGlob string) ([]string, error) {
var allMatchedFiles []string
globArgsList := GetGlobArgsList(includesGlob)
if globArgsList == nil {
return []string{}, errors.New("Invalid glob pattern")
}
if len(globArgsList) < 1 {
return []string{}, errors.New("No files found")
}
for _, pattern := range globArgsList {
tmpFilesList, err := GetMatchedFiles(source, pattern)
if err != nil {
return []string{}, err
}
allMatchedFiles = append(allMatchedFiles, tmpFilesList...)
}
return BatchCopyFiles(source, allMatchedFiles, targetPath, s3Bucket, defaultRegion, GetCopyBatchSize())
}
const baseURL = "https://s3.console.aws.amazon.com/s3/"
//
//