Repository navigation
Expand file tree
/
Copy pathconnection.go
More file actions
106 lines (80 loc) · 2.16 KB
/
Copy pathconnection.go
File metadata and controls
106 lines (80 loc) · 2.16 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"log"
"net/http"
"os"
"os/exec"
"github.com/labstack/echo"
)
// ready checks whether sqlcl is ready or not by querying localhost:9080.
func readyConnection(ctx echo.Context) error {
if ttyd.IsRunning() {
return ctx.String(http.StatusOK, "true")
}
return ctx.String(http.StatusServiceUnavailable, "false")
}
// start starts ttyd with the provided connection.
func startConnection(ctx echo.Context) error {
// get container config from post body ContainerInput
var containerInfo ContainerInfo
err := ctx.Bind(&containerInfo)
if err != nil {
logger.Error("JSON body error: ", err)
return ctx.String(http.StatusBadRequest, "JSON body error")
}
if err := ttyd.Start(containerInfo); err != nil {
log.Printf("failed to start ttyd error is: %s\n", err)
return echo.NewHTTPError(http.StatusInternalServerError)
}
return ctx.String(http.StatusOK, "true")
}
func stopConnection(ctx echo.Context) error {
if err := ttyd.Stop(); err != nil {
log.Printf("failed to stop ttyd: %s\n", err)
return echo.NewHTTPError(http.StatusInternalServerError)
}
return ctx.String(http.StatusOK, "true")
}
type TTYD struct {
process *os.Process
}
func (t *TTYD) Start(containerInfo ContainerInfo) error {
if !t.IsStarted() {
args := []string{"-W", "-u", "1000", "-g", "1000", "-t", "titleFixed='sqlcmd'"}
args = append(args, "/bin/bash", "/cli/sql.sh", containerInfo.Port, containerInfo.Databasename, containerInfo.SApassword)
cmd := exec.Command("/usr/bin/ttyd", args...)
if err := cmd.Start(); err != nil {
return err
}
t.process = cmd.Process
log.Println("started ttyd")
}
return nil
}
func (t *TTYD) Stop() error {
if !t.IsStarted() {
return nil
}
if err := t.process.Kill(); err != nil {
log.Printf("failed to stop ttyd: %s\n", err)
return err
}
t.process.Wait()
t.process = nil
return nil
}
func (t TTYD) IsStarted() bool {
return t.process != nil
}
func (t *TTYD) IsRunning() bool {
if !t.IsStarted() {
return false
}
url := "http://localhost:7681/" // 7681 is the default port for ttyd
resp, err := http.Get(url)
if err != nil {
log.Println(err)
return false
}
return resp.StatusCode == http.StatusOK
}