17
17
package build
18
18
19
19
import (
20
+ "bufio"
20
21
"bytes"
21
22
"flag"
22
23
"fmt"
@@ -116,7 +117,6 @@ func render(tpl *template.Template, outputFile string, outputPerm os.FileMode, x
116
117
// the form sftp://[user@]host[:port].
117
118
func UploadSFTP (identityFile , host , dir string , files []string ) error {
118
119
sftp := exec .Command ("sftp" )
119
- sftp .Stdout = os .Stdout
120
120
sftp .Stderr = os .Stderr
121
121
if identityFile != "" {
122
122
sftp .Args = append (sftp .Args , "-i" , identityFile )
@@ -131,6 +131,10 @@ func UploadSFTP(identityFile, host, dir string, files []string) error {
131
131
if err != nil {
132
132
return fmt .Errorf ("can't create stdin pipe for sftp: %v" , err )
133
133
}
134
+ stdout , err := sftp .StdoutPipe ()
135
+ if err != nil {
136
+ return fmt .Errorf ("can't create stdout pipe for sftp: %v" , err )
137
+ }
134
138
if err := sftp .Start (); err != nil {
135
139
return err
136
140
}
@@ -139,24 +143,34 @@ func UploadSFTP(identityFile, host, dir string, files []string) error {
139
143
fmt .Fprintln (in , "put" , f , path .Join (dir , filepath .Base (f )))
140
144
}
141
145
fmt .Fprintln (in , "exit" )
142
- // Avoid travis timout after 10m of inactivity by printing something
143
- // every 8 minutes.
144
- done := make (chan bool )
146
+ // Some issue with the PPA sftp server makes it so the server does not
147
+ // respond properly to a 'bye', 'exit' or 'quit' from the client.
148
+ // To work around that, we check the output, and when we see the client
149
+ // exit command, we do a hard exit.
150
+ // See
151
+ // https://github.com/kolban-google/sftp-gcs/issues/23
152
+ // https://github.com/mscdex/ssh2/pull/1111
153
+ aborted := false
145
154
go func () {
146
- for {
147
- select {
148
- case <- time .After (8 * time .Minute ):
149
- fmt .Println ("keepalive log" )
150
- continue
151
- case <- done :
152
- return
155
+ scanner := bufio .NewScanner (stdout )
156
+ for scanner .Scan () {
157
+ txt := scanner .Text ()
158
+ fmt .Println (txt )
159
+ if txt == "sftp> exit" {
160
+ // Give it .5 seconds to exit (server might be fixed), then
161
+ // hard kill it from the outside
162
+ time .Sleep (500 * time .Millisecond )
163
+ aborted = true
164
+ sftp .Process .Kill ()
153
165
}
154
-
155
166
}
156
167
}()
157
168
stdin .Close ()
158
- defer close (done )
159
- return sftp .Wait ()
169
+ err = sftp .Wait ()
170
+ if aborted {
171
+ return nil
172
+ }
173
+ return err
160
174
}
161
175
162
176
// FindMainPackages finds all 'main' packages in the given directory and returns their
0 commit comments