fix db module imports

This commit is contained in:
Arthur Lu 2023-08-03 19:51:09 +00:00
parent 698f53314b
commit 7b80445cf0
4 changed files with 12 additions and 8 deletions

View File

@ -7,7 +7,7 @@ import morgan from "morgan";
import api from "./package.js"; import api from "./package.js";
import * as pve from "./pve.js"; import * as pve from "./pve.js";
import * as utils from "./utils.js"; import * as utils from "./utils.js";
import db from "./db.js"; import LocalDB from "./db.js";
import parseArgs from "minimist"; import parseArgs from "minimist";
global.argv = parseArgs(process.argv.slice(2), { global.argv = parseArgs(process.argv.slice(2), {
@ -19,16 +19,16 @@ global.argv = parseArgs(process.argv.slice(2), {
global.api = api; global.api = api;
global.pve = pve; global.pve = pve;
global.utils = utils; global.utils = utils;
global.db = new db(global.argv.localdb); global.db = new LocalDB(global.argv.localdb);
const app = express(); const app = express();
global.app = app; global.app = app;
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser()); app.use(cookieParser());
app.use(cors({ origin: db.hostname })); app.use(cors({ origin: global.db.hostname }));
app.use(morgan("combined")); app.use(morgan("combined"));
global.server = app.listen(db.listenPort, () => { global.server = app.listen(global.db.listenPort, () => {
console.log(`proxmoxaas-api v${api.version} listening on port ${global.db.listenPort}`); console.log(`proxmoxaas-api v${api.version} listening on port ${global.db.listenPort}`);
}); });

View File

@ -1,7 +1,4 @@
import axios from "axios"; import axios from "axios";
import db from "./db.js";
const pveAPI = db.pveAPI;
const pveAPIToken = db.pveAPIToken;
/** /**
* Send HTTP request to proxmox API. Allows requests to be made with user cookie credentials or an API token for controlled priviledge elevation. * Send HTTP request to proxmox API. Allows requests to be made with user cookie credentials or an API token for controlled priviledge elevation.
@ -13,6 +10,7 @@ const pveAPIToken = db.pveAPIToken;
* @returns {Obejct} HTTP response object or HTTP error object * @returns {Obejct} HTTP response object or HTTP error object
*/ */
export async function requestPVE (path, method, cookies, body = null, token = null) { export async function requestPVE (path, method, cookies, body = null, token = null) {
const pveAPI = global.db.pveAPI;
const url = `${pveAPI}${path}`; const url = `${pveAPI}${path}`;
const content = { const content = {
method, method,
@ -53,6 +51,7 @@ export async function requestPVE (path, method, cookies, body = null, token = nu
* @param {Object} res response object of ProxmoxAAS API call. * @param {Object} res response object of ProxmoxAAS API call.
*/ */
export async function handleResponse (node, result, res) { export async function handleResponse (node, result, res) {
const pveAPIToken = global.db.pveAPIToken;
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay)); const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
if (result.data.data && typeof (result.data.data) === "string" && result.data.data.startsWith("UPID:")) { if (result.data.data && typeof (result.data.data) === "string" && result.data.data.startsWith("UPID:")) {
const upid = result.data.data; const upid = result.data.data;
@ -144,6 +143,7 @@ export async function getUsedResources (req, resourceMeta) {
* @returns {Objetc} k-v pairs of specific disk data, including storage and size of unused disks. * @returns {Objetc} k-v pairs of specific disk data, including storage and size of unused disks.
*/ */
export async function getDiskInfo (node, type, vmid, disk) { export async function getDiskInfo (node, type, vmid, disk) {
const pveAPIToken = global.db.pveAPIToken;
try { try {
const config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken); const config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken);
const storageID = config.data.data[disk].split(":")[0]; const storageID = config.data.data[disk].split(":")[0];
@ -166,6 +166,7 @@ export async function getDiskInfo (node, type, vmid, disk) {
* @returns {Object} k-v pairs of specific device data, including device name and manufacturer. * @returns {Object} k-v pairs of specific device data, including device name and manufacturer.
*/ */
export async function getDeviceInfo (node, type, vmid, qid) { export async function getDeviceInfo (node, type, vmid, qid) {
const pveAPIToken = global.db.pveAPIToken;
try { try {
const result = (await requestPVE(`/nodes/${node}/hardware/pci`, "GET", null, null, pveAPIToken)).data.data; const result = (await requestPVE(`/nodes/${node}/hardware/pci`, "GET", null, null, pveAPIToken)).data.data;
const deviceData = []; const deviceData = [];
@ -193,6 +194,7 @@ export async function getDeviceInfo (node, type, vmid, qid) {
* @returns {Array.<Object>} array of k-v pairs of specific device data, including device name and manufacturer, which are available on the specified node. * @returns {Array.<Object>} array of k-v pairs of specific device data, including device name and manufacturer, which are available on the specified node.
*/ */
export async function getNodeAvailDevices (node, cookies) { export async function getNodeAvailDevices (node, cookies) {
const pveAPIToken = global.db.pveAPIToken;
// get node pci devices // get node pci devices
let nodeAvailPci = (await requestPVE(`/nodes/${node}/hardware/pci`, "GET", cookies, null, pveAPIToken)).data.data; let nodeAvailPci = (await requestPVE(`/nodes/${node}/hardware/pci`, "GET", cookies, null, pveAPIToken)).data.data;
// for each node container, get its config and remove devices which are already used // for each node container, get its config and remove devices which are already used

View File

@ -8,6 +8,7 @@ const requestPVE = global.pve.requestPVE;
* request and responses passed through to/from proxmox * request and responses passed through to/from proxmox
*/ */
router.get("/*", async (req, res) => { // proxy endpoint for GET proxmox api with no token router.get("/*", async (req, res) => { // proxy endpoint for GET proxmox api with no token
console.log(req.url);
const path = req.url.replace("/api/proxmox", ""); const path = req.url.replace("/api/proxmox", "");
const result = await requestPVE(path, "GET", req.cookies); const result = await requestPVE(path, "GET", req.cookies);
res.status(result.status).send(result.data); res.status(result.status).send(result.data);

View File

@ -1,7 +1,6 @@
import { createHash } from "crypto"; import { createHash } from "crypto";
import { getUsedResources, requestPVE } from "./pve.js"; import { getUsedResources, requestPVE } from "./pve.js";
import db from "./db.js";
/** /**
* Check if a user is authorized to access a specified vm, or the cluster in general. * Check if a user is authorized to access a specified vm, or the cluster in general.
@ -11,6 +10,7 @@ import db from "./db.js";
* @returns {boolean} true if the user is authorized to access the specific vm or cluster in general, false otheriwse. * @returns {boolean} true if the user is authorized to access the specific vm or cluster in general, false otheriwse.
*/ */
export async function checkAuth (cookies, res, vmpath = null) { export async function checkAuth (cookies, res, vmpath = null) {
const db = global.db;
let auth = false; let auth = false;
if (db.getUserConfig(cookies.username) === null) { if (db.getUserConfig(cookies.username) === null) {
@ -43,6 +43,7 @@ export async function checkAuth (cookies, res, vmpath = null) {
* @returns {{used: Object, avail: Object, max: Object, resources: Object}} used, available, maximum, and resource metadata for the specified user. * @returns {{used: Object, avail: Object, max: Object, resources: Object}} used, available, maximum, and resource metadata for the specified user.
*/ */
export async function getUserResources (req, username) { export async function getUserResources (req, username) {
const db = global.db;
const dbResources = db.getGlobalConfig().resources; const dbResources = db.getGlobalConfig().resources;
const used = await getUsedResources(req, dbResources); const used = await getUsedResources(req, dbResources);
const max = db.getUserConfig(username).resources.max; const max = db.getUserConfig(username).resources.max;