UNPKG

@containernerds/incus-client

Version:

A Node.js client for automating Incus (LXD) servers.

171 lines (170 loc) 5.98 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InstancesAPI = exports.createInstanceSpec = exports.createInstanceSource = void 0; const axios_1 = __importDefault(require("axios")); const fs_1 = __importDefault(require("fs")); const types_1 = require("../types"); const utils_1 = require("../utils"); const createInstanceSource = (overrides) => { return { ...types_1.defaultInstanceSource, ...overrides, }; }; exports.createInstanceSource = createInstanceSource; const createInstanceSpec = (overrides) => { return { architecture: 'x86_64', config: {}, devices: {}, ephemeral: false, instance_type: 't1.micro', profiles: ['default'], restore: '', source: (0, exports.createInstanceSource)({ alias: 'ubuntu/22.04' }), start: true, stateful: false, type: 'container', ...overrides, }; }; exports.createInstanceSpec = createInstanceSpec; class InstancesAPI { client; defaultInstanceSource; project; constructor(baseURL, httpsAgent, project) { this.client = axios_1.default.create({ baseURL, httpsAgent }); this.defaultInstanceSource = { allow_inconsistent: false, instance_only: false, live: false, mode: 'pull', protocol: 'simplestreams', server: 'https://images.linuxcontainers.org', type: 'image', }; this.project = project; } ; async listInstances() { const response = await this.client.get(`/1.0/instances?project=${this.project}`); return response.data; } ; async createInstance(instanceSpec) { let instanceSource = { ...this.defaultInstanceSource, }; const response = await this.client.post(`/1.0/instances?project=${this.project}`, { ...this.defaultInstanceSource, ...instanceSpec }); return response.data; } ; // TODO: NEED TO TEST THIS, NOT SURE IF IT WORKS async createInstanceFromBackup(backupFile) { const backup = fs_1.default.readFileSync(backupFile); const response = await this.client.post(`/1.0/instances?project=${this.project}`, { backup, }); return response.data; } ; /** * Gets a specific instance (basic struct). * * @param {string} instanceName * @returns {Promise<any>} A promise that resolves with the response data. * @throws {Error} If the request fails. */ async getInstance(instanceName) { const response = await this.client.get(`/1.0/instances/${instanceName}?project=${this.project}`); return response.data; } ; /** * Deletes an instance by its name. * This also deletes anything owned by the instance such as snapshots and backups. * * @param {string} instanceName - The name of the instance to delete. * @returns {Promise<any>} A promise that resolves with the response data. * @throws {Error} If the request fails. */ async deleteInstance(instanceName, force = false) { if (force) { await this.changeInstanceState(instanceName, types_1.InstanceRunningState.STOP, force); } ; const response = await this.client.delete(`/1.0/instances/${instanceName}?project=${this.project}`); return response.data; } ; /** * Gets the state of an instance. * * @param {string} instanceName - The name of the instance to get the state of. * @returns {Promise<any>} A promise that resolves with the response data. * @throws {Error} If the request fails. */ async getInstanceState(instanceName) { const response = await this.client.get(`/1.0/instances/${instanceName}/state?project=${this.project}`); return response.data; } ; /** * Changes the state of an instance. * * @param {string} instanceName - The name of the instance to change the state of. * @param {string} state - The state to change the instance to. * @param {boolean} force - Whether to force the state change. * @param {boolean} stateful - Whether to perform a stateful change. * @param {number} timeout - The time to wait for the state change to complete. * @returns {Promise<any>} A promise that resolves with the response data. * @throws {Error} If the request fails. */ async changeInstanceState(instanceName, state, force = false, stateful = false, timeout = 30) { const response = await this.client.put(`/1.0/instances/${instanceName}/state?project=${this.project}`, { action: state, force, stateful, timeout, }); // stop:Stopped // start:Running // restart:Running // TODO support freeze, unfreeze, migrate, snapshot, restore, delete const linked = { 'stop': 'Stopped', 'start': 'Running', 'restart': 'Running', }; if (response.status === 202) { const pollForStatus = await (0, utils_1.asyncPoll)(() => { const res = this.getInstanceState(instanceName).then((data) => { if (data.metadata.status === linked[state]) { return true; } else { return false; } ; }).catch((error) => { return false; }); return res; }, 5, 5000); return pollForStatus; } else { return response.data; } } ; } exports.InstancesAPI = InstancesAPI; ;