-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
helper_unix.go
74 lines (66 loc) · 1.6 KB
/
helper_unix.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// +build !windows
package openshift
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/bootstrap/docker/errors"
)
func CheckSocat() error {
_, err := exec.LookPath("socat")
if err != nil {
return err
}
return nil
}
func KillExistingSocat() error {
_, err := os.Stat(SocatPidFile)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
pidStr, err := ioutil.ReadFile(SocatPidFile)
if err != nil {
return err
}
defer os.Remove(SocatPidFile)
pid, err := strconv.Atoi(string(pidStr))
if err != nil {
return err
}
process, err := os.FindProcess(pid)
if err != nil {
return err
}
return process.Kill()
}
func SaveSocatPid(pid int) error {
parentDir := filepath.Dir(SocatPidFile)
err := os.MkdirAll(parentDir, 0755)
if err != nil {
return err
}
return ioutil.WriteFile(SocatPidFile, []byte(strconv.Itoa(pid)), 0644)
}
func (h *Helper) startSocatTunnel() error {
// Previous process should have been killed with
// 'oc cluster down', call again here in case it wasn't
err := KillExistingSocat()
if err != nil {
glog.V(1).Infof("error: cannot kill socat: %v", err)
}
cmd := exec.Command("socat", "TCP-L:8443,reuseaddr,fork,backlog=20,bind=127.0.0.1", "SYSTEM:\"docker exec -i origin socat - TCP\\:localhost\\:8443,nodelay\"")
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err = cmd.Start()
if err != nil {
return errors.NewError("cannot start socat tunnel").WithCause(err)
}
glog.V(1).Infof("Started socat with pid: %d", cmd.Process.Pid)
return SaveSocatPid(cmd.Process.Pid)
}