70 lines
2.3 KiB
JavaScript
70 lines
2.3 KiB
JavaScript
const endpoint = 'http://127.0.0.1:15001/gateway/api/trpc';
|
|
|
|
const post = async (procedure, input, sessionToken) => {
|
|
const response = await fetch(`${endpoint}/${procedure}`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'content-type': 'application/json',
|
|
...(sessionToken ? { 'x-session-token': sessionToken } : {}),
|
|
},
|
|
body: JSON.stringify(input),
|
|
});
|
|
const body = await response.json();
|
|
if (!response.ok) {
|
|
const code = body?.error?.data?.code;
|
|
const message = body?.error?.message ?? JSON.stringify(body);
|
|
const error = new Error(`${procedure} failed (${response.status}, ${code ?? 'unknown'}): ${message}`);
|
|
error.code = code;
|
|
throw error;
|
|
}
|
|
return body?.result?.data;
|
|
};
|
|
|
|
const token = process.env.GATEWAY_BOOTSTRAP_TOKEN;
|
|
const username = process.env.INITIAL_ADMIN_USERNAME;
|
|
const password = process.env.INITIAL_ADMIN_PASSWORD;
|
|
if (!token || !username || !password) {
|
|
console.log('Initial admin bootstrap is disabled because its environment values are incomplete.');
|
|
process.exit(0);
|
|
}
|
|
|
|
let sessionToken;
|
|
try {
|
|
const result = await post('auth.bootstrapLocal', {
|
|
token,
|
|
username,
|
|
password,
|
|
displayName: process.env.INITIAL_ADMIN_DISPLAY_NAME || 'Administrator',
|
|
});
|
|
sessionToken = result?.sessionToken;
|
|
if (!sessionToken) throw new Error('bootstrap response did not include a session token');
|
|
console.log('Created the initial superuser without printing credentials.');
|
|
} catch (error) {
|
|
if (error?.code === 'CONFLICT') {
|
|
console.log('Initial superuser already exists; bootstrap was left unchanged.');
|
|
process.exit(0);
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
const profilePorts = new Map([
|
|
['gateway', 15001],
|
|
['che', 15003],
|
|
['kwe', 15005],
|
|
['pwe', 15007],
|
|
['twe', 15009],
|
|
['nya', 15011],
|
|
['pya', 15013],
|
|
['hwe', 15015],
|
|
]);
|
|
const profiles = (process.env.BOOTSTRAP_PROFILES || 'gateway,che,kwe,pwe,twe,nya,pya,hwe')
|
|
.split(',')
|
|
.map((value) => value.trim())
|
|
.filter((value) => value && value !== 'gateway');
|
|
for (const profile of profiles) {
|
|
const apiPort = profilePorts.get(profile);
|
|
if (!apiPort) throw new Error(`No reserved API port is defined for profile: ${profile}`);
|
|
await post('admin.profiles.upsert', { profile, scenario: 'default', apiPort, status: 'STOPPED' }, sessionToken);
|
|
}
|
|
console.log(`Registered ${profiles.length} stopped profiles for branch-selectable Admin deployments.`);
|