-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
178 lines (149 loc) · 4.8 KB
/
repo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"errors"
"fmt"
"net/http"
"net/url"
"os"
"os/exec"
"regexp"
"time"
)
func git_exec(dir string, args ...string) (out []byte, err error) {
path, _ := exec.LookPath("git")
cmd := exec.Command(path, args...)
cmd.Dir = dir
return cmd.Output()
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func (ctx requestContext) projRepoHandler(w http.ResponseWriter, req *http.Request) {
git := CreateGitHandler(ctx.Project.Repo)
// test if repo exists
exists, err := exists(ctx.Project.Repo)
if err != nil {
http.Error(w, "Couldn't read project repo dir", http.StatusInternalServerError)
return
}
if !exists {
err := os.MkdirAll(ctx.Project.Repo, 0770)
if err != nil {
http.Error(w, "Couldn't create project repo dir", http.StatusInternalServerError)
return
}
if _, err = git_exec(ctx.Project.Repo, "init", "--bare"); err != nil {
http.Error(w, "Couldn't init project repo", http.StatusInternalServerError)
return
}
if _, err = git_exec(ctx.Project.Repo, "config", "http.receivepack", "true"); err != nil {
http.Error(w, "Couldn't configure http.receivepack on project repo", http.StatusInternalServerError)
return
}
}
http.StripPrefix(fmt.Sprintf("/projects/%s/repo", ctx.vars["project"]), git).ServeHTTP(w, req)
}
func gitURL(path string) (*url.URL, error) {
rx := regexp.MustCompile("([\\-\\.\\w]+)@([\\-\\.\\w]+):([\\-\\.\\w]+)")
if parts := rx.FindStringSubmatch(path); parts != nil {
path = fmt.Sprintf("ssh://%s@%s/%s", parts[1], parts[2], parts[3])
}
return url.Parse(path)
}
func urlToDir(url *url.URL) (string, error) {
rx := regexp.MustCompile(".*/([\\-\\.\\w]+?)(.git)?$")
if parts := rx.FindStringSubmatch(url.RequestURI()); parts != nil {
return parts[1], nil
}
return "", errors.New("Couldn't parse repository name from URL")
}
func (ctx requestContext) projRepoReceivePackHandler(w http.ResponseWriter, req *http.Request) {
git := CreateGitHandler(ctx.Project.Repo)
wrapper := &GitIgnoreFlushWriter{w}
http.StripPrefix(fmt.Sprintf("/projects/%s/repo", ctx.vars["project"]), git).ServeHTTP(wrapper, req)
defer fmt.Fprintln(w, "0000")
gow := &GitOutputWriter{w}
major := NewHerokuStyleLogger(gow, true)
minor := NewHerokuStyleLogger(gow, false)
major.Println("kantan receiving push")
yml, err := git_exec(ctx.Project.Repo, "cat-file", "blob", "master:.kantan.yml")
if err != nil {
major.Println("Couldn't read kantan config")
minor.Println("Create .kantan.yml in the repository, containing \"buildpack: git@uri:for/buildpack\"")
return
}
c, err := Parse(yml)
if err != nil {
major.Println("Couldn't parse deploy config")
return
}
bpUrl, err := gitURL(c.Buildpack)
if err != nil {
major.Println("Couldn't parse buildpack URL")
return
}
bpDir, err := urlToDir(bpUrl)
if err != nil {
major.Println(err)
return
}
buildpackPath := fmt.Sprintf("%s/buildpacks/%s", ctx.globalContext.kantan_root, bpDir)
exists, err := exists(buildpackPath)
if err != nil {
major.Printf("Couldn't read buildpack dir (%s)", buildpackPath)
return
}
if !exists {
major.Printf("Fetching buildpack \"%s\" from %s", bpDir, c.Buildpack)
err = os.MkdirAll(buildpackPath, 0770)
if _, err = git_exec(buildpackPath, "clone", c.Buildpack, buildpackPath); err != nil {
major.Printf("Couldn't clone buildpack %s into %s", c.Buildpack, buildpackPath)
return
}
minor.Println("Cleaning buildpack cache")
if os.RemoveAll(ctx.Project.Cache) != nil {
minor.Printf("Couldn't remove old cache, leaving in place")
}
} else {
minor.Printf("Using existing buildpack \"%s\"", bpDir)
git_exec(buildpackPath, "clean", "-fd")
}
if os.MkdirAll(ctx.Project.Cache, 0770) != nil {
major.Println("Couldn't create cache dir")
return
}
releaseId := time.Now().Format("20060102150405")
releaseDir := fmt.Sprintf("%s/releases/%s", ctx.Project.Path, releaseId)
minor.Printf("Preparing new release %s", releaseId)
if os.MkdirAll(releaseDir, 0770) != nil {
major.Printf("Couldn't create release dir %s", releaseDir)
return
}
if _, err = git_exec(releaseDir, "clone", "--local", "--no-hardlinks", "--depth", "1", "--recursive", ctx.Project.Repo, "."); err != nil {
major.Printf("Couldn't export HEAD to release dir")
return
}
compileCmd := exec.Command(fmt.Sprintf("%s/bin/compile", buildpackPath), releaseDir, ctx.Project.Cache)
compileCmd.Dir = ctx.Project.Cache
outReader, err := compileCmd.StdoutPipe()
if err != nil {
major.Println("Couldn't get stdout of compile script")
return
}
errReader, err := compileCmd.StderrPipe()
if err != nil {
major.Println("Couldn't get stderr of compile script")
return
}
go PipeToGitWriter(outReader, *gow)
go PipeToGitWriter(errReader, *gow)
compileCmd.Run()
major.Println("Done")
}