drone-s3-sync/main.go

240 lines
4.4 KiB
Go
Raw Normal View History

2015-10-16 20:09:45 +00:00
package main
import (
"errors"
2015-10-16 20:09:45 +00:00
"fmt"
"os"
"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"
"github.com/drone/drone-go/plugin"
2015-10-16 20:09:45 +00:00
)
2015-12-20 00:15:04 +00:00
const maxConcurrent = 100
type job struct {
local string
remote string
action string
}
type result struct {
j job
err error
}
type app struct {
vargs *PluginArgs
workspace *drone.Workspace
client AWS
jobs []job
}
var (
buildCommit string
)
var MissingAwsValuesMessage = "Must set access_key, secret_key, and bucket"
2015-10-16 20:09:45 +00:00
func main() {
fmt.Printf("Drone S3 Sync Plugin built from %s\n", buildCommit)
a := newApp()
2015-10-16 20:09:45 +00:00
err := a.loadVargs()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = a.sanitizeInputs()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
2015-10-16 20:09:45 +00:00
2016-03-14 21:12:37 +00:00
a.createClient()
a.createSyncJobs()
a.createInvalidateJob()
a.runJobs()
fmt.Println("done!")
}
func newApp() *app {
return &app{
vargs: &PluginArgs{},
workspace: &drone.Workspace{},
jobs: make([]job, 1, 1),
}
}
func (a *app) loadVargs() error {
plugin.Param("vargs", a.vargs)
plugin.Param("workspace", a.workspace)
2016-03-14 21:12:37 +00:00
err := plugin.Parse()
return err
}
func (a *app) createClient() {
a.client = NewAWS(*a.vargs)
}
func (a *app) sanitizeInputs() error {
vargs := a.vargs
workspace := a.workspace
if len(a.vargs.Key) == 0 || len(a.vargs.Secret) == 0 || len(a.vargs.Bucket) == 0 {
return errors.New(MissingAwsValuesMessage)
2015-10-16 20:09:45 +00:00
}
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-10-16 20:09:45 +00:00
if strings.HasPrefix(vargs.Target, "/") {
vargs.Target = vargs.Target[1:]
}
return nil
}
func (a *app) createSyncJobs() {
vargs := a.vargs
remote, err := a.client.List(vargs.Target)
2015-10-16 20:09:45 +00:00
if err != nil {
fmt.Println(err)
2015-10-16 20:09:45 +00:00
os.Exit(1)
}
2015-12-20 00:15:04 +00:00
local := make([]string, 1, 1)
2015-12-20 00:15:04 +00:00
err = filepath.Walk(vargs.Source, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
localPath := path
if vargs.Source != "." {
localPath = strings.TrimPrefix(path, vargs.Source)
if strings.HasPrefix(localPath, "/") {
localPath = localPath[1:]
}
}
local = append(local, localPath)
a.jobs = append(a.jobs, job{
2015-12-20 00:15:04 +00:00
local: filepath.Join(vargs.Source, localPath),
remote: filepath.Join(vargs.Target, localPath),
action: "upload",
})
return nil
})
if err != nil {
fmt.Println(err)
os.Exit(1)
2015-10-16 20:09:45 +00:00
}
2015-12-20 00:15:04 +00:00
for path, location := range vargs.Redirects {
path = strings.TrimPrefix(path, "/")
local = append(local, path)
a.jobs = append(a.jobs, job{
2015-12-20 00:15:04 +00:00
local: path,
remote: location,
action: "redirect",
})
}
2016-03-14 21:12:37 +00:00
if a.vargs.Delete {
for _, r := range remote {
found := false
for _, l := range local {
if l == r {
found = true
break
}
}
if !found {
a.jobs = append(a.jobs, job{
local: "",
remote: r,
action: "delete",
})
}
}
}
}
2015-12-20 00:15:04 +00:00
func (a *app) createInvalidateJob() {
if len(a.vargs.CloudFrontDistribution) > 0 {
a.jobs = append(a.jobs, job{
2016-03-10 01:22:54 +00:00
local: "",
remote: filepath.Join("/", a.vargs.Target, "*"),
2016-03-10 01:22:54 +00:00
action: "invalidateCloudFront",
})
}
}
2016-03-10 01:22:54 +00:00
func (a *app) runJobs() {
vargs := a.vargs
client := a.client
2015-12-20 00:15:04 +00:00
jobChan := make(chan struct{}, maxConcurrent)
results := make(chan *result, len(a.jobs))
2016-03-14 21:12:37 +00:00
var invalidateJob *job
2015-12-20 00:15:04 +00:00
2015-12-31 20:01:09 +00:00
fmt.Printf("Synchronizing with bucket \"%s\"\n", vargs.Bucket)
for _, j := range a.jobs {
2015-12-20 00:15:04 +00:00
jobChan <- struct{}{}
go func(j job) {
var err error
2015-12-20 00:15:04 +00:00
if j.action == "upload" {
err = client.Upload(j.local, j.remote)
} else if j.action == "redirect" {
err = client.Redirect(j.local, j.remote)
} else if j.action == "delete" {
2015-12-20 00:15:04 +00:00
err = client.Delete(j.remote)
2016-03-10 01:22:54 +00:00
} else if j.action == "invalidateCloudFront" {
2016-03-14 21:12:37 +00:00
invalidateJob = &j
// err = client.Invalidate(j.remote)
2015-12-20 00:15:04 +00:00
} else {
err = nil
}
results <- &result{j, err}
<-jobChan
}(j)
}
for _ = range a.jobs {
2015-12-20 00:15:04 +00:00
r := <-results
if r.err != nil {
2015-12-31 20:01:09 +00:00
fmt.Printf("ERROR: failed to %s %s to %s: %+v\n", r.j.action, r.j.local, r.j.remote, r.err)
os.Exit(1)
}
}
2016-03-14 21:12:37 +00:00
if invalidateJob != nil {
err := client.Invalidate(invalidateJob.remote)
if err != nil {
fmt.Printf("ERROR: failed to %s %s to %s: %+v\n", invalidateJob.action, invalidateJob.local, invalidateJob.remote, err)
os.Exit(1)
}
}
2015-10-16 20:09:45 +00:00
}
func debug(format string, args ...interface{}) {
if os.Getenv("DEBUG") != "" {
2015-12-20 00:15:04 +00:00
fmt.Printf(format+"\n", args...)
} else {
fmt.Printf(".")
}
}