@wocker/ws
Version:
Docker workspace for web projects
168 lines (167 loc) • 6.81 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProxyService = void 0;
const core_1 = require("@wocker/core");
const utils_1 = require("@wocker/utils");
const Path = __importStar(require("path"));
const env_1 = require("../../../env");
const docker_1 = require("../../docker");
let ProxyService = class ProxyService extends core_1.ProxyService {
constructor(appConfigService, fs, dockerService) {
super();
this.appConfigService = appConfigService;
this.fs = fs;
this.dockerService = dockerService;
this.containerName = "proxy.workspace";
this.imageName = "wocker-proxy:1.0.1";
this.oldImages = [
"wocker-proxy:1.0.0"
];
}
async init(project) {
const appPort = await (0, utils_1.promptInput)({
message: "App port",
type: "number",
default: parseInt(project.getEnv("VIRTUAL_PORT", "80"))
});
project.setEnv("VIRTUAL_PORT", appPort.toString());
project.save();
}
async start(restart, rebuild) {
if (restart || rebuild) {
await this.stop();
}
let container = await this.dockerService.getContainer(this.containerName);
if (!container) {
console.info("Proxy starting...");
await this.build(rebuild);
const fs = this.fs;
if (!this.fs.exists("certs/ca")) {
this.fs.mkdir("certs/ca", {
recursive: true,
mode: 0o700
});
}
if (!this.fs.exists("certs/projects")) {
this.fs.mkdir("certs/projects", {
recursive: true,
mode: 0o700
});
}
if (!fs.exists("nginx/vhost.d")) {
fs.mkdir("nginx/vhost.d", {
recursive: true,
mode: 0o700
});
}
const httpPort = this.appConfigService.getMeta("PROXY_HTTP_PORT", "80");
const httpsPort = this.appConfigService.getMeta("PROXY_HTTPS_PORT", "443");
const sshPort = this.appConfigService.getMeta("PROXY_SSH_PORT", "22");
container = await this.dockerService.createContainer({
name: this.containerName,
image: this.imageName,
restart: "always",
env: {
DEFAULT_HOST: "localhost",
TRUST_DOWNSTREAM_PROXY: "true"
},
ports: [
`${httpPort}:80`,
`${httpsPort}:443`,
...this.appConfigService.getMeta("PROXY_SSH_PASSWORD") ? [
`${sshPort}:22`
] : []
],
volumes: [
"/var/run/docker.sock:/tmp/docker.sock:ro",
`${fs.path("certs/projects")}:/etc/nginx/certs`,
`${fs.path("certs/ca")}:/etc/nginx/ca-certs`,
`${fs.path("nginx/vhost.d")}:/etc/nginx/vhost.d`
],
network: "workspace"
});
}
const { State: { Status } } = await container.inspect();
if (["created", "exited"].includes(Status)) {
await container.start();
console.info("Proxy started");
}
}
async stop() {
await this.dockerService.removeContainer(this.containerName);
}
async build(rebuild) {
let exists = await this.dockerService.imageExists(this.imageName);
if (rebuild && exists) {
await this.dockerService.imageRm(this.imageName);
exists = false;
}
if (exists) {
return;
}
for (const oldImage of this.oldImages) {
await this.dockerService.imageRm(oldImage);
}
await this.dockerService.buildImage({
version: this.appConfigService.isExperimentalEnabled("buildKit") ? "2" : "1",
tag: this.imageName,
buildArgs: {
SSH_PASSWORD: this.appConfigService.getMeta("PROXY_SSH_PASSWORD")
},
context: Path.join(env_1.PLUGINS_DIR, "proxy"),
dockerfile: "./Dockerfile"
});
}
async logs() {
await this.dockerService.logs(this.containerName);
}
};
exports.ProxyService = ProxyService;
exports.ProxyService = ProxyService = __decorate([
(0, core_1.Injectable)("PROXY_SERVICE"),
__metadata("design:paramtypes", [core_1.AppConfigService,
core_1.AppFileSystemService,
docker_1.DockerService])
], ProxyService);