diff --git a/src/backends/backends.js b/src/backends/backends.js index a98b042..1830183 100644 --- a/src/backends/backends.js +++ b/src/backends/backends.js @@ -183,7 +183,7 @@ class USER_BACKEND extends BACKEND { * @param {{id: string, realm: string}} user * @param {{id: string}} group * @param {Object} params authentication params, usually req.cookies - * @returns {AtomicChange} atomic change object + * @returns {AtomicChange} atomic change object */ delUserFromGroup (user, group, params) {} } @@ -191,7 +191,72 @@ class USER_BACKEND extends BACKEND { /** * Interface for proxmox api backends. */ -export class PVE_BACKEND extends BACKEND {} +export class PVE_BACKEND extends BACKEND { + /** + * Get and return node data. + * Returns the node data or null if the node does not exist. + * @param {string} node node id + * @returns {} + */ + getNode (node) {} + + /** + * Send a signal to synchronize a node after some change has been made. + * * @param {string} node node id + */ + syncNode (node) {} + + /** + * Get and return instance data. + * Returns the instance data or null if the instance does not exist. + * @param {string} node node id + * @param {string} type instance type + * @param {string} vmid instance id + */ + getInstance (node, type, instance) {} + + /** + * Send a signal to synchronize an instance after some change has been made. + * @param {string} node node id + * @param {string} instance instance id + */ + syncInstance (node, instance) {} + + /** + * Get meta data for a specific disk. Adds info that is not normally available in a instance's config. + * @param {string} node containing the query disk. + * @param {string} instance with query disk. + * @param {string} disk name of the query disk, ie. sata0. + * @returns {Objetc} k-v pairs of specific disk data, including storage and size of unused disks. + */ + async getDisk (node, instance, disk) {} + + /** + * Get meta data for a specific net. Adds info that is not normally available in a instance's config. + * @param {string} node containing the query net. + * @param {string} instance with query net. + * @param {string} netid id number of the query net, ie. 0 -> net0. + * @returns {Objetc} k-v pairs of specific net data, including rate and vlan. + */ + async getNet (node, instance, netid) {} + + /** + * Get meta data for a specific device. Adds info that is not normally available in a instance's config. + * @param {string} node containing the query device. + * @param {string} instance with query device. + * @param {string} deviceid id number of the query device, ie. 0 -> pci0. + * @returns {Objetc} k-v pairs of specific device data, including name and manfacturer. + */ + async getDevice (node, instance, deviceid) {} + + /** + * Get user resource data including used, available, and maximum resources. + * @param {{id: string, realm: string}} user object of user to get resource data. + * @param {Object} cookies object containing k-v store of cookies + * @returns {{used: Object, avail: Object, max: Object, resources: Object}} used, available, maximum, and resource metadata for the specified user. + */ + getUserResources (user, cookies) {} +} /** * Interface for user database backends. @@ -216,7 +281,12 @@ class USER_BACKEND_MANAGER extends USER_BACKEND { } getBackendsByUser (user) { - return this.#config.realm[user.realm]; + if (user != null) { + return this.#config.realm[user.realm]; + } + else { + return null; + } } addUser (user, attributes, params) {} diff --git a/src/backends/pve.js b/src/backends/pve.js index 5481cb9..6af5230 100644 --- a/src/backends/pve.js +++ b/src/backends/pve.js @@ -74,11 +74,22 @@ export default class PVE extends PVE_BACKEND { const token = this.#pveAPIToken; content.headers.Authorization = `PVEAPIToken=${token.user}@${token.realm}!${token.id}=${token.uuid}`; } + else if (auth && auth.root) { + const rootauth = await global.pve.requestPVE("/access/ticket", "POST", null, this.#pveRoot); + if (!(rootauth.status === 200)) { + return rootauth.response; + } + const rootcookie = rootauth.data.data.ticket; + const rootcsrf = rootauth.data.data.CSRFPreventionToken; + content.headers.CSRFPreventionToken = rootcsrf; + content.headers.Cookie = `PVEAuthCookie=${rootcookie}; CSRFPreventionToken=${rootcsrf}`; + } try { return await axios.request(url, content); } catch (error) { + console.log(`backends: error ocuured in pve.requestPVE: ${error}`); return error.response; } } @@ -125,66 +136,6 @@ export default class PVE extends PVE_BACKEND { } } - /** - * Get meta data for a specific pci device. Adds info that is not normally available in a instance's config. - * @param {string} node containing the query device. - * @param {string} qid pci bus id number of the query device, ie. 89ab:cd:ef.0. - * @returns {Object} k-v pairs of specific device data, including device name and manufacturer. - */ - async getDeviceInfo (node, qid) { - try { - const result = (await this.requestPVE(`/nodes/${node}/hardware/pci`, "GET", { token: true })).data.data; - const deviceData = []; - result.forEach((element) => { - if (element.id.startsWith(qid)) { - deviceData.push(element); - } - }); - deviceData.sort((a, b) => { - return a.id < b.id; - }); - const device = deviceData[0]; - device.subfn = structuredClone(deviceData.slice(1)); - return device; - } - catch { - return null; - } - } - - /** - * Get available devices on specific node. - * @param {string} node to get devices from. - * @returns {Array.} array of k-v pairs of specific device data, including device name and manufacturer, which are available on the specified node. - */ - async getNodeAvailDevices (node) { - // get node pci devices - let nodeAvailPci = this.requestPVE(`/nodes/${node}/hardware/pci`, "GET", { token: true }); - // for each node container, get its config and remove devices which are already used - const vms = (await this.requestPVE(`/nodes/${node}/qemu`, "GET", { token: true })).data.data; - - const promises = []; - for (const vm of vms) { - promises.push(this.requestPVE(`/nodes/${node}/qemu/${vm.vmid}/config`, "GET", { token: true })); - } - const configs = await Promise.all(promises); - configs.forEach((e, i) => { - configs[i] = e.data.data; - }); - - nodeAvailPci = (await nodeAvailPci).data.data; - - for (const config of configs) { - Object.keys(config).forEach((key) => { - if (key.startsWith("hostpci")) { - const deviceID = config[key].split(",")[0]; - nodeAvailPci = nodeAvailPci.filter(element => !element.id.includes(deviceID)); - } - }); - } - return nodeAvailPci; - } - /** * Send HTTP request to PAAS Fabric * @param {string} path HTTP path, prepended with the proxmox API base url. @@ -220,7 +171,7 @@ export default class PVE extends PVE_BACKEND { return null; } - return res.data.instance; + return res.data.node; } async syncNode (node) { @@ -241,13 +192,6 @@ export default class PVE extends PVE_BACKEND { this.requestFabric(`/nodes/${node}/instances/${vmid}/sync`, "POST"); } - /** - * Get meta data for a specific disk. Adds info that is not normally available in a instance's config. - * @param {string} node containing the query disk. - * @param {string} instance with query disk. - * @param {string} disk name of the query disk, ie. sata0. - * @returns {Objetc} k-v pairs of specific disk data, including storage and size of unused disks. - */ async getDisk (node, instance, disk) { const config = await this.getInstance(node, instance); if (config != null && config.volumes[disk] != null) { @@ -258,6 +202,26 @@ export default class PVE extends PVE_BACKEND { } } + async getNet (node, instance, netid) { + const config = await this.getInstance(node, instance); + if (config != null && config.nets[netid] != null) { + return config.nets[netid]; + } + else { + return null; + } + } + + async getDevice (node, instance, deviceid) { + const config = await this.getInstance(node, instance); + if (config != null && config.devices[deviceid] != null) { + return config.devices[deviceid]; + } + else { + return null; + } + } + async getUserResources (user, cookies) { // get user resources with vm filter const res = await this.requestPVE("/cluster/resources?type=vm", "GET", { cookies }); diff --git a/src/utils.js b/src/utils.js index f4bf4a2..d8026f9 100644 --- a/src/utils.js +++ b/src/utils.js @@ -188,12 +188,8 @@ export async function getUserResources (req, user) { // count pci device resources in devices for (const deviceid in config.devices) { const device = config.devices[deviceid]; - let name = ""; - for (const subsystemid in device) { - const subsystem = device[subsystemid]; - name += `${subsystem.device_name}:${subsystem.subsystem_device_name},`; - } - + const name = device.device_name; + // if the node has a node specific rule, add it there if (nodeName in userResources.pci.nodes) { const index = userResources.pci.nodes[nodeName].findIndex((availEelement) => name.includes(availEelement.match)); if (index >= 0) { @@ -209,6 +205,7 @@ export async function getUserResources (req, user) { userResources.pci.global[index].avail--; } } + // finally, add the device to the total map const index = userResources.pci.total.findIndex((availEelement) => name.includes(availEelement.match)); if (index >= 0) { userResources.pci.total[index].used++;