/
linux_args.go
83 lines (73 loc) · 1.79 KB
/
linux_args.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
/*
* Xenon
*
* Copyright 2018 The Xenon Authors.
* Code is licensed under the GPLv3.
*
*/
package mysqld
import (
"config"
"fmt"
"path/filepath"
)
var (
_ ArgsHandler = &LinuxArgs{}
)
const (
bash = "bash"
mysqldsafe = "bin/mysqld_safe"
mysqladmin = "bin/mysqladmin"
)
// LinuxArgs tuple.
type LinuxArgs struct {
conf *config.BackupConfig
ArgsHandler
}
// NewLinuxArgs creates new LinuxArgs.
func NewLinuxArgs(conf *config.BackupConfig) *LinuxArgs {
return &LinuxArgs{
conf: conf,
}
}
// Start used to start mysqld.
func (l *LinuxArgs) Start() []string {
safe57 := filepath.Join(l.conf.Basedir, mysqldsafe)
args := []string{
"-c",
fmt.Sprintf("%s --defaults-file=%s > /dev/null&", safe57, l.conf.DefaultsFile),
}
return args
}
// Stop used to stop the mysqld.
func (l *LinuxArgs) Stop() []string {
admin57 := filepath.Join(l.conf.Basedir, mysqladmin)
args := []string{
"-c",
}
if l.conf.Passwd == "" {
args = append(args, fmt.Sprintf("%s -h%s -u%s -P%d shutdown", admin57, l.conf.Host, l.conf.Admin, l.conf.Port))
} else {
args = append(args, fmt.Sprintf("%s -h%s -u%s -p%s -P%d shutdown", admin57, l.conf.Host, l.conf.Admin, l.conf.Passwd, l.conf.Port))
}
return args
}
// IsRunning used to check the mysqld is running or not.
func (l *LinuxArgs) IsRunning() []string {
// [m] is a trick to stop you picking up the actual grep process itself
safe57 := fmt.Sprintf("[m]ysqld_safe --defaults-file=%s", l.conf.DefaultsFile)
args := []string{
"-c",
fmt.Sprintf("ps aux | grep '%s' | wc -l", safe57),
}
return args
}
// Kill used to kill -9 the mysqld process.
func (l *LinuxArgs) Kill() []string {
args := []string{
"-c",
}
args = append(args,
fmt.Sprintf("kill -9 $(ps aux | grep '[-]-defaults-file=%s' | awk '{print $2}')", l.conf.DefaultsFile))
return args
}