UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

108 lines 4.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TerminalServer = void 0; const resolver_1 = require("../commands/resolver"); const helpers_1 = require("./helpers"); class TerminalServer { constructor(session, client) { this.session = session; this.client = client; } // ANCHOR - Public Methods async createConfig(workloadName, location, replica, container, stdin, tty, quiet) { var _a, _b; // Use the first location of the GVC if a location is not specified if (!location) { // Resolve GVC link const gvcSelfLink = (0, resolver_1.resolveToLink)('gvc', this.session.context.gvc, this.session.context); // Fetch GVC const gvc = await this.client.get(gvcSelfLink); // Determine location self link let locationSelfLink = ''; if (((_b = (_a = gvc.spec) === null || _a === void 0 ? void 0 : _a.staticPlacement) === null || _b === void 0 ? void 0 : _b.locationLinks) && gvc.spec.staticPlacement.locationLinks.length > 0) { locationSelfLink = gvc.spec.staticPlacement.locationLinks[0]; } else { this.session.abort({ message: 'ERROR: Gvc has no locations' }); } // Extract location name from location self link location = locationSelfLink.split('/location/')[1]; // Log the location that was picked this.session.err((0, helpers_1.defaultingToMessage)('location', location)); } // Determine deployment self link const workloadSelfLink = (0, resolver_1.kindResolver)('workload').resourceLink(workloadName, this.session.context); const deploymentSelfLink = `${workloadSelfLink}/deployment/${location}`; // Fetch deployment const deployment = await this.client.get(deploymentSelfLink); if (!deployment.status || !deployment.status.remote) { this.session.abort({ message: 'ERROR: No deployment is present in the workload.' }); } // Extract remote host endpoint from deployment status const remoteHost = deployment.status.remote; // Fetch the first replica if no replica was specified by the user if (!replica) { const replicaListLink = `${remoteHost}/replicas/${workloadSelfLink}`; const replicaList = await this.client.get(replicaListLink); if (replicaList.items.length === 0) { this.session.abort({ message: 'ERROR: No replica is present in the workload deployment.' }); } else { replica = replicaList.items[0]; } // Log the replica that was picked this.session.err((0, helpers_1.defaultingToMessage)('replica', replica)); } // Fetch the first container if no container was specified by the user if (!container) { const workload = await this.client.get(workloadSelfLink); // If there is only one container, use its name if (workload.spec.containers.length === 1) { container = workload.spec.containers[0].name; } else { // Find a container with ports const containerSpec = workload.spec.containers.find((c) => c.ports && c.ports.length > 0); // Determine container name, use first if the one with ports was not found let containerName = ''; if (!containerSpec) { containerName = workload.spec.containers[0].name; } else { containerName = containerSpec.name; } container = containerName; } // Log the container that was picked this.session.err((0, helpers_1.defaultingToMessage)('container', container)); } // Determine /exec endpoint let remoteSocket; // If the endpoint is specified as an env variable, then use it if (process.env.TERMINAL_SERVER_URL) { remoteSocket = `${process.env.TERMINAL_SERVER_URL}/exec`; } else { remoteSocket = `${remoteHost.replace('https://', 'wss://')}/exec`; } // Prepare token const token = this.session.request.token.replace('Bearer ', ''); return { remoteSocket: remoteSocket, request: { requestVersion: 'v4', token, org: this.session.context.org, gvc: this.session.context.gvc, pod: replica, container: container, contextId: this.session.id, stdin, tty, quiet, }, }; } } exports.TerminalServer = TerminalServer; //# sourceMappingURL=terminal-server.js.map