-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
106 lines (83 loc) · 2.06 KB
/
build.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
package builder
import (
"encoding/json"
"github.com/radiodan/debug/radiodan"
"github.com/radiodan/debug/utils"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
var appNames = []string{
"buttons", "cease", "example", "magic",
"server", "updater", "debug",
}
func Build() radiodan.DebugInfo {
d := radiodan.DebugInfo{Timestamp: time.Now()}
// fetch ip address(es)
d.Hostname = utils.Hostname()
d.Addresses = utils.IpAddresses()
// test for internet connection
d.InternetConnection = utils.CheckConnection()
// log active apps
d.Applications = checkApps()
return d
}
func checkApps() (apps []radiodan.RadiodanApplication) {
for _, appName := range appNames {
apps = append(apps, checkApp(appName))
}
return
}
func checkApp(appName string) (app radiodan.RadiodanApplication) {
app.Name = appName
app.Deploy = fetchDeployFile(app)
app.LogTail = fetchLogFile(app)
app.Pid = fetchPidFile(app)
app.IsRunning = utils.CheckProcess(app.Pid)
return
}
func fetchDeployFile(app radiodan.RadiodanApplication) (output radiodan.Deploy) {
file, err := ioutil.ReadFile(app.DeployFile())
if err != nil {
log.Println("[!] Could not open file", app.DeployFile())
return
}
err = json.Unmarshal(file, &output)
if err != nil {
log.Println("[!] Could not marshal file", err)
}
return
}
func fetchLogFile(app radiodan.RadiodanApplication) (output string) {
path := app.LogFile()
_, err := os.Stat(path)
if err != nil {
log.Printf("[!] Could not open file %s", path)
return
}
stdout, err := exec.Command("/usr/bin/tail", "-n 100", path).Output()
if err != nil {
log.Printf("[!] Command failed: %s", err)
} else {
output = string(stdout)
}
return
}
func fetchPidFile(app radiodan.RadiodanApplication) (output int64) {
path := app.PidFile()
file, err := ioutil.ReadFile(path)
if err != nil {
log.Println("[!] Could not open file", path)
return
}
pidString := strings.Trim(string(file), "\n")
output, err = strconv.ParseInt(pidString, 10, 0)
if err != nil {
log.Println("[!] Could not parse pid as integer", pidString)
}
return
}