Skip to content

Commit 90f089a

Browse files
arschlesAaron Schlesinger
authored andcommitted
feat(build.go): proceed with rewriting the build script in go
1 parent e5a7f03 commit 90f089a

3 files changed

Lines changed: 82 additions & 36 deletions

File tree

pkg/gitreceive/build.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"strings"
77
)
88

9-
const shortShaIdx = 8
9+
const (
10+
shortShaIdx = 8
11+
)
1012

1113
type errGitShaTooShort struct {
1214
sha string
@@ -102,22 +104,49 @@ func build(conf *Config, newRev string) error {
102104
if err := os.MkdirAll(buildDir, os.ModeDir); err != nil {
103105
return errMkdir{dir: buildDir, err: err}
104106
}
105-
106-
//TODO: make tmp_dir
107+
tmpDir := os.TempDir()
107108

108109
//
109110
// cd $REPO_DIR
110111
// # use Procfile if provided, otherwise try default process types from ./release
111112
// git archive --format=tar.gz ${GIT_SHA} > ${APP_NAME}.tar.gz
113+
cmd := exec.Command("git", "archive", "--format=tar.gz", fmt.Sprintf("%s > %s.tar.gz", gitSha, appName))
114+
cmd.Path = repoDir
115+
cmd.Stdout = os.Stdout
116+
cmd.Stderr = os.Stderr
117+
if err := cmd.Run(); err != nil {
118+
log.Err("running %s", strings.Join(cmd.Args, " "))
119+
os.Exit(1)
120+
}
112121
// tar -xzf ${APP_NAME}.tar.gz -C $TMP_DIR/
122+
cmd := exec.Command("tar", "-xzf", fmt.Sprintf("%s.tar.gz", appName), "-C", fmt.Sprintf("%s/", tmpDir))
123+
cmd.Path = repoDir
124+
cmd.Stdout = os.Stdout
125+
cmd.Stderr = os.Stderr
126+
if err := cmd.Run(); err != nil {
127+
log.Err("running %s", strings.Join(cmd.Args, " "))
128+
os.Exit(1)
129+
}
130+
113131
// USING_DOCKERFILE=true
114132
// if [ -f $TMP_DIR/Procfile ]; then
115133
// PROCFILE=$(cat $TMP_DIR/Procfile | yaml2json-procfile)
116134
// USING_DOCKERFILE=false
117135
// else
118136
// PROCFILE="{}"
119137
// fi
120-
//
138+
139+
usingDockerfile := true
140+
rawProcFile, err := ioutil.ReadFile(fmt.Sprintf("%s/Procfile", tmpDir))
141+
if err != nil {
142+
usingDockerfile = false
143+
}
144+
procFile, err := pkg.YamlToJSON(rawProcfile)
145+
if err != nil {
146+
log.Err("procfile %s/Procfile is not valid JSON [%s]", tmpDir, err)
147+
os.Exit(1)
148+
}
149+
121150
// if [[ ! -f /var/run/secrets/object/store/access-key-id ]]; then
122151
// if $USING_DOCKERFILE ; then
123152
// l1=`grep -n "object-store" /etc/deis-dockerbuilder.yaml | head -n1 |cut -d ":" -f1`
@@ -138,6 +167,18 @@ func build(conf *Config, newRev string) error {
138167
// cp /etc/deis-slugbuilder.yaml /etc/${SLUG_NAME}.yaml
139168
// fi
140169
// fi
170+
creds, err := getStorageCreds()
171+
if err == errMissingKey || err == errMissingSecret {
172+
log.Err(err.Error())
173+
os.Exit(1)
174+
}
175+
176+
// both key and secret are missing, so proceed as if using fetcher
177+
if err == os.ErrNotExist {
178+
179+
}
180+
181+
//
141182
//
142183
// git archive --format=tar.gz ${GIT_SHA} > ${APP_NAME}.tar.gz
143184
//

pkg/gitreceive/storage_creds.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package gitreceive
2+
3+
const (
4+
accessKeyIDFile = "/var/run/secrets/object/store/access-key-id"
5+
accessSecretKeyFile = "/var/run/secrets/object/store/access-secret-key"
6+
)
7+
8+
var (
9+
errMissingKey = fmt.Sprintf("missing %s", accessKeyIDFile)
10+
errMissingSecret = fmt.Sprintf("missing %s", accessSecretKeyFile)
11+
)
12+
13+
type storageCreds struct {
14+
key string
15+
secret string
16+
}
17+
18+
// getStorageCreds gets storage credentials from accessKeyIDFile and accessSecretKeyFile.
19+
// returns os.ErrNotExist if both files are missing and otherwise, if a file was missing,
20+
// returns errMissingKey or errMissingSecret according to the file
21+
func getStorageCreds() (*storageCreds, error) {
22+
accessKeyIDBytes, accessKeyErr := ioutil.ReadFile(accessKeyIDFile)
23+
accessSecretKeyBytes, accessSecretKeyErr := ioutil.ReadFile(accessSecretKeyFile)
24+
if accessKeyErr == os.ErrNotExist && accessSecretKeyErr == os.ErrNotExist {
25+
return nil, os.ErrNotExist
26+
}
27+
if accessKeyErr != nil && accessSecretKeyErr == nil {
28+
return nil, errMissingKey
29+
}
30+
if accessKeyErr == nil && accessSecretKeyErr != nil {
31+
return nil, errMissingSecret
32+
}
33+
return storageCreds{
34+
key: string(accessKeyIDBytes),
35+
secret: string(accessSecretKeyBytes),
36+
}, nil
37+
}

pkg/src/yaml2json-procfile.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)