Skip to content

Commit d611b8a

Browse files
author
smothiki
committed
ref(cookoo): remove cookoo dependency from builder
1 parent 8215d19 commit d611b8a

9 files changed

Lines changed: 234 additions & 575 deletions

File tree

boot.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66
"runtime"
77

8-
cookoolog "github.com/Masterminds/cookoo/log"
98
"github.com/codegangsta/cli"
109
"github.com/deis/builder/pkg"
1110
"github.com/deis/builder/pkg/cleaner"
@@ -36,7 +35,6 @@ func init() {
3635
func main() {
3736
if os.Getenv("DEBUG") == "true" {
3837
pkglog.DefaultLogger.SetDebug(true)
39-
cookoolog.Level = cookoolog.LogDebug
4038
log.Printf("Running in debug mode")
4139
}
4240

glide.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import:
88
- package: github.com/kelseyhightower/envconfig
99
- package: github.com/gorilla/mux
1010
version: e444e69cbd2e2e3e0749a2f3c717cec491552bbf
11-
- package: github.com/Masterminds/cookoo
12-
version: 110a04ff7dc3e7c9b86c1a2413906a16a2bb65cb
13-
subpackages:
14-
- log
15-
- safely
16-
- fmt
1711
- package: golang.org/x/crypto
1812
version: f7445b17d61953e333441674c2d11e91ae4559d3
1913
subpackages:

pkg/builder.go

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ package pkg
66

77
import (
88
"fmt"
9-
"log"
10-
"os"
119

12-
"github.com/Masterminds/cookoo"
13-
clog "github.com/Masterminds/cookoo/log"
1410
"github.com/deis/builder/pkg/sshd"
11+
"github.com/deis/pkg/log"
1512
)
1613

1714
// Return codes that will be sent to the shell.
@@ -29,38 +26,15 @@ const (
2926
//
3027
// Run returns on of the Status* status code constants.
3128
func RunBuilder(sshHostIP string, sshHostPort int, gitHomeDir string, sshServerCircuit *sshd.Circuit, pushLock sshd.RepositoryLock) int {
32-
reg, router, ocxt := cookoo.Cookoo()
33-
log.SetFlags(0) // Time is captured elsewhere.
34-
35-
// We layer the context to add better logging and also synchronize
36-
// access so that goroutines don't get into race conditions.
37-
cxt := cookoo.SyncContext(ocxt)
38-
cxt.Put("cookoo.Router", router)
39-
cxt.AddLogger("stdout", os.Stdout)
40-
41-
// Build the routes. See routes.go.
42-
routes(reg)
43-
44-
// Bootstrap the background services. If this fails, we stop.
45-
if err := router.HandleRequest("boot", cxt, false); err != nil {
46-
clog.Errf(cxt, "Fatal errror on boot: %s", err)
29+
address := fmt.Sprintf("%s:%d", sshHostIP, sshHostPort)
30+
cfg, err := sshd.Configure()
31+
if err != nil {
32+
log.Err("SSH server configuration failed: %s", err)
4733
return StatusLocalError
4834
}
49-
50-
cxt.Put(sshd.Address, fmt.Sprintf("%s:%d", sshHostIP, sshHostPort))
51-
52-
// Supply route names for handling various internal routing. While this
53-
// isn't necessary for Cookoo, it makes it easy for us to mock these
54-
// routes in tests. c.f. sshd/server.go
55-
cxt.Put("route.sshd.pubkeyAuth", "pubkeyAuth")
56-
cxt.Put("route.sshd.sshPing", "sshPing")
57-
cxt.Put("route.sshd.sshGitReceive", "sshGitReceive")
58-
59-
// Start the SSH service.
60-
// TODO: We could refactor Serve to be a command, and then run this as
61-
// a route.
62-
if err := sshd.Serve(reg, router, sshServerCircuit, gitHomeDir, pushLock, cxt); err != nil {
63-
clog.Errf(cxt, "SSH server failed: %s", err)
35+
receivetype := "gitreceive"
36+
if err := sshd.Serve(cfg, sshServerCircuit, gitHomeDir, pushLock, address, receivetype); err != nil {
37+
log.Err("SSH server failed: %s", err)
6438
return StatusLocalError
6539
}
6640

pkg/commands.go

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

pkg/git/git.go

Lines changed: 37 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ import (
1515
"sync"
1616
"text/template"
1717

18-
"github.com/deis/builder/pkg/controller"
19-
20-
"github.com/Masterminds/cookoo"
21-
"github.com/Masterminds/cookoo/log"
18+
"github.com/deis/pkg/log"
2219
"golang.org/x/crypto/ssh"
2320
)
2421

@@ -46,119 +43,79 @@ var preReceiveHookTpl = template.Must(template.New("hooks").Parse(preReceiveHook
4643

4744
// Receive receives a Git repo.
4845
// This will only work for git-receive-pack.
49-
//
50-
// Params:
51-
// - operation (string): e.g. git-receive-pack
52-
// - repoName (string): The repository name, in the form '/REPO.git'.
53-
// - channel (ssh.Channel): The channel.
54-
// - request (*ssh.Request): The channel.
55-
// - gitHome (string): Defaults to /home/git.
56-
// - userInfo (*controller.UserInfo): Deis user information.
57-
//
58-
// Returns:
59-
// - nothing
60-
func Receive(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
61-
if ok, z := p.Requires("channel", "request", "userinfo"); !ok {
62-
return nil, fmt.Errorf("Missing requirements %q", z)
63-
}
64-
repoName := p.Get("repoName", "").(string)
65-
operation := p.Get("operation", "").(string)
66-
channel := p.Get("channel", nil).(ssh.Channel)
67-
gitHome := p.Get("gitHome", "/home/git").(string)
68-
userinfo := p.Get("userinfo", nil).(*controller.UserInfo)
46+
func Receive(
47+
repo, operation, gitHome string,
48+
channel ssh.Channel,
49+
fingerprint, username, conndata, receivetype string) error {
6950

70-
log.Debugf(c, "receiving git repo name: %s, operation: %s, fingerprint: %s, user: %s", repoName, operation, userinfo.Fingerprint, userinfo.Username)
71-
72-
repo, err := cleanRepoName(repoName)
73-
if err != nil {
74-
log.Warnf(c, "Illegal repo name: %s.", err)
75-
channel.Stderr().Write([]byte("No repo given"))
76-
return nil, err
77-
}
51+
log.Info("receiving git repo name: %s, operation: %s, fingerprint: %s, user: %s", repo, operation, fingerprint, username)
7852

79-
if ok := checkIfAllowed(repo, userinfo.Apps); !ok {
80-
return nil, fmt.Sprintf("The user %v has no permission in application %v", userinfo.Username, repo)
53+
if receivetype == "mock" {
54+
channel.Write([]byte("OK"))
55+
return nil
8156
}
82-
83-
repo += ".git"
84-
8557
repoPath := filepath.Join(gitHome, repo)
86-
log.Debugf(c, "creating repo directory %s", repoPath)
87-
if _, err := createRepo(c, repoPath); err != nil {
58+
log.Info("creating repo directory %s", repoPath)
59+
if _, err := createRepo(repoPath); err != nil {
8860
err = fmt.Errorf("Did not create new repo (%s)", err)
89-
log.Warnf(c, err.Error())
90-
return nil, err
61+
62+
return err
9163
}
9264

93-
log.Debugf(c, "writing pre-receive hook under %s", repoPath)
94-
if err := createPreReceiveHook(c, gitHome, repoPath); err != nil {
65+
log.Info("writing pre-receive hook under %s", repoPath)
66+
if err := createPreReceiveHook(gitHome, repoPath); err != nil {
9567
err = fmt.Errorf("Did not write pre-receive hook (%s)", err)
96-
log.Warnf(c, err.Error())
97-
return nil, err
68+
return err
9869
}
9970

10071
cmd := exec.Command("git-shell", "-c", fmt.Sprintf("%s '%s'", operation, repo))
101-
log.Infof(c, strings.Join(cmd.Args, " "))
72+
log.Info(strings.Join(cmd.Args, " "))
10273

10374
var errbuff bytes.Buffer
10475

10576
cmd.Dir = gitHome
10677
cmd.Env = []string{
107-
fmt.Sprintf("RECEIVE_USER=%s", userinfo.Username),
78+
fmt.Sprintf("RECEIVE_USER=%s", username),
10879
fmt.Sprintf("RECEIVE_REPO=%s", repo),
109-
fmt.Sprintf("RECEIVE_FINGERPRINT=%s", userinfo.Fingerprint),
80+
fmt.Sprintf("RECEIVE_FINGERPRINT=%s", fingerprint),
11081
fmt.Sprintf("SSH_ORIGINAL_COMMAND=%s '%s'", operation, repo),
111-
fmt.Sprintf("SSH_CONNECTION=%s", c.Get("SSH_CONNECTION", "0 0 0 0").(string)),
82+
fmt.Sprintf("SSH_CONNECTION=%s", conndata),
11283
}
11384
cmd.Env = append(cmd.Env, os.Environ()...)
11485

115-
log.Debugf(c, "Working Dir: %s", cmd.Dir)
116-
log.Debugf(c, "Environment: %s", strings.Join(cmd.Env, ","))
86+
log.Debug("Working Dir: %s", cmd.Dir)
87+
log.Debug("Environment: %s", strings.Join(cmd.Env, ","))
11788

11889
inpipe, err := cmd.StdinPipe()
11990
if err != nil {
120-
return nil, err
91+
return err
12192
}
12293
cmd.Stdout = channel
12394
cmd.Stderr = io.MultiWriter(channel.Stderr(), &errbuff)
12495

12596
if err := cmd.Start(); err != nil {
12697
err = fmt.Errorf("Failed to start git pre-receive hook: %s (%s)", err, errbuff.Bytes())
127-
log.Warnf(c, err.Error())
128-
return nil, err
98+
return err
12999
}
130100

131101
if _, err := io.Copy(inpipe, channel); err != nil {
132102
err = fmt.Errorf("Failed to write git objects into the git pre-receive hook (%s)", err)
133-
log.Warnf(c, err.Error())
134-
return nil, err
103+
return err
135104
}
136105

137106
fmt.Println("Waiting for git-receive to run.")
138107
fmt.Println("Waiting for deploy.")
139108
if err := cmd.Wait(); err != nil {
140109
err = fmt.Errorf("Failed to run git pre-receive hook: %s (%s)", errbuff.Bytes(), err)
141-
log.Errf(c, err.Error())
142-
return nil, err
110+
return err
143111
}
144112
if errbuff.Len() > 0 {
145-
log.Warnf(c, "Unreported error: %s", errbuff.Bytes())
113+
log.Err("Unreported error: %s", errbuff.Bytes())
114+
return errors.New(errbuff.String())
146115
}
147-
log.Infof(c, "Deploy complete.\n")
116+
log.Info("Deploy complete.")
148117

149-
return nil, nil
150-
}
151-
152-
// cleanRepoName cleans a repository name for a git-sh operation.
153-
func cleanRepoName(name string) (string, error) {
154-
if len(name) == 0 {
155-
return name, errors.New("Empty repo name.")
156-
}
157-
if strings.Contains(name, "..") {
158-
return "", errors.New("Cannot change directory in file name.")
159-
}
160-
name = strings.Replace(name, "'", "", -1)
161-
return strings.TrimPrefix(strings.TrimSuffix(name, ".git"), "/"), nil
118+
return nil
162119
}
163120

164121
var createLock sync.Mutex
@@ -169,26 +126,26 @@ var createLock sync.Mutex
169126
//
170127
// Returns a bool indicating whether a project was created (true) or already
171128
// existed (false).
172-
func createRepo(c cookoo.Context, repoPath string) (bool, error) {
129+
func createRepo(repoPath string) (bool, error) {
173130
createLock.Lock()
174131
defer createLock.Unlock()
175132

176133
fi, err := os.Stat(repoPath)
177134
if err == nil && fi.IsDir() {
178135
// Nothing to do.
179-
log.Infof(c, "Directory %s already exists.", repoPath)
136+
log.Debug("Directory %s already exists.", repoPath)
180137
return false, nil
181138
} else if os.IsNotExist(err) {
182-
log.Infof(c, "Creating new directory at %s", repoPath)
139+
log.Debug("Creating new directory at %s", repoPath)
183140
// Create directory
184141
if err := os.MkdirAll(repoPath, 0755); err != nil {
185-
log.Warnf(c, "Failed to create repository: %s", err)
142+
log.Err("Failed to create repository: %s", err)
186143
return false, err
187144
}
188145
cmd := exec.Command("git", "init", "--bare")
189146
cmd.Dir = repoPath
190147
if out, err := cmd.CombinedOutput(); err != nil {
191-
log.Warnf(c, "git init output: %s", out)
148+
log.Info("git init output: %s", out)
192149
return false, err
193150
}
194151

@@ -200,27 +157,17 @@ func createRepo(c cookoo.Context, repoPath string) (bool, error) {
200157
}
201158

202159
// createPreReceiveHook renders preReceiveHookTpl to repoPath/hooks/pre-receive
203-
func createPreReceiveHook(c cookoo.Context, gitHome, repoPath string) error {
160+
func createPreReceiveHook(gitHome, repoPath string) error {
204161
// parse & generate the template anew each receive for each new git home
205162
var hookByteBuf bytes.Buffer
206163
if err := preReceiveHookTpl.Execute(&hookByteBuf, map[string]string{"GitHome": gitHome}); err != nil {
207164
return err
208165
}
209166

210167
writePath := filepath.Join(repoPath, "hooks", "pre-receive")
211-
log.Debugf(c, "Writing pre-receive hook to %s", writePath)
168+
log.Info("Writing pre-receive hook to %s", writePath)
212169
if err := ioutil.WriteFile(writePath, hookByteBuf.Bytes(), 0755); err != nil {
213170
return fmt.Errorf("Cannot write pre-receive hook to %s (%s)", writePath, err)
214171
}
215172
return nil
216173
}
217-
218-
// checkIfAllowed verifies if an application is contained in a list of allowed applications
219-
func checkIfAllowed(app string, validApps []string) bool {
220-
for _, validApp := range validApps {
221-
if validApp == app {
222-
return true
223-
}
224-
}
225-
return false
226-
}

0 commit comments

Comments
 (0)