11package gitreceive
22
33import (
4+ "bytes"
45 "fmt"
6+ "io/ioutil"
7+ "os"
8+ "os/exec"
59 "path/filepath"
610 "strings"
11+ "time"
712
813 "code.google.com/p/go-uuid/uuid"
14+ "github.com/deis/builder/pkg"
15+ "github.com/deis/builder/pkg/log"
916)
1017
1118const (
@@ -49,9 +56,6 @@ func build(conf *Config, newRev string) error {
4956 os .Exit (1 )
5057 }
5158
52- tarURL := fmt .Sprintf ("%s://%s:%s/git/home/%s/tar" , storage .schema (), storage .host (), storage .port (), slugName )
53- pushURL := fmt .Sprintf ("%s://%s:%s/git/hom/%s/push" , storage .schema (), storage .host (), storage .port (), slugName )
54-
5559 // #!/usr/bin/env bash
5660 // #
5761 // # builder hook called on every git receive-pack
@@ -132,13 +136,17 @@ func build(conf *Config, newRev string) error {
132136 // # create temporary directory inside the build dir for this push
133137 // TMP_DIR=$(mktemp -d -p $BUILD_DIR)
134138 slugName := fmt .Sprintf ("%s:git-%s" , appName , shortSha )
135- metaName := strings .Replace (slugName , ":" , "-" )
139+ metaName := strings .Replace (slugName , ":" , "-" , - 1 )
136140 tmpImage := fmt .Sprintf ("%s:%s/%s" , conf .RegistryHost , conf .RegistryPort , conf .ImageName )
137141 if err := os .MkdirAll (buildDir , os .ModeDir ); err != nil {
138- return errMkdir {dir : buildDir , err : err }
142+ log .Err ("making the build directory %s (%s)" , buildDir , err )
143+ os .Exit (1 )
139144 }
140145 tmpDir := os .TempDir ()
141146
147+ tarURL := fmt .Sprintf ("%s://%s:%s/git/home/%s/tar" , storage .schema (), storage .host (), storage .port (), slugName )
148+ pushURL := fmt .Sprintf ("%s://%s:%s/git/hom/%s/push" , storage .schema (), storage .host (), storage .port (), slugName )
149+
142150 //
143151 // cd $REPO_DIR
144152 // # use Procfile if provided, otherwise try default process types from ./release
@@ -152,11 +160,11 @@ func build(conf *Config, newRev string) error {
152160 os .Exit (1 )
153161 }
154162 // tar -xzf ${APP_NAME}.tar.gz -C $TMP_DIR/
155- cmd := exec .Command ("tar" , "-xzf" , fmt .Sprintf ("%s.tar.gz" , appName ), "-C" , fmt .Sprintf ("%s/" , tmpDir ))
156- cmd .Dir = repoDir
157- cmd .Stdout = os .Stdout
158- cmd .Stderr = os .Stderr
159- if err := cmd .Run (); err != nil {
163+ tarCmd := exec .Command ("tar" , "-xzf" , fmt .Sprintf ("%s.tar.gz" , appName ), "-C" , fmt .Sprintf ("%s/" , tmpDir ))
164+ tarCmd .Dir = repoDir
165+ tarCmd .Stdout = os .Stdout
166+ tarCmd .Stderr = os .Stderr
167+ if err := tarCmd .Run (); err != nil {
160168 log .Err ("running %s" , strings .Join (cmd .Args , " " ))
161169 os .Exit (1 )
162170 }
@@ -174,7 +182,7 @@ func build(conf *Config, newRev string) error {
174182 if err != nil {
175183 usingDockerfile = false
176184 }
177- procFile , err := pkg .YamlToJSON (rawProcfile )
185+ procFile , err := pkg .YamlToJSON (rawProcFile )
178186 if err != nil {
179187 log .Err ("procfile %s/Procfile is not valid JSON [%s]" , tmpDir , err )
180188 os .Exit (1 )
@@ -236,14 +244,14 @@ func build(conf *Config, newRev string) error {
236244 var finalManifest string
237245 if usingDockerfile {
238246 buildPodName = fmt .Sprintf ("%s-%s" , tmpImage , uuid .New ())
239- finalManifest = strings .Replace (string (fileBytes ), "repo_name" , buildPodName )
240- finalManifest = strings .Replace (finalManifest , "puturl" , pushURL )
241- finalManifest = strings .Replace (finalManifest , "tar-url" , tarURL )
247+ finalManifest = strings .Replace (string (fileBytes ), "repo_name" , buildPodName , - 1 )
248+ finalManifest = strings .Replace (finalManifest , "puturl" , pushURL , - 1 )
249+ finalManifest = strings .Replace (finalManifest , "tar-url" , tarURL , - 1 )
242250 } else {
243251 buildPodName = fmt .Sprintf ("%s-%s" , slugName , uuid .New ())
244- finalManifest = strings .Replace (string (fileBytes ), "repo_name" , buildPodName )
245- finalManifest = strings .Replace (finalManifest , "puturl" , pushURL )
246- finalManifest = strings .Replace (finalManifest , "tar-url" , tarURL )
252+ finalManifest = strings .Replace (string (fileBytes ), "repo_name" , buildPodName , - 1 )
253+ finalManifest = strings .Replace (finalManifest , "puturl" , pushURL , - 1 )
254+ finalManifest = strings .Replace (finalManifest , "tar-url" , tarURL , - 1 )
247255 }
248256
249257 if err := ioutil .WriteFile (finalManifestFileName , []byte (finalManifest ), os .ModePerm ); err != nil {
@@ -253,11 +261,11 @@ func build(conf *Config, newRev string) error {
253261 //
254262 // git archive --format=tar.gz ${GIT_SHA} > ${APP_NAME}.tar.gz
255263
256- cmd := exec .Command ("git" , "archive" , "--format=tar.gz" , fmt .Sprintf ("%s > %s.tar.gz" , gitSha , appName ))
257- cmd .Dir = repoDir
258- cmd .Stdout = os .Stdout
259- cmd .Stderr = os .Stderr
260- if err := cmd .Run (); err != nil {
264+ gitArchiveCmd := exec .Command ("git" , "archive" , "--format=tar.gz" , fmt .Sprintf ("%s > %s.tar.gz" , gitSha , appName ))
265+ gitArchiveCmd .Dir = repoDir
266+ gitArchiveCmd .Stdout = os .Stdout
267+ gitArchiveCmd .Stderr = os .Stderr
268+ if err := gitArchiveCmd .Run (); err != nil {
261269 log .Err ("running %s" , strings .Join (cmd .Args , " " ))
262270 os .Exit (1 )
263271 }
@@ -273,7 +281,7 @@ func build(conf *Config, newRev string) error {
273281 // mkdir -p /var/minio-conf
274282 // CONFIG_DIR=/var/minio-conf
275283 // MC_PREFIX="mc -C $CONFIG_DIR --quiet"
276- configDir = "/var/minio-conf"
284+ configDir : = "/var/minio-conf"
277285 if err := os .MkdirAll (configDir , os .ModePerm ); err != nil {
278286 log .Err ("creating minio config file (%s)" , err )
279287 os .Exit (1 )
@@ -289,8 +297,8 @@ func build(conf *Config, newRev string) error {
289297 "host" ,
290298 "add" ,
291299 fmt .Sprintf ("%s://%s:%s" , storage .schema (), storage .host (), storage .port ()),
292- storageCreds .key ,
293- storageCreds .secret ,
300+ creds .key ,
301+ creds .secret ,
294302 )
295303 if err := configCmd .Run (); err != nil {
296304 log .Err ("configuring the minio client (%s)" , err )
@@ -317,7 +325,7 @@ func build(conf *Config, newRev string) error {
317325 )
318326 cpCmd .Dir = repoDir
319327 if err := cpCmd .Run (); err != nil {
320- log .Err ("copying %s.tar.gz to %s (%s)" , apName , tarURL , err )
328+ log .Err ("copying %s.tar.gz to %s (%s)" , appName , tarURL , err )
321329 os .Exit (1 )
322330 }
323331
@@ -351,15 +359,15 @@ func build(conf *Config, newRev string) error {
351359 getCmd := exec .Command (
352360 "kubectl" ,
353361 fmt .Sprintf ("--namespace=%s" , conf .PodNamespace ),
354- fmt .SPrintf ("get" ),
355- fmt .SPrintf ("pods" ),
362+ fmt .Sprintf ("get" ),
363+ fmt .Sprintf ("pods" ),
356364 "-o" ,
357365 "yaml" ,
358366 buildPodName ,
359367 )
360368 for {
361369 var out bytes.Buffer
362- getCmd .Stdout = out
370+ getCmd .Stdout = & out
363371 if err := getCmd .Run (); err != nil {
364372 log .Err ("running %s while determining if builder pod %s is running (%s)" , buildPodName , err )
365373 os .Exit (1 )
@@ -380,7 +388,7 @@ func build(conf *Config, newRev string) error {
380388 )
381389 logsCmd .Stdout = os .Stdout
382390 if err := logsCmd .Run (); err != nil {
383- log .Err ("running %s to get builder logs (%s)" , strings .Join (logsCmd .Args ), err )
391+ log .Err ("running %s to get builder logs (%s)" , strings .Join (logsCmd .Args , " " ), err )
384392 os .Exit (1 )
385393 }
386394
0 commit comments