-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodd_rsync.go
84 lines (74 loc) · 1.66 KB
/
modd_rsync.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"github.com/shirou/gopsutil/process"
)
type ModdPid struct {
Pid int32 `json:"pid"`
}
// Start the modd proccess with modd conf file
func StartModd(env Env) {
filename := "opennebula_modd-" + env.environment + ".conf"
cmd := exec.Command("modd", "-f", filename)
cmd.Dir = env.config.ConfigPath
errs := cmd.Start()
if errs != nil {
fmt.Println("Unable to run: ", errs)
return
}
WriteModdPid(cmd.Process.Pid, env)
}
func StopModd(env Env) {
pid := GetModdPid(env)
p, err := process.NewProcess(pid)
if err != nil {
fmt.Println(err)
return
}
name, err := p.Name()
if err != nil {
fmt.Println(err)
return
}
if name != "modd" {
return
}
process, err := os.FindProcess(int(pid))
if err != nil {
fmt.Printf("ERR: Process pid: %d could not be found\n", pid)
return
}
err = process.Kill()
if err != nil {
fmt.Println(err)
}
moddPIDPath := GetModdPIDPath(env)
os.Remove(moddPIDPath) // Remove when done
}
func GetModdPIDPath(env Env) string {
moddPidName := env.config.ConfigPath + "/modd_pid-" + env.environment + ".json"
return moddPidName
}
func WriteModdPid(pid int, env Env) {
modd_pid := ModdPid{int32(pid)}
file, _ := json.MarshalIndent(modd_pid, "", " ")
moddPIDPath := GetModdPIDPath(env)
_ = ioutil.WriteFile(moddPIDPath, file, 0644)
}
func GetModdPid(env Env) int32 {
var configuration ModdPid
moddPIDPath := GetModdPIDPath(env)
configFile, err := os.Open(moddPIDPath)
if err != nil {
fmt.Println(err.Error())
return 0
}
defer configFile.Close()
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&configuration)
return configuration.Pid
}