-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
465 lines (417 loc) · 14.4 KB
/
app.js
File metadata and controls
465 lines (417 loc) · 14.4 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
class App extends React.Component {
state = {
shouldAskForAwsKey: !window.localStorage["aws-key"],
awsKey: window.localStorage["aws-key"] || "",
isUpdatingStatus: true,
machineStatus: "secondary"
}
getApi = () => {
const [id, secret] = this.state.awsKey.split(":")
const awsApi = new AWS.EC2({
region: "eu-central-1",
apiVersion: "2016-11-15",
credentials: { accessKeyId: id, secretAccessKey: secret, region: "eu-central-1" }
})
return {
getInstanceStatus: () =>
new Promise((resolve, reject) => {
awsApi.describeInstanceStatus({ InstanceIds: ["i-062dbff0aacf41af1"], IncludeAllInstances: true }, (err, data) => {
if (err) reject(err)
else resolve(data.InstanceStatuses[0].InstanceState.Name)
})
}),
getMinecraftStatus: () =>
fetch("https://minecraft.deltaidea.com:5000/minecraft-status", { cache: "no-store" }).then(r => r.json()),
getMapStatus: () =>
fetch("https://minecraft.deltaidea.com:5000/map-status", { cache: "no-store" }).then(r => r.json()),
startInstance: () =>
new Promise((resolve, reject) => {
awsApi.startInstances({ InstanceIds: ["i-062dbff0aacf41af1"]}, (err, data) => {
if (err) reject(err)
else resolve(data)
})
}),
stopInstance: () =>
fetch("https://minecraft.deltaidea.com:5000/system-shutdown", {
method: "POST",
cache: "no-store",
body: `key=${this.state.awsKey}`
}).then(r => r.json()),
}
}
askForAwsKey = () => {
this.setState({ shouldAskForAwsKey: true })
}
changeAwsKey = awsKey => {
window.localStorage["aws-key"] = awsKey
this.setState({ shouldAskForAwsKey: false, awsKey })
}
cancelAwsKeyChange = () => {
this.setState({ shouldAskForAwsKey: false })
}
setUpdating = isUpdatingStatus => {
this.setState({ isUpdatingStatus })
}
setMachineStatus = machineStatus => {
this.setState({ machineStatus })
}
componentDidUpdate() {
$('[data-toggle="tooltip"]').tooltip()
$('[data-toggle="popover"]').popover()
}
render() {
return (
<React.Fragment>
<Navbar canAskForAwsKey={!this.state.shouldAskForAwsKey} askForAwsKey={this.askForAwsKey} />
{
this.state.shouldAskForAwsKey
? <KeyForm
onChange={this.changeAwsKey}
canCancel={!!this.state.awsKey}
onCancel={this.cancelAwsKeyChange}
/>
: <React.Fragment>
<StatusList api={this.getApi()} setUpdating={this.setUpdating} setMachineStatus={this.setMachineStatus} />
<Actions api={this.getApi()} isUpdatingStatus={this.state.isUpdatingStatus} machineStatus={this.state.machineStatus} />
</React.Fragment>
}
</React.Fragment>
)
}
}
class Navbar extends React.Component {
render() {
return (
<nav className="navbar navbar-expand-lg navbar-dark bg-dark">
<a className="navbar-brand" href="#">
<img src="./grass.jpg" width="30" height="30" className="d-inline-block align-top" alt="" />
{" "}DQ Server
</a>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarMain">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarMain">
<ul className="navbar-nav mr-auto">
<li className="nav-item">
<a className="nav-link" href="http://minecraft.deltaidea.com">World map</a>
</li>
<li className="nav-item">
<a className="nav-link" href="https://trello.com/b/VI1FemyT">Trello</a>
</li>
<li className="nav-item">
<a className="nav-link" href="https://github.com/dq-server">GitHub</a>
</li>
</ul>
{
this.props.canAskForAwsKey &&
<button className="btn btn-link" onClick={this.props.askForAwsKey}>Change AWS Key</button>
}
</div>
</nav>
)
}
}
class KeyForm extends React.Component {
state = {
value: "",
showValidationError: false,
}
handleChange = event => {
this.setState({ value: event.target.value, showValidationError: false })
}
handleSubmit = event => {
event.preventDefault()
if (/.+:.+/.test(this.state.value)) {
this.props.onChange(this.state.value)
} else {
this.setState({ showValidationError: true })
}
}
render() {
return (
<div className="container">
<div className="row justify-content-center my-4 my-md-5">
<div className="col-md-8">
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<input
type="password"
className="form-control bg-secondary text-white"
placeholder="AWS Key"
autoComplete="off"
value={this.value}
onChange={this.handleChange}
/>
</div>
</form>
{
this.props.canCancel &&
<div className="form-group">
<button className="btn btn-secondary" onClick={this.props.onCancel}>Cancel</button>
</div>
}
{ this.state.showValidationError && <div className="text-danger mt-2">Invalid key</div> }
</div>
</div>
</div>
)
}
}
class StatusList extends React.Component {
state = {
updateTimerId: null,
isMachineUpdateInProgress: true,
machineStatus: "secondary",
machineMessage: "Updating...",
isMinecraftUpdateInProgress: true,
minecraftStatus: "secondary",
minecraftMessage: "Updating...",
isMapUpdateInProgress: true,
mapStatus: "secondary",
mapMessage: "Updating..."
}
updateUpstreamUpdateFlag = () => {
if (
!this.state.isMachineUpdateInProgress &&
!this.state.isMinecraftUpdateInProgress &&
!this.state.isMapUpdateInProgress
) {
this.props.setUpdating(false)
} else {
this.props.setUpdating(true)
}
}
updateMachineStatus = async () => {
this.setState({
isMachineUpdateInProgress: true,
machineStatus: "secondary",
machineMessage: "Updating...",
isMinecraftUpdateInProgress: true,
minecraftStatus: "secondary",
minecraftMessage: "Updating...",
isMapUpdateInProgress: true,
mapStatus: "secondary",
mapMessage: "Updating..."
}, this.updateUpstreamUpdateFlag)
let rawStatus, err
try {
rawStatus = await this.props.api.getInstanceStatus()
} catch (_err) {
err = _err
}
let status, message
if (rawStatus === "running") {
status = "success"
message = "Online"
} else if (rawStatus === "pending") {
status = "warning"
message = "Starting up"
} else if (rawStatus === "shutting-down" || rawStatus === "stopping") {
status = "warning"
message = "Shutting down"
} else {
status = "danger"
message = "Offline"
}
this.setState({
isMachineUpdateInProgress: false,
machineStatus: status,
machineMessage: (err && err.message) || message
})
this.props.setMachineStatus(status)
if (status !== "success") throw new Error()
}
updateMinecraftStatus = async () => {
try {
const data = await this.props.api.getMinecraftStatus()
this.setState({
isMinecraftUpdateInProgress: false,
minecraftStatus: data.players.online > 0 ? "success" : "warning",
minecraftMessage: data.players.online > 0 ? "Online" : "Empty"
}, this.updateUpstreamUpdateFlag)
} catch (err) {
console.error(err)
this.setState({
isMinecraftUpdateInProgress: false,
minecraftStatus: "danger",
minecraftMessage: err.message
}, this.updateUpstreamUpdateFlag)
}
}
updateMapStatus = async () => {
try {
const data = await this.props.api.getMapStatus()
this.setState({
isMapUpdateInProgress: false,
mapStatus: data.status === 200 ? "success" : "danger",
mapMessage: data.status === 200 ? "Online" : r.statusText
}, this.updateUpstreamUpdateFlag)
} catch (err) {
console.error(err)
this.setState({
isMapUpdateInProgress: false,
mapStatus: "danger",
mapMessage: err.message
}, this.updateUpstreamUpdateFlag)
}
}
updateAll = () => {
this.updateMachineStatus()
.catch(() => {
this.setState({
isMinecraftUpdateInProgress: false,
minecraftStatus: "danger",
minecraftMessage: "Offline",
isMapUpdateInProgress: false,
mapStatus: "danger",
mapMessage: "Offline"
}, this.updateUpstreamUpdateFlag)
return true
})
.then((wasErrorHandled) => {
if (!wasErrorHandled) {
this.updateMinecraftStatus()
this.updateMapStatus()
}
})
}
componentDidMount() {
this.updateAll()
const updateTimerId = setInterval(this.updateAll, 60000)
this.setState({ updateTimerId })
}
componentWillUnmount() {
clearInterval(this.state.updateTimerId)
}
getCardBorderColor = () => {
const s1 = this.state.machineStatus
const s2 = this.state.minecraftStatus
const s3 = this.state.mapStatus
let color = "secondary"
if (s1 === "success" && s2 === "success" && s3 === "success") color = "success"
if (s1 === "warning" || s2 === "warning" || s3 === "warning") color = "warning"
if (s1 === "danger" || s2 === "danger" || s3 === "danger") color = "danger"
return color
}
getCardHeader = () => {
if (this.state.minecraftStatus === "success") return "Minecraft running"
if (this.state.machineStatus === "danger") return "Machine down"
if (this.state.minecraftStatus === "warning") return "No one's playing"
return "Systems health"
}
render() {
return (
<div className="container">
<div className="row justify-content-center my-4 my-md-5">
<div className="col-md-8">
<div className={`card border-${this.getCardBorderColor()}`}>
<h4 className="card-header">{this.getCardHeader()}</h4>
<ul className="list-group list-group-flush">
<li className="list-group-item d-flex justify-content-between align-items-center">
<span>Underlying machine</span>
<span className={`text-${this.state.machineStatus} text-capitalize`}>{this.state.machineMessage}</span>
</li>
<li className="list-group-item d-flex justify-content-between align-items-center">
<span>Minecraft server</span>
<span className={`text-${this.state.minecraftStatus} text-capitalize`}>{this.state.minecraftMessage}</span>
</li>
<li className="list-group-item d-flex justify-content-between align-items-center">
<span>World map</span>
<span className={`text-${this.state.mapStatus} text-capitalize`}>{this.state.mapMessage}</span>
</li>
</ul>
</div>
</div>
</div>
</div>
)
}
}
class Actions extends React.Component {
state = {
actionResultMessage: "",
wasActionSuccessful: false,
isActionInProgress: false
}
startInstance = async event => {
this.setState({ isActionInProgress: true })
try {
await this.props.api.startInstance()
this.setState({
isActionInProgress: false,
wasActionSuccessful: true,
actionResultMessage: "The server is starting up. You may have to wait 30-60 seconds."
})
} catch (e) {
this.setState({
isActionInProgress: false,
wasActionSuccessful: false,
actionResultMessage: err && err.message
})
}
}
stopInstance = async event => {
this.setState({ isActionInProgress: true })
try {
const result = await this.props.api.stopInstance()
this.setState({
isActionInProgress: false,
wasActionSuccessful: result.result == "success",
actionResultMessage: result.message
})
} catch (err) {
this.setState({
isActionInProgress: false,
wasActionSuccessful: false,
actionResultMessage: err && err.message
})
}
}
render() {
return (
<div className="container">
<div className="row justify-content-center my-4 my-md-5">
<div className="col-md-8">
<div className="card text-white bg-dark mb-3">
<h4 className="card-header">Actions</h4>
<div className="card-body">
{
this.props.machineStatus === "danger" &&
<button
type="button"
className="card-link btn btn-primary"
disabled={!this.state.isActionInProgress && !this.props.isUpdatingStatus ? undefined : "disabled"}
onClick={this.startInstance}
>Start the machine</button>
}
{
this.props.machineStatus === "success" &&
<React.Fragment>
<div className="mb-3">
The server will stay online indefinitely. Write <code>--system-shutdown</code> in the game
chat or press the button below to stop the instance.
</div>
<button
type="button"
className="card-link btn btn-primary"
disabled={!this.state.isActionInProgress && !this.props.isUpdatingStatus ? undefined : "disabled"}
onClick={this.stopInstance}
>Stop the machine</button>
</React.Fragment>
}
{this.state.isActionInProgress && <div className="mt-3 text-muted">Wait...</div>}
{
this.state.actionResultMessage && !this.state.isActionInProgress &&
<div className={`mt-3 ${this.state.wasActionSuccessful ? "text-success" : "text-danger"}`}>
{this.state.actionResultMessage}
</div>
}
</div>
</div>
</div>
</div>
</div>
)
}
}
ReactDOM.render(<App />, document.querySelector('#root'))