Files
devstar_plugin/src/remote-container.ts

87 lines
3.8 KiB
TypeScript
Raw Normal View History

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')
import * as utils from './utils';
export default class RemoteContainer {
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();
vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: "正在容器中安装vscode-server及devstar插件",
cancellable: false
}, async (progress) => {
try {
await ssh.connect({
host: host,
username: username,
password: password,
port: port
});
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('安装完成!');
// 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');
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");
}});
});
}
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);
}
}