-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
277 lines (244 loc) · 6.75 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// SPDX-License-Identifier: Apache-2.0
package main
import (
"log"
"math/big"
"os"
"radicle-cloud/cloud"
"radicle-cloud/db"
"radicle-cloud/eth"
"radicle-cloud/utils"
"time"
"github.com/joho/godotenv"
)
var l *log.Logger
func init() {
l = log.New(os.Stderr, "[MAIN] ", log.Ldate|log.Ltime|log.Lshortfile)
setup()
}
func main() {
var currentBlock uint64 = 0
go eth.UpdateCurrentBlock(¤tBlock)
for currentBlock == 0 {
l.Println("Initializing current block")
time.Sleep(time.Second)
}
channels := map[string]chan eth.Event{}
ethEvents := make(chan eth.Event)
stateEvents := make(chan db.Dep)
go runEthListener(ethEvents, ¤tBlock)
go terminateExpiringOrgs(stateEvents, ¤tBlock)
for {
// stream events from contract
e := <-ethEvents
if err := db.UpsertEvent(e); err != nil {
l.Fatal("Failed to upsert event", e, err)
}
// if event has been removed, a reorg has happened
if e.Removed {
stateAfterRemoval(&e, ¤tBlock)
}
ch, chCreated := getOrCreateOrgChannel(e.Org, &channels)
if chCreated {
go processEventsForOrg(e.Org, ch, stateEvents)
}
ch <- e
}
}
func getOrCreateOrgChannel(org string, channels *map[string]chan eth.Event) (chan eth.Event, bool) {
chs := *channels
if ch, ok := chs[org]; ok {
return ch, false
}
chs[org] = make(chan eth.Event, 100)
return chs[org], true
}
func processEventsForOrg(org string, ch chan eth.Event, stateEvents chan db.Dep) {
for e := range ch {
tries := 3
for {
tries--
if ok := processEvent(e, stateEvents); ok {
break
}
if tries == 0 {
l.Fatal("Failed to process event for org", org, e)
}
time.Sleep(time.Second * 10)
}
}
}
func processEvent(e eth.Event, stateEvents chan db.Dep) bool {
l.Printf("Processing: %+v\n", e)
if e.Type == eth.DeploymentStoppedEvent {
provider, err := db.GetProvider(e.Org)
if err != nil {
l.Fatal("Failed to get provider for org", e.Org, err)
}
stateEvents <- db.Dep{Org: e.Org, Expiry: e.Expiry, Provider: provider}
return true
}
// upsert deployment, update expiry if already exists
provider, ip, status, err := db.UpsertDep(e)
if err != nil {
l.Fatal("Error upserting org", e.Org, err)
}
if status == db.RunningStatus && provider != "" {
// org existed, updated expiry, and we can exit
stateEvents <- db.Dep{Org: e.Org, Expiry: e.Expiry, Provider: provider}
return true
}
switch status {
// case db.InitialStatus:
// straight ahead
case db.AllocatedStatus:
goto ALLOCATED
case db.SetupFailedStatus:
goto SETUP
case db.RunningStatus:
goto RUNNING
}
// reserve a server
provider, ip, err = cloud.ReserveServer(e.Org)
if err != nil {
l.Println("Couldn't reserve server", err)
return false
}
// update ip and provider for deployment and set status to allocated
if err = db.UpdateOrgServer(e.Org, ip, provider); err != nil {
l.Println("Failed updating ip for org", e.Org, err)
return false
}
ALLOCATED:
// create dns record for org subdomain
if err = cloud.CreateDNS(e.Org, ip); err != nil {
if err.Error() != "HTTP status 400: Record already exists. (81057)" {
l.Println("Failed to create dns record for org", e.Org, err)
return false
}
}
SETUP:
RUNNING:
if status != db.RunningStatus {
// run ansible for initial setup
err = cloud.RunAnsible(e.Org, ip, 10)
} else {
// ansible already configured this
err = nil
}
if err != nil {
// failed after 10 retries
l.Println("Failed to complete configuration after 10 tries for", e.Org, ip, err)
if err = db.SetStatus(e.Org, "setup-failed"); err != nil {
l.Println("Failed to set status to 'setup-failed' for", e.Org, ip, err)
}
} else {
l.Println("Configured org", e.Org, "with ip", ip)
// flip status to runnning
if err = db.SetStatus(e.Org, "running"); err != nil {
l.Println("Failed to set status to 'running' for", e.Org, ip, err)
} else {
l.Printf("Org %s status set to 'running' in DB", e.Org)
stateEvents <- db.Dep{Org: e.Org, Expiry: e.Expiry, Provider: provider}
if err = db.MarkEventProcessed(e.BlockAndTx); err != nil {
return true
}
return false
}
}
return false
}
func setup() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file", err)
}
db.Setup()
cloud.Setup()
}
func getLastProcessedBlock(current *uint64) *big.Int {
var last big.Int
lastProcessed, err := db.GetLastProcessedBlock()
if err != nil {
l.Println("Error getting last processed block", err)
last.SetUint64(*current)
}
last.SetUint64(lastProcessed)
return &last
}
func runEthListener(ec chan eth.Event, currentBlock *uint64) {
for {
eth.StartListening(ec, getLastProcessedBlock(currentBlock))
time.Sleep(time.Second * 5)
}
}
func terminateExpiringOrgs(c chan db.Dep, currentBlock *uint64) {
// list all deployments with ascending expiring date
deps, err := db.ListDeployments()
if err != nil {
l.Fatal("Can't list Deployments", err)
}
s := new(utils.ExpiryState).Init().AddDeps(deps)
for {
// more than an event can be in a block so loop through all of them
for {
// take a peek to check if smallest block in heap has expired
block, ok := s.Peek()
if !ok {
// break out of loop if there's nothing
break
}
if block <= *currentBlock {
for _, dep := range s.GetDeps(block) {
l.Printf("Deployment for org=%s has expired\n", dep.Org)
if cloud.TerminateOrg(dep.Org, dep.Provider) {
l.Println("Cloud resource was terminated for", dep.Org, "in", dep.Provider)
}
if err := db.DeleteOrg(dep.Org); err != nil {
time.Sleep(5 * time.Second)
l.Fatalf("Failed to delete org=%s provider=%s err=%v\n", dep.Org, dep.Provider, err)
}
}
s.Next() // clean up
} else {
// min of heap is still higher than current block
break
}
}
nextAwake := 3600 * time.Second
if block, ok := s.Peek(); ok {
nextAwake = time.Duration((block-*currentBlock)*14) * time.Second
}
select {
// sleep until next org expires
case <-time.After(nextAwake):
// or a new event happens
case e := <-c:
s.AddOrUpdateDep(e)
}
}
}
func stateAfterRemoval(e *eth.Event, currentBlock *uint64) {
events, err := db.ListOrgEvents(e.Org)
if err != nil {
l.Fatal("Error processing removal for org", e.Org, err)
}
if len(events) > 0 {
event := events[0]
// if expiry is still valid, this is the latest state
if event.Expiry > *currentBlock {
// swap expiry with last valid state
e.Expiry = event.Expiry
// if this event is confirmed, remove other events before it
if event.BlockNumber+9 <= *currentBlock {
if err := db.DeleteOrgEventsBefore(e.Org, event.BlockNumber); err != nil {
l.Println("Failed to delete events before", event.BlockNumber, "for", e.Org)
}
}
return
}
}
// no events means deployment should be removed
e.Type = eth.DeploymentStoppedEvent
e.Expiry = *currentBlock
}