Skip to content

Commit 71a09fb

Browse files
committed
Merge pull request #128 from aledbf/continue-106
ref(*): use S3 client instead of mc CLI
2 parents bc5e070 + 39d8ef3 commit 71a09fb

13 files changed

Lines changed: 282 additions & 198 deletions

File tree

glide.lock

Lines changed: 33 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package: github.com/deis/builder
2-
flatten: true
2+
ignore:
3+
- appengine
4+
- appengine/memcache
5+
- appengine/datastore
6+
- appengine/user
37
import:
48
- package: github.com/kelseyhightower/envconfig
59
- package: github.com/coreos/go-etcd
@@ -21,14 +25,15 @@ import:
2125
- package: gopkg.in/yaml.v2
2226
version: eca94c41d994ae2215d455ce578ae6e2dc6ee516
2327
- package: github.com/labstack/gommon
24-
- package: https://github.com/pborman/uuid
28+
- package: github.com/pborman/uuid
2529
- package: github.com/deis/pkg
2630
subpackages:
2731
- /time
2832
- package: github.com/codegangsta/cli
2933
version: a65b733b303f0055f8d324d805f393cd3e7a7904
30-
ignore:
31-
- appengine
32-
- appengine/memcache
33-
- appengine/datastore
34-
- appengine/user
34+
- package: github.com/aws/aws-sdk-go
35+
version: 87b1e60a50b09e4812dee560b33a238f67305804
36+
subpackages:
37+
- aws
38+
- aws/session
39+
- service/s3

pkg/gitreceive/build.go

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
4841
func 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
}

pkg/gitreceive/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Config struct {
1818
Username string `envconfig:"USERNAME" required:"true"`
1919
Fingerprint string `envconfig:"FINGERPRINT" required:"true"`
2020
PodNamespace string `envconfig:"POD_NAMESPACE" required:"true"`
21+
StorageRegion string `envconfig:"STORAGE_REGION" default:"us-east-1"`
2122
}
2223

2324
func (c Config) App() string {

pkg/gitreceive/run.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/deis/builder/pkg/gitreceive/log"
11+
"github.com/deis/builder/pkg/gitreceive/storage"
1112
)
1213

1314
const (
@@ -30,6 +31,12 @@ func Run(conf *Config) error {
3031
return fmt.Errorf("couldn't get builder key from %s (%s)", builderKeyLocation, err)
3132
}
3233
builderKey := string(builderKeyBytes)
34+
35+
s3Client, err := storage.GetClient(conf.StorageRegion)
36+
if err != nil {
37+
return fmt.Errorf("configuring S3 client (%s)", err)
38+
}
39+
3340
scanner := bufio.NewScanner(os.Stdin)
3441
for scanner.Scan() {
3542
line := scanner.Text()
@@ -46,7 +53,7 @@ func Run(conf *Config) error {
4653
}
4754
// if we're processing a receive-pack on an existing repo, run a build
4855
if strings.HasPrefix(conf.SSHOriginalCommand, "git-receive-pack") {
49-
if err := build(conf, builderKey, newRev); err != nil {
56+
if err := build(conf, s3Client, builderKey, newRev); err != nil {
5057
return err
5158
}
5259
}

0 commit comments

Comments
 (0)