-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcall.mjs
executable file
·214 lines (185 loc) · 7.75 KB
/
call.mjs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env node
import { call, generateEnvs, _checkDeeplinksStatus, _generateAndFillEnvs } from './imports/engine-server.js';
import commandLineArgs from 'command-line-args';
import repl from 'repl';
import { DeepClient } from './imports/client.js';
import { generateApolloClient } from '@deep-foundation/hasura/client.js';
import { exec, spawn } from 'child_process';
import crypto from 'crypto';
import https from 'https';
import fs from 'fs';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import axios from 'axios';
import { promisify } from 'util';
import pckg from './package.json' with { type: "json" };
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const isGitpod = !!process?.env?.GITPOD_WORKSPACE_ID;
const execP = promisify(exec);
// const __filename = fileURLToPath(import.meta.url);
// const cwd = dirname(__filename);
const cwd = process.cwd();
function generateRandomKey(length) {
return crypto.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
}
async function getPublicIP() {
return (await axios.get('https://api.ipify.org'))?.data;
}
async function gitpodUrl(port) {
const r = await execP(`echo $(gp url ${port})`);
return r.stdout.replace('\n','')
}
// const DEEPLINKS_CALL_OPTIONS = process.env.DEEPLINKS_CALL_OPTIONS || '{ "operation": "run" }';
const DEEPLINKS_CALL_OPTIONS = process.env.DEEPLINKS_CALL_OPTIONS;
const _exec = (command) => {
const bash = exec(command, (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
bash.stdout.on('data', function (data) {
console.log(data.toString());
});
bash.stderr.on('data', function (data) {
console.log(data.toString());
});
bash.on('exit', function (code) {
console.log('child process exited with code: ' + code.toString());
});
};
const optionDefinitions = [
{ name: 'config', type: String },
{ name: 'exec', type: Boolean },
{ name: 'up', type: Boolean },
{ name: 'down', type: Boolean },
{ name: 'snapshot', type: Boolean }, // restore from snapshot
{ name: 'bash', type: String },
{ name: 'migrate', type: Boolean }, // migrate
{ name: 'unmigrate', type: Boolean }, // unmigrate
{ name: 'generate', type: Boolean },
{ name: 'deeplinks', type: String },
{ name: 'perception', type: String },
{ name: 'ssl', type: Boolean },
{ name: 'envs', type: Boolean },
{ name: 'force', type: Boolean },
{ name: 'localhost', type: Boolean },
];
const options = commandLineArgs(optionDefinitions);
console.log(pckg.name, pckg.version);
if (options.generate && (options.force || !fs.existsSync(`${cwd}/deep.config.json`))) {
const hasuraKey = generateRandomKey(32);
const postgresKey = 'd2ef4e87ecc262ff4615887006d8b7b4'; // generateRandomKey(32);
const minioAccess = generateRandomKey(32);
const minioSecret = generateRandomKey(32);
// const jwt_secret = JSON.stringify(jwt({ secret: hasuraKey })).replace('"', '\"');
let publicIp;
try {
publicIp = await getPublicIP();
} catch(e) {}
const deeplinks = options.deeplinks || `http://${publicIp}:3006`;
const perception = options.perception || `http://${publicIp}:3007`;
if ((!options.deeplinks || !options.perception) && !publicIp) throw new Error(`--perception and --deeplinks must be defined, or publicIp available`);
const jwtSecret = `'{\"type\":\"HS256\",\"key\":\"${crypto.randomBytes(50).toString('base64')}\"}'`;
const generated = {
"operation": "run",
"envs": {
"DEEPLINKS_PUBLIC_URL": isGitpod ? await gitpodUrl(3006) : `${deeplinks}`,
"NEXT_PUBLIC_DEEPLINKS_URL": isGitpod ? await gitpodUrl(3006) : `${deeplinks}`,
"NEXT_PUBLIC_GQL_PATH": isGitpod ? await gitpodUrl(3006)+'/gql' : `${deeplinks}/gql`,
"NEXT_PUBLIC_GQL_SSL": isGitpod ? "1" : "0",
"NEXT_PUBLIC_DEEPLINKS_SERVER": isGitpod ? await gitpodUrl(3007) : `${perception}`,
"NEXT_PUBLIC_ENGINES_ROUTE": "0",
"NEXT_PUBLIC_DISABLE_CONNECTOR": "1",
"JWT_SECRET": jwtSecret,
"HASURA_GRAPHQL_JWT_SECRET": jwtSecret,
"DEEP_HASURA_GRAPHQL_JWT_SECRET": jwtSecret,
"DEEPLINKS_HASURA_STORAGE_URL": "http://deep-hasura-storage:8000/",
"HASURA_ENDPOINT": "http://deep-hasura:8080/v1",
"DEEPLINKS_HASURA_PATH": "deep-hasura:8080",
"DOCKER_GQL_URL": "http://deep-links:3006/gql",
"DOCKER_DEEPLINKS_URL": "http://deep-links:3006",
"MIGRATIONS_DEEPLINKS_URL": "http://deep-links:3006",
"HASURA_GRAPHQL_ADMIN_SECRET": hasuraKey,
"MIGRATIONS_HASURA_SECRET": hasuraKey,
"DEEPLINKS_HASURA_SECRET": hasuraKey,
"POSTGRES_USER": 'postgres',
"POSTGRES_PASSWORD": postgresKey,
"HASURA_GRAPHQL_DATABASE_URL": `postgres://postgres:${postgresKey}@deep-postgres:5432/postgres?sslmode=disable`,
'DEEP_HASURA_GRAPHQL_LOG_LEVEL': 'error',
"POSTGRES_MIGRATIONS_SOURCE": `postgres://postgres:${postgresKey}@deep-postgres:5432/postgres?sslmode=disable`,
"RESTORE_VOLUME_FROM_SNAPSHOT": '0',
"MANUAL_MIGRATIONS": '0',
// "RESTORE_VOLUME_FROM_SNAPSHOT": options.last || isGitpod ? '1': '0',
// "MANUAL_MIGRATIONS": options.migrate ? '1': '0',
"MINIO_ROOT_USER": minioAccess,
"MINIO_ROOT_PASSWORD": minioSecret,
"S3_ACCESS_KEY": minioAccess,
"S3_SECRET_KEY": minioSecret,
}
};
console.log(generated);
fs.writeFileSync(cwd+'/deep.config.json', JSON.stringify(generated, null, 2));
}
(async() => {
let deepConfig;
try {
deepConfig = JSON.parse(fs.readFileSync(cwd+'/deep.config.json', { encoding: 'utf8' }));
} catch(e) {}
if (!options.config && !deepConfig) {
console.log(`${cwd}/deep.config.json or -c "$(cat your/path/to/deep.config.json)" is not defined`);
return;
}
const config = deepConfig || JSON.parse(options.config || DEEPLINKS_CALL_OPTIONS);
if (options.localhost) {
config.envs.DEEPLINKS_PUBLIC_URL = 'http://localhost:3006'
config.envs.NEXT_PUBLIC_DEEPLINKS_URL = 'http://localhost:3006'
config.envs.NEXT_PUBLIC_GQL_PATH = 'http://localhost:3006/gql'
}
console.log('config', config);
if (config && options.down) {
_exec(`cd ${__dirname} && docker compose -p deep down`);
}
if (config && options.up) {
await call(config);
}
const envs = generateEnvs({ envs: { ...(config?.envs || {}) }, isDeeplinksDocker: 0 });
const envsStr = _generateAndFillEnvs({ envs, isDeeplinksDocker: 0 })
if (options.envs) {
console.log('envs', envs);
console.log('ENVS', envsStr);
}
if (options.snapshot) {
if (options.up) await execP(`npx -y -q wait-on --timeout 100000 ${envs.NEXT_PUBLIC_GQL_PATH}`);
_exec(`${envsStr} cd ${__dirname} && docker compose -p deep stop hasura postgres storage minio && (docker exec deep-links sh -c "npm run snapshot:last" || true) && docker compose -p deep start hasura postgres storage minio`);
}
if (options.migrate) {
await execP(`npx -y -q wait-on --timeout 100000 ${envs.NEXT_PUBLIC_GQL_PATH}`);
_exec(`${envsStr} cd ${__dirname} && npm run migrate`);
}
if (options.unmigrate) {
await execP(`npx -y -q wait-on --timeout 100000 ${envs.NEXT_PUBLIC_GQL_PATH}`);
_exec(`${envsStr} cd ${__dirname} && npm run unmigrate`);
}
if (options.exec) {
await execP(`npx -y -q wait-on --timeout 100000 ${envs.NEXT_PUBLIC_GQL_PATH}`);
const start = envs.NEXT_PUBLIC_GQL_PATH.indexOf('://') + 3;
const path = envs.NEXT_PUBLIC_GQL_PATH.slice(start);
const deep = new DeepClient({
apolloClient: generateApolloClient({
path: path,
ssl: !!+envs.DEEPLINKS_HASURA_SSL,
secret: envs.DEEPLINKS_HASURA_SECRET,
}),
})
const r = repl.start('> ');
r.context.__dirname = __dirname;
r.context.config = config;
r.context.deep = deep;
}
if (options.bash) {
_exec(`${envsStr} cd ${__dirname} && ${options.bash}`);
}
})()