-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathsetup.ts
More file actions
executable file
·322 lines (275 loc) · 7.86 KB
/
setup.ts
File metadata and controls
executable file
·322 lines (275 loc) · 7.86 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env bun
import { copyFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { $ } from "bun";
import chalk from "chalk";
// eslint-disable-next-line @typescript-eslint/no-require-imports
const Docker = require("dockerode");
const isWindows = process.platform === "win32";
const docker = new Docker();
function log(message: string) {
console.log(message);
}
function logStep(step: number, total: number, message: string) {
log(`\n${chalk.cyan(`[${step}/${total}]`)} ${chalk.cyan(message)}`);
}
async function checkCommand(command: string): Promise<boolean> {
try {
if (isWindows) {
await $`where ${command}`.quiet();
} else {
await $`which ${command}`.quiet();
}
return true;
} catch {
return false;
}
}
async function checkDockerRunning(): Promise<boolean> {
try {
await $`docker info`.quiet();
return true;
} catch {
return false;
}
}
async function checkContainerHealth(containerName: string): Promise<boolean> {
try {
const container = docker.getContainer(containerName);
const inspect = await container.inspect();
// Check health status if healthcheck is configured
if (inspect.State.Health) {
return inspect.State.Health.Status === "healthy";
}
// Fall back to checking if container is running
return inspect.State.Status === "running";
} catch {
// Container might not exist yet or inspect failed
return false;
}
}
async function waitForService(
service: string,
containerName: string,
maxAttempts = 60
): Promise<boolean> {
log(chalk.yellow(`Waiting for ${service} to be ready...`));
// First, wait for container to exist
let containerExists = false;
for (let i = 0; i < 10; i++) {
try {
const container = docker.getContainer(containerName);
await container.inspect();
containerExists = true;
break;
} catch {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
if (!containerExists) {
log(
chalk.yellow(
`⚠️ Container ${containerName} not found, continuing anyway...`
)
);
return false;
}
// Now wait for health check
for (let i = 0; i < maxAttempts; i++) {
const isHealthy = await checkContainerHealth(containerName);
if (isHealthy) {
log(chalk.green(`${service} is ready!`));
return true;
}
// Show progress every 5 seconds
if (i > 0 && i % 5 === 0) {
log(chalk.gray(` Still waiting... (${i * 2}s)`));
}
await new Promise((resolve) => setTimeout(resolve, 2000));
}
return false;
}
async function main() {
log(chalk.bold("🚀 Databuddy Setup Script"));
log(chalk.cyan("=".repeat(50)));
const steps = [
"Check prerequisites",
"Install dependencies",
"Set up environment variables",
"Start Docker services",
"Wait for services to be ready",
"Set up database schema",
"Initialize ClickHouse",
"Build SDK",
];
let currentStep = 0;
// Step 1: Check prerequisites
currentStep += 1;
logStep(currentStep, steps.length, steps[0] ?? "");
const hasDocker = await checkCommand("docker");
const hasDockerCompose =
(await checkCommand("docker-compose")) ||
(await checkCommand("docker compose"));
if (!hasDocker) {
log(chalk.red("❌ Docker is not installed. Please install Docker first."));
process.exit(1);
}
if (!hasDockerCompose) {
log(
chalk.red(
"❌ docker-compose is not installed. Please install docker-compose first."
)
);
process.exit(1);
}
const dockerRunning = await checkDockerRunning();
if (!dockerRunning) {
log(
chalk.yellow(
"⚠️ Docker daemon is not running. Please start Docker Desktop."
)
);
log(chalk.yellow(" The script will attempt to start services anyway..."));
}
log(chalk.green("✅ Prerequisites check passed"));
// Step 2: Install dependencies
currentStep += 1;
logStep(currentStep, steps.length, steps[1] ?? "");
log(chalk.blue("Running: bun install"));
try {
await $`bun install`;
log(chalk.green("✅ Dependencies installed successfully"));
} catch (error) {
log(chalk.red(`❌ Failed to install dependencies: ${String(error)}`));
process.exit(1);
}
// Step 3: Set up environment variables
currentStep += 1;
logStep(currentStep, steps.length, steps[2] ?? "");
const envExamplePath = join(process.cwd(), ".env.example");
const envPath = join(process.cwd(), ".env");
if (!existsSync(envExamplePath)) {
log(chalk.yellow("⚠️ .env.example not found, skipping environment setup"));
} else if (existsSync(envPath)) {
log(chalk.yellow("⚠️ .env already exists, skipping copy"));
} else {
try {
copyFileSync(envExamplePath, envPath);
log(chalk.green("✅ Created .env from .env.example"));
log(
chalk.yellow("⚠️ Please review and update .env with your configuration")
);
} catch (error) {
log(chalk.red(`❌ Failed to create .env: ${String(error)}`));
process.exit(1);
}
}
// Step 4: Start Docker services
currentStep += 1;
logStep(currentStep, steps.length, steps[3] ?? "");
const dockerComposeCmd = (await checkCommand("docker-compose"))
? "docker-compose"
: "docker compose";
log(chalk.blue(`Running: ${dockerComposeCmd} up -d`));
try {
await $`${dockerComposeCmd} up -d`;
log(chalk.green("✅ Docker services started"));
} catch (error) {
log(chalk.red(`❌ Failed to start Docker services: ${String(error)}`));
log(chalk.yellow(" Make sure Docker is running and try again"));
process.exit(1);
}
// Step 5: Wait for services to be ready
currentStep += 1;
logStep(currentStep, steps.length, steps[4] ?? "");
const postgresReady = await waitForService(
"PostgreSQL",
"databuddy-postgres"
);
if (!postgresReady) {
log(
chalk.yellow(
"⚠️ PostgreSQL did not become ready in time, continuing anyway..."
)
);
}
const clickhouseReady = await waitForService(
"ClickHouse",
"databuddy-clickhouse"
);
if (!clickhouseReady) {
log(
chalk.yellow(
"⚠️ ClickHouse did not become ready in time, continuing anyway..."
)
);
}
const redisReady = await waitForService("Redis", "databuddy-redis");
if (!redisReady) {
log(
chalk.yellow(
"⚠️ Redis did not become ready in time, continuing anyway..."
)
);
}
// Step 6: Set up database schema
currentStep += 1;
logStep(currentStep, steps.length, steps[5] ?? "");
log(chalk.blue("Running: bun run db:push"));
try {
await $`bun run db:push`;
log(chalk.green("✅ Database schema applied"));
} catch (error) {
log(chalk.red(`❌ Failed to apply database schema: ${String(error)}`));
log(
chalk.yellow(
" You may need to wait a bit longer for PostgreSQL to be ready"
)
);
process.exit(1);
}
// Step 7: Initialize ClickHouse
currentStep += 1;
logStep(currentStep, steps.length, steps[6] ?? "");
log(chalk.blue("Running: bun run clickhouse:init"));
try {
await $`bun run clickhouse:init`;
log(chalk.green("✅ ClickHouse initialized"));
} catch (error) {
log(chalk.red(`❌ Failed to initialize ClickHouse: ${String(error)}`));
log(
chalk.yellow(
" You may need to wait a bit longer for ClickHouse to be ready"
)
);
process.exit(1);
}
// Step 8: Build SDK
currentStep += 1;
logStep(currentStep, steps.length, steps[7] ?? "");
log(chalk.blue("Running: bun run sdk:build"));
try {
await $`bun run sdk:build`;
log(chalk.green("✅ SDK built successfully"));
} catch (error) {
log(chalk.red(`❌ Failed to build SDK: ${String(error)}`));
process.exit(1);
}
// Success!
log(chalk.green(`\n${"=".repeat(50)}`));
log(chalk.green("✅ Setup completed successfully!"));
log(chalk.green("=".repeat(50)));
log(chalk.bold("\nNext steps:"));
log(chalk.cyan(" 1. Review and update your .env file if needed"));
log(chalk.cyan(" 2. Run 'bun run dev' to start development servers"));
log(
chalk.cyan(
" 3. (Optional) Run 'bun run db:seed <WEBSITE_ID> [DOMAIN] [EVENT_COUNT]' to seed sample data"
)
);
log(chalk.bold("\nHappy coding! 🎉"));
}
main().catch((error) => {
log(chalk.red(`\n❌ Setup failed: ${String(error)}`));
process.exit(1);
});