File: shell_terminal.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (57 lines) | stat: -rw-r--r-- 1,148 bytes parent folder | download
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
//go:build !windows
// +build !windows

package shell

import (
	"errors"
	"net/http"
	"os"
	"os/exec"

	"github.com/creack/pty"

	terminalsession "gitlab.com/gitlab-org/gitlab-runner/session/terminal"
	terminal "gitlab.com/gitlab-org/gitlab-terminal"
)

type terminalConn struct {
	shellFd *os.File
}

func (t terminalConn) Start(w http.ResponseWriter, r *http.Request, timeoutCh, disconnectCh chan error) {
	proxy := terminal.NewFileDescriptorProxy(1) // one stopper: terminal exit handler

	terminalsession.ProxyTerminal(
		timeoutCh,
		disconnectCh,
		proxy.StopCh,
		func() {
			terminal.ProxyFileDescriptor(w, r, t.shellFd, proxy)
		},
	)
}

func (t terminalConn) Close() error {
	return t.shellFd.Close()
}

func (s *executor) Connect() (terminalsession.Conn, error) {
	if s.Shell().Shell == "pwsh" {
		return nil, errors.New("not yet supported")
	}

	cmd := exec.Command(s.BuildShell.Command, s.BuildShell.Arguments...)
	if cmd == nil {
		return nil, errors.New("failed to generate shell command")
	}

	shellFD, err := pty.Start(cmd)
	if err != nil {
		return nil, err
	}

	session := terminalConn{shellFd: shellFD}

	return session, nil
}