-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
163 lines (138 loc) · 3.65 KB
/
main.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
159
160
161
162
163
package main
import (
"encoding/json"
"net/http"
"os"
"sort"
"time"
log "github.com/Sirupsen/logrus"
"github.com/asdine/storm"
"github.com/jmoiron/jsonq"
"github.com/resin-io/edge-node-manager/api"
"github.com/resin-io/edge-node-manager/application"
"github.com/resin-io/edge-node-manager/config"
"github.com/resin-io/edge-node-manager/device"
"github.com/resin-io/edge-node-manager/process"
"github.com/resin-io/edge-node-manager/supervisor"
)
var (
// This variable will be populated at build time with the current version tag
version string
// This variable defines the delay between each processing loop
loopDelay time.Duration
)
func main() {
log.Info("Starting edge-node-manager")
if err := checkVersion(); err != nil {
log.Error("Unable to check if edge-node-manager is up to date")
}
supervisor.WaitUntilReady()
for {
// Run processing loop
loop()
// Delay between processing each set of applications to prevent 100% CPU usage
time.Sleep(loopDelay)
}
}
func init() {
log.SetLevel(config.GetLogLevel())
log.SetFormatter(&log.TextFormatter{ForceColors: true, DisableTimestamp: true})
var err error
loopDelay, err = config.GetLoopDelay()
if err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Fatal("Unable to load loop delay")
}
dbDir := config.GetDbDir()
if err := os.MkdirAll(dbDir, os.ModePerm); err != nil {
log.WithFields(log.Fields{
"Directory": dbDir,
"Error": err,
}).Fatal("Unable to create database directory")
}
db, err := storm.Open(config.GetDbPath())
if err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Fatal("Unable to open database")
}
defer db.Close()
if err := db.Init(&device.Device{}); err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Fatal("Unable to initialise database")
}
go func() {
router := api.NewRouter()
port := ":1337"
log.WithFields(log.Fields{
"Port": port,
}).Debug("Initialising incoming supervisor API")
if err := http.ListenAndServe(port, router); err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Fatal("Unable to initialise incoming supervisor API")
}
}()
}
func checkVersion() error {
resp, err := http.Get("https://api.github.com/repos/resin-io/edge-node-manager/releases/latest")
if err != nil {
return err
}
defer resp.Body.Close()
data := map[string]interface{}{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return err
}
latest, err := jsonq.NewQuery(data).String("tag_name")
if err != nil {
return err
}
if version == latest {
log.WithFields(log.Fields{
"Current version": version,
}).Info("edge-node-manager upto date")
} else {
log.WithFields(log.Fields{
"Current version": version,
"Latest version": latest,
"Update command": "git push resin master:resin-nocache",
}).Warn("Please update edge-node-manager")
}
return nil
}
func loop() {
// Get applications from the supervisor
bytes, errs := supervisor.DependentApplicationsList()
if errs != nil {
log.WithFields(log.Fields{
"Errors": errs,
}).Error("Unable to get applications")
return
}
// Unmarshal applications
applications, err := application.Unmarshal(bytes)
if err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Error("Unable to unmarshal applications")
return
}
// Sort applications to ensure they run in order
var keys []int
for key := range applications {
keys = append(keys, key)
}
sort.Ints(keys)
// Process applications
for _, key := range keys {
if errs := process.Run(applications[key]); errs != nil {
log.WithFields(log.Fields{
"Application": applications[key],
"Errors": errs,
}).Error("Unable to process application")
}
}
}