@wocker/core
Version:
Core of the Wocker
283 lines (282 loc) • 10.4 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Project = void 0;
const AsyncStorage_1 = require("../../../core/AsyncStorage");
const volumeParse_1 = require("../../../utils/volumeParse");
const ProjectRepository_1 = require("../repositories/ProjectRepository");
class Project {
constructor(data) {
const { name, services = {}, buildArgs = {}, env = {}, metadata = {}, ports = [], extraHosts = {}, volumes = [] } = data, rest = __rest(data, ["name", "services", "buildArgs", "env", "metadata", "ports", "extraHosts", "volumes"]);
this.id = this.name = name;
this.services = services;
this.buildArgs = buildArgs;
this.env = env;
this.metadata = metadata;
this.ports = ports;
this.extraHosts = extraHosts;
this.volumes = volumes;
Object.assign(this, rest);
}
get containerName() {
return `${this.name}.workspace`;
}
get domains() {
const host = this.getEnv("VIRTUAL_HOST");
if (!host) {
return [];
}
return host.split(",");
}
hasDomain(domain) {
return this.domains.includes(domain);
}
addDomain(addDomain) {
let domains = [
...this.domains.filter((domain) => {
return domain !== addDomain;
}),
addDomain
];
this.setEnv("VIRTUAL_HOST", domains.join(","));
}
removeDomain(removeDomain) {
if (!this.hasDomain(removeDomain)) {
return;
}
let domains = this.domains.filter((domain) => {
return domain !== removeDomain;
});
this.setEnv("VIRTUAL_HOST", domains.join(","));
}
clearDomains() {
this.unsetEnv("VIRTUAL_HOST");
}
linkPort(hostPort, containerPort) {
this.ports = [
...this.ports.filter((link) => {
return link !== `${hostPort}:${containerPort}`;
}),
`${hostPort}:${containerPort}`
];
}
unlinkPort(hostPort, containerPort) {
if (!this.ports) {
return;
}
this.ports = this.ports.filter((link) => {
return link !== `${hostPort}:${containerPort}`;
});
}
setBuildArg(name, value, service) {
if (service) {
if (!this.services[service]) {
throw new Error(`Service "${service}" not found`);
}
if (!this.services[service].buildArgs) {
this.services[service].buildArgs = {};
}
this.services[service].buildArgs[name] = value;
return;
}
this.buildArgs[name] = value;
}
unsetBuildArg(name, service) {
if (service) {
if (!this.services[service]) {
throw new Error(`Service "${service}" not found`);
}
if (this.services[service].buildArgs && name in this.services[service].buildArgs) {
delete this.services[service].buildArgs[name];
if (Object.keys(this.services[service].buildArgs).length === 0) {
delete this.services[service].buildArgs;
}
}
return;
}
if (name in this.buildArgs) {
delete this.buildArgs[name];
}
}
hasEnv(name) {
if (!this.env) {
return false;
}
return this.env.hasOwnProperty(name);
}
getEnv(name, defaultValue) {
const { [name]: value = defaultValue } = this.env || {};
return value;
}
setEnv(name, value, service) {
if (!this.env) {
this.env = {};
}
if (service) {
if (!this.services[service]) {
throw new Error(`Service "${service}" not found`);
}
if (!this.services[service].env) {
this.services[service].env = {};
}
this.services[service].env[name] = typeof value === "boolean"
? (value ? "true" : "false")
: value;
return;
}
this.env[name] = typeof value === "boolean"
? (value ? "true" : "false")
: value;
}
unsetEnv(name, service) {
if (service) {
if (!this.services[service]) {
throw new Error(`Service "${service}" not found`);
}
if (this.services[service].env && name in this.services[service].env) {
delete this.services[service].env[name];
if (Object.keys(this.services[service].env).length === 0) {
delete this.services[service].env;
}
}
return;
}
if (name in this.env) {
delete this.env[name];
}
}
hasMeta(name) {
return !!this.metadata && this.metadata.hasOwnProperty(name);
}
getMeta(name, defaultValue) {
const { [name]: value = defaultValue } = this.metadata || {};
return value;
}
setMeta(name, value) {
if (!this.metadata) {
this.metadata = {};
}
this.metadata[name] = typeof value === "boolean"
? (value ? "true" : "false")
: value;
}
unsetMeta(name) {
if (name in this.metadata) {
delete this.metadata[name];
}
}
getVolumeBySource(source) {
return (this.volumes || []).find((volume) => {
return (0, volumeParse_1.volumeParse)(volume).source === source;
});
}
getVolumeByDestination(destination) {
return (this.volumes || []).find((volume) => {
return (0, volumeParse_1.volumeParse)(volume).destination === destination;
});
}
volumeMount(...volumes) {
if (volumes.length === 0) {
return;
}
const [volume, ...restVolumes] = volumes;
const { destination } = (0, volumeParse_1.volumeParse)(volume);
this.volumes = [
...(this.volumes || []).filter((v) => {
return v !== this.getVolumeByDestination(destination);
}),
volume
];
this.volumeMount(...restVolumes);
}
volumeUnmount(...volumes) {
if (!this.volumes || volumes.length === 0) {
return;
}
const [volume, ...restVolumes] = volumes;
const v = (0, volumeParse_1.volumeParse)(volume);
this.volumes = this.volumes.filter((mounted) => {
const m = (0, volumeParse_1.volumeParse)(mounted);
return v.source !== m.source && v.destination !== m.destination;
});
if (this.volumes.length === 0) {
return;
}
this.volumeUnmount(...restVolumes);
}
addExtraHost(host, domain) {
if (!this.extraHosts) {
this.extraHosts = {};
}
this.extraHosts[host] = domain;
}
removeExtraHost(host) {
if (!this.extraHosts || !this.extraHosts[host]) {
return;
}
delete this.extraHosts[host];
}
getSecret(key, byDefault) {
return __awaiter(this, void 0, void 0, function* () {
const container = AsyncStorage_1.AsyncStorage.getContainer(), keystoreService = container.get("KEYSTORE_SERVICE");
return keystoreService.get(`p:${this.name}:${key}`, byDefault);
});
}
setSecret(key, value) {
return __awaiter(this, void 0, void 0, function* () {
const container = AsyncStorage_1.AsyncStorage.getContainer(), keystoreService = container.get("KEYSTORE_SERVICE");
return keystoreService.set(`p:${this.name}:${key}`, value);
});
}
unsetSecret(key) {
return __awaiter(this, void 0, void 0, function* () {
const container = AsyncStorage_1.AsyncStorage.getContainer(), keystoreService = container.get("KEYSTORE_SERVICE");
return keystoreService.delete(`p:${this.name}:${key}`);
});
}
save() {
const container = AsyncStorage_1.AsyncStorage.getContainer(), projectRepository = container.get(ProjectRepository_1.ProjectRepository);
projectRepository.save(this);
}
toObject() {
return {
name: this.name,
type: this.type,
path: this.path,
imageName: this.imageName,
dockerfile: this.dockerfile,
composefile: this.composefile,
preset: this.preset,
presetMode: this.presetMode,
cmd: this.cmd,
scripts: this.scripts,
services: Object.keys(this.services).length > 0 ? this.services : undefined,
buildArgs: Object.keys(this.buildArgs).length > 0 ? this.buildArgs : undefined,
env: Object.keys(this.env).length > 0 ? this.env : undefined,
metadata: Object.keys(this.metadata).length > 0 ? this.metadata : undefined,
ports: this.ports.length > 0 ? this.ports : undefined,
extraHosts: Object.keys(this.extraHosts).length ? this.extraHosts : undefined,
volumes: this.volumes.length > 0 ? this.volumes : undefined,
};
}
}
exports.Project = Project;