2015-10-16 20:09:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-11-13 23:13:38 +00:00
|
|
|
"encoding/json"
|
2015-10-16 20:09:45 +00:00
|
|
|
"fmt"
|
2015-11-13 23:13:38 +00:00
|
|
|
"mime"
|
2015-10-16 20:09:45 +00:00
|
|
|
"os"
|
2015-11-13 23:13:38 +00:00
|
|
|
"path/filepath"
|
2015-10-16 20:09:45 +00:00
|
|
|
"strings"
|
|
|
|
|
2015-11-14 00:17:17 +00:00
|
|
|
"github.com/drone/drone-go/drone"
|
2015-11-13 23:13:38 +00:00
|
|
|
"github.com/drone/drone-go/plugin"
|
2015-11-14 15:49:43 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
2015-10-16 20:09:45 +00:00
|
|
|
)
|
|
|
|
|
2015-11-13 23:13:38 +00:00
|
|
|
type AWS struct {
|
|
|
|
client *s3.S3
|
2015-11-14 15:49:43 +00:00
|
|
|
uploader *s3manager.Uploader
|
2015-11-13 23:13:38 +00:00
|
|
|
remote []string
|
|
|
|
local []string
|
|
|
|
vargs PluginArgs
|
|
|
|
}
|
|
|
|
|
|
|
|
type StringMap struct {
|
|
|
|
parts map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StringMap) UnmarshalJSON(b []byte) error {
|
|
|
|
if len(b) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := map[string]string{}
|
|
|
|
if err := json.Unmarshal(b, &p); err != nil {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p["_string_"] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
e.parts = p
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StringMap) IsEmpty() bool {
|
|
|
|
if e == nil || len(e.parts) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StringMap) IsString() bool {
|
|
|
|
if e.IsEmpty() || len(e.parts) != 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := e.parts["_string_"]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StringMap) String() string {
|
|
|
|
if e.IsEmpty() || !e.IsString() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.parts["_string_"]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StringMap) Map() map[string]string {
|
|
|
|
if e.IsEmpty() || e.IsString() {
|
|
|
|
return map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.parts
|
|
|
|
}
|
|
|
|
|
|
|
|
type PluginArgs struct {
|
|
|
|
Key string `json:"access_key"`
|
|
|
|
Secret string `json:"secret_key"`
|
|
|
|
Bucket string `json:"bucket"`
|
|
|
|
Region string `json:"region"`
|
|
|
|
Source string `json:"source"`
|
|
|
|
Target string `json:"target"`
|
|
|
|
Delete bool `json:"delete"`
|
|
|
|
Access StringMap `json:"acl"`
|
|
|
|
ContentType StringMap `json:"content_type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(vargs PluginArgs) AWS {
|
2015-11-14 15:49:43 +00:00
|
|
|
sess := session.New(&aws.Config{
|
|
|
|
Credentials: credentials.NewStaticCredentials(vargs.Key, vargs.Secret, ""),
|
|
|
|
Region: aws.String(vargs.Region),
|
|
|
|
})
|
|
|
|
client := s3.New(sess)
|
|
|
|
uploader := s3manager.NewUploader(sess)
|
2015-11-13 23:13:38 +00:00
|
|
|
remote := make([]string, 1, 1)
|
|
|
|
local := make([]string, 1, 1)
|
|
|
|
|
2015-11-14 15:49:43 +00:00
|
|
|
a := AWS{client, uploader, remote, local, vargs}
|
|
|
|
return a
|
2015-11-13 23:13:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 15:49:43 +00:00
|
|
|
func (a *AWS) visit(path string, info os.FileInfo, err error) error {
|
2015-11-13 23:59:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-13 23:13:38 +00:00
|
|
|
if path == "." {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-14 15:49:43 +00:00
|
|
|
localPath := strings.TrimPrefix(path, a.vargs.Source)
|
2015-11-14 00:24:54 +00:00
|
|
|
if strings.HasPrefix(localPath, "/") {
|
|
|
|
localPath = localPath[1:]
|
|
|
|
}
|
|
|
|
|
2015-11-14 15:49:43 +00:00
|
|
|
a.local = append(a.local, localPath)
|
2015-11-13 23:13:38 +00:00
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
2015-11-14 15:49:43 +00:00
|
|
|
access := ""
|
|
|
|
if a.vargs.Access.IsString() {
|
|
|
|
access = a.vargs.Access.String()
|
|
|
|
} else if !a.vargs.Access.IsEmpty() {
|
|
|
|
accessMap := a.vargs.Access.Map()
|
2015-11-13 23:13:38 +00:00
|
|
|
for pattern := range accessMap {
|
2015-11-14 00:24:54 +00:00
|
|
|
if match, _ := filepath.Match(pattern, localPath); match == true {
|
2015-11-14 15:49:43 +00:00
|
|
|
access = accessMap[pattern]
|
2015-11-13 23:13:38 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if access == "" {
|
2015-11-14 15:49:43 +00:00
|
|
|
access = "private"
|
2015-11-13 23:13:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 00:24:54 +00:00
|
|
|
fileExt := filepath.Ext(localPath)
|
2015-11-13 23:13:38 +00:00
|
|
|
var contentType string
|
2015-11-14 15:49:43 +00:00
|
|
|
if a.vargs.ContentType.IsString() {
|
|
|
|
contentType = a.vargs.ContentType.String()
|
|
|
|
} else if !a.vargs.ContentType.IsEmpty() {
|
|
|
|
contentMap := a.vargs.ContentType.Map()
|
2015-11-13 23:13:38 +00:00
|
|
|
for patternExt := range contentMap {
|
|
|
|
if patternExt == fileExt {
|
|
|
|
contentType = contentMap[patternExt]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if contentType == "" {
|
|
|
|
contentType = mime.TypeByExtension(fileExt)
|
|
|
|
}
|
|
|
|
|
2015-11-14 15:49:43 +00:00
|
|
|
fmt.Printf("Uploading \"%s\" with Content-Type \"%s\" and permissions \"%s\"\n", localPath, contentType, access)
|
|
|
|
_, err = a.uploader.Upload(&s3manager.UploadInput{
|
|
|
|
Bucket: aws.String(a.vargs.Bucket),
|
|
|
|
Key: aws.String(filepath.Join(a.vargs.Target, localPath)),
|
|
|
|
Body: file,
|
|
|
|
ContentType: aws.String(contentType),
|
|
|
|
ACL: aws.String(access),
|
|
|
|
})
|
2015-11-13 23:13:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-14 15:49:43 +00:00
|
|
|
func (a *AWS) List(path string) (*s3.ListObjectsOutput, error) {
|
|
|
|
return a.client.ListObjects(&s3.ListObjectsInput{
|
|
|
|
Bucket: aws.String(a.vargs.Bucket),
|
|
|
|
Prefix: aws.String(path),
|
|
|
|
})
|
2015-11-13 23:13:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 15:49:43 +00:00
|
|
|
func (a *AWS) Cleanup() error {
|
|
|
|
for _, remote := range a.remote {
|
2015-11-13 23:13:38 +00:00
|
|
|
found := false
|
2015-11-14 15:49:43 +00:00
|
|
|
for _, local := range a.local {
|
2015-11-13 23:13:38 +00:00
|
|
|
if local == remote {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2015-11-14 15:49:43 +00:00
|
|
|
fmt.Printf("Removing remote file \"%s\"\n", remote)
|
|
|
|
_, err := a.client.DeleteObject(&s3.DeleteObjectInput{
|
|
|
|
Bucket: aws.String(a.vargs.Bucket),
|
|
|
|
Key: aws.String(remote),
|
|
|
|
})
|
2015-11-13 23:13:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-10-16 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2015-11-13 23:13:38 +00:00
|
|
|
vargs := PluginArgs{}
|
2015-11-14 00:17:17 +00:00
|
|
|
workspace := drone.Workspace{}
|
2015-10-16 20:09:45 +00:00
|
|
|
|
|
|
|
plugin.Param("vargs", &vargs)
|
2015-11-14 00:17:17 +00:00
|
|
|
plugin.Param("workspace", &workspace)
|
2015-11-13 23:13:38 +00:00
|
|
|
if err := plugin.Parse(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-10-16 20:09:45 +00:00
|
|
|
|
2015-11-13 23:13:38 +00:00
|
|
|
if len(vargs.Key) == 0 || len(vargs.Secret) == 0 || len(vargs.Bucket) == 0 {
|
2015-10-16 20:09:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(vargs.Region) == 0 {
|
|
|
|
vargs.Region = "us-east-1"
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(vargs.Source) == 0 {
|
|
|
|
vargs.Source = "."
|
|
|
|
}
|
2015-11-14 00:17:17 +00:00
|
|
|
vargs.Source = filepath.Join(workspace.Path, vargs.Source)
|
2015-11-14 00:11:26 +00:00
|
|
|
|
2015-10-16 20:09:45 +00:00
|
|
|
if strings.HasPrefix(vargs.Target, "/") {
|
|
|
|
vargs.Target = vargs.Target[1:]
|
|
|
|
}
|
|
|
|
|
2015-11-13 23:13:38 +00:00
|
|
|
client := NewClient(vargs)
|
2015-10-16 20:09:45 +00:00
|
|
|
|
2015-11-13 23:13:38 +00:00
|
|
|
resp, err := client.List(vargs.Target)
|
2015-10-16 20:09:45 +00:00
|
|
|
if err != nil {
|
2015-11-13 23:13:38 +00:00
|
|
|
fmt.Println(err)
|
2015-10-16 20:09:45 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2015-11-13 23:13:38 +00:00
|
|
|
for _, item := range resp.Contents {
|
2015-11-14 15:49:43 +00:00
|
|
|
client.remote = append(client.remote, *item.Key)
|
2015-10-16 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 23:13:38 +00:00
|
|
|
err = filepath.Walk(vargs.Source, client.visit)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2015-10-16 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 23:13:38 +00:00
|
|
|
if vargs.Delete {
|
|
|
|
err = client.Cleanup()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2015-10-16 20:09:45 +00:00
|
|
|
}
|