@@ -10,8 +10,10 @@ import (
1010 "strings"
1111 "time"
1212
13+ "github.com/aws/aws-sdk-go/service/s3"
1314 "github.com/deis/builder/pkg"
1415 "github.com/deis/builder/pkg/gitreceive/log"
16+ "github.com/deis/builder/pkg/gitreceive/storage"
1517 "github.com/pborman/uuid"
1618 "gopkg.in/yaml.v2"
1719)
@@ -36,15 +38,6 @@ func repoCmd(repoDir, first string, others ...string) *exec.Cmd {
3638 return cmd
3739}
3840
39- // mcCmd returns a command to execute the 'mc' binary, so that it reads config from configDir.
40- // the command outputs its stderr to os.Stderr
41- func mcCmd (configDir string , args ... string ) * exec.Cmd {
42- cmd := exec .Command ("mc" , "-C" , configDir , "--quiet" )
43- cmd .Args = append (cmd .Args , args ... )
44- cmd .Stderr = os .Stderr
45- return cmd
46- }
47-
4841func kGetCmd (podNS , podName string ) * exec.Cmd {
4942 return exec .Command ("kubectl" , fmt .Sprintf ("--namespace=%s" , podNS ), "get" , "pods" , "-o" , "yaml" , podName )
5043}
@@ -60,16 +53,7 @@ func run(cmd *exec.Cmd) error {
6053 return cmd .Run ()
6154}
6255
63- func build (conf * Config , builderKey , gitSha string ) error {
64- storage , err := getStorageConfig ()
65- if err != nil {
66- return err
67- }
68- creds , err := getStorageCreds ()
69- if err == errMissingKey || err == errMissingSecret {
70- return err
71- }
72-
56+ func build (conf * Config , s3Client * s3.S3 , builderKey , gitSha string ) error {
7357 repo := conf .Repository
7458 if len (gitSha ) <= shortShaIdx {
7559 return errGitShaTooShort {sha : gitSha }
@@ -87,10 +71,12 @@ func build(conf *Config, builderKey, gitSha string) error {
8771 }
8872 tmpDir := os .TempDir ()
8973
90- tarURL := fmt .Sprintf ("%s://%s:%s/git/home/%s/tar" , storage .schema (), storage .host (), storage .port (), slugName )
74+ tarObjKey := fmt .Sprintf ("home/%s/tar" , slugName )
75+ tarURL := fmt .Sprintf ("%s/git/%s" , s3Client .Endpoint , tarObjKey )
9176
9277 // this is where workflow tells slugrunner to download the slug from, so we have to tell slugbuilder to upload it to here
93- pushURL := fmt .Sprintf ("%s://%s:%s/git/home/%s/push" , storage .schema (), storage .host (), storage .port (), fmt .Sprintf ("%s:git-%s" , appName , gitSha ))
78+ pushObjKey := fmt .Sprintf ("home/%s/push" , fmt .Sprintf ("%s:git-%s" , appName , gitSha ))
79+ pushURL := fmt .Sprintf ("%s/%s" , s3Client .Endpoint , pushObjKey )
9480
9581 // Get the application config from the controller, so we can check for a custom buildpack URL
9682 appConf , err := getAppConfig (conf , builderKey , conf .Username , appName )
@@ -114,6 +100,7 @@ func build(conf *Config, builderKey, gitSha string) error {
114100 if err := run (gitArchiveCmd ); err != nil {
115101 return fmt .Errorf ("running %s (%s)" , strings .Join (gitArchiveCmd .Args , " " ), err )
116102 }
103+ absAppTgz := fmt .Sprintf ("%s/%s" , repoDir , appTgz )
117104
118105 // untar the archive into the temp dir
119106 tarCmd := repoCmd (repoDir , "tar" , "-xzf" , appTgz , "-C" , fmt .Sprintf ("%s/" , tmpDir ))
@@ -138,23 +125,22 @@ func build(conf *Config, builderKey, gitSha string) error {
138125 }
139126
140127 var srcManifest string
141- if creds == nil {
142- // both key and secret are missing, proceed with no credentials
143- if usingDockerfile {
144- srcManifest = "/etc/deis-dockerbuilder-no-creds.yaml"
145- } else {
146- srcManifest = "/etc/deis-slugbuilder-no-creds.yaml"
147- }
148- } else if err == nil {
128+
129+ creds := storage .CredsOK ()
130+ if creds {
149131 // both key and secret are in place, so proceed with credentials
150132 if usingDockerfile {
151133 srcManifest = "/etc/deis-dockerbuilder.yaml"
152134 } else {
153135 srcManifest = "/etc/deis-slugbuilder.yaml"
154136 }
155- } else if err != nil {
156- // unexpected error, fail
157- return fmt .Errorf ("unexpected error (%s)" , err )
137+ } else {
138+ // both key and secret are missing, proceed with no credentials
139+ if usingDockerfile {
140+ srcManifest = "/etc/deis-dockerbuilder-no-creds.yaml"
141+ } else {
142+ srcManifest = "/etc/deis-slugbuilder-no-creds.yaml"
143+ }
158144 }
159145
160146 fileBytes , err := ioutil .ReadFile (srcManifest )
@@ -184,25 +170,17 @@ func build(conf *Config, builderKey, gitSha string) error {
184170 return fmt .Errorf ("writing final manifest %s (%s)" , finalManifestFileLocation , err )
185171 }
186172
187- configDir := "/var/minio-conf "
188- if err := os . MkdirAll ( configDir , os . ModePerm ); err != nil {
189- return fmt . Errorf ( "creating minio config file (%s) " , err )
173+ bucketName := "git "
174+ if err := storage . CreateBucket ( s3Client , bucketName ); err != nil {
175+ log . Warn ( "create bucket error: %+v " , err )
190176 }
191177
192- configCmd := mcCmd ( configDir , "config" , "host" , "add" , fmt . Sprintf ( "%s://%s:%s" , storage . schema (), storage . host (), storage . port ()), creds . key , creds . secret )
193- if err := run ( configCmd ); err != nil {
194- return fmt .Errorf ("configuring the minio client (%s)" , err )
178+ appTgzReader , err := os . Open ( absAppTgz )
179+ if err != nil {
180+ return fmt .Errorf ("opening %s for read (%s)" , appTgz , err )
195181 }
196-
197- makeBucketCmd := mcCmd (configDir , "mb" , fmt .Sprintf ("%s://%s:%s/git" , storage .schema (), storage .host (), storage .port ()))
198- // Don't look for errors here. Buckets may already exist
199- // https://github.com/deis/builder/issues/80 will eliminate this distaste
200- run (makeBucketCmd )
201-
202- cpCmd := mcCmd (configDir , "cp" , appTgz , tarURL )
203- cpCmd .Dir = repoDir
204- if err := run (cpCmd ); err != nil {
205- return fmt .Errorf ("copying %s to %s (%s)" , appTgz , tarURL , err )
182+ if err := storage .UploadObject (s3Client , bucketName , tarObjKey , appTgzReader ); err != nil {
183+ return fmt .Errorf ("uploading %s to %s/%s (%v)" , absAppTgz , bucketName , tarObjKey , err )
206184 }
207185
208186 log .Info ("Starting build... but first, coffee!" )
@@ -253,11 +231,13 @@ func build(conf *Config, builderKey, gitSha string) error {
253231 }
254232
255233 // poll the s3 server to ensure the slug exists
234+ // TODO: time out looking
256235 for {
257- // for now, assume the error indicates that the slug wasn't there, nothing else
258- // TODO: implement https://github.com/deis/builder/issues/80, which will clean this up siginficantly
259- lsCmd := mcCmd (configDir , "ls" , pushURL )
260- if err := run (lsCmd ); err == nil {
236+ exists , err := storage .ObjectExists (s3Client , bucketName , pushObjKey )
237+ if err != nil {
238+ return fmt .Errorf ("Checking if object %s/%s exists (%s)" , bucketName , pushObjKey , err )
239+ }
240+ if exists {
261241 break
262242 }
263243 }
0 commit comments