2022-06-07 02:29:45 -04:00
|
|
|
|
const exec = require('await-exec')
|
|
|
|
|
const express = require('express')
|
|
|
|
|
const app = express()
|
|
|
|
|
const port = 2344
|
|
|
|
|
|
2022-06-14 01:50:50 -04:00
|
|
|
|
require('dotenv').config();
|
|
|
|
|
if(!process.env.authToken){
|
|
|
|
|
console.error("No Auth token set!! – fill out .env-example and rename to .env!");
|
|
|
|
|
}
|
2022-06-07 02:29:45 -04:00
|
|
|
|
|
|
|
|
|
app.get('/cscclassprofilestaging', async (req, res) => {
|
2022-06-14 01:50:50 -04:00
|
|
|
|
console.log("Recieved build request", req);
|
|
|
|
|
|
2022-06-09 00:30:52 -04:00
|
|
|
|
const buildId = req.query.ref;
|
2022-06-14 01:50:50 -04:00
|
|
|
|
const name = `${buildId}-csc-class-profile-staging`
|
2022-06-09 00:30:52 -04:00
|
|
|
|
const publicUrl = `${name}-snedadah.k8s.csclub.cloud`
|
|
|
|
|
const image = `registry.cloud.csclub.uwaterloo.ca/snedadah/csc-class-profile-staging:${buildId}`
|
|
|
|
|
|
2022-06-14 01:50:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!req.headers.authorization) {
|
|
|
|
|
console.log("No auth!");
|
|
|
|
|
return res.status(403).json({ error: 'No credentials sent!' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(req.headers.authorization != process.env.authToken){
|
|
|
|
|
console.log("Invalid auth!");
|
|
|
|
|
return res.status(401).json({error: 'Invalid Auth Token!'});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log("Doing deploy for for branch: " + req.query.ref);
|
2022-06-07 02:29:45 -04:00
|
|
|
|
|
2022-06-09 00:30:52 -04:00
|
|
|
|
await call(`kubectl delete deployment ${name}`);
|
2022-06-07 02:29:45 -04:00
|
|
|
|
await call(`kubectl create deployment ${name} --image ${image} --port=80`);
|
|
|
|
|
await call(`kubectl expose deployment ${name}`);
|
2022-06-09 00:30:52 -04:00
|
|
|
|
await call(`kubectl create ingress ${name} --rule='${publicUrl}/*=${name}:80'`);
|
2022-06-14 01:50:50 -04:00
|
|
|
|
console.log("Deployed to " + publicUrl);
|
2022-06-07 02:29:45 -04:00
|
|
|
|
|
2022-06-09 00:30:52 -04:00
|
|
|
|
res.send(publicUrl);
|
2022-06-07 02:29:45 -04:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
async function call(cmd){
|
|
|
|
|
let out
|
|
|
|
|
try{
|
|
|
|
|
out = await exec(cmd);
|
|
|
|
|
}catch(e)
|
|
|
|
|
{
|
|
|
|
|
console.log(e, out)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
app.listen(port, () => {
|
2022-06-09 00:30:52 -04:00
|
|
|
|
console.log(`Listening on port ${port}`)
|
2022-06-07 02:29:45 -04:00
|
|
|
|
})
|