421 lines
11 KiB
JavaScript
421 lines
11 KiB
JavaScript
const express = require('express')
|
|
const router = express.Router()
|
|
const db = require('../mongo');
|
|
const passport = require('../passport');
|
|
const fs = require('fs-extra');
|
|
const azurecli = require('../azurecli');
|
|
|
|
|
|
import { queues, TF_APPLY_QUEUE, TF_APPLY_QSEOK_QUEUE, TF_DESTROY_QUEUE } from '../queues';
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/me:
|
|
* get:
|
|
* description: Get all users (Only admin)
|
|
* summary: Get all users (Only admin)
|
|
* produces:
|
|
* - application/json
|
|
* responses:
|
|
* 200:
|
|
* description: User
|
|
*/
|
|
router.get('/', passport.ensureAuthenticatedAndAdmin, async (req, res, next) => {
|
|
try {
|
|
const result = await db.user.get();
|
|
return res.json(result);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/me:
|
|
* get:
|
|
* description: Get profile logged-in user
|
|
* summary: Get logged-in user
|
|
* produces:
|
|
* - application/json
|
|
* responses:
|
|
* 200:
|
|
* description: User
|
|
*/
|
|
router.get('/me', passport.ensureAuthenticated, async (req, res, next) => {
|
|
try {
|
|
const result = await db.user.getSingle(req.user._id);
|
|
return res.json(result);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}:
|
|
* get:
|
|
* description: Get profile for an user
|
|
* summary: Get profile for an user
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* produces:
|
|
* - application/json
|
|
* responses:
|
|
* 200:
|
|
* description: User
|
|
*/
|
|
router.get('/:userId', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
try {
|
|
const result = await db.user.getSingle(req.params.userId);
|
|
return res.json(result);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}:
|
|
* put:
|
|
* description: Update profile for an user
|
|
* summary: Update profile for an user
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* - in: body
|
|
* name: body
|
|
* description: User object
|
|
* required: true
|
|
* produces:
|
|
* - application/json
|
|
* responses:
|
|
* 200:
|
|
* description: User
|
|
*/
|
|
router.put('/:userId', passport.ensureAuthenticatedAndAdmin, async (req, res, next) => {
|
|
|
|
try {
|
|
const result = await db.user.update(req.params.userId, req.body);
|
|
return res.json(result);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}/provisions:
|
|
* post:
|
|
* description: Start a new Terraform provision
|
|
* summary: Start a new Terraform provision
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* - in: body
|
|
* name: body
|
|
* description: Provision object
|
|
* required: true
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* scenario:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: Provision
|
|
*/
|
|
router.post('/:userId/provisions', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
try {
|
|
|
|
req.body.user = req.params.userId;
|
|
|
|
const mongoJob = await db.provision.add(req.body);
|
|
const scenarioSource = await db.scenario.getScenarioByName(req.body.scenario);
|
|
|
|
if ( mongoJob.scenario === "azqmi-qseok" ){
|
|
queues[TF_APPLY_QSEOK_QUEUE].add("tf_apply_qseok_job", {
|
|
scenario: req.body.scenario,
|
|
vmType: req.body.vmType,
|
|
nodeCount: req.body.nodeCount,
|
|
id: mongoJob._id,
|
|
user: req.user,
|
|
_scenario: scenarioSource
|
|
});
|
|
} else {
|
|
queues[TF_APPLY_QUEUE].add("tf_apply_job", {
|
|
scenario: req.body.scenario,
|
|
vmType: req.body.vmType,
|
|
nodeCount: req.body.nodeCount,
|
|
id: mongoJob._id,
|
|
user: req.user,
|
|
_scenario: scenarioSource
|
|
});
|
|
}
|
|
|
|
return res.status(200).json(mongoJob);
|
|
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}/provisions/{id}:
|
|
* delete:
|
|
* description: Delete Provision by ID
|
|
* summary: Delete a Terraform Provision by ID
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* - name: id
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* responses:
|
|
* 200:
|
|
* description: Provision
|
|
* 404:
|
|
* description: Not found
|
|
*
|
|
*/
|
|
router.delete('/:userId/provisions/:id', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
try {
|
|
const mongoJob = await db.provision.getSingle(req.params.id);
|
|
if (!mongoJob){
|
|
return res.status(404).json({"msg": "Not found privision with id "+req.params.id});
|
|
}
|
|
const delDest = await db.destroy.del(mongoJob.destroy._id);
|
|
const delProv = await db.provision.update(req.params.id, {"isDeleted": true});
|
|
|
|
//Move folder
|
|
if (fs.existsSync(`/provisions/${mongoJob.scenario}_${req.params.id}`)) {
|
|
fs.moveSync(`/provisions/${mongoJob.scenario}_${req.params.id}`, `/provisions/deleted/${mongoJob.scenario}_${req.params.id}`, { overwrite: true })
|
|
}
|
|
return res.json({"provision": delProv, "destroy": delDest});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}/provisions/{id}/deallocatevms:
|
|
* post:
|
|
* description: Stop all VMs for this provision
|
|
* summary: Stop all VMs for this provision
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* - name: id
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* responses:
|
|
* 200:
|
|
* description: Provision
|
|
* 404:
|
|
* description: Not found
|
|
*
|
|
*/
|
|
router.post('/:userId/provisions/:id/deallocatevms', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
try {
|
|
let mongoJob = await db.provision.getSingle(req.params.id);
|
|
if (!mongoJob){
|
|
return res.status(404).json({"msg": "Not found privision with id "+req.params.id});
|
|
}
|
|
|
|
let rgName = mongoJob.scenario.toUpperCase();
|
|
rgName = rgName.replace(/AZQMI/g, 'QMI');
|
|
rgName = rgName + "-" + mongoJob._id.toString();
|
|
|
|
db.provision.update(req.params.id, {"statusVms": "Stopping"});
|
|
azurecli.deallocate(rgName, function(err, res){
|
|
if (err) {
|
|
db.provision.update(req.params.id, {"statusVms": "Error_Stopping"});
|
|
} else {
|
|
db.provision.update(req.params.id, {"statusVms": "Stopped"});
|
|
}
|
|
});
|
|
|
|
return res.json({"statusVms": "Stopping"});
|
|
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}/provisions/{id}/startvms:
|
|
* post:
|
|
* description: Start all VMs for this provision
|
|
* summary: Start all VMs for this provision
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* - name: id
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* responses:
|
|
* 200:
|
|
* description: Provision
|
|
* 404:
|
|
* description: Not found
|
|
*
|
|
*/
|
|
router.post('/:userId/provisions/:id/startvms', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
try {
|
|
let mongoJob = await db.provision.getSingle(req.params.id);
|
|
if (!mongoJob){
|
|
return res.status(404).json({"msg": "Not found privision with id "+req.params.id});
|
|
}
|
|
let rgName = mongoJob.scenario.toUpperCase();
|
|
rgName = rgName.replace(/AZQMI/g, 'QMI');
|
|
rgName = rgName + "-" + mongoJob._id.toString();
|
|
|
|
db.provision.update(req.params.id, {"statusVms": "Starting"});
|
|
azurecli.start(rgName, function(err, res){
|
|
if (err) {
|
|
db.provision.update(req.params.id, {"statusVms": "Error_Starting"});
|
|
} else {
|
|
db.provision.update(req.params.id, {"statusVms": "Running"});
|
|
}
|
|
});
|
|
|
|
return res.json({"statusVms": "Starting"});
|
|
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}/destroyprovisions:
|
|
* post:
|
|
* description: Destroy a Terraform Provision
|
|
* summary: Destroy a Terraform Provision
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* - in: body
|
|
* name: body
|
|
* description: Provision object
|
|
* required: true
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* id:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: Provision
|
|
*/
|
|
router.post('/:userId/destroyprovisions', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
try {
|
|
|
|
req.body.userId = req.params.userId;
|
|
const destroyJob = await db.destroy.add({ "user": req.body.userId });
|
|
const mongoJob = await db.provision.update(req.body.id, {"destroy": destroyJob._id});
|
|
const scenarioSource = await db.scenario.getScenarioByName(mongoJob.scenario);
|
|
|
|
queues[TF_DESTROY_QUEUE].add("tf_destroy_job", {
|
|
scenario: mongoJob.scenario,
|
|
provId: mongoJob._id,
|
|
user: req.user,
|
|
id: destroyJob._id,
|
|
_scenario: scenarioSource
|
|
});
|
|
|
|
return res.status(200).json(mongoJob);
|
|
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}/provisions:
|
|
* get:
|
|
* description: Get all Provisions for an User
|
|
* summary: Get all Provisions for an User
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* responses:
|
|
* 200:
|
|
* description: JSON Array
|
|
*/
|
|
router.get('/:userId/provisions', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
|
|
try {
|
|
const result = await db.provision.get({"user": req.params.userId, "isDeleted": false});
|
|
return res.json(result);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}/destroyprovisions:
|
|
* get:
|
|
* description: Get all Destroy Provisions for an User
|
|
* summary: Get all Destroy Provisions for an User
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* responses:
|
|
* 200:
|
|
* description: JSON Array
|
|
*/
|
|
router.get('/:userId/destroyprovisions', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
|
|
try {
|
|
const result = await db.destroy.get({"user": req.params.userId});
|
|
return res.json(result);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
|
|
module.exports = router; |