Skip to content

Commit 44e8564

Browse files
committed
add other db support
1 parent ec89167 commit 44e8564

File tree

4 files changed

+106
-54
lines changed

4 files changed

+106
-54
lines changed

src/Explorer/Tabs/CloudShellTab/CloudShellTabComponent.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { Terminal } from "xterm";
33
import { FitAddon } from 'xterm-addon-fit';
44
import "xterm/css/xterm.css";
55
import { TerminalKind } from "../../../Contracts/ViewModels";
6-
import { getAuthorizationHeader } from "../../../Utils/AuthorizationUtils";
7-
import { getCommands } from "./Commands";
86
import { startCloudShellterminal } from "./UseTerminal";
97

108
export interface CloudShellTerminalProps {
@@ -39,8 +37,7 @@ export const CloudShellTerminalComponent: React.FC<CloudShellTerminalProps> = ({
3937
const handleResize = () => fitAddon.fit();
4038
window.addEventListener('resize', handleResize);
4139

42-
const authorizationHeader = getAuthorizationHeader()
43-
socketRef.current = startCloudShellterminal(term, getCommands(shellType), authorizationHeader.token);
40+
socketRef.current = startCloudShellterminal(term, shellType);
4441

4542
term.onData((data) => {
4643
if (socketRef.current && socketRef.current.readyState === WebSocket.OPEN) {

src/Explorer/Tabs/CloudShellTab/Commands.tsx

+86-46
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,94 @@
33
*/
44

55
import { TerminalKind } from "../../../Contracts/ViewModels";
6+
import { userContext } from "../../../UserContext";
67

7-
export const getCommands = (terminalKind: TerminalKind): string => {
8-
if (!Commands[terminalKind]) {
9-
throw new Error(`Unsupported terminal kind: ${terminalKind}`);
8+
export const getCommands = (terminalKind: TerminalKind, key: string) => {
9+
let databaseacc = userContext.databaseAccount;
10+
let endpoint;
11+
switch (terminalKind) {
12+
case TerminalKind.Postgres:
13+
endpoint = databaseacc.properties.postgresqlEndpoint;
14+
break;
15+
case TerminalKind.Mongo:
16+
endpoint = databaseacc.properties.mongoEndpoint;
17+
break;
18+
case TerminalKind.VCoreMongo:
19+
endpoint = databaseacc.properties.vcoreMongoEndpoint;
20+
break;
21+
case TerminalKind.Cassandra:
22+
endpoint = databaseacc.properties.cassandraEndpoint;
23+
break;
24+
default:
25+
throw new Error("Unknown Terminal Kind");
1026
}
11-
return Commands[terminalKind].join("\n").concat("\n");
27+
28+
let config = {
29+
host: getHostFromUrl(endpoint),
30+
name: databaseacc.name,
31+
password: key,
32+
endpoint: endpoint
33+
};
34+
35+
return commands(terminalKind, config).join("\n").concat("\n");
1236
};
1337

14-
export const Commands: Record<TerminalKind, string[]> = {
15-
[TerminalKind.Postgres]: [
16-
"curl -s https://ipinfo.io",
17-
"curl -LO https://ftp.postgresql.org/pub/source/v15.2/postgresql-15.2.tar.bz2",
18-
"tar -xvjf postgresql-15.2.tar.bz2",
19-
"cd postgresql-15.2",
20-
"mkdir ~/pgsql",
21-
"curl -LO https://ftp.gnu.org/gnu/readline/readline-8.1.tar.gz",
22-
"tar -xvzf readline-8.1.tar.gz",
23-
"cd readline-8.1",
24-
"./configure --prefix=$HOME/pgsql"
25-
],
26-
[TerminalKind.Mongo]: [
27-
"curl -s https://ipinfo.io",
28-
"curl -LO https://downloads.mongodb.com/compass/mongosh-2.3.8-linux-x64.tgz",
29-
"tar -xvzf mongosh-2.3.8-linux-x64.tgz",
30-
"mkdir -p ~/mongosh && mv mongosh-2.3.8-linux-x64/* ~/mongosh/",
31-
"echo 'export PATH=$PATH:$HOME/mongosh/bin' >> ~/.bashrc",
32-
"source ~/.bashrc",
33-
"mongosh --version"
34-
],
35-
[TerminalKind.VCoreMongo]: [
36-
"curl -s https://ipinfo.io",
37-
"curl -LO https://downloads.mongodb.com/compass/mongosh-2.3.8-linux-x64.tgz",
38-
"tar -xvzf mongosh-2.3.8-linux-x64.tgz",
39-
"mkdir -p ~/mongosh && mv mongosh-2.3.8-linux-x64/* ~/mongosh/",
40-
"echo 'export PATH=$PATH:$HOME/mongosh/bin' >> ~/.bashrc",
41-
"source ~/.bashrc",
42-
"mongosh --version"
43-
],
44-
[TerminalKind.Cassandra]: [
45-
"curl -s https://ipinfo.io",
46-
"curl -LO https://downloads.apache.org/cassandra/4.1.2/apache-cassandra-4.1.2-bin.tar.gz",
47-
"tar -xvzf apache-cassandra-4.1.2-bin.tar.gz",
48-
"cd apache-cassandra-4.1.2",
49-
"mkdir ~/cassandra",
50-
"echo 'export CASSANDRA_HOME=$HOME/cassandra' >> ~/.bashrc",
51-
"source ~/.bashrc"
52-
],
53-
[TerminalKind.Default]: [
54-
"echo Unknown Shell"
55-
],
38+
export interface CommandConfig {
39+
host: string,
40+
name: string,
41+
password: string,
42+
endpoint: string
43+
}
44+
45+
export const commands = (terminalKind: TerminalKind, config?: CommandConfig): string[] => {
46+
switch (terminalKind) {
47+
case TerminalKind.Postgres:
48+
return [
49+
"curl -s https://ipinfo.io",
50+
"curl -LO https://ftp.postgresql.org/pub/source/v15.2/postgresql-15.2.tar.bz2",
51+
"tar -xvjf postgresql-15.2.tar.bz2",
52+
"cd postgresql-15.2",
53+
"mkdir ~/pgsql",
54+
"curl -LO https://ftp.gnu.org/gnu/readline/readline-8.1.tar.gz",
55+
"tar -xvzf readline-8.1.tar.gz",
56+
"cd readline-8.1",
57+
"./configure --prefix=$HOME/pgsql"
58+
];
59+
case TerminalKind.Mongo || TerminalKind.VCoreMongo:
60+
return [
61+
"curl -s https://ipinfo.io",
62+
"curl -LO https://downloads.mongodb.com/compass/mongosh-2.3.8-linux-x64.tgz",
63+
"tar -xvzf mongosh-2.3.8-linux-x64.tgz",
64+
"mkdir -p ~/mongosh && mv mongosh-2.3.8-linux-x64/* ~/mongosh/",
65+
"echo 'export PATH=$PATH:$HOME/mongosh/bin' >> ~/.bashrc",
66+
"source ~/.bashrc",
67+
"mongosh --version",
68+
`mongosh --host ${config.host} --port 10255 --username ${config.name} --password ${config.password} --ssl --sslAllowInvalidCertificates`
69+
];
70+
case TerminalKind.Cassandra:
71+
return [
72+
"curl -s https://ipinfo.io",
73+
"curl -LO https://archive.apache.org/dist/cassandra/5.0.3/apache-cassandra-5.0.3-bin.tar.gz",
74+
"tar -xvzf apache-cassandra-5.0.3-bin.tar.gz",
75+
"mkdir -p ~/cassandra && mv apache-cassandra-5.0.3/* ~/cassandra/",
76+
"echo 'export PATH=$PATH:$HOME/cassandra/bin' >> ~/.bashrc",
77+
"echo 'export SSL_VERSION=TLSv1_2' >> ~/.bashrc",
78+
"echo 'export SSL_VALIDATE=false' >> ~/.bashrc",
79+
"source ~/.bashrc",
80+
81+
`cqlsh ${config.host} 10350 -u ${config.name} -p ${config.password} --ssl --protocol-version=4`
82+
];
83+
default:
84+
return ["echo Unknown Shell"];
85+
}
86+
}
87+
88+
const getHostFromUrl = (mongoEndpoint: string): string => {
89+
try {
90+
const url = new URL(mongoEndpoint);
91+
return url.hostname;
92+
} catch (error) {
93+
console.error("Invalid Mongo Endpoint URL:", error);
94+
return "";
95+
}
5696
};

src/Explorer/Tabs/CloudShellTab/Data.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ export const putEphemeralUserSettings = async (userSubscriptionId: string, userR
5151
preferredOsType: OsType.Linux,
5252
preferredShellType: ShellType.Bash,
5353
preferredLocation: userRegion,
54-
networkType: NetworkType.Default,
54+
terminalSettings: {
55+
fontSize: "Medium",
56+
fontStyle: "monospace"
57+
},
58+
vnetSettings: {
59+
networkProfileResourceId: "/subscriptions/80be3961-0521-4a0a-8570-5cd5a4e2f98c/resourceGroups/neesharma-stage/providers/Microsoft.Network/networkProfiles/aci-networkProfile-eastus2",
60+
relayNamespaceResourceId: "/subscriptions/80be3961-0521-4a0a-8570-5cd5a4e2f98c/resourceGroups/neesharma-stage/providers/Microsoft.Relay/namespaces/neesharma-stage-relay-namespace",
61+
location: "eastus2"
62+
},
63+
networkType: NetworkType.Isolated,
5564
sessionType: SessionType.Ephemeral,
5665
userSubscription: userSubscriptionId,
5766
}

src/Explorer/Tabs/CloudShellTab/UseTerminal.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
*/
44

55
import { Terminal } from "xterm";
6+
import { TerminalKind } from "../../../Contracts/ViewModels";
67
import { userContext } from "../../../UserContext";
78
import { AttachAddon } from "./AttachAddOn";
9+
import { getCommands } from "./Commands";
810
import { authorizeSession, connectTerminal, getNormalizedRegion, getUserSettings, provisionConsole, putEphemeralUserSettings, registerCloudShellProvider, validateUserSettings, verifyCloudshellProviderRegistration } from "./Data";
911
import { LogError, LogInfo } from "./LogFormatter";
12+
import { listKeys } from "Utils/arm/generatedClients/cosmos/databaseAccounts";
1013

11-
export const startCloudShellterminal = async (xterminal: Terminal, initCommands: string, authorizationToken: any) => {
14+
export const startCloudShellterminal = async (xterminal: Terminal, shellType: TerminalKind) => {
1215

1316
// validate that the subscription id is registered in the Cloudshell namespace
1417
try {
@@ -43,9 +46,12 @@ export const startCloudShellterminal = async (xterminal: Terminal, initCommands:
4346
return{};
4447
}
4548

46-
const socket = new WebSocket(socketUri);
49+
let socket = new WebSocket(socketUri);
4750

48-
configureSocket(socket, socketUri, xterminal, initCommands, 0);
51+
let keys = await listKeys(userContext.subscriptionId, userContext.resourceGroup, userContext.databaseAccount.name);
52+
53+
const initCommands = getCommands(shellType, keys.primaryMasterKey);
54+
socket = configureSocket(socket, socketUri, xterminal, initCommands, 0);
4955

5056
const attachAddon = new AttachAddon(socket);
5157
xterminal.loadAddon(attachAddon);

0 commit comments

Comments
 (0)