92 lines
3.2 KiB
JavaScript
92 lines
3.2 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const db = require('qmi-cloud-common/mongo');
|
|
const passport = require('../passport');
|
|
|
|
/**
|
|
* @swagger
|
|
* /divvy/updates:
|
|
* post:
|
|
* description: DivvyCloud updates webhook
|
|
* tags:
|
|
* - admin
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* produces:
|
|
* - application/json
|
|
* responses:
|
|
* 200:
|
|
* description: Notifications
|
|
*/
|
|
router.post('/updates', passport.ensureAuthenticatedAndAdmin, async (req, res, next) => {
|
|
const now = new Date().toISOString();
|
|
try {
|
|
|
|
let event = req.body;
|
|
|
|
|
|
if ( event.cloudName === 'QMI Automation' ) {
|
|
|
|
console.log(`DivvyCloud (${now})# event received for subscription (${event.cloudName}) - provision (${event.provID}) -> new status (${event.instanceState})`);
|
|
|
|
if ( event.provID && event.provID !== 'None' ) {
|
|
|
|
let provision = await db.provision.getById(event.provID);
|
|
|
|
if ( provision ) {
|
|
|
|
let id = provision._id.toString();
|
|
|
|
console.log(`DivvyCloud (${now})# provision (${id}) - scenario is (${provision.scenario} - v${provision.scenarioVersion})`);
|
|
|
|
if ( provision.status === 'provisioned' ) {
|
|
|
|
if ( event.instanceState === 'Stopped' ) {
|
|
|
|
if ( provision.statusVms === 'Stopped' ) {
|
|
console.log(`DivvyCloud (${now})# provision (${id}) - VMs were already Stopped!`);
|
|
} else {
|
|
let timeRunning = db.utils.getNewTimeRunning(provision);
|
|
await db.provision.update(id, {"statusVms": "Stopped", "timeRunning": timeRunning, "stoppedFrom": new Date(), "pendingNextAction": undefined});
|
|
console.log(`DivvyCloud (${now})# provision (${id}) - VMs changed to Stopped!`);
|
|
}
|
|
|
|
} else if ( event.instanceState === 'Running' ) {
|
|
|
|
if ( provision.statusVms === 'Running' ) {
|
|
console.log(`DivvyCloud (${now})# provision (${id}) - VMs were already Running!`);
|
|
} else {
|
|
await db.provision.update(id, {"statusVms": "Running", "runningFrom": new Date(), "pendingNextAction": undefined});
|
|
console.log(`DivvyCloud (${now})# provision (${id}) - VMs changed to Running!`);
|
|
}
|
|
}
|
|
|
|
} else {
|
|
console.log(`DivvyCloud (${now})# provision (${event.provID}) - Scenario not yet 'provisioned'`);
|
|
}
|
|
|
|
} else {
|
|
console.log(`DivvyCloud (${now})# provision (${event.provID}) - Provision not found.`);
|
|
}
|
|
|
|
} else {
|
|
console.log(`DivvyCloud (${now})# 'provID' attribute is missing.`);
|
|
}
|
|
|
|
} else {
|
|
//console.log(`DivvyCloud (${now}): event received for subscription (${event.cloudName}) --> won't be processed`);
|
|
}
|
|
|
|
return res.json(req.body);
|
|
|
|
} catch (error) {
|
|
console.log(`DivvyCloud (${now})# error!!!!`, error);
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
module.exports = router; |