This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Dashboard.coffee
206 lines (191 loc) · 5.23 KB
/
Dashboard.coffee
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
Q = require 'q'
util = require 'util'
myMockRequestHandler = ({cluster, host, options}) ->
deferred = Q.defer()
setTimeout( (->
if host.id is 'host'
deferred.resolve
backend:
build: '42'
version: '1.3.4'
release: '1.3.4-42'
states: (started: true)
frontend:
build: '41'
version: '1.2.1'
release: '1.2.1-41'
states: (started: true)
else if host.id is 'app1'
deferred.resolve
backend:
build: '41'
version: '1.3.4'
release: '1.3.4-41'
states: (started: true)
frontend:
build: '41'
version: '1.2.1'
release: '1.2.1-41'
states: (started: true)
support:
build: '10'
version: '1.0.1'
release: '1.0.1-10'
states: (started: true)
else if host.id is 'app2'
deferred.resolve
backend:
build: '42'
version: '1.3.4'
release: '1.3.4-42'
states: (stopped: true)
frontend:
build: '40'
version: '1.2.1'
release: '1.2.1-40'
states: (stopped: true)
support:
build: '10'
version: '1.0.1'
release: '1.0.1-10'
states: (started: true)
else
# Fake an unreachable host exception.
deferred.reject level: 'connection-socket', code: 'ENOTFOUND', message: 'Fake: Host not configured (example).'
), 1)
return deferred.promise
myMockAfterRequestIsModuleStillUp2Date = ({data}) ->
if data
for own moduleId, moduleData of data
switch moduleId
when 'backend'
moduleData.available =
build: '42'
version: '1.3.4'
release: '1.3.4-42'
when 'frontend'
moduleData.available =
build: '41'
version: '1.2.1'
release: '1.2.1-41'
return data
myMockSendResultsToDashboard = ({data, host, cluster, handler, dashboard}) ->
for own moduleId, module of data
result =
clusterId: cluster.id
hostId: host.id
moduleId: moduleId
data: module
states : module.states
dashboard.updateModule result
myMockActionHandler = (type, context, dashboard) ->
if type is 'update'
cluster = dashboard.getCluster(context.clusterId)
host = dashboard.getHost(cluster, context.hostId)
module_ = dashboard.getModule(host, context.moduleId)
module_.states.updating = on
dashboard.updateModule
clusterId: cluster.id
hostId: host.id
moduleId: module_.id
data: module_
states : module_.states
# Update complete in 5 secs.
setTimeout((->
module_.states.updating = off
module_.build = parseInt(module_.data.build) + 1
module_.release = "#{module_.data.version}-#{module_.build}"
dashboard.updateModule
clusterId: cluster.id
hostId: host.id
moduleId: module_.id
data: module_
states : module_.states
), 5000)
# Revert update in 20 secs.
setTimeout((->
module_.build = parseInt(module_.data.build) - 1
module_.release = "#{module_.data.version}-#{module_.build}"
dashboard.updateModule
clusterId: cluster.id
hostId: host.id
moduleId: module_.id
data: module_
states : module_.states
), 20000)
return
module.exports = (app) ->
app.init(interval: 120, port: 3000)
app.initActionHandler myMockActionHandler
app.initDashboard ( (dashboard) ->
dashboard.handler 'defaultHandler', (
steps: [(
fn: myMockRequestHandler
options: (
actions: [(
id: 'checkFreshness'
fn: myMockAfterRequestIsModuleStillUp2Date
)]
finalizes: [(
id: 'apply_config'
fn: myMockSendResultsToDashboard
)]
) # end-options
)]
)
dashboard.cluster 'demo1', (
display: 'Demo Cluster 1'
hosts:
'host':
hostname: 'host.cluster1.lan'
modules:
'backend':
display: 'Backend'
'frontend':
display: 'Frontend'
handlers:
main:
type: 'defaultHandler'
)
dashboard.cluster 'demo2', (
display: 'Demo Cluster 2'
hosts:
app1:
display: 'App 1'
hostname: 'host1.cluster2.lan'
modules:
backend:
display: 'Backend'
frontend:
display: 'Frontend'
support:
display: 'Support'
handlers:
main:
type: 'defaultHandler'
app2:
display: 'App 2'
hostname: 'host2.cluster2.lan'
modules:
backend:
display: 'Backend'
frontend:
display: 'Frontend'
support:
display: 'Support'
handlers:
main:
type: 'defaultHandler'
db1:
display: 'Db 2'
hostname: 'db1.cluster2.lan'
modules:
db:
display: 'Database'
support:
display: 'Support'
handlers:
main:
type: 'defaultHandler'
)
)