-
Notifications
You must be signed in to change notification settings - Fork 231
/
system.go
143 lines (128 loc) · 3.08 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
package util
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
ps "github.com/mitchellh/go-ps"
)
// GetDaemonRunning fetch daemon pid if exist
func GetDaemonRunning(componentName string) int {
files, _ := ioutil.ReadDir(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
}
// IsPidFileExist check pid file is exist or not
func IsPidFileExist() bool {
files, _ := ioutil.ReadDir(KtHome)
for _, f := range files {
if strings.HasSuffix(f.Name(), fmt.Sprintf("-%d.pid", os.Getpid())) {
return true
}
}
return false
}
// 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(UserHome, ".kube", "config")
}
return kubeconfig
}
// CreateDirIfNotExist create dir
func CreateDirIfNotExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
panic(err)
}
}
}
// WritePidFile write pid to file
func WritePidFile(componentName string) error {
pidFile := fmt.Sprintf("%s/%s-%d.pid", KtHome, componentName, os.Getpid())
return ioutil.WriteFile(pidFile, []byte(fmt.Sprintf("%d", os.Getpid())), 0644)
}
// GetJvmrcFilePath get jvmrc file from jvmrc dir
func GetJvmrcFilePath(jvmrcDir string) string {
if jvmrcDir != "" {
folder, err := os.Stat(jvmrcDir)
if err == nil && folder.IsDir() {
return filepath.Join(jvmrcDir, ".jvmrc")
}
}
return ""
}
// 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 time stamp
func GetTimestamp() string {
return strconv.FormatInt(time.Now().Unix(), 10)
}
// 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"
}