Skip to content

Commit a9d5543

Browse files
committed
devshell: Don't output status if not on a tty
This is just better behavior for the case of e.g. `cosa run -x "somecmd" > out.txt` to execute a command and capture the output - we don't want to intermix status stuff in there. Further, this makes it easier to test the code when not on a tty (in e.g. CI).
1 parent 3d5071f commit a9d5543

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

mantle/cmd/kola/devshell.go

+30-11
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ func displayStatusMsg(status, msg string, termMaxWidth int) {
6464
}
6565

6666
func runDevShellSSH(ctx context.Context, builder *platform.QemuBuilder, conf *conf.Conf, sshCommand string) error {
67-
if !term.IsTerminal(0) {
68-
return fmt.Errorf("stdin is not a tty")
67+
ontty := term.IsTerminal(0)
68+
if sshCommand == "" {
69+
if !ontty {
70+
return fmt.Errorf("stdin is not a tty")
71+
}
6972
}
7073
termMaxWidth, _, err := term.GetSize(0)
7174
if err != nil {
@@ -171,6 +174,7 @@ func runDevShellSSH(ctx context.Context, builder *platform.QemuBuilder, conf *co
171174

172175
// Start the SSH client
173176
sc := newSshClient(ip, agent.Socket, sshCommand)
177+
sc.ontty = ontty
174178
go sc.controlStartStop()
175179

176180
ready := false
@@ -187,8 +191,10 @@ func runDevShellSSH(ctx context.Context, builder *platform.QemuBuilder, conf *co
187191
// handle console messages. If SSH is not ready, then display a
188192
// a status message on the console.
189193
case serialMsg := <-serialChan:
190-
if !ready {
191-
displayStatusMsg(statusMsg, serialMsg, termMaxWidth)
194+
if ontty {
195+
if !ready {
196+
displayStatusMsg(statusMsg, serialMsg, termMaxWidth)
197+
}
192198
}
193199
lastMsg = serialMsg
194200
// monitor the err channel
@@ -202,7 +208,9 @@ func runDevShellSSH(ctx context.Context, builder *platform.QemuBuilder, conf *co
202208

203209
// monitor the instance state
204210
case <-qemuWaitChan:
205-
displayStatusMsg("DONE", "QEMU instance terminated", termMaxWidth)
211+
if ontty {
212+
displayStatusMsg("DONE", "QEMU instance terminated", termMaxWidth)
213+
}
206214
return nil
207215

208216
// monitor the machine state events from console/serial logs
@@ -233,17 +241,23 @@ func runDevShellSSH(ctx context.Context, builder *platform.QemuBuilder, conf *co
233241
statusMsg = "QEMU guest is booting"
234242
}
235243
}
236-
displayStatusMsg(fmt.Sprintf("EVENT | %s", statusMsg), lastMsg, termMaxWidth)
244+
if ontty {
245+
displayStatusMsg(fmt.Sprintf("EVENT | %s", statusMsg), lastMsg, termMaxWidth)
246+
}
237247

238248
// monitor the SSH connection
239249
case err := <-sc.errChan:
240250
if err == nil {
241251
sc.controlChan <- sshNotReady
242-
displayStatusMsg("SESSION", "Clean exit from SSH, terminating instance", termMaxWidth)
252+
if ontty {
253+
displayStatusMsg("SESSION", "Clean exit from SSH, terminating instance", termMaxWidth)
254+
}
243255
return nil
244256
} else if sshCommand != "" {
245257
sc.controlChan <- sshNotReady
246-
displayStatusMsg("SESSION", "SSH command exited, terminating instance", termMaxWidth)
258+
if ontty {
259+
displayStatusMsg("SESSION", "SSH command exited, terminating instance", termMaxWidth)
260+
}
247261
return err
248262
}
249263
if ready {
@@ -456,6 +470,7 @@ type sshClient struct {
456470
port string
457471
agent string
458472
cmd string
473+
ontty bool
459474
controlChan chan sshControlMessage
460475
errChan chan error
461476
sshCmd *exec.Cmd
@@ -512,8 +527,10 @@ func (sc *sshClient) start() {
512527
if sc.cmd != "" {
513528
sshArgs = append(sshArgs, "--", sc.cmd)
514529
}
515-
fmt.Printf("\033[2K\r") // clear serial console line
516-
fmt.Printf("[SESSION] Starting SSH\r") // and stage a status msg which will be erased
530+
if sc.ontty {
531+
fmt.Printf("\033[2K\r") // clear serial console line
532+
fmt.Printf("[SESSION] Starting SSH\r") // and stage a status msg which will be erased
533+
}
517534
sshCmd := exec.Command(sshArgs[0], sshArgs[1:]...)
518535
sshCmd.Stdin = os.Stdin
519536
sshCmd.Stdout = os.Stdout
@@ -532,7 +549,9 @@ func (sc *sshClient) start() {
532549
for scanner.Scan() {
533550
msg := scanner.Text()
534551
if strings.Contains(msg, "Connection to 127.0.0.1 closed") {
535-
displayStatusMsg("SSH", "connection closed", 0)
552+
if sc.ontty {
553+
displayStatusMsg("SSH", "connection closed", 0)
554+
}
536555
}
537556
}
538557
}()

tests/test-cosa-run.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# This verifies e.g. `cosa run`.
33
set -xeuo pipefail
44
tmpdir=$(mktemp -d -p /var/tmp)
5-
cd ${tmpdir}
5+
cd "${tmpdir}"
66
coreos-installer download -a s390x -p qemu -f qcow2.xz --decompress
7-
cosa run --arch s390x *.qcow2 -x "cat /proc/cpuinfo" > cpuinfo.txt
7+
cosa run --arch s390x ./*.qcow2 -x "cat /proc/cpuinfo" > cpuinfo.txt
88
grep -F 'IBM/S390' cpuinfo.txt
99
cd -
1010
rm "${tmpdir}" -rf

0 commit comments

Comments
 (0)