-
Notifications
You must be signed in to change notification settings - Fork 230
/
system.go
109 lines (96 loc) · 2.42 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
package util
import (
"fmt"
"io/ioutil"
"os"
"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 ""
}
func GetTimestamp() string {
return strconv.FormatInt(time.Now().Unix(), 10)
}
// 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"
}