|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import * as net from 'net'; |
| 3 | +import * as cp from 'child_process'; |
| 4 | +import { ServerOptions, Executable, StreamInfo } from 'vscode-languageclient'; |
| 5 | + |
| 6 | +import { ConnectionHandler } from '../handler'; |
| 7 | +import { ConnectionStatus, ConnectionType, IConnectionConfiguration } from '../interfaces'; |
| 8 | +import { ISettings } from '../settings'; |
| 9 | +import { PuppetStatusBar } from '../PuppetStatusBar'; |
| 10 | +import { OutputChannelLogger } from '../logging/outputchannel'; |
| 11 | +import { CommandEnvironmentHelper } from '../helpers/commandHelper'; |
| 12 | + |
| 13 | +export class DockerConnectionHandler extends ConnectionHandler { |
| 14 | + private name: string; |
| 15 | + |
| 16 | + constructor( |
| 17 | + context: vscode.ExtensionContext, |
| 18 | + settings: ISettings, |
| 19 | + statusBar: PuppetStatusBar, |
| 20 | + logger: OutputChannelLogger, |
| 21 | + config: IConnectionConfiguration, |
| 22 | + ) { |
| 23 | + super(context, settings, statusBar, logger, config); |
| 24 | + this.logger.debug(`Configuring ${ConnectionType[this.connectionType]}::${this.protocolType} connection handler`); |
| 25 | + |
| 26 | + /* |
| 27 | + The docker container will be assigned a random port on creation, so we don't |
| 28 | + know it unitl we ask via a docker command. Using the unique name created in |
| 29 | + createUniqueDockerName() we can get the port that the Puppet Language |
| 30 | + Server is running on in getDockerPort(). |
| 31 | + */ |
| 32 | + this.name = this.createUniqueDockerName(); |
| 33 | + |
| 34 | + let exe: Executable = this.getDockerExecutable(this.name, this.settings.editorService.docker.imageName); |
| 35 | + this.logger.debug('Editor Services will invoke with: ' + exe.command + ' ' + exe.args.join(' ')); |
| 36 | + |
| 37 | + /* |
| 38 | + We start the docker container and then listen on stdout for the line that |
| 39 | + indicates the Puppet Language Server is running and ready to accept |
| 40 | + connections. This takes some time, so we can't call start() right away. |
| 41 | + We then call getDockerPort to get the port to connect to. |
| 42 | + */ |
| 43 | + var proc = cp.spawn(exe.command, exe.args); |
| 44 | + var isRunning: boolean = false; |
| 45 | + proc.stdout.on('data', data => { |
| 46 | + if (/LANGUAGE SERVER RUNNING/.test(data.toString())) { |
| 47 | + settings.editorService.tcp.port = this.getDockerPort(this.name); |
| 48 | + isRunning = true; |
| 49 | + this.start(); |
| 50 | + } |
| 51 | + if (!isRunning) { |
| 52 | + this.logger.debug('Editor Service STDOUT: ' + data.toString()); |
| 53 | + } |
| 54 | + }); |
| 55 | + proc.stderr.on('data', data => { |
| 56 | + if (!isRunning) { |
| 57 | + this.logger.debug('Editor Service STDERR: ' + data.toString()); |
| 58 | + } |
| 59 | + }); |
| 60 | + proc.on('close', exitCode => { |
| 61 | + this.logger.debug('Editor Service terminated with exit code: ' + exitCode); |
| 62 | + if (!isRunning) { |
| 63 | + this.setConnectionStatus('Failure', ConnectionStatus.Failed, 'Could not start the docker container'); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + // This is always a remote connection |
| 69 | + get connectionType(): ConnectionType { |
| 70 | + return ConnectionType.Remote; |
| 71 | + } |
| 72 | + |
| 73 | + createServerOptions(): ServerOptions { |
| 74 | + let serverOptions = () => { |
| 75 | + let socket = net.connect({ |
| 76 | + port: this.settings.editorService.tcp.port, |
| 77 | + host: this.settings.editorService.tcp.address, |
| 78 | + }); |
| 79 | + |
| 80 | + let result: StreamInfo = { |
| 81 | + writer: socket, |
| 82 | + reader: socket, |
| 83 | + }; |
| 84 | + return Promise.resolve(result); |
| 85 | + }; |
| 86 | + return serverOptions; |
| 87 | + } |
| 88 | + |
| 89 | + /* |
| 90 | + Options defined in getDockerArguments() should ensure docker cleans up |
| 91 | + the container on exit, but we do this to ensure the container goes away |
| 92 | + */ |
| 93 | + cleanup(): void { |
| 94 | + this.stopLanguageServerDockerProcess(this.name); |
| 95 | + } |
| 96 | + |
| 97 | + /* |
| 98 | + Unlike stdio or tcp, we don't much care about the shell env variables when |
| 99 | + starting docker containers. We only need docker on the PATH in order for |
| 100 | + this to work, so we copy what's already there and leave most of it be. |
| 101 | + */ |
| 102 | + private getDockerExecutable(containerName: string, imageName: string): Executable { |
| 103 | + let exe: Executable = { |
| 104 | + command: this.getDockerCommand(process.platform), |
| 105 | + args: this.getDockerArguments(containerName, imageName), |
| 106 | + options: {}, |
| 107 | + }; |
| 108 | + |
| 109 | + exe.options.env = CommandEnvironmentHelper.shallowCloneObject(process.env); |
| 110 | + exe.options.stdio = 'pipe'; |
| 111 | + |
| 112 | + switch (process.platform) { |
| 113 | + case 'win32': |
| 114 | + break; |
| 115 | + default: |
| 116 | + exe.options.shell = true; |
| 117 | + break; |
| 118 | + } |
| 119 | + |
| 120 | + CommandEnvironmentHelper.cleanEnvironmentPath(exe); |
| 121 | + |
| 122 | + // undefined or null values still appear in the child spawn environment variables |
| 123 | + // In this case these elements should be removed from the Object |
| 124 | + CommandEnvironmentHelper.removeEmptyElements(exe.options.env); |
| 125 | + |
| 126 | + return exe; |
| 127 | + } |
| 128 | + |
| 129 | + /* |
| 130 | + This creates a sufficiently unique name for a docker container that won't |
| 131 | + conflict with other containers on a system, but known enough for us to find |
| 132 | + it if we lose track of it somehow |
| 133 | + */ |
| 134 | + private createUniqueDockerName() { |
| 135 | + return 'puppet-vscode-xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
| 136 | + const r = (Math.random() * 16) | 0, |
| 137 | + v = c === 'x' ? r : (r & 0x3) | 0x8; |
| 138 | + return v.toString(16); |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + /* |
| 143 | + This uses docker to query what random port was assigned the container we |
| 144 | + created, and a regex to parse the port number out of the result |
| 145 | + */ |
| 146 | + private getDockerPort(name: string) { |
| 147 | + let cmd: string = this.getDockerCommand(process.platform); |
| 148 | + let args: Array<string> = ['port', name, '8082']; |
| 149 | + var proc = cp.spawnSync(cmd, args); |
| 150 | + let regex = /:(\d+)$/m; |
| 151 | + return Number(regex.exec(proc.stdout.toString())[1]); |
| 152 | + } |
| 153 | + |
| 154 | + // this stops and removes docker containers forcibly |
| 155 | + private stopLanguageServerDockerProcess(name: string): void { |
| 156 | + let cmd: string = this.getDockerCommand(process.platform); |
| 157 | + let args: Array<string> = ['rm', '--force', name]; |
| 158 | + let spawn_options: cp.SpawnOptions = {}; |
| 159 | + spawn_options.stdio = 'pipe'; |
| 160 | + cp.spawn(cmd, args, spawn_options); |
| 161 | + } |
| 162 | + |
| 163 | + // platform specific docker command |
| 164 | + private getDockerCommand(platform: string): string { |
| 165 | + switch (platform) { |
| 166 | + case 'win32': |
| 167 | + return 'docker.exe'; |
| 168 | + default: |
| 169 | + return 'docker'; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // docker specific arguments to start the container how we need it started |
| 174 | + private getDockerArguments(containerName: string, imageName: string) { |
| 175 | + let args = [ |
| 176 | + 'run', // run a new container |
| 177 | + '--rm', // automatically remove container when it exits |
| 178 | + '-i', // interactive |
| 179 | + '-P', // publish all exposed ports to random ports |
| 180 | + '--name', |
| 181 | + containerName, // assign a name to the container |
| 182 | + imageName, // image to use |
| 183 | + ]; |
| 184 | + return args; |
| 185 | + } |
| 186 | +} |
0 commit comments