107 lines
4.2 KiB
JavaScript
107 lines
4.2 KiB
JavaScript
const fs = require('fs');
|
|
const { getAuth } = require('./auth_manager');
|
|
|
|
async function main() {
|
|
const tokenData = await getAuth();
|
|
const token = tokenData.token;
|
|
|
|
console.log('=== Step 1: Generating Static Nginx Config ===');
|
|
const configRes = await fetch('https://graphql.ghaymah.systems/v1/graphql', {
|
|
method: 'POST',
|
|
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
query: `mutation GenerateStaticConfig($framework: String!) {
|
|
generateStaticConfig(arg1: { framework: $framework }) {
|
|
nginxConfig
|
|
appConfig
|
|
}
|
|
}`,
|
|
variables: { framework: 'static' }
|
|
})
|
|
});
|
|
const configData = await configRes.json();
|
|
if (configData.errors) {
|
|
throw new Error('Config error: ' + JSON.stringify(configData.errors));
|
|
}
|
|
const { nginxConfig, appConfig } = configData.data.generateStaticConfig;
|
|
console.log('Got nginxConfig and appConfig');
|
|
|
|
// Deployment ID: 24 hex characters
|
|
const deploymentId = Array.from(crypto.getRandomValues(new Uint8Array(12)), b => b.toString(16).padStart(2, '0')).join('');
|
|
console.log('Generated Deployment ID:', deploymentId);
|
|
|
|
console.log('=== Step 2: Requesting S3 Upload URLs ===');
|
|
async function getUploadUrl(fileName, contentType) {
|
|
const res = await fetch('https://graphql.ghaymah.systems/v1/graphql', {
|
|
method: 'POST',
|
|
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
query: `mutation CreateStaticUploadURL($deploymentId: String!, $fileName: String!, $contentType: String!) {
|
|
createStaticUploadURL(arg1: { deploymentId: $deploymentId, fileName: $fileName, contentType: $contentType }) {
|
|
url objectKey bucket endpoint expiresIn
|
|
}
|
|
}`,
|
|
variables: { deploymentId, fileName, contentType }
|
|
})
|
|
});
|
|
const d = await res.json();
|
|
if (d.errors) throw new Error(`UploadURL error for ${fileName}: ` + JSON.stringify(d.errors));
|
|
return d.data.createStaticUploadURL;
|
|
}
|
|
|
|
const [urlZip, urlNginx, urlApp] = await Promise.all([
|
|
getUploadUrl('app.zip', 'application/zip'),
|
|
getUploadUrl('nginx.conf', 'text/plain'),
|
|
getUploadUrl('app.conf', 'text/plain')
|
|
]);
|
|
console.log('Got all 3 upload URLs');
|
|
|
|
console.log('=== Step 3: Uploading Files to S3 ===');
|
|
const zipBuffer = fs.readFileSync('D:/cumin/ghaymah_dns_net_sec_tests/soc_gateway_ui/app.zip');
|
|
|
|
const [upZip, upNginx, upApp] = await Promise.all([
|
|
fetch(urlZip.url, { method: 'PUT', headers: { 'Content-Type': 'application/zip' }, body: zipBuffer }),
|
|
fetch(urlNginx.url, { method: 'PUT', headers: { 'Content-Type': 'text/plain' }, body: nginxConfig }),
|
|
fetch(urlApp.url, { method: 'PUT', headers: { 'Content-Type': 'text/plain' }, body: appConfig })
|
|
]);
|
|
console.log('Upload status: app.zip =', upZip.status, '| nginx.conf =', upNginx.status, '| app.conf =', upApp.status);
|
|
|
|
console.log('=== Step 4: Creating Static App: ghaymah-soc-gateway ===');
|
|
const createRes = await fetch('https://graphql.ghaymah.systems/v1/graphql', {
|
|
method: 'POST',
|
|
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
query: `mutation CreateStaticApp($input: StaticAppInput!) {
|
|
createStaticApp(arg1: $input) {
|
|
id name image status url ports { name number hostname }
|
|
}
|
|
}`,
|
|
variables: {
|
|
input: {
|
|
name: 'ghaymah-soc-gateway',
|
|
deploymentId: deploymentId
|
|
}
|
|
}
|
|
})
|
|
});
|
|
|
|
const createData = await createRes.json();
|
|
console.log('createStaticApp response:', JSON.stringify(createData, null, 2));
|
|
|
|
const app = createData.data?.createStaticApp;
|
|
const publicUrl = app?.url || (app?.ports && app.ports[0]?.hostname ? `https://${app.ports[0].hostname}` : null);
|
|
console.log('\n>>> STATIC SOC GATEWAY DEPLOYED! <<<');
|
|
console.log('Public URL:', publicUrl);
|
|
|
|
const deploymentMeta = {
|
|
deploymentId,
|
|
appId: app?.id,
|
|
name: 'ghaymah-soc-gateway',
|
|
publicUrl,
|
|
deployedAt: new Date().toISOString()
|
|
};
|
|
fs.writeFileSync('D:/cumin/ghaymah_dns_net_sec_tests/soc_static_gateway.json', JSON.stringify(deploymentMeta, null, 2));
|
|
}
|
|
|
|
main().catch(console.error);
|