2022-05-03 09:45:54 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Settings for the Plugin.
|
|
|
|
type Settings struct {
|
|
|
|
Endpoint string
|
|
|
|
AccessKey string
|
|
|
|
SecretKey string
|
|
|
|
Bucket string
|
|
|
|
Region string
|
|
|
|
Source string
|
|
|
|
Target string
|
|
|
|
Delete bool
|
2022-05-29 20:36:25 +00:00
|
|
|
ACL map[string]string
|
2022-05-03 09:45:54 +00:00
|
|
|
CacheControl map[string]string
|
|
|
|
ContentType map[string]string
|
|
|
|
ContentEncoding map[string]string
|
|
|
|
Metadata map[string]map[string]string
|
|
|
|
Redirects map[string]string
|
|
|
|
CloudFrontDistribution string
|
|
|
|
DryRun bool
|
|
|
|
PathStyle bool
|
|
|
|
Client AWS
|
|
|
|
Jobs []Job
|
|
|
|
MaxConcurrency int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Job struct {
|
|
|
|
local string
|
|
|
|
remote string
|
|
|
|
action string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Result struct {
|
|
|
|
j Job
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate handles the settings validation of the plugin.
|
|
|
|
func (p *Plugin) Validate() error {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while retrieving working directory: %w", err)
|
|
|
|
}
|
2023-02-08 09:14:25 +00:00
|
|
|
|
2022-05-03 09:45:54 +00:00
|
|
|
p.settings.Source = filepath.Join(wd, p.settings.Source)
|
|
|
|
p.settings.Target = strings.TrimPrefix(p.settings.Target, "/")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute provides the implementation of the plugin.
|
|
|
|
func (p *Plugin) Execute() error {
|
|
|
|
p.settings.Jobs = make([]Job, 1)
|
|
|
|
p.settings.Client = NewAWS(p)
|
|
|
|
|
|
|
|
if err := p.createSyncJobs(); err != nil {
|
|
|
|
return fmt.Errorf("error while creating sync job: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.settings.CloudFrontDistribution) > 0 {
|
|
|
|
p.settings.Jobs = append(p.settings.Jobs, Job{
|
|
|
|
local: "",
|
|
|
|
remote: filepath.Join("/", p.settings.Target, "*"),
|
|
|
|
action: "invalidateCloudFront",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.runJobs(); err != nil {
|
|
|
|
return fmt.Errorf("error while creating sync job: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) createSyncJobs() error {
|
|
|
|
remote, err := p.settings.Client.List(p.settings.Target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-08 09:14:25 +00:00
|
|
|
local := make([]string, 0)
|
2022-05-03 09:45:54 +00:00
|
|
|
|
|
|
|
err = filepath.Walk(p.settings.Source, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil || info.IsDir() {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
localPath := path
|
|
|
|
if p.settings.Source != "." {
|
|
|
|
localPath = strings.TrimPrefix(path, p.settings.Source)
|
|
|
|
localPath = strings.TrimPrefix(localPath, "/")
|
|
|
|
}
|
|
|
|
local = append(local, localPath)
|
|
|
|
p.settings.Jobs = append(p.settings.Jobs, Job{
|
|
|
|
local: filepath.Join(p.settings.Source, localPath),
|
|
|
|
remote: filepath.Join(p.settings.Target, localPath),
|
|
|
|
action: "upload",
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for path, location := range p.settings.Redirects {
|
|
|
|
path = strings.TrimPrefix(path, "/")
|
|
|
|
local = append(local, path)
|
|
|
|
p.settings.Jobs = append(p.settings.Jobs, Job{
|
|
|
|
local: path,
|
|
|
|
remote: location,
|
|
|
|
action: "redirect",
|
|
|
|
})
|
|
|
|
}
|
2023-02-08 09:14:25 +00:00
|
|
|
|
2022-05-03 09:45:54 +00:00
|
|
|
if p.settings.Delete {
|
2023-02-08 09:14:25 +00:00
|
|
|
for _, remote := range remote {
|
2022-05-03 09:45:54 +00:00
|
|
|
found := false
|
2023-02-08 09:14:25 +00:00
|
|
|
remotePath := strings.TrimPrefix(remote, p.settings.Target+"/")
|
|
|
|
|
2022-05-03 09:45:54 +00:00
|
|
|
for _, l := range local {
|
2023-02-08 09:14:25 +00:00
|
|
|
if l == remotePath {
|
2022-05-03 09:45:54 +00:00
|
|
|
found = true
|
2023-02-08 09:14:25 +00:00
|
|
|
|
2022-05-03 09:45:54 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
p.settings.Jobs = append(p.settings.Jobs, Job{
|
|
|
|
local: "",
|
2023-02-08 09:14:25 +00:00
|
|
|
remote: remote,
|
2022-05-03 09:45:54 +00:00
|
|
|
action: "delete",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) runJobs() error {
|
|
|
|
client := p.settings.Client
|
|
|
|
jobChan := make(chan struct{}, p.settings.MaxConcurrency)
|
|
|
|
results := make(chan *Result, len(p.settings.Jobs))
|
2023-02-08 09:14:25 +00:00
|
|
|
|
2022-05-03 09:45:54 +00:00
|
|
|
var invalidateJob *Job
|
|
|
|
|
|
|
|
logrus.Infof("Synchronizing with bucket '%s'", p.settings.Bucket)
|
2023-02-08 09:14:25 +00:00
|
|
|
|
|
|
|
for _, job := range p.settings.Jobs {
|
2022-05-03 09:45:54 +00:00
|
|
|
jobChan <- struct{}{}
|
2023-02-08 09:14:25 +00:00
|
|
|
|
|
|
|
go func(job Job) {
|
2022-05-03 09:45:54 +00:00
|
|
|
var err error
|
2023-02-08 09:14:25 +00:00
|
|
|
|
|
|
|
switch job.action {
|
2022-05-03 09:45:54 +00:00
|
|
|
case "upload":
|
2023-02-08 09:14:25 +00:00
|
|
|
err = client.Upload(job.local, job.remote)
|
2022-05-03 09:45:54 +00:00
|
|
|
case "redirect":
|
2023-02-08 09:14:25 +00:00
|
|
|
err = client.Redirect(job.local, job.remote)
|
2022-05-03 09:45:54 +00:00
|
|
|
case "delete":
|
2023-02-08 09:14:25 +00:00
|
|
|
err = client.Delete(job.remote)
|
2022-05-03 09:45:54 +00:00
|
|
|
case "invalidateCloudFront":
|
2023-02-08 09:14:25 +00:00
|
|
|
invalidateJob = &job
|
2022-05-03 09:45:54 +00:00
|
|
|
default:
|
|
|
|
err = nil
|
|
|
|
}
|
2023-02-08 09:14:25 +00:00
|
|
|
results <- &Result{job, err}
|
|
|
|
|
2022-05-03 09:45:54 +00:00
|
|
|
<-jobChan
|
2023-02-08 09:14:25 +00:00
|
|
|
}(job)
|
2022-05-03 09:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for range p.settings.Jobs {
|
|
|
|
r := <-results
|
|
|
|
if r.err != nil {
|
2023-02-08 09:14:25 +00:00
|
|
|
return fmt.Errorf("failed to %s %s to %s: %w", r.j.action, r.j.local, r.j.remote, r.err)
|
2022-05-03 09:45:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if invalidateJob != nil {
|
|
|
|
err := client.Invalidate(invalidateJob.remote)
|
|
|
|
if err != nil {
|
2023-02-08 09:14:25 +00:00
|
|
|
return fmt.Errorf("failed to %s %s to %s: %w", invalidateJob.action, invalidateJob.local, invalidateJob.remote, err)
|
2022-05-03 09:45:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|