-
Notifications
You must be signed in to change notification settings - Fork 0
/
carbon_clickhouse.go
96 lines (83 loc) · 1.79 KB
/
carbon_clickhouse.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
"text/template"
"github.com/phayes/freeport"
)
type CarbonClickhouse struct {
bin string
configFile string
configTpl string
address string
cmd *exec.Cmd
}
func CarbonClickhouseStart(bin, configTpl, testDir, chAddr, config string) (*CarbonClickhouse, error) {
var err error
if len(bin) == 0 {
return nil, fmt.Errorf("bin not set")
}
_ = os.RemoveAll(filepath.Join(storeDir, "carbon-clickhouse"))
c := &CarbonClickhouse{bin: bin, configTpl: configTpl}
port, err := freeport.GetFreePort()
if err != nil {
return nil, err
}
c.address = "127.0.0.1:" + strconv.Itoa(port)
tmpl, err := template.New(configTpl).ParseFiles(filepath.Join(testDir, configTpl))
if err != nil {
return nil, err
}
param := struct {
CH_ADDR string
CCH_ADDR string
}{
CH_ADDR: chAddr,
CCH_ADDR: c.address,
}
c.configFile = config
f, err := os.OpenFile(c.configFile, os.O_WRONLY|os.O_CREATE, 0644)
f.Truncate(0)
if err != nil {
return nil, err
}
err = tmpl.Execute(f, param)
if err != nil {
return nil, err
}
c.cmd = exec.Command(bin, "-config", c.configFile)
c.cmd.Stdout = os.Stdout
c.cmd.Stderr = os.Stderr
//c.cmd.Env = append(c.cmd.Env, "TZ=UTC")
err = c.cmd.Start()
if err != nil {
return nil, err
}
return c, nil
}
func (c *CarbonClickhouse) Stop() error {
if c.cmd == nil {
return nil
}
var err error
if err = c.cmd.Process.Kill(); err == nil {
if err = c.cmd.Wait(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
ec := status.ExitStatus()
if ec == 0 || ec == -1 {
return nil
}
}
}
}
}
return err
}
func (c *CarbonClickhouse) Address() string {
return c.address
}