@@ -12,25 +12,13 @@ import (
1212
1313 "github.com/aws/aws-sdk-go/service/s3"
1414 "github.com/deis/builder/pkg"
15+ "github.com/deis/builder/pkg/gitreceive/git"
1516 "github.com/deis/builder/pkg/gitreceive/log"
1617 "github.com/deis/builder/pkg/gitreceive/storage"
1718 "github.com/pborman/uuid"
1819 "gopkg.in/yaml.v2"
1920)
2021
21- const (
22- // this constant represents the length of a shortened git sha - 8 characters long
23- shortShaIdx = 8
24- )
25-
26- type errGitShaTooShort struct {
27- sha string
28- }
29-
30- func (e errGitShaTooShort ) Error () string {
31- return fmt .Sprintf ("git sha %s was too short" , e .sha )
32- }
33-
3422// repoCmd returns exec.Command(first, others...) with its current working directory repoDir
3523func repoCmd (repoDir , first string , others ... string ) * exec.Cmd {
3624 cmd := exec .Command (first , others ... )
@@ -53,30 +41,26 @@ func run(cmd *exec.Cmd) error {
5341 return cmd .Run ()
5442}
5543
56- func build (conf * Config , s3Client * s3.S3 , builderKey , gitSha string ) error {
44+ func build (conf * Config , s3Client * s3.S3 , builderKey , rawGitSha string ) error {
5745 repo := conf .Repository
58- if len (gitSha ) <= shortShaIdx {
59- return errGitShaTooShort {sha : gitSha }
46+ gitSha , err := git .NewSha (rawGitSha )
47+ if err != nil {
48+ return err
6049 }
61- shortSha := gitSha [ 0 : 8 ]
50+
6251 appName := conf .App ()
6352
6453 repoDir := filepath .Join (conf .GitHome , repo )
6554 buildDir := filepath .Join (repoDir , "build" )
6655
67- slugName := fmt .Sprintf ("%s:git-%s" , appName , shortSha )
56+ slugName := fmt .Sprintf ("%s:git-%s" , appName , gitSha . Short )
6857 imageName := strings .Replace (slugName , ":" , "-" , - 1 )
6958 if err := os .MkdirAll (buildDir , os .ModeDir ); err != nil {
7059 return fmt .Errorf ("making the build directory %s (%s)" , buildDir , err )
7160 }
7261 tmpDir := os .TempDir ()
7362
74- tarObjKey := fmt .Sprintf ("home/%s/tar" , slugName )
75- tarURL := fmt .Sprintf ("%s/git/%s" , s3Client .Endpoint , tarObjKey )
76-
77- // this is where workflow tells slugrunner to download the slug from, so we have to tell slugbuilder to upload it to here
78- pushObjKey := fmt .Sprintf ("home/%s/push" , fmt .Sprintf ("%s:git-%s" , appName , gitSha ))
79- pushURL := fmt .Sprintf ("%s/%s" , s3Client .Endpoint , pushObjKey )
63+ slugBuilderInfo := storage .NewSlugBuilderInfo (s3Client , appName , slugName , gitSha )
8064
8165 // Get the application config from the controller, so we can check for a custom buildpack URL
8266 appConf , err := getAppConfig (conf , builderKey , conf .Username , appName )
@@ -94,7 +78,7 @@ func build(conf *Config, s3Client *s3.S3, builderKey, gitSha string) error {
9478
9579 // build a tarball from the new objects
9680 appTgz := fmt .Sprintf ("%s.tar.gz" , appName )
97- gitArchiveCmd := repoCmd (repoDir , "git" , "archive" , "--format=tar.gz" , fmt .Sprintf ("--output=%s" , appTgz ), gitSha )
81+ gitArchiveCmd := repoCmd (repoDir , "git" , "archive" , "--format=tar.gz" , fmt .Sprintf ("--output=%s" , appTgz ), gitSha . Full )
9882 gitArchiveCmd .Stdout = os .Stdout
9983 gitArchiveCmd .Stderr = os .Stderr
10084 if err := run (gitArchiveCmd ); err != nil {
@@ -153,15 +137,15 @@ func build(conf *Config, s3Client *s3.S3, builderKey, gitSha string) error {
153137 var finalManifest string
154138 uid := uuid .New ()[:8 ]
155139 if usingDockerfile {
156- buildPodName = fmt .Sprintf ("dockerbuild-%s-%s-%s" , appName , shortSha , uid )
140+ buildPodName = fmt .Sprintf ("dockerbuild-%s-%s-%s" , appName , gitSha . Short , uid )
157141 finalManifest = strings .Replace (string (fileBytes ), "repo_name" , buildPodName , - 1 )
158- finalManifest = strings .Replace (finalManifest , "puturl" , pushURL , - 1 )
159- finalManifest = strings .Replace (finalManifest , "tar-url" , tarURL , - 1 )
142+ finalManifest = strings .Replace (finalManifest , "puturl" , slugBuilderInfo . PushURL , - 1 )
143+ finalManifest = strings .Replace (finalManifest , "tar-url" , slugBuilderInfo . TarURL , - 1 )
160144 } else {
161- buildPodName = fmt .Sprintf ("slugbuild-%s-%s-%s" , appName , shortSha , uid )
145+ buildPodName = fmt .Sprintf ("slugbuild-%s-%s-%s" , appName , gitSha . Short , uid )
162146 finalManifest = strings .Replace (string (fileBytes ), "repo_name" , buildPodName , - 1 )
163- finalManifest = strings .Replace (finalManifest , "puturl" , pushURL , - 1 )
164- finalManifest = strings .Replace (finalManifest , "tar-url" , tarURL , - 1 )
147+ finalManifest = strings .Replace (finalManifest , "puturl" , slugBuilderInfo . PushURL , - 1 )
148+ finalManifest = strings .Replace (finalManifest , "tar-url" , slugBuilderInfo . TarURL , - 1 )
165149 finalManifest = strings .Replace (finalManifest , "buildurl" , buildPackURL , - 1 )
166150 }
167151
@@ -179,8 +163,8 @@ func build(conf *Config, s3Client *s3.S3, builderKey, gitSha string) error {
179163 if err != nil {
180164 return fmt .Errorf ("opening %s for read (%s)" , appTgz , err )
181165 }
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 )
166+ if err := storage .UploadObject (s3Client , bucketName , slugBuilderInfo . TarKey , appTgzReader ); err != nil {
167+ return fmt .Errorf ("uploading %s to %s/%s (%v)" , absAppTgz , bucketName , slugBuilderInfo . TarKey , err )
184168 }
185169
186170 log .Info ("Starting build... but first, coffee!" )
@@ -233,9 +217,9 @@ func build(conf *Config, s3Client *s3.S3, builderKey, gitSha string) error {
233217 // poll the s3 server to ensure the slug exists
234218 // TODO: time out looking
235219 for {
236- exists , err := storage .ObjectExists (s3Client , bucketName , pushObjKey )
220+ exists , err := storage .ObjectExists (s3Client , bucketName , slugBuilderInfo . PushKey )
237221 if err != nil {
238- return fmt .Errorf ("Checking if object %s/%s exists (%s)" , bucketName , pushObjKey , err )
222+ return fmt .Errorf ("Checking if object %s/%s exists (%s)" , bucketName , slugBuilderInfo . PushKey , err )
239223 }
240224 if exists {
241225 break
@@ -247,7 +231,7 @@ func build(conf *Config, s3Client *s3.S3, builderKey, gitSha string) error {
247231 log .Info ("Launching..." )
248232
249233 buildHook := & pkg.BuildHook {
250- Sha : gitSha ,
234+ Sha : gitSha . Full ,
251235 ReceiveUser : conf .Username ,
252236 ReceiveRepo : appName ,
253237 Image : appName ,
0 commit comments