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';
|
|
|
|
const { NodeSSH } = require('node-ssh')
|
2024-07-30 23:35:08 +08:00
|
|
|
import * as utils from './utils';
|
2024-07-11 12:42:23 +08:00
|
|
|
|
|
|
|
export default class RemoteContainer {
|
|
|
|
|
2024-07-30 23:35:08 +08:00
|
|
|
async firstConnect(host: string, username: string, password: string, port: number): Promise<string> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
// connect the host and add the public key to the remote authorized_keys
|
|
|
|
const defaultPublicKeyPath = path.join(os.homedir(), '.ssh', 'id_rsa.pub');
|
|
|
|
// TODO: if there is no public key, generate one
|
|
|
|
const publicKey = fs.readFileSync(defaultPublicKeyPath, 'utf8');
|
|
|
|
|
|
|
|
// first connect to the remote host using password
|
|
|
|
const ssh = new NodeSSH();
|
2024-08-22 11:53:39 +08:00
|
|
|
vscode.window.withProgress({
|
|
|
|
location: vscode.ProgressLocation.Notification,
|
|
|
|
title: "正在容器中安装vscode-server及devstar插件",
|
|
|
|
cancellable: false
|
|
|
|
}, async (progress) => {
|
2024-07-30 23:35:08 +08:00
|
|
|
try {
|
|
|
|
await ssh.connect({
|
|
|
|
host: host,
|
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
port: port
|
|
|
|
});
|
2024-08-22 11:53:39 +08:00
|
|
|
progress.report({ message: "连接成功,开始安装" });
|
|
|
|
|
|
|
|
// install vscode-server and devstar extension
|
|
|
|
const vscodeCommitId = await utils.getVsCodeCommitId();
|
|
|
|
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-07-30 23:35:08 +08:00
|
|
|
|
|
|
|
// add the public key to the remote authorized_keys
|
|
|
|
await ssh.execCommand(`mkdir -p ~/.ssh && echo '${publicKey}' >> ~/.ssh/authorized_keys`);
|
|
|
|
console.log('Public key added to remote authorized_keys');
|
2024-08-22 11:53:39 +08:00
|
|
|
|
2024-07-30 23:35:08 +08:00
|
|
|
await ssh.dispose();
|
|
|
|
|
|
|
|
// only connect successfully then save the host info
|
|
|
|
// TODO: request change password
|
|
|
|
// TODO: use remote.SSH.configFile
|
|
|
|
const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
|
|
|
|
// append the host to the local ssh config file
|
|
|
|
const sshConfigContent =
|
|
|
|
`\nHost ${host}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ~/.ssh/id_rsa\n `;
|
|
|
|
// append the host to the local ssh config file
|
|
|
|
fs.writeFileSync(sshConfigPath, sshConfigContent, { encoding: 'utf8', flag: 'a' });
|
|
|
|
console.log('Host registered in local ssh config');
|
|
|
|
resolve("success");
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error adding public key: ', error);
|
|
|
|
await ssh.dispose();
|
|
|
|
reject("failed");
|
2024-08-22 11:53:39 +08:00
|
|
|
}});
|
2024-07-30 23:35:08 +08:00
|
|
|
});
|
2024-07-11 12:42:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
openRemoteFolder(host: string, path: string): void {
|
|
|
|
const command = `code --remote ssh-remote+${host} ${path} --reuse-window`
|
|
|
|
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
|
|
|
terminal.show(true);
|
|
|
|
terminal.sendText(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|