-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
138 lines (126 loc) · 3.36 KB
/
index.js
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
var cache = require('memory-cache');
var express = require('express');
var fleetctl = require('../lib/fleetctl');
var router = express.Router();
router.get('/', function(req, res) {
res.render('index', {title: 'CoreGI'});
});
router.get('/api', function(req, res) {
res.json({
machines: 'Enumerate the current hosts in the cluster.',
units: 'List the current state of units in the cluster.',
unitFiles: 'List the units that exist in the cluster.',
start: 'Instruct systemd to start one or more units in the cluster, first submitting and loading if necessary.',
stop: 'Instruct systemd to stop one or more units in the cluster.',
destroy: 'Destroy one or more units in the cluster.'
});
});
router.get('/api/machines', function(req, res) {
var machines = cache.get('machines') || [];
res.json(machines);
});
router.get('/api/units', function(req, res) {
var units = cache.get('units') || [];
res.json(units);
});
router.get('/api/units/:unit', function(req, res) {
var units = cache.get('units') || [];
var unit = {error: 'unit not found'};
for(var index in units) {
if(units[index].unit === req.params.unit) {
unit = units[index];
break;
}
}
if(unit.error) {
res.status(404).json(unit);
}
else {
res.json(unit);
}
});
router.put('/api/units/:unit', function(req, res) {
if(req.params && req.query) {
if(req.query.action === 'start') {
fleetctl.start(req.params, function(err) {
if(err) {
res.status(400).json({error: 'error starting unit'});
}
else {
res.status(200).end();
}
});
}
else if(req.query.action === 'stop') {
fleetctl.stop(req.params, function(err) {
if(err) {
res.status(400).json({error: 'error stopping unit'});
}
else {
res.status(200).end();
}
});
}
else {
res.status(400).json({error: 'unknown action'});
}
}
else {
res.status(400).json({error: 'no unit provided'});
}
});
router.delete('/api/units/:unit', function(req, res) {
if(req.params && req.query) {
fleetctl.destroy(req.params, function(err) {
if(err) {
res.status(400).json({error: 'error destroying unit'});
}
else {
res.status(200).end();
}
});
}
else {
res.status(400).json({error: 'no unit provided'});
}
});
router.get('/api/unitFiles', function(req, res) {
var unitFiles = cache.get('unitFiles') || [];
res.json(unitFiles);
});
router.get('/api/unitFiles/:unit', function(req, res) {
var unitFiles = cache.get('unitFiles') || [];
var unitFile = {error: 'unit not found'};
for(var index in unitFiles) {
if(unitFiles[index].unit === req.params.unit) {
unitFile = unitFiles[index];
break;
}
}
if(unitFile.error) {
res.status(404).json(unitFile);
}
else {
res.json(unitFile);
}
});
router.delete('/api/unitFiles/:unit', function(req, res) {
if(req.params && req.query) {
fleetctl.destroy(req.params, function(err) {
if(err) {
res.status(400).json({error: 'error destroying unitFile'});
}
else {
res.status(200).end();
}
});
}
else {
res.status(400).json({error: 'no unitFile provided'});
}
});
router.get('/api/keys', function(req, res) {
var keys = cache.get('keys') || [];
res.json(keys);
});
module.exports = router;