-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclient_loader.ts
More file actions
76 lines (69 loc) · 2.46 KB
/
client_loader.ts
File metadata and controls
76 lines (69 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import fs from "fs";
import path from "path";
import { env } from "process";
import { ClientOptions, GridClient, NetworkEnv } from "../src";
const network = env.NETWORK as NetworkEnv;
const mnemonic = env.MNEMONIC;
const storeSecret = env.STORE_SECRET;
const ssh_key = env.SSH_KEY;
let config: { ssh_key: string } & ClientOptions;
setConfig();
/**
* Loads configuration either from environment variables or from a config.json file.
* If environment variables are not fully set, it falls back to loading from the config.json file.
*/
function setConfig() {
if (network === undefined || mnemonic === undefined || ssh_key === undefined) {
console.log("Credentials not all found in env variables. Loading all credentials from default config.json...");
config = JSON.parse(fs.readFileSync(path.join(__dirname, "./config.json"), "utf-8"));
} else {
console.log("Credentials loaded from env variables...");
config = {
network: network,
mnemonic: mnemonic,
storeSecret: storeSecret,
ssh_key: ssh_key,
};
}
}
/**
* Function to instantiate and connect a GridClient with a specified project name.
*
* The project name is important to list your deployments.
* @example
*
* const deploymentName = 'testVM'
* const projectName = 'vm/' + deploymentName
* const gc = await getClient(projectName)
* await gc.machines.deploy({...});
* // When you deploy using this project name, you'll only be able to list your deployments using this same project name.
* await gc.machines.getObj(deploymentName);
*
* @param projectName - Optional project name to use with the GridClient instance.
* @returns {Promise<GridClient>} - A promise that resolves to a connected GridClient instance.
*/
async function getClient(projectName = ""): Promise<GridClient> {
const gridClient = new GridClient({
network: config.network,
mnemonic: config.mnemonic,
storeSecret: config.storeSecret,
projectName,
});
await gridClient.connect();
return gridClient;
}
/**
* Function to instantiate and connect a new GridClient using the existing mnemonic as the store secret.
*
* @returns {Promise<GridClient>} - A promise that resolves to a connected GridClient instance.
*/
async function getNewClient(): Promise<GridClient> {
const gridClient = new GridClient({
network: config.network,
mnemonic: config.mnemonic,
storeSecret: config.mnemonic,
});
await gridClient.connect();
return gridClient;
}
export { config, getClient, getNewClient };