update to es6 and fix some syntax errors
This commit is contained in:
parent
631237d857
commit
2deda42ec5
20
db.js
20
db.js
@ -1,9 +1,9 @@
|
||||
const fs = require("fs");
|
||||
import { readFileSync, writeFileSync } from "fs";
|
||||
|
||||
template = "localdb.json.template"
|
||||
filename = "localdb.json";
|
||||
let template = "localdb.json.template"
|
||||
let filename = "localdb.json";
|
||||
|
||||
let db = JSON.parse(fs.readFileSync(template));
|
||||
let db = JSON.parse(readFileSync(template));
|
||||
try {
|
||||
load();
|
||||
}
|
||||
@ -12,19 +12,17 @@ catch {
|
||||
}
|
||||
|
||||
function load () {
|
||||
db = JSON.parse(fs.readFileSync(filename));
|
||||
db = JSON.parse(readFileSync(filename));
|
||||
}
|
||||
|
||||
function save () {
|
||||
fs.writeFileSync(filename, JSON.stringify(db));
|
||||
writeFileSync(filename, JSON.stringify(db));
|
||||
}
|
||||
|
||||
function getResourceConfig() {
|
||||
export function getResourceConfig() {
|
||||
return db.resources;
|
||||
}
|
||||
|
||||
function getUserConfig (username) {
|
||||
export function getUserConfig (username) {
|
||||
return db.users[username];
|
||||
}
|
||||
|
||||
module.exports = {getUserConfig, getResourceConfig};
|
||||
}
|
29
main.js
29
main.js
@ -1,14 +1,15 @@
|
||||
const express = require("express");
|
||||
const bodyParser = require("body-parser");
|
||||
const cookieParser = require("cookie-parser")
|
||||
const cors = require("cors");
|
||||
const helmet = require("helmet");
|
||||
const morgan = require("morgan");
|
||||
var api = require("./package.json");
|
||||
import express from "express";
|
||||
import bodyParser from "body-parser";
|
||||
import cookieParser from "cookie-parser";
|
||||
import cors from "cors";
|
||||
import helmet from "helmet";
|
||||
import morgan from "morgan";
|
||||
//import { version as _version } from "./package.json";
|
||||
let api = {version: "0.0.1"};
|
||||
|
||||
const {pveAPIToken, listenPort, domain} = require("./vars.js");
|
||||
const {checkAuth, requestPVE, handleResponse, getDiskInfo} = require("./pve.js");
|
||||
const {getUserData, approveResources} = require("./utils.js")
|
||||
import { pveAPIToken, listenPort, domain } from "./vars.js";
|
||||
import { checkAuth, requestPVE, handleResponse, getDiskInfo } from "./pve.js";
|
||||
import { getUserData, approveResources } from "./utils.js";
|
||||
|
||||
const app = express();
|
||||
app.use(helmet());
|
||||
@ -19,7 +20,7 @@ app.use(morgan("combined"));
|
||||
|
||||
|
||||
app.get("/api/version", (req, res) => {
|
||||
res.status(200).send({version: api.version});
|
||||
res.status(200).send({version: _version});
|
||||
});
|
||||
|
||||
app.get("/api/echo", (req, res) => {
|
||||
@ -32,13 +33,13 @@ app.get("/api/auth", async (req, res) => {
|
||||
});
|
||||
|
||||
app.get("/api/proxmox/*", async (req, res) => { // proxy endpoint for GET proxmox api with no token
|
||||
path = req.url.replace("/api/proxmox", "");
|
||||
let path = req.url.replace("/api/proxmox", "");
|
||||
let result = await requestPVE(path, "GET", req.cookies);
|
||||
res.status(result.status).send(result.data);
|
||||
});
|
||||
|
||||
app.post("/api/proxmox/*", async (req, res) => { // proxy endpoint for POST proxmox api with no token
|
||||
path = req.url.replace("/api/proxmox", "");
|
||||
let path = req.url.replace("/api/proxmox", "");
|
||||
let result = await requestPVE(path, "POST", req.cookies, JSON.stringify(req.body)); // need to stringify body because of other issues
|
||||
res.status(result.status).send(result.data);
|
||||
});
|
||||
@ -235,7 +236,7 @@ app.post("/api/instance", async (req, res) => {
|
||||
memory: req.body.memory,
|
||||
pool: user.instances.pool
|
||||
};
|
||||
for (key of Object.keys(user.instances.templates[req.body.type])) {
|
||||
for (let key of Object.keys(user.instances.templates[req.body.type])) {
|
||||
action[key] = user.instances.templates[req.body.type][key];
|
||||
}
|
||||
if (req.body.type === "lxc") {
|
||||
|
@ -3,6 +3,7 @@
|
||||
"version": "0.0.1",
|
||||
"description": "REST API for ProxmoxAAS",
|
||||
"main": "main.js",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"axios": "^1.3.2",
|
||||
"body-parser": "^1.20.1",
|
||||
|
22
pve.js
22
pve.js
@ -1,7 +1,7 @@
|
||||
const axios = require('axios');
|
||||
const {pveAPI, pveAPIToken} = require("./vars.js");
|
||||
import axios from 'axios';
|
||||
import { pveAPI, pveAPIToken } from "./vars.js";
|
||||
|
||||
async function checkAuth (cookies, res, vmpath = null) {
|
||||
export async function checkAuth (cookies, res, vmpath = null) {
|
||||
let auth = false;
|
||||
if (vmpath) {
|
||||
let result = await requestPVE(`/${vmpath}/config`, "GET", cookies);
|
||||
@ -18,7 +18,7 @@ async function checkAuth (cookies, res, vmpath = null) {
|
||||
}
|
||||
}
|
||||
|
||||
async function requestPVE (path, method, cookies, body = null, token = null) {
|
||||
export async function requestPVE (path, method, cookies, body = null, token = null) {
|
||||
let url = `${pveAPI}${path}`;
|
||||
let content = {
|
||||
method: method,
|
||||
@ -50,7 +50,7 @@ async function requestPVE (path, method, cookies, body = null, token = null) {
|
||||
}
|
||||
}
|
||||
|
||||
async function handleResponse (node, result, res) {
|
||||
export async function handleResponse (node, result, res) {
|
||||
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
|
||||
if (result.data.data) {
|
||||
let upid = result.data.data;
|
||||
@ -78,7 +78,7 @@ async function handleResponse (node, result, res) {
|
||||
}
|
||||
}
|
||||
|
||||
async function getUsedResources (req, resourceMeta) {
|
||||
export async function getUsedResources (req, resourceMeta) {
|
||||
let response = await requestPVE("/cluster/resources", "GET", req.cookies);
|
||||
let used = {};
|
||||
let diskprefixes = [];
|
||||
@ -90,11 +90,11 @@ async function getUsedResources (req, resourceMeta) {
|
||||
}
|
||||
}
|
||||
}
|
||||
for (instance of response.data.data) {
|
||||
for (let instance of response.data.data) {
|
||||
if (instance.type === "lxc" || instance.type === "qemu") {
|
||||
let config = await requestPVE(`/nodes/${instance.node}/${instance.type}/${instance.vmid}/config`, "GET", req.cookies);
|
||||
config = config.data.data;
|
||||
for (key of Object.keys(config)) {
|
||||
for (let key of Object.keys(config)) {
|
||||
if (Object.keys(used).includes(key) && resourceMeta[key].type === "numeric") {
|
||||
used[key] += config[key];
|
||||
}
|
||||
@ -108,13 +108,11 @@ async function getUsedResources (req, resourceMeta) {
|
||||
return used;
|
||||
}
|
||||
|
||||
async function getDiskInfo (node, type, vmid, disk) {
|
||||
export async function getDiskInfo (node, type, vmid, disk) {
|
||||
let config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken);
|
||||
let storageID = config.data.data[disk].split(":")[0];
|
||||
let volID = config.data.data[disk].split(",")[0];
|
||||
let volInfo = await requestPVE(`/nodes/${node}/storage/${storageID}/content/${volID}`, "GET", null, null, pveAPIToken);
|
||||
volInfo.data.data.storage = storageID;
|
||||
return volInfo.data.data;
|
||||
}
|
||||
|
||||
module.exports = {checkAuth, requestPVE, handleResponse, getUsedResources, getDiskInfo};
|
||||
}
|
14
utils.js
14
utils.js
@ -1,7 +1,7 @@
|
||||
const {getUsedResources} = require("./pve.js");
|
||||
const {getUserConfig, getResourceConfig} = require("./db.js");
|
||||
import { getUsedResources } from "./pve.js";
|
||||
import { getUserConfig, getResourceConfig } from "./db.js";
|
||||
|
||||
async function getUserData (req, username) {
|
||||
export async function getUserData (req, username) {
|
||||
let resources = await getAllocatedResources(req, username);
|
||||
let instances = getUserConfig(req.cookies.username).instances;
|
||||
return {resources: resources, instances: instances};
|
||||
@ -11,14 +11,14 @@ async function getAllocatedResources (req, username) {
|
||||
let dbResources = getResourceConfig();
|
||||
let used = await getUsedResources(req, dbResources);
|
||||
let max = getUserConfig(username).resources.max;
|
||||
avail = {};
|
||||
let avail = {};
|
||||
Object.keys(max).forEach((k) => {
|
||||
avail[k] = max[k] - used[k];
|
||||
});
|
||||
return {used: used, max: max, avail: avail, units: dbResources};
|
||||
}
|
||||
|
||||
async function approveResources (req, username, request) {
|
||||
export async function approveResources (req, username, request) {
|
||||
|
||||
let avail = (await getAllocatedResources(req, username)).avail;
|
||||
|
||||
@ -32,6 +32,4 @@ async function approveResources (req, username, request) {
|
||||
}
|
||||
});
|
||||
return approved;
|
||||
}
|
||||
|
||||
module.exports = {getUserData, approveResources}
|
||||
}
|
Loading…
Reference in New Issue
Block a user