add other db support

This commit is contained in:
Sourabh Jain 2025-03-17 06:35:19 +05:30
parent ec891671b6
commit 44e85647e4
4 changed files with 106 additions and 54 deletions

View File

@ -3,8 +3,6 @@ import { Terminal } from "xterm";
import { FitAddon } from 'xterm-addon-fit'; import { FitAddon } from 'xterm-addon-fit';
import "xterm/css/xterm.css"; import "xterm/css/xterm.css";
import { TerminalKind } from "../../../Contracts/ViewModels"; import { TerminalKind } from "../../../Contracts/ViewModels";
import { getAuthorizationHeader } from "../../../Utils/AuthorizationUtils";
import { getCommands } from "./Commands";
import { startCloudShellterminal } from "./UseTerminal"; import { startCloudShellterminal } from "./UseTerminal";
export interface CloudShellTerminalProps { export interface CloudShellTerminalProps {
@ -39,8 +37,7 @@ export const CloudShellTerminalComponent: React.FC<CloudShellTerminalProps> = ({
const handleResize = () => fitAddon.fit(); const handleResize = () => fitAddon.fit();
window.addEventListener('resize', handleResize); window.addEventListener('resize', handleResize);
const authorizationHeader = getAuthorizationHeader() socketRef.current = startCloudShellterminal(term, shellType);
socketRef.current = startCloudShellterminal(term, getCommands(shellType), authorizationHeader.token);
term.onData((data) => { term.onData((data) => {
if (socketRef.current && socketRef.current.readyState === WebSocket.OPEN) { if (socketRef.current && socketRef.current.readyState === WebSocket.OPEN) {

View File

@ -3,54 +3,94 @@
*/ */
import { TerminalKind } from "../../../Contracts/ViewModels"; import { TerminalKind } from "../../../Contracts/ViewModels";
import { userContext } from "../../../UserContext";
export const getCommands = (terminalKind: TerminalKind): string => { export const getCommands = (terminalKind: TerminalKind, key: string) => {
if (!Commands[terminalKind]) { let databaseacc = userContext.databaseAccount;
throw new Error(`Unsupported terminal kind: ${terminalKind}`); let endpoint;
switch (terminalKind) {
case TerminalKind.Postgres:
endpoint = databaseacc.properties.postgresqlEndpoint;
break;
case TerminalKind.Mongo:
endpoint = databaseacc.properties.mongoEndpoint;
break;
case TerminalKind.VCoreMongo:
endpoint = databaseacc.properties.vcoreMongoEndpoint;
break;
case TerminalKind.Cassandra:
endpoint = databaseacc.properties.cassandraEndpoint;
break;
default:
throw new Error("Unknown Terminal Kind");
} }
return Commands[terminalKind].join("\n").concat("\n");
let config = {
host: getHostFromUrl(endpoint),
name: databaseacc.name,
password: key,
endpoint: endpoint
};
return commands(terminalKind, config).join("\n").concat("\n");
}; };
export const Commands: Record<TerminalKind, string[]> = { export interface CommandConfig {
[TerminalKind.Postgres]: [ host: string,
"curl -s https://ipinfo.io", name: string,
"curl -LO https://ftp.postgresql.org/pub/source/v15.2/postgresql-15.2.tar.bz2", password: string,
"tar -xvjf postgresql-15.2.tar.bz2", endpoint: string
"cd postgresql-15.2", }
"mkdir ~/pgsql",
"curl -LO https://ftp.gnu.org/gnu/readline/readline-8.1.tar.gz", export const commands = (terminalKind: TerminalKind, config?: CommandConfig): string[] => {
"tar -xvzf readline-8.1.tar.gz", switch (terminalKind) {
"cd readline-8.1", case TerminalKind.Postgres:
"./configure --prefix=$HOME/pgsql" return [
], "curl -s https://ipinfo.io",
[TerminalKind.Mongo]: [ "curl -LO https://ftp.postgresql.org/pub/source/v15.2/postgresql-15.2.tar.bz2",
"curl -s https://ipinfo.io", "tar -xvjf postgresql-15.2.tar.bz2",
"curl -LO https://downloads.mongodb.com/compass/mongosh-2.3.8-linux-x64.tgz", "cd postgresql-15.2",
"tar -xvzf mongosh-2.3.8-linux-x64.tgz", "mkdir ~/pgsql",
"mkdir -p ~/mongosh && mv mongosh-2.3.8-linux-x64/* ~/mongosh/", "curl -LO https://ftp.gnu.org/gnu/readline/readline-8.1.tar.gz",
"echo 'export PATH=$PATH:$HOME/mongosh/bin' >> ~/.bashrc", "tar -xvzf readline-8.1.tar.gz",
"source ~/.bashrc", "cd readline-8.1",
"mongosh --version" "./configure --prefix=$HOME/pgsql"
], ];
[TerminalKind.VCoreMongo]: [ case TerminalKind.Mongo || TerminalKind.VCoreMongo:
"curl -s https://ipinfo.io", return [
"curl -LO https://downloads.mongodb.com/compass/mongosh-2.3.8-linux-x64.tgz", "curl -s https://ipinfo.io",
"tar -xvzf mongosh-2.3.8-linux-x64.tgz", "curl -LO https://downloads.mongodb.com/compass/mongosh-2.3.8-linux-x64.tgz",
"mkdir -p ~/mongosh && mv mongosh-2.3.8-linux-x64/* ~/mongosh/", "tar -xvzf mongosh-2.3.8-linux-x64.tgz",
"echo 'export PATH=$PATH:$HOME/mongosh/bin' >> ~/.bashrc", "mkdir -p ~/mongosh && mv mongosh-2.3.8-linux-x64/* ~/mongosh/",
"source ~/.bashrc", "echo 'export PATH=$PATH:$HOME/mongosh/bin' >> ~/.bashrc",
"mongosh --version" "source ~/.bashrc",
], "mongosh --version",
[TerminalKind.Cassandra]: [ `mongosh --host ${config.host} --port 10255 --username ${config.name} --password ${config.password} --ssl --sslAllowInvalidCertificates`
"curl -s https://ipinfo.io", ];
"curl -LO https://downloads.apache.org/cassandra/4.1.2/apache-cassandra-4.1.2-bin.tar.gz", case TerminalKind.Cassandra:
"tar -xvzf apache-cassandra-4.1.2-bin.tar.gz", return [
"cd apache-cassandra-4.1.2", "curl -s https://ipinfo.io",
"mkdir ~/cassandra", "curl -LO https://archive.apache.org/dist/cassandra/5.0.3/apache-cassandra-5.0.3-bin.tar.gz",
"echo 'export CASSANDRA_HOME=$HOME/cassandra' >> ~/.bashrc", "tar -xvzf apache-cassandra-5.0.3-bin.tar.gz",
"source ~/.bashrc" "mkdir -p ~/cassandra && mv apache-cassandra-5.0.3/* ~/cassandra/",
], "echo 'export PATH=$PATH:$HOME/cassandra/bin' >> ~/.bashrc",
[TerminalKind.Default]: [ "echo 'export SSL_VERSION=TLSv1_2' >> ~/.bashrc",
"echo Unknown Shell" "echo 'export SSL_VALIDATE=false' >> ~/.bashrc",
], "source ~/.bashrc",
`cqlsh ${config.host} 10350 -u ${config.name} -p ${config.password} --ssl --protocol-version=4`
];
default:
return ["echo Unknown Shell"];
}
}
const getHostFromUrl = (mongoEndpoint: string): string => {
try {
const url = new URL(mongoEndpoint);
return url.hostname;
} catch (error) {
console.error("Invalid Mongo Endpoint URL:", error);
return "";
}
}; };

View File

@ -51,7 +51,16 @@ export const putEphemeralUserSettings = async (userSubscriptionId: string, userR
preferredOsType: OsType.Linux, preferredOsType: OsType.Linux,
preferredShellType: ShellType.Bash, preferredShellType: ShellType.Bash,
preferredLocation: userRegion, preferredLocation: userRegion,
networkType: NetworkType.Default, terminalSettings: {
fontSize: "Medium",
fontStyle: "monospace"
},
vnetSettings: {
networkProfileResourceId: "/subscriptions/80be3961-0521-4a0a-8570-5cd5a4e2f98c/resourceGroups/neesharma-stage/providers/Microsoft.Network/networkProfiles/aci-networkProfile-eastus2",
relayNamespaceResourceId: "/subscriptions/80be3961-0521-4a0a-8570-5cd5a4e2f98c/resourceGroups/neesharma-stage/providers/Microsoft.Relay/namespaces/neesharma-stage-relay-namespace",
location: "eastus2"
},
networkType: NetworkType.Isolated,
sessionType: SessionType.Ephemeral, sessionType: SessionType.Ephemeral,
userSubscription: userSubscriptionId, userSubscription: userSubscriptionId,
} }

View File

@ -3,12 +3,15 @@
*/ */
import { Terminal } from "xterm"; import { Terminal } from "xterm";
import { TerminalKind } from "../../../Contracts/ViewModels";
import { userContext } from "../../../UserContext"; import { userContext } from "../../../UserContext";
import { AttachAddon } from "./AttachAddOn"; import { AttachAddon } from "./AttachAddOn";
import { getCommands } from "./Commands";
import { authorizeSession, connectTerminal, getNormalizedRegion, getUserSettings, provisionConsole, putEphemeralUserSettings, registerCloudShellProvider, validateUserSettings, verifyCloudshellProviderRegistration } from "./Data"; import { authorizeSession, connectTerminal, getNormalizedRegion, getUserSettings, provisionConsole, putEphemeralUserSettings, registerCloudShellProvider, validateUserSettings, verifyCloudshellProviderRegistration } from "./Data";
import { LogError, LogInfo } from "./LogFormatter"; import { LogError, LogInfo } from "./LogFormatter";
import { listKeys } from "Utils/arm/generatedClients/cosmos/databaseAccounts";
export const startCloudShellterminal = async (xterminal: Terminal, initCommands: string, authorizationToken: any) => { export const startCloudShellterminal = async (xterminal: Terminal, shellType: TerminalKind) => {
// validate that the subscription id is registered in the Cloudshell namespace // validate that the subscription id is registered in the Cloudshell namespace
try { try {
@ -43,9 +46,12 @@ export const startCloudShellterminal = async (xterminal: Terminal, initCommands:
return{}; return{};
} }
const socket = new WebSocket(socketUri); let socket = new WebSocket(socketUri);
configureSocket(socket, socketUri, xterminal, initCommands, 0); let keys = await listKeys(userContext.subscriptionId, userContext.resourceGroup, userContext.databaseAccount.name);
const initCommands = getCommands(shellType, keys.primaryMasterKey);
socket = configureSocket(socket, socketUri, xterminal, initCommands, 0);
const attachAddon = new AttachAddon(socket); const attachAddon = new AttachAddon(socket);
xterminal.loadAddon(attachAddon); xterminal.loadAddon(attachAddon);