portainer-redeploy
Version:
A TypeScript client for redeploy a stack in Portainer
64 lines • 2.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const https_1 = __importDefault(require("https"));
const axios_1 = __importDefault(require("axios"));
class PortainerClient {
constructor(baseDomain, apiKey, selfSigned) {
this.baseDomain = baseDomain;
this.apiKey = apiKey;
if (selfSigned) {
axios_1.default.defaults.httpsAgent = new https_1.default.Agent({ rejectUnauthorized: false });
}
}
async getRequest(url, method = 'GET', payload) {
const config = {
method,
url: this.baseDomain + url,
headers: {
'Content-Type': 'text/plain',
'X-API-Key': this.apiKey,
},
data: payload,
timeout: 9990000,
};
try {
const resp = await (0, axios_1.default)(config);
return resp;
}
catch (error) {
console.error('Error while fetching stack', error);
process.exit(1);
}
}
async getEndpoints() {
const url = '/api/endpoints';
return this.getRequest(url);
}
async getStacks() {
const url = '/api/stacks';
return this.getRequest(url);
}
async getStack(stackId) {
const url = `/api/stacks/${stackId}`;
return this.getRequest(url);
}
async redeployStack(stackId, payload) {
const jsonText = {
env: payload.Env,
prune: true,
pullImage: true,
repositoryUsername: payload.GitConfig.Authentication.Username,
repositoryAuthentication: true,
repositoryReferenceName: payload.GitConfig.ReferenceName,
};
console.log(jsonText);
const url = `/api/stacks/${stackId}/git/redeploy?endpointId=${payload.EndpointId}`;
return this.getRequest(url, 'PUT', JSON.stringify(jsonText));
}
}
exports.default = PortainerClient;
//# sourceMappingURL=portainerClient.js.map