-
Notifications
You must be signed in to change notification settings - Fork 20
/
dashboard.service.js
executable file
·214 lines (186 loc) · 5.77 KB
/
dashboard.service.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
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
const { Command } = require('commander');
const path = require('path');
const fs = require('fs');
const yaml = require('js-yaml');
const execSync = require("child_process").execSync;
const chalk = require('chalk');
const pkg = require('./package.json');
const config = loadYamlConfig();
const program = new Command();
const COMPONETS = {
GATEWAY: 'gateway',
PROMETHEUS: 'prometheus',
STATS_EXPORTER: 'stats-exporter',
WEBSERVER: 'webserver'
}
program.version(`NebulaGraph Dashboard Community v${pkg.version}`, '-v --version', 'NebulaGraph Dashboard version');
program
.name('dashboard');
program
.command('start')
.argument('<service>', 'service type')
.description(`start service, all|${COMPONETS.GATEWAY}|${COMPONETS.STATS_EXPORTER}|${COMPONETS.PROMETHEUS}|${COMPONETS.WEBSERVER}`)
.action((service) => {
checkAndUpdateConfig();
startServices(service);
});
program
.command('stop')
.argument('<service>', 'service type')
.description(`stop service, all|${COMPONETS.GATEWAY}|${COMPONETS.STATS_EXPORTER}|${COMPONETS.PROMETHEUS}|${COMPONETS.WEBSERVER}`)
.action((service) => {
stopServices(service);
})
program
.command('status')
.argument('<service>', 'service type')
.description(`status service, all|${COMPONETS.GATEWAY}|${COMPONETS.STATS_EXPORTER}|${COMPONETS.PROMETHEUS}|${COMPONETS.WEBSERVER}`)
.action((service) => {
statusServices(service)
})
program
.command('restart')
.argument('<service>', 'service type')
.description(`restart service, all|${COMPONETS.GATEWAY}|${COMPONETS.STATS_EXPORTER}|${COMPONETS.PROMETHEUS}|${COMPONETS.WEBSERVER}`)
.action((service) => {
restartServices(service)
})
program.parse();
function loadYamlConfig() {
const doc = yaml.load(fs.readFileSync(path.resolve(process.cwd(), 'config.yaml'), 'utf8'));
return doc;
}
function INFO(...info) {
console.log(chalk.green(info.join(' ')))
}
function ERROR(...info) {
console.log(chalk.red(info.join(' ')))
}
function checkConfig(config) {
if (!config.port) {
throw new Error('port is required, no port info found in config.yaml');
}
if (!config['stats-exporter']) {
throw new Error('stats-exporter is required, no stats-exporter info found in config.yaml');
}
if (!config.gateway) {
throw new Error('gateway is required, no gateway info found in config.yaml');
}
if (!(config.prometheus)) {
throw new Error('prometheus is required, no prometheus info found in config.yaml');
}
return true;
}
async function updateGatewayConfig() {
const gatewayConfig = config.gateway;
const gatewayPath = path.resolve(process.cwd(), 'vendors/nebula-http-gateway/conf/app.conf');
fs.readFile(gatewayPath, 'utf8', function (err, data) {
if (err) {
return INFO(err);
}
let result = data.replace(/httpport = \S*/g, `httpport = ${gatewayConfig.port}`);
result = result.replace(/runmode = \S*/g, `runmode = ${gatewayConfig.runmode}`);
fs.writeFileSync(gatewayPath, result, 'utf8');
});
}
function updateStatsExporterConfig() {
const statsExporterPath = path.resolve(process.cwd(), 'vendors/nebula-stats-exporter/config.yaml');
const statsExporterConfig = config['nebula-cluster'];
const content = {
clusters: [
{
name: statsExporterConfig.name,
instances: []
}
]
};
['metad', 'storaged', 'graphd'].forEach((type) => {
statsExporterConfig[type].forEach(item => {
content.clusters[0].instances.push({
name: item.name,
endpointIP: item.endpointIP,
endpointPort: item.endpointPort,
componentType: type
})
})
});
const result = yaml.dump(content);
fs.writeFileSync(statsExporterPath, result, 'utf8', function (err) {
if (err) return INFO(err);
});
}
function updatePrometheusConfig() {
const prometheusConfig = config.prometheus;
const prometheusPath = path.resolve(process.cwd(), 'vendors/prometheus/prometheus.yml');
const content = {
global: {
scrape_interval: prometheusConfig.scrape_interval,
evaluation_interval: prometheusConfig.evaluation_interval
},
scrape_configs: [
{
job_name: 'node-exporter',
static_configs: [
{
targets: config['node-exporter'].map(item => `${item.ip}:${item.port}`)
}
]
},
{
job_name: 'nebula-stats-exporter',
static_configs: [
{
targets: [`${config['stats-exporter'].ip}:${config['stats-exporter'].nebulaPort}`]
}
]
}
]
};
const result = yaml.dump(content);
fs.writeFileSync(prometheusPath, result, 'utf8', function (err) {
if (err) return INFO(err);
});
}
function startServices(type) {
try {
const dashoardShFile = path.resolve(process.cwd(), 'scripts/dashboard.sh');
execSync(`bash ${dashoardShFile} start ${type}`, {stdio: 'inherit'})
} catch (error) {
ERROR(error)
}
}
function restartServices(type) {
try {
const dashoardShFile = path.resolve(process.cwd(), 'scripts/dashboard.sh');
execSync(`bash ${dashoardShFile} restart ${type}`, {stdio: 'inherit'})
} catch (error) {
ERROR(error)
}
}
function stopServices(type) {
try {
const dashoardShFile = path.resolve(process.cwd(), 'scripts/dashboard.sh');
execSync(`bash ${dashoardShFile} stop ${type}`, {stdio: 'inherit'})
} catch (error) {
ERROR(error)
}
}
function statusServices(type) {
try {
const dashoardShFile = path.resolve(process.cwd(), 'scripts/dashboard.sh');
execSync(`bash ${dashoardShFile} status ${type}`, {stdio: 'inherit'})
} catch (error) {
ERROR(error)
}
}
function checkAndUpdateConfig() {
if (!checkConfig(config)) {
return;
}
// update gawteway config
updateGatewayConfig();
// update stats exporter config
updateStatsExporterConfig()
// update prometheus config
updatePrometheusConfig();
}