github.com/secure-build/gitlab-runner@v12.5.0+incompatible/executors/shell/executor_shell_terminal.go (about)

     1  // +build !windows
     2  
     3  package shell
     4  
     5  import (
     6  	"errors"
     7  	"net/http"
     8  	"os"
     9  	"os/exec"
    10  
    11  	"github.com/kr/pty"
    12  	"gitlab.com/gitlab-org/gitlab-terminal"
    13  
    14  	terminalsession "gitlab.com/gitlab-org/gitlab-runner/session/terminal"
    15  )
    16  
    17  type terminalConn struct {
    18  	shellFd *os.File
    19  }
    20  
    21  func (t terminalConn) Start(w http.ResponseWriter, r *http.Request, timeoutCh, disconnectCh chan error) {
    22  	proxy := terminal.NewFileDescriptorProxy(1) // one stopper: terminal exit handler
    23  
    24  	terminalsession.ProxyTerminal(
    25  		timeoutCh,
    26  		disconnectCh,
    27  		proxy.StopCh,
    28  		func() {
    29  			terminal.ProxyFileDescriptor(w, r, t.shellFd, proxy)
    30  		},
    31  	)
    32  }
    33  
    34  func (t terminalConn) Close() error {
    35  	return t.shellFd.Close()
    36  }
    37  
    38  func (s *executor) Connect() (terminalsession.Conn, error) {
    39  	cmd := exec.Command(s.BuildShell.Command, s.BuildShell.Arguments...)
    40  	if cmd == nil {
    41  		return nil, errors.New("Failed to generate shell command")
    42  	}
    43  
    44  	shellFD, err := pty.Start(cmd)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	session := terminalConn{shellFd: shellFD}
    50  
    51  	return session, nil
    52  }