-
Notifications
You must be signed in to change notification settings - Fork 231
/
system.go
158 lines (141 loc) · 3.53 KB
/
system.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package util
import (
"fmt"
"github.com/alibaba/kt-connect/pkg/common"
"github.com/rs/zerolog/log"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
fs "github.com/fsnotify/fsnotify"
ps "github.com/mitchellh/go-ps"
)
// GetDaemonRunning fetch daemon pid if exist
func GetDaemonRunning(componentName string) int {
files, _ := ioutil.ReadDir(common.KtHome)
for _, f := range files {
if strings.HasPrefix(f.Name(), componentName) && strings.HasSuffix(f.Name(), ".pid") {
from := len(componentName) + 1
to := len(f.Name()) - len(".pid")
pid, err := strconv.Atoi(f.Name()[from:to])
if err == nil && IsProcessExist(pid) {
return pid
}
}
}
return -1
}
// IsProcessExist check whether specified process still running
func IsProcessExist(pid int) bool {
proc, err := ps.FindProcess(pid)
if proc == nil || err != nil {
return false
}
return strings.Contains(proc.Executable(), "ktctl")
}
// KubeConfig location of kube-config file
func KubeConfig() string {
kubeconfig := os.Getenv("KUBECONFIG")
if len(kubeconfig) == 0 {
kubeconfig = filepath.Join(common.UserHome, ".kube", "config")
}
return kubeconfig
}
// CreateDirIfNotExist create dir
func CreateDirIfNotExist(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
return err
}
}
return nil
}
// WritePidFile write pid to file
func WritePidFile(componentName string, ch chan os.Signal) error {
pidFile := fmt.Sprintf("%s/%s-%d.pid", common.KtHome, componentName, os.Getpid())
if err := ioutil.WriteFile(pidFile, []byte(fmt.Sprintf("%d", os.Getpid())), 0644); err != nil {
return err
}
go watchPidFile(pidFile, ch)
return nil
}
func watchPidFile(pidFile string, ch chan os.Signal) {
watcher, err := fs.NewWatcher()
if err != nil {
log.Warn().Err(err).Msgf("Failed to create pid file watcher")
}
defer watcher.Close()
if err = watcher.Add(pidFile); err != nil {
log.Warn().Err(err).Msgf("Unable to watch pid file")
}
for event := range watcher.Events {
log.Debug().Msgf("Received event %s", event)
if event.Op & fs.Remove == fs.Remove || event.Op & fs.Rename == fs.Rename {
log.Info().Msgf("Pid file was removed")
ch <-os.Interrupt
}
}
}
// FixFileOwner set owner to original user when run with sudo
func FixFileOwner(path string) {
var uid int
var gid int
sudoUid := os.Getenv("SUDO_UID")
if sudoUid == "" {
uid = os.Getuid()
} else {
uid, _ = strconv.Atoi(sudoUid)
}
sudoGid := os.Getenv("SUDO_GID")
if sudoGid == "" {
gid = os.Getuid()
} else {
gid, _ = strconv.Atoi(sudoGid)
}
_ = os.Chown(path, uid, gid)
}
// GetTimestamp get current timestamp
func GetTimestamp() string {
return strconv.FormatInt(time.Now().Unix(), 10)
}
// ParseTimestamp parse timestamp to unix time
func ParseTimestamp(timestamp string) int64 {
unixTime, err := strconv.ParseInt(timestamp, 10, 64)
if err != nil {
return -1
}
return unixTime
}
// GetLocalUserName get current username
func GetLocalUserName() string {
sudoUser := os.Getenv("SUDO_USER")
if sudoUser != "" {
return sudoUser
}
u, err := user.Current()
if err != nil {
return ""
}
return u.Username
}
// IsCmd check running in windows cmd shell
func IsCmd() bool {
proc, _ := ps.FindProcess(os.Getppid())
if proc != nil && !strings.Contains(proc.Executable(), "cmd.exe") {
return false
}
return true
}
// IsWindows check runtime is windows
func IsWindows() bool {
return runtime.GOOS == "windows"
}
// IsLinux check runtime is windows
func IsLinux() bool {
return runtime.GOOS == "linux"
}