forked from hawtio/hawtio-online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
207 lines (186 loc) · 6.53 KB
/
gulpfile.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
const gulp = require('gulp'),
Hub = require('gulp-hub'),
del = require('del'),
fs = require('fs'),
path = require('path'),
argv = require('yargs').argv,
uri = require('urijs'),
logger = require('js-logger'),
mime = require('mime-types'),
hawtio = require('@hawtio/node-backend');
const config = {
master : argv.master,
mode : argv.mode || 'namespace',
namespace : argv.namespace || 'hawtio',
};
function getMaster() {
const master = config.master || process.env.OPENSHIFT_MASTER;
if (!master) {
console.error('The --master option or the OPENSHIFT_MASTER environment variable must be set!');
process.exit(1);
}
return master;
}
function osconsole(_, res, _) {
const master = getMaster();
let answer;
if (config.mode === 'namespace') {
answer =
`window.OPENSHIFT_CONFIG = {
master_uri : new URI().query('').path('/master').toString(),
hawtio : {
mode : '${config.mode}',
namespace : '${config.namespace}',
},
openshift : {
oauth_metadata_uri : new URI().query('').path('/master/.well-known/oauth-authorization-server').toString(),
oauth_client_id : 'system:serviceaccount:${config.namespace}:hawtio-online-dev',
scope : 'user:info user:check-access role:edit:${config.namespace}',
},
}`;
} else if (config.mode === 'cluster') {
answer =
`window.OPENSHIFT_CONFIG = {
master_uri : new URI().query('').path('/master').toString(),
hawtio : {
mode : '${config.mode}',
},
openshift : {
oauth_metadata_uri : new URI().query('').path('/master/.well-known/oauth-authorization-server').toString(),
oauth_client_id : 'hawtio-online-dev',
scope : 'user:info user:check-access user:list-projects role:edit:*',
},
}`;
} else {
console.error('Invalid value for the Hawtio Online mode, must be one of [cluster, namespace]');
process.exit(1);
}
res.set('Content-Type', 'application/javascript');
res.send(answer);
}
function backend(root, liveReload) {
// Lets disable unauthorised TLS for self-signed development certificates
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const master = getMaster();
console.log('Using OpenShift URL:', master);
const master_api = uri.parse(master);
hawtio.setConfig({
logLevel : logger.INFO,
port : 2772,
staticAssets : [
{
path : '/online',
dir : path.join(root, 'online'),
},
{
path : '/integration',
dir : path.join(root, 'integration'),
}
],
staticProxies: [
{
port : master_api.port,
proto : master_api.protocol,
path : '/master',
hostname : master_api.hostname,
targetPath : '/',
}
],
fallback : {
'^/online.*' : 'packages/online/index.html',
'^/integration.*' : 'packages/integration/index.html',
},
liveReload : {
enabled : liveReload,
}
});
hawtio.use('/online/osconsole/config.js', osconsole);
hawtio.use('/integration/osconsole/config.js', osconsole);
hawtio.use('/management', function (req, res, next) {
const url = /\/management\/namespaces\/(.+)\/pods\/(http|https):([^/]+)\/(.+)/;
const match = req.originalUrl.match(url);
const redirectPath = `/master/api/v1/namespaces/${match[1]}/pods/${match[2]}:${match[3]}/proxy/${match[4]}`;
console.log('redirect:', redirectPath);
if (match) {
// 307 - post redirect
res.redirect(307, redirectPath);
} else {
next();
}
});
hawtio.use('/', function (req, res, next) {
if (req.originalUrl === '/') {
res.redirect('/online');
} else {
next();
}
});
}
const hub = new Hub([
'./packages/common/gulpfile.js',
'./packages/online/gulpfile.js',
'./packages/integration/gulpfile.js',
]);
gulp.registry(hub);
// Helpers
const task = (name, fn) => {
fn.displayName = name;
return fn;
};
const chdir = dir => done => {
process.chdir(path.join(__dirname, dir));
done();
};
// Workspace tasks
gulp.task('build', gulp.series('common::build', gulp.parallel('online::build', 'integration::build')));
// TODO: parallel site build
gulp.task('site', gulp.series(
task('clean site dir', () => del('docker/site/')),
task('Set cwd to online dir', chdir('packages/online')),
'online::site',
task('Set cwd to root dir', chdir('.')),
task('Copy online site', () => gulp.src('packages/online/site/**/*')
.pipe(gulp.dest('docker/site/online'))),
task('Set cwd to integration dir', chdir('packages/integration')),
'integration::site',
task('Set cwd to root dir', chdir('.')),
task('Copy integration site', () => gulp.src('packages/integration/site/**/*')
.pipe(gulp.dest('docker/site/integration')))
));
gulp.task('serve-site', () => {
backend('docker/site', false);
return hawtio.listen(server => console.log(`Hawtio console started at http://localhost:${server.address().port}`));
});
// Override the reload tasks
hub._registry[path.join(__dirname, 'packages/common/gulpfile.js')]
.set('common::reload',
task('Reload common package dependencies', () => gulp.src(['packages/online', 'packages/integration']).pipe(hawtio.reload())));
hub._registry[path.join(__dirname, 'packages/online/gulpfile.js')]
.set('online::reload',
task('Reload online package', () => gulp.src('packages/online').pipe(hawtio.reload())));
hub._registry[path.join(__dirname, 'packages/integration/gulpfile.js')]
.set('integration::reload',
task('Reload integration package', () => gulp.src('packages/integration').pipe(hawtio.reload())));
gulp.task('default', gulp.parallel(
'common::watch',
'online::watch',
'integration::watch',
task('Start Hawtio backend', function () {
backend('packages', true);
// Serve images from @hawtio/integration
hawtio.use('/integration/img', (req, res) => {
const file = path.join(__dirname, 'packages/integration/node_modules/@hawtio/integration/dist/img', req.url);
if (fs.existsSync(file)) {
res.writeHead(200, {
'Content-Type' : mime.contentType(path.extname(file)),
'Content-Disposition': `attachment; filename=${file}`,
});
fs.createReadStream(file).pipe(res);
} else {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end(`File ${file} does not exist in dependencies`);
}
});
return hawtio.listen(server => console.log(`Hawtio console started at http://localhost:${server.address().port}`));
})
));