@@ -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
164121var 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