mirror of
https://github.com/thegeeklab/wp-s3-action.git
synced 2024-11-22 01:00:40 +00:00
first iteration using native s3 library
This commit is contained in:
parent
e1a8ff1282
commit
eb8b4e32bd
27
DOCS.md
27
DOCS.md
@ -8,8 +8,6 @@ Use the S3 sync plugin to synchronize files and folders with an Amazon S3 bucket
|
|||||||
* `source` - location of folder to sync
|
* `source` - location of folder to sync
|
||||||
* `target` - target folder in your S3 bucket
|
* `target` - target folder in your S3 bucket
|
||||||
* `delete` - deletes files in the target not found in the source
|
* `delete` - deletes files in the target not found in the source
|
||||||
* `include` - don't exclude files that match the specified pattern
|
|
||||||
* `exclude` - exclude files that match the specified pattern
|
|
||||||
* `content_type` - override default mime-tpyes to use this value
|
* `content_type` - override default mime-tpyes to use this value
|
||||||
|
|
||||||
The following is a sample S3 configuration in your .drone.yml file:
|
The following is a sample S3 configuration in your .drone.yml file:
|
||||||
@ -26,3 +24,28 @@ publish:
|
|||||||
target: /target/location
|
target: /target/location
|
||||||
delete: true
|
delete: true
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Both `acl` and `content_type` can be passed as a string value to apply to all files, or as a map to apply to a subset of files.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
publish:
|
||||||
|
s3_sync:
|
||||||
|
acl:
|
||||||
|
"public/*": public-read
|
||||||
|
"private/*": private
|
||||||
|
content_type:
|
||||||
|
".svg": image/svg+xml
|
||||||
|
region: "us-east-1"
|
||||||
|
bucket: "my-bucket.s3-website-us-east-1.amazonaws.com"
|
||||||
|
access_key: "970d28f4dd477bc184fbd10b376de753"
|
||||||
|
secret_key: "9c5785d3ece6a9cdefa42eb99b58986f9095ff1c"
|
||||||
|
source: folder/to/archive
|
||||||
|
target: /target/location
|
||||||
|
delete: true
|
||||||
|
```
|
||||||
|
|
||||||
|
In the case of `acl` the key of the map is a glob as matched by [filepath.Match](https://golang.org/pkg/path/filepath/#Match). If there are no matches in your settings for a given file, the default is `"private"`.
|
||||||
|
|
||||||
|
The `content_type` field the key is an extension including the leading dot `.`. If you want to set a content type for files with no extension, set the key to the empty string `""`. If there are no matches for the `content_type` of any file, one will automatically be determined for you.
|
||||||
|
323
main.go
323
main.go
@ -1,161 +1,244 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"mime"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/drone/drone-plugin-go/plugin"
|
"github.com/drone/drone-go/plugin"
|
||||||
|
"launchpad.net/goamz/aws"
|
||||||
|
"launchpad.net/goamz/s3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type S3 struct {
|
type AWS struct {
|
||||||
Key string `json:"access_key"`
|
client *s3.S3
|
||||||
Secret string `json:"secret_key"`
|
bucket *s3.Bucket
|
||||||
Bucket string `json:"bucket"`
|
remote []string
|
||||||
|
local []string
|
||||||
|
vargs PluginArgs
|
||||||
|
}
|
||||||
|
|
||||||
// us-east-1
|
type StringMap struct {
|
||||||
// us-west-1
|
parts map[string]string
|
||||||
// us-west-2
|
}
|
||||||
// eu-west-1
|
|
||||||
// ap-southeast-1
|
|
||||||
// ap-southeast-2
|
|
||||||
// ap-northeast-1
|
|
||||||
// sa-east-1
|
|
||||||
Region string `json:"region"`
|
|
||||||
|
|
||||||
// Indicates the files ACL, which should be one
|
func (e *StringMap) UnmarshalJSON(b []byte) error {
|
||||||
// of the following:
|
if len(b) == 0 {
|
||||||
// private
|
return nil
|
||||||
// public-read
|
}
|
||||||
// public-read-write
|
|
||||||
// authenticated-read
|
|
||||||
// bucket-owner-read
|
|
||||||
// bucket-owner-full-control
|
|
||||||
Access string `json:"acl"`
|
|
||||||
|
|
||||||
// Copies the files from the specified directory.
|
p := map[string]string{}
|
||||||
// Regexp matching will apply to match multiple
|
if err := json.Unmarshal(b, &p); err != nil {
|
||||||
// files
|
var s string
|
||||||
//
|
if err := json.Unmarshal(b, &s); err != nil {
|
||||||
// Examples:
|
return err
|
||||||
// /path/to/file
|
}
|
||||||
// /path/to/*.txt
|
p["_string_"] = s
|
||||||
// /path/to/*/*.txt
|
}
|
||||||
// /path/to/**
|
|
||||||
Source string `json:"source"`
|
|
||||||
Target string `json:"target"`
|
|
||||||
|
|
||||||
// Include or exclude all files or objects from the command
|
e.parts = p
|
||||||
// that matches the specified pattern.
|
return nil
|
||||||
Include string `json:"include"`
|
}
|
||||||
Exclude string `json:"exclude"`
|
|
||||||
|
|
||||||
// Files that exist in the destination but not in the source
|
func (e *StringMap) IsEmpty() bool {
|
||||||
// are deleted during sync.
|
if e == nil || len(e.parts) == 0 {
|
||||||
Delete bool `json:"delete"`
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// Specify an explicit content type for this operation. This
|
return false
|
||||||
// value overrides any guessed mime types.
|
}
|
||||||
ContentType string `json:"content_type"`
|
|
||||||
|
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 {
|
||||||
|
auth := aws.Auth{AccessKey: vargs.Key, SecretKey: vargs.Secret}
|
||||||
|
region := aws.Regions[vargs.Region]
|
||||||
|
client := s3.New(auth, region)
|
||||||
|
bucket := client.Bucket(vargs.Bucket)
|
||||||
|
remote := make([]string, 1, 1)
|
||||||
|
local := make([]string, 1, 1)
|
||||||
|
|
||||||
|
aws := AWS{client, bucket, remote, local, vargs}
|
||||||
|
return aws
|
||||||
|
}
|
||||||
|
|
||||||
|
func (aws *AWS) visit(path string, info os.FileInfo, err error) error {
|
||||||
|
if path == "." {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
aws.local = append(aws.local, path)
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
var access s3.ACL
|
||||||
|
if aws.vargs.Access.IsString() {
|
||||||
|
access = s3.ACL(aws.vargs.Access.String())
|
||||||
|
} else if !aws.vargs.Access.IsEmpty() {
|
||||||
|
accessMap := aws.vargs.Access.Map()
|
||||||
|
for pattern := range accessMap {
|
||||||
|
if match, _ := filepath.Match(pattern, path); match == true {
|
||||||
|
access = s3.ACL(accessMap[pattern])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if access == "" {
|
||||||
|
access = s3.ACL("private")
|
||||||
|
}
|
||||||
|
|
||||||
|
fileExt := filepath.Ext(path)
|
||||||
|
var contentType string
|
||||||
|
if aws.vargs.ContentType.IsString() {
|
||||||
|
contentType = aws.vargs.ContentType.String()
|
||||||
|
} else if !aws.vargs.ContentType.IsEmpty() {
|
||||||
|
contentMap := aws.vargs.ContentType.Map()
|
||||||
|
for patternExt := range contentMap {
|
||||||
|
if patternExt == fileExt {
|
||||||
|
contentType = contentMap[patternExt]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if contentType == "" {
|
||||||
|
contentType = mime.TypeByExtension(fileExt)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Uploading %s with Content-Type %s and permissions %s\n", path, contentType, access)
|
||||||
|
err = aws.bucket.PutReader(path, file, info.Size(), contentType, access)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (aws *AWS) List(path string) (*s3.ListResp, error) {
|
||||||
|
return aws.bucket.List(path, "", "", 10000)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (aws *AWS) Cleanup() error {
|
||||||
|
for _, remote := range aws.remote {
|
||||||
|
found := false
|
||||||
|
for _, local := range aws.local {
|
||||||
|
if local == remote {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
fmt.Println("Removing remote file ", remote)
|
||||||
|
err := aws.bucket.Del(remote)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
workspace := plugin.Workspace{}
|
vargs := PluginArgs{}
|
||||||
vargs := S3{}
|
|
||||||
|
|
||||||
plugin.Param("workspace", &workspace)
|
|
||||||
plugin.Param("vargs", &vargs)
|
plugin.Param("vargs", &vargs)
|
||||||
plugin.MustParse()
|
if err := plugin.Parse(); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
// skip if AWS key or SECRET are empty. A good example for this would
|
if len(vargs.Key) == 0 || len(vargs.Secret) == 0 || len(vargs.Bucket) == 0 {
|
||||||
// be forks building a project. S3 might be configured in the source
|
|
||||||
// repo, but not in the fork
|
|
||||||
if len(vargs.Key) == 0 || len(vargs.Secret) == 0 {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure a default region is set
|
|
||||||
if len(vargs.Region) == 0 {
|
if len(vargs.Region) == 0 {
|
||||||
vargs.Region = "us-east-1"
|
vargs.Region = "us-east-1"
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure a default access is set
|
|
||||||
// let's be conservative and assume private
|
|
||||||
if len(vargs.Access) == 0 {
|
|
||||||
vargs.Access = "private"
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure a default source is set
|
|
||||||
if len(vargs.Source) == 0 {
|
if len(vargs.Source) == 0 {
|
||||||
vargs.Source = "."
|
vargs.Source = "."
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the target starts with a "/" we need
|
|
||||||
// to remove it, otherwise we might adding
|
|
||||||
// a 3rd slash to s3://
|
|
||||||
if strings.HasPrefix(vargs.Target, "/") {
|
if strings.HasPrefix(vargs.Target, "/") {
|
||||||
vargs.Target = vargs.Target[1:]
|
vargs.Target = vargs.Target[1:]
|
||||||
}
|
}
|
||||||
vargs.Target = fmt.Sprintf("s3://%s/%s", vargs.Bucket, vargs.Target)
|
|
||||||
|
|
||||||
cmd := command(vargs)
|
if vargs.Target != "" && !strings.HasSuffix(vargs.Target, "/") {
|
||||||
cmd.Env = os.Environ()
|
vargs.Target = fmt.Sprintf("%s/", vargs.Target)
|
||||||
cmd.Env = append(cmd.Env, "AWS_ACCESS_KEY_ID="+vargs.Key)
|
}
|
||||||
cmd.Env = append(cmd.Env, "AWS_SECRET_ACCESS_KEY="+vargs.Secret)
|
|
||||||
cmd.Dir = workspace.Path
|
|
||||||
cmd.Stdout = os.Stdout
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
trace(cmd)
|
|
||||||
|
|
||||||
// run the command and exit if failed.
|
client := NewClient(vargs)
|
||||||
err := cmd.Run()
|
|
||||||
|
resp, err := client.List(vargs.Target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
for _, item := range resp.Contents {
|
||||||
// command is a helper function that returns the command
|
client.remote = append(client.remote, item.Key)
|
||||||
// and arguments to upload to aws from the command line.
|
}
|
||||||
func command(s S3) *exec.Cmd {
|
|
||||||
|
err = filepath.Walk(vargs.Source, client.visit)
|
||||||
// command line args
|
if err != nil {
|
||||||
args := []string{
|
fmt.Println(err)
|
||||||
"s3",
|
os.Exit(1)
|
||||||
"sync",
|
}
|
||||||
s.Source,
|
|
||||||
s.Target,
|
if vargs.Delete {
|
||||||
"--acl",
|
err = client.Cleanup()
|
||||||
s.Access,
|
if err != nil {
|
||||||
"--region",
|
fmt.Println(err)
|
||||||
s.Region,
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// append delete flag if specified
|
|
||||||
if s.Delete {
|
|
||||||
args = append(args, "--delete")
|
|
||||||
}
|
|
||||||
// appends exclude flag if specified
|
|
||||||
if len(s.Exclude) != 0 {
|
|
||||||
args = append(args, "--exclude")
|
|
||||||
args = append(args, s.Exclude)
|
|
||||||
}
|
|
||||||
// append include flag if specified
|
|
||||||
if len(s.Include) != 0 {
|
|
||||||
args = append(args, "--include")
|
|
||||||
args = append(args, s.Include)
|
|
||||||
}
|
|
||||||
// appends content-type if specified
|
|
||||||
if len(s.ContentType) != 0 {
|
|
||||||
args = append(args, "--content-type")
|
|
||||||
args = append(args, s.ContentType)
|
|
||||||
}
|
|
||||||
|
|
||||||
return exec.Command("aws", args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// trace writes each command to standard error (preceded by a ‘$ ’) before it
|
|
||||||
// is executed. Used for debugging your build.
|
|
||||||
func trace(cmd *exec.Cmd) {
|
|
||||||
fmt.Println("$", strings.Join(cmd.Args, " "))
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user