0
0
mirror of https://github.com/thegeeklab/wp-s3-action.git synced 2024-11-28 19:30:35 +00:00
wp-s3-action/aws.go

387 lines
9.0 KiB
Go
Raw Normal View History

package main
import (
"crypto/md5"
"fmt"
"io"
"mime"
"os"
"path/filepath"
2017-10-22 07:24:40 +00:00
"strings"
2016-03-10 01:22:54 +00:00
"time"
"github.com/aws/aws-sdk-go/aws"
2015-12-31 20:19:34 +00:00
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
2016-07-22 00:27:25 +00:00
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/ryanuber/go-glob"
)
type AWS struct {
2016-03-10 01:22:54 +00:00
client *s3.S3
cfClient *cloudfront.CloudFront
remote []string
local []string
2016-07-22 00:27:25 +00:00
plugin *Plugin
}
2016-07-22 00:27:25 +00:00
func NewAWS(p *Plugin) AWS {
2017-10-28 08:00:06 +00:00
sessCfg := &aws.Config{
2017-10-22 07:24:40 +00:00
Credentials: credentials.NewStaticCredentials(p.Key, p.Secret, ""),
S3ForcePathStyle: aws.Bool(p.PathStyle),
Region: aws.String(p.Region),
2017-10-28 08:00:06 +00:00
}
if p.Endpoint != "" {
sessCfg.Endpoint = &p.Endpoint
sessCfg.DisableSSL = aws.Bool(strings.HasPrefix(p.Endpoint, "http://"))
}
sess := session.New(sessCfg)
c := s3.New(sess)
2016-03-10 01:22:54 +00:00
cf := cloudfront.New(sess)
r := make([]string, 1, 1)
l := make([]string, 1, 1)
2016-07-22 00:27:25 +00:00
return AWS{c, cf, r, l, p}
}
2015-12-20 00:15:04 +00:00
func (a *AWS) Upload(local, remote string) error {
2016-07-22 00:27:25 +00:00
p := a.plugin
2015-12-20 00:15:04 +00:00
if local == "" {
return nil
}
2015-12-20 00:15:04 +00:00
file, err := os.Open(local)
if err != nil {
return err
}
defer file.Close()
2016-10-19 17:56:28 +00:00
var access string
2016-07-22 00:27:25 +00:00
for pattern := range p.Access {
if match := glob.Glob(pattern, local); match == true {
access = p.Access[pattern]
break
}
}
if access == "" {
access = "private"
}
2015-12-20 00:15:04 +00:00
fileExt := filepath.Ext(local)
2016-07-22 00:27:25 +00:00
var contentType string
2016-07-22 00:27:25 +00:00
for patternExt := range p.ContentType {
if patternExt == fileExt {
contentType = p.ContentType[patternExt]
break
}
}
2016-07-22 00:27:25 +00:00
if contentType == "" {
contentType = mime.TypeByExtension(fileExt)
}
var contentEncoding string
2016-07-22 00:27:25 +00:00
for patternExt := range p.ContentEncoding {
if patternExt == fileExt {
contentEncoding = p.ContentEncoding[patternExt]
break
}
}
2016-10-19 17:56:28 +00:00
var cacheControl string
for pattern := range p.CacheControl {
if match := glob.Glob(pattern, local); match == true {
cacheControl = p.CacheControl[pattern]
break
}
}
metadata := map[string]*string{}
2016-07-22 00:27:25 +00:00
for pattern := range p.Metadata {
if match := glob.Glob(pattern, local); match == true {
for k, v := range p.Metadata[pattern] {
metadata[k] = aws.String(v)
}
2016-07-22 00:27:25 +00:00
break
}
}
head, err := a.client.HeadObject(&s3.HeadObjectInput{
2016-07-22 00:27:25 +00:00
Bucket: aws.String(p.Bucket),
2015-12-20 00:15:04 +00:00
Key: aws.String(remote),
})
2016-11-19 13:34:46 +00:00
if err != nil && err.(awserr.Error).Code() != "404" {
if err.(awserr.Error).Code() == "404" {
2015-12-31 20:19:34 +00:00
return err
}
debug("\"%s\" not found in bucket, uploading with Content-Type \"%s\" and permissions \"%s\"", local, contentType, access)
var putObject = &s3.PutObjectInput{
2016-07-22 00:27:25 +00:00
Bucket: aws.String(p.Bucket),
2015-12-31 20:19:34 +00:00
Key: aws.String(remote),
Body: file,
ContentType: aws.String(contentType),
ACL: aws.String(access),
Metadata: metadata,
}
2016-10-19 17:56:28 +00:00
if len(cacheControl) > 0 {
putObject.CacheControl = aws.String(cacheControl)
}
2016-07-22 00:27:25 +00:00
if len(contentEncoding) > 0 {
putObject.ContentEncoding = aws.String(contentEncoding)
}
2016-11-19 13:34:46 +00:00
// skip upload during dry run
if a.plugin.DryRun {
return nil
}
_, err = a.client.PutObject(putObject)
return err
}
2015-12-31 20:19:34 +00:00
hash := md5.New()
io.Copy(hash, file)
sum := fmt.Sprintf("\"%x\"", hash.Sum(nil))
2015-12-31 20:19:34 +00:00
if sum == *head.ETag {
shouldCopy := false
2015-12-31 20:19:34 +00:00
if head.ContentType == nil && contentType != "" {
debug("Content-Type has changed from unset to %s", contentType)
shouldCopy = true
}
2015-12-31 20:19:34 +00:00
if !shouldCopy && head.ContentType != nil && contentType != *head.ContentType {
debug("Content-Type has changed from %s to %s", *head.ContentType, contentType)
shouldCopy = true
}
if !shouldCopy && head.ContentEncoding == nil && contentEncoding != "" {
debug("Content-Encoding has changed from unset to %s", contentEncoding)
shouldCopy = true
}
if !shouldCopy && head.ContentEncoding != nil && contentEncoding != *head.ContentEncoding {
debug("Content-Encoding has changed from %s to %s", *head.ContentEncoding, contentEncoding)
shouldCopy = true
}
2016-10-19 17:56:28 +00:00
if !shouldCopy && head.CacheControl == nil && cacheControl != "" {
debug("Cache-Control has changed from unset to %s", cacheControl)
shouldCopy = true
}
if !shouldCopy && head.CacheControl != nil && cacheControl != *head.CacheControl {
debug("Cache-Control has changed from %s to %s", *head.CacheControl, cacheControl)
shouldCopy = true
}
2015-12-31 20:19:34 +00:00
if !shouldCopy && len(head.Metadata) != len(metadata) {
debug("Count of metadata values has changed for %s", local)
shouldCopy = true
}
2015-12-31 20:19:34 +00:00
if !shouldCopy && len(metadata) > 0 {
for k, v := range metadata {
if hv, ok := head.Metadata[k]; ok {
if *v != *hv {
debug("Metadata values have changed for %s", local)
shouldCopy = true
break
}
}
}
2015-12-31 20:19:34 +00:00
}
2015-12-31 20:19:34 +00:00
if !shouldCopy {
grant, err := a.client.GetObjectAcl(&s3.GetObjectAclInput{
2016-07-22 00:27:25 +00:00
Bucket: aws.String(p.Bucket),
2015-12-31 20:19:34 +00:00
Key: aws.String(remote),
})
if err != nil {
return err
}
2015-12-31 20:19:34 +00:00
previousAccess := "private"
for _, g := range grant.Grants {
gt := *g.Grantee
if gt.URI != nil {
if *gt.URI == "http://acs.amazonaws.com/groups/global/AllUsers" {
if *g.Permission == "READ" {
previousAccess = "public-read"
} else if *g.Permission == "WRITE" {
previousAccess = "public-read-write"
}
} else if *gt.URI == "http://acs.amazonaws.com/groups/global/AllUsers" {
if *g.Permission == "READ" {
previousAccess = "authenticated-read"
}
}
}
}
2015-12-31 20:19:34 +00:00
if previousAccess != access {
debug("Permissions for \"%s\" have changed from \"%s\" to \"%s\"", remote, previousAccess, access)
shouldCopy = true
}
}
2015-12-31 20:19:34 +00:00
if !shouldCopy {
debug("Skipping \"%s\" because hashes and metadata match", local)
return nil
}
2015-12-31 20:19:34 +00:00
debug("Updating metadata for \"%s\" Content-Type: \"%s\", ACL: \"%s\"", local, contentType, access)
var copyObject = &s3.CopyObjectInput{
2016-07-22 00:27:25 +00:00
Bucket: aws.String(p.Bucket),
2015-12-31 20:19:34 +00:00
Key: aws.String(remote),
2016-07-22 00:27:25 +00:00
CopySource: aws.String(fmt.Sprintf("%s/%s", p.Bucket, remote)),
2015-12-31 20:19:34 +00:00
ACL: aws.String(access),
ContentType: aws.String(contentType),
Metadata: metadata,
MetadataDirective: aws.String("REPLACE"),
}
2016-10-19 17:56:28 +00:00
if len(cacheControl) > 0 {
copyObject.CacheControl = aws.String(cacheControl)
}
2016-07-22 00:27:25 +00:00
if len(contentEncoding) > 0 {
copyObject.ContentEncoding = aws.String(contentEncoding)
}
2016-11-19 13:34:46 +00:00
// skip update if dry run
if a.plugin.DryRun {
return nil
}
_, err = a.client.CopyObject(copyObject)
2015-12-31 20:19:34 +00:00
return err
2015-12-31 20:52:34 +00:00
} else {
2015-12-31 21:19:41 +00:00
_, err = file.Seek(0, 0)
if err != nil {
return err
}
2015-12-31 20:52:34 +00:00
debug("Uploading \"%s\" with Content-Type \"%s\" and permissions \"%s\"", local, contentType, access)
var putObject = &s3.PutObjectInput{
2016-07-22 00:27:25 +00:00
Bucket: aws.String(p.Bucket),
2015-12-31 20:52:34 +00:00
Key: aws.String(remote),
Body: file,
ContentType: aws.String(contentType),
ACL: aws.String(access),
Metadata: metadata,
}
2016-10-19 17:56:28 +00:00
if len(cacheControl) > 0 {
putObject.CacheControl = aws.String(cacheControl)
}
2016-07-22 00:27:25 +00:00
if len(contentEncoding) > 0 {
putObject.ContentEncoding = aws.String(contentEncoding)
}
2016-11-19 13:34:46 +00:00
// skip upload if dry run
if a.plugin.DryRun {
return nil
}
_, err = a.client.PutObject(putObject)
2015-12-31 20:52:34 +00:00
return err
}
}
2015-12-20 00:15:04 +00:00
func (a *AWS) Redirect(path, location string) error {
2016-07-22 00:27:25 +00:00
p := a.plugin
2015-12-31 20:25:40 +00:00
debug("Adding redirect from \"%s\" to \"%s\"", path, location)
2016-11-19 13:34:46 +00:00
if a.plugin.DryRun {
return nil
}
2015-12-20 00:15:04 +00:00
_, err := a.client.PutObject(&s3.PutObjectInput{
Bucket: aws.String(p.Bucket),
Key: aws.String(path),
ACL: aws.String("public-read"),
2015-12-20 00:15:04 +00:00
WebsiteRedirectLocation: aws.String(location),
})
return err
}
2015-12-04 19:24:34 +00:00
2015-12-20 00:15:04 +00:00
func (a *AWS) Delete(remote string) error {
2016-07-22 00:27:25 +00:00
p := a.plugin
2015-12-31 20:25:40 +00:00
debug("Removing remote file \"%s\"", remote)
2016-11-19 13:34:46 +00:00
if a.plugin.DryRun {
return nil
}
2015-12-20 00:15:04 +00:00
_, err := a.client.DeleteObject(&s3.DeleteObjectInput{
2016-07-22 00:27:25 +00:00
Bucket: aws.String(p.Bucket),
2015-12-20 00:15:04 +00:00
Key: aws.String(remote),
})
return err
2015-12-04 19:24:34 +00:00
}
2015-12-20 00:15:04 +00:00
func (a *AWS) List(path string) ([]string, error) {
2016-07-22 00:27:25 +00:00
p := a.plugin
2015-12-20 00:15:04 +00:00
remote := make([]string, 1, 1)
resp, err := a.client.ListObjects(&s3.ListObjectsInput{
2016-07-22 00:27:25 +00:00
Bucket: aws.String(p.Bucket),
Prefix: aws.String(path),
})
if err != nil {
2015-12-20 00:15:04 +00:00
return remote, err
}
for _, item := range resp.Contents {
2015-12-20 00:15:04 +00:00
remote = append(remote, *item.Key)
}
for *resp.IsTruncated {
resp, err = a.client.ListObjects(&s3.ListObjectsInput{
2016-07-22 00:27:25 +00:00
Bucket: aws.String(p.Bucket),
Prefix: aws.String(path),
2015-12-20 00:15:04 +00:00
Marker: aws.String(remote[len(remote)-1]),
})
if err != nil {
2015-12-20 00:15:04 +00:00
return remote, err
}
for _, item := range resp.Contents {
2015-12-20 00:15:04 +00:00
remote = append(remote, *item.Key)
}
}
2015-12-20 00:15:04 +00:00
return remote, nil
}
2016-03-10 01:22:54 +00:00
func (a *AWS) Invalidate(invalidatePath string) error {
2016-07-22 00:27:25 +00:00
p := a.plugin
2016-03-10 01:22:54 +00:00
debug("Invalidating \"%s\"", invalidatePath)
_, err := a.cfClient.CreateInvalidation(&cloudfront.CreateInvalidationInput{
2016-07-22 00:27:25 +00:00
DistributionId: aws.String(p.CloudFrontDistribution),
2016-03-10 01:22:54 +00:00
InvalidationBatch: &cloudfront.InvalidationBatch{
CallerReference: aws.String(time.Now().Format(time.RFC3339Nano)),
Paths: &cloudfront.Paths{
Quantity: aws.Int64(1),
Items: []*string{
aws.String(invalidatePath),
},
},
},
})
return err
}