github.com/kvattikuti/drone@v0.2.1-0.20140603034306-d400229a327a/pkg/plugin/publish/s3.go (about)

     1  package publish
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/drone/drone/pkg/build/buildfile"
     8  )
     9  
    10  type S3 struct {
    11  	Key    string `yaml:"access_key,omitempty"`
    12  	Secret string `yaml:"secret_key,omitempty"`
    13  	Bucket string `yaml:"bucket,omitempty"`
    14  
    15  	// us-east-1
    16  	// us-west-1
    17  	// us-west-2
    18  	// eu-west-1
    19  	// ap-southeast-1
    20  	// ap-southeast-2
    21  	// ap-northeast-1
    22  	// sa-east-1
    23  	Region string `yaml:"region,omitempty"`
    24  
    25  	// Indicates the files ACL, which should be one
    26  	// of the following:
    27  	//     private
    28  	//     public-read
    29  	//     public-read-write
    30  	//     authenticated-read
    31  	//     bucket-owner-read
    32  	//     bucket-owner-full-control
    33  	Access string `yaml:"acl,omitempty"`
    34  
    35  	// Copies the files from the specified directory.
    36  	// Regexp matching will apply to match multiple
    37  	// files
    38  	//
    39  	// Examples:
    40  	//    /path/to/file
    41  	//    /path/to/*.txt
    42  	//    /path/to/*/*.txt
    43  	//    /path/to/**
    44  	Source string `yaml:"source,omitempty"`
    45  	Target string `yaml:"target,omitempty"`
    46  
    47  	// Recursive uploads
    48  	Recursive bool `yaml:"recursive"`
    49  
    50  	Branch string `yaml:"branch,omitempty"`
    51  }
    52  
    53  func (s *S3) Write(f *buildfile.Buildfile) {
    54  
    55  	// skip if AWS key or SECRET are empty. A good example for this would
    56  	// be forks building a project. S3 might be configured in the source
    57  	// repo, but not in the fork
    58  	if len(s.Key) == 0 || len(s.Secret) == 0 {
    59  		return
    60  	}
    61  
    62  	// debugging purposes so we can see if / where something is failing
    63  	f.WriteCmdSilent("echo 'publishing to Amazon S3 ...'")
    64  
    65  	// install the AWS cli using PIP
    66  	f.WriteCmdSilent("[ -f /usr/bin/sudo ] || pip install awscli 1> /dev/null 2> /dev/null")
    67  	f.WriteCmdSilent("[ -f /usr/bin/sudo ] && sudo pip install awscli 1> /dev/null 2> /dev/null")
    68  
    69  	f.WriteEnv("AWS_ACCESS_KEY_ID", s.Key)
    70  	f.WriteEnv("AWS_SECRET_ACCESS_KEY", s.Secret)
    71  
    72  	// make sure a default region is set
    73  	if len(s.Region) == 0 {
    74  		s.Region = "us-east-1"
    75  	}
    76  
    77  	// make sure a default access is set
    78  	// let's be conservative and assume private
    79  	if len(s.Access) == 0 {
    80  		s.Access = "private"
    81  	}
    82  
    83  	// if the target starts with a "/" we need
    84  	// to remove it, otherwise we might adding
    85  	// a 3rd slash to s3://
    86  	if strings.HasPrefix(s.Target, "/") {
    87  		s.Target = s.Target[1:]
    88  	}
    89  
    90  	switch s.Recursive {
    91  	case true:
    92  		f.WriteCmd(fmt.Sprintf(`aws s3 cp %s s3://%s/%s --recursive --acl %s --region %s`, s.Source, s.Bucket, s.Target, s.Access, s.Region))
    93  	case false:
    94  		f.WriteCmd(fmt.Sprintf(`aws s3 cp %s s3://%s/%s --acl %s --region %s`, s.Source, s.Bucket, s.Target, s.Access, s.Region))
    95  	}
    96  }