2024-07-11 12:42:23 +08:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as os from 'os';
|
|
|
|
import * as vscode from 'vscode';
|
2024-10-21 11:39:07 +08:00
|
|
|
import * as rd from 'readline'
|
2024-07-11 12:42:23 +08:00
|
|
|
const { NodeSSH } = require('node-ssh')
|
2024-10-21 11:39:07 +08:00
|
|
|
|
2024-07-30 23:35:08 +08:00
|
|
|
import * as utils from './utils';
|
2024-10-28 19:02:56 +08:00
|
|
|
import User from './user';
|
2024-07-11 12:42:23 +08:00
|
|
|
|
|
|
|
export default class RemoteContainer {
|
2024-11-12 19:07:30 +08:00
|
|
|
private user: User;
|
2024-10-28 19:02:56 +08:00
|
|
|
|
2024-11-12 19:07:30 +08:00
|
|
|
constructor(user: User) {
|
2024-10-28 19:02:56 +08:00
|
|
|
this.user = user
|
|
|
|
}
|
2024-07-11 12:42:23 +08:00
|
|
|
|
2024-10-21 11:39:07 +08:00
|
|
|
async firstConnect(host: string, username: string, port: number): Promise<string>; // connect with key
|
|
|
|
// deprecated
|
2024-11-12 19:07:30 +08:00
|
|
|
async firstConnect(host: string, username: string, port: number, password: string): Promise<string>;
|
2024-07-30 23:35:08 +08:00
|
|
|
|
2024-10-21 11:39:07 +08:00
|
|
|
async firstConnect(host: string, username: string, port: number, password?: string): Promise<string> {
|
2025-01-16 02:54:45 +08:00
|
|
|
return new Promise(async (resolve) => {
|
2024-07-30 23:35:08 +08:00
|
|
|
const ssh = new NodeSSH();
|
2024-08-22 11:53:39 +08:00
|
|
|
vscode.window.withProgress({
|
2024-11-12 19:07:30 +08:00
|
|
|
location: vscode.ProgressLocation.Notification,
|
|
|
|
title: "正在容器中安装vscode-server及devstar插件",
|
|
|
|
cancellable: false
|
2024-08-22 11:53:39 +08:00
|
|
|
}, async (progress) => {
|
2024-11-12 19:07:30 +08:00
|
|
|
try {
|
|
|
|
if (password === undefined) {
|
|
|
|
// connect with key
|
|
|
|
await ssh.connect({
|
|
|
|
host: host,
|
|
|
|
username: username,
|
|
|
|
port: port,
|
|
|
|
privateKeyPath: this.user.getUserPrivateKeyPath()
|
|
|
|
});
|
|
|
|
progress.report({ message: "连接成功,开始安装" });
|
|
|
|
|
|
|
|
// install vscode-server and devstar extension
|
2025-01-08 18:36:59 +08:00
|
|
|
const vscodeCommitId = await utils.getVsCodeCommitId()
|
|
|
|
if ("" != vscodeCommitId) {
|
|
|
|
const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
|
|
|
|
const installVscodeServerScript = `
|
|
|
|
mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
|
|
|
|
if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
|
|
|
|
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
|
|
|
else
|
|
|
|
wget ${vscodeServerUrl} -O vscode-server-linux-x64.tar.gz && \\
|
|
|
|
mv vscode-server-linux-x64.tar.gz ~/.vscode-server/bin/${vscodeCommitId} && \\
|
|
|
|
cd ~/.vscode-server/bin/${vscodeCommitId} && \\
|
|
|
|
tar -xvzf vscode-server-linux-x64.tar.gz --strip-components 1 && \\
|
|
|
|
rm vscode-server-linux-x64.tar.gz && \\
|
|
|
|
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
|
|
|
fi
|
|
|
|
`;
|
|
|
|
await ssh.execCommand(installVscodeServerScript);
|
|
|
|
console.log("vscode-server and extension installed");
|
|
|
|
vscode.window.showInformationMessage('安装完成!');
|
|
|
|
}
|
2024-11-12 19:07:30 +08:00
|
|
|
|
2025-01-08 18:36:59 +08:00
|
|
|
await ssh.dispose();
|
2024-11-12 19:07:30 +08:00
|
|
|
} else {
|
|
|
|
// connect with password (deprecate in future)
|
|
|
|
await ssh.connect({
|
|
|
|
host: host,
|
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
port: port
|
|
|
|
});
|
|
|
|
progress.report({ message: "连接成功,开始安装" });
|
|
|
|
|
|
|
|
// install vscode-server and devstar extension
|
2025-01-08 18:36:59 +08:00
|
|
|
const vscodeCommitId = await utils.getVsCodeCommitId()
|
|
|
|
if ("" != vscodeCommitId) {
|
|
|
|
const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
|
|
|
|
const installVscodeServerScript = `
|
|
|
|
mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
|
|
|
|
if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
|
|
|
|
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
|
|
|
else
|
|
|
|
wget ${vscodeServerUrl} -O vscode-server-linux-x64.tar.gz && \\
|
|
|
|
mv vscode-server-linux-x64.tar.gz ~/.vscode-server/bin/${vscodeCommitId} && \\
|
|
|
|
cd ~/.vscode-server/bin/${vscodeCommitId} && \\
|
|
|
|
tar -xvzf vscode-server-linux-x64.tar.gz --strip-components 1 && \\
|
|
|
|
rm vscode-server-linux-x64.tar.gz && \\
|
|
|
|
~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
|
|
|
|
fi
|
|
|
|
`;
|
|
|
|
await ssh.execCommand(installVscodeServerScript);
|
|
|
|
console.log("vscode-server and extension installed");
|
|
|
|
vscode.window.showInformationMessage('安装完成!');
|
2024-11-12 19:07:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
await ssh.dispose();
|
|
|
|
}
|
2024-10-21 11:39:07 +08:00
|
|
|
|
2025-01-16 02:54:45 +08:00
|
|
|
// only connect successfully then save the host info
|
|
|
|
await this.storeHostInfo(host, port, username)
|
|
|
|
|
|
|
|
resolve('success')
|
2024-11-12 19:07:30 +08:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to install vscode-server and extension: ', error);
|
2024-10-21 11:39:07 +08:00
|
|
|
await ssh.dispose();
|
|
|
|
}
|
2024-11-12 19:07:30 +08:00
|
|
|
});
|
2024-07-30 23:35:08 +08:00
|
|
|
});
|
2024-07-11 12:42:23 +08:00
|
|
|
}
|
|
|
|
|
2025-01-16 02:54:45 +08:00
|
|
|
async storeHostInfo(host:string, port:number, username:string): Promise<void> {
|
|
|
|
const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
|
|
|
|
// check if the host and related info exist in local ssh config file before saving
|
|
|
|
var canAppendSSHConfig = true
|
|
|
|
if (fs.existsSync(sshConfigPath)) {
|
|
|
|
var reader = rd.createInterface(fs.createReadStream(sshConfigPath))
|
|
|
|
|
|
|
|
for await (const line of reader) {
|
|
|
|
// host format: hostname:port
|
|
|
|
if (line.includes(`Host ${host}-${port}`)) {
|
|
|
|
// the container ssh info exists
|
|
|
|
canAppendSSHConfig = false
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canAppendSSHConfig) {
|
|
|
|
// save the host to the local ssh config file
|
|
|
|
const hostInConfig = `${host}-${port}` // host format: hostname-port
|
|
|
|
const privateKeyPath = this.user.getUserPrivateKeyPath();
|
|
|
|
const newSShConfigContent =
|
|
|
|
`\nHost ${hostInConfig}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ${privateKeyPath}\n `;
|
|
|
|
fs.writeFileSync(sshConfigPath, newSShConfigContent, { encoding: 'utf8', flag: 'a' });
|
|
|
|
console.log('Host registered in local ssh config');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-11 12:42:23 +08:00
|
|
|
|
2025-02-19 09:58:31 +08:00
|
|
|
static openRemoteFolder(host: string, port: number, username: string, path: string): void {
|
2024-10-28 19:46:26 +08:00
|
|
|
var host = `${host}-${port}`
|
|
|
|
const command = `code --remote ssh-remote+${username}@${host} ${path} --reuse-window`
|
2024-07-11 12:42:23 +08:00
|
|
|
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
|
|
|
terminal.show(true);
|
|
|
|
terminal.sendText(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|