Repository navigation
Expand file tree
/
Copy pathinit.sh
More file actions
executable file
·312 lines (276 loc) · 9.71 KB
/
Copy pathinit.sh
File metadata and controls
executable file
·312 lines (276 loc) · 9.71 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
#!/bin/bash
# CoreStack Initialization Script
# Supports: Linux, MacOS, with/without Docker, online/offline environments
# Usage: ./init.sh [--docker|--no-docker] [--offline]
set -e
# Resolve paths relative to the repository, including calls from another directory.
cd "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Default options
USE_DOCKER=""
OFFLINE_MODE=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--docker)
USE_DOCKER=true
shift
;;
--no-docker)
USE_DOCKER=false
shift
;;
--offline)
OFFLINE_MODE=true
shift
;;
--help)
echo "CoreStack Initialization Script"
echo ""
echo "Usage: ./init.sh [OPTIONS]"
echo ""
echo "Options:"
echo " --docker Use Docker for PostgreSQL, Redis, and Temporal"
echo " --no-docker Use local installations (manual setup required)"
echo " --offline Offline mode (skip online checks, use npm cache)"
echo " --help Show this help message"
echo ""
echo "Examples:"
echo " ./init.sh --docker # Auto-detect, prefer Docker"
echo " ./init.sh --no-docker # Use local services"
echo " ./init.sh --docker --offline # Docker setup in offline environment"
exit 0
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
echo "Use --help for usage information"
exit 1
;;
esac
done
echo -e "${BLUE}╔════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ CoreStack Initialization Script ║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════════╝${NC}"
echo ""
# Detect OS
detect_os() {
echo -e "${BLUE}[1/8] Detecting operating system...${NC}"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
OS="linux"
echo -e "${GREEN}✓ Detected: Linux${NC}"
elif [[ "$OSTYPE" == "darwin"* ]]; then
OS="macos"
echo -e "${GREEN}✓ Detected: MacOS${NC}"
else
echo -e "${RED}✗ Unsupported OS: $OSTYPE${NC}"
echo -e "${YELLOW} This script supports Linux and MacOS only${NC}"
exit 1
fi
echo ""
}
# Check Node.js and npm
check_node() {
echo -e "${BLUE}[2/8] Checking Node.js and npm...${NC}"
if ! command -v node &> /dev/null; then
echo -e "${RED}✗ Node.js is not installed${NC}"
echo -e "${YELLOW} Please install Node.js 18+ from https://nodejs.org${NC}"
exit 1
fi
NODE_VERSION=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
if [ "$NODE_VERSION" -lt 18 ]; then
echo -e "${RED}✗ Node.js version $NODE_VERSION is too old${NC}"
echo -e "${YELLOW} Please upgrade to Node.js 18 or higher${NC}"
exit 1
fi
if ! command -v npm &> /dev/null; then
echo -e "${RED}✗ npm is not installed${NC}"
exit 1
fi
echo -e "${GREEN}✓ Node.js $(node -v)${NC}"
echo -e "${GREEN}✓ npm $(npm -v)${NC}"
echo ""
}
# Auto-detect Docker availability if not specified
detect_docker() {
echo -e "${BLUE}[3/8] Checking Docker availability...${NC}"
if [ -z "$USE_DOCKER" ]; then
if command -v docker &> /dev/null && docker ps &> /dev/null; then
USE_DOCKER=true
echo -e "${GREEN}✓ Docker is available and running${NC}"
echo -e "${YELLOW} Will use Docker for services (PostgreSQL, Redis, Temporal)${NC}"
else
USE_DOCKER=false
echo -e "${YELLOW}⚠ Docker not available or not running${NC}"
echo -e "${YELLOW} Will use local services (requires manual setup)${NC}"
fi
else
if [ "$USE_DOCKER" = true ]; then
if ! command -v docker &> /dev/null; then
echo -e "${RED}✗ Docker not found but --docker was specified${NC}"
exit 1
fi
if ! docker ps &> /dev/null; then
echo -e "${RED}✗ Docker daemon not running${NC}"
echo -e "${YELLOW} Please start Docker and try again${NC}"
exit 1
fi
echo -e "${GREEN}✓ Docker is available and running${NC}"
else
echo -e "${YELLOW}⚠ Using local services (--no-docker specified)${NC}"
fi
fi
echo ""
}
# Install dependencies
install_dependencies() {
echo -e "${BLUE}[4/8] Installing dependencies...${NC}"
if [ "$OFFLINE_MODE" = true ]; then
echo -e "${YELLOW}⚠ Offline mode: Using npm cache only${NC}"
npm install --offline --prefer-offline
echo -e "${GREEN}✓ Dependencies installed from cache${NC}"
else
npm install
echo -e "${GREEN}✓ Dependencies installed${NC}"
fi
echo ""
}
# Setup environment variables
setup_env() {
echo -e "${BLUE}[5/8] Setting up environment variables...${NC}"
if [ -f .env ]; then
echo -e "${YELLOW}⚠ .env file already exists${NC}"
read -p " Overwrite? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW} Skipping .env setup${NC}"
echo ""
return
fi
fi
# Copy template
cp .env.example .env
# Generate secure secrets
JWT_SECRET=$(openssl rand -base64 32 2>/dev/null || node -e "console.log(require('crypto').randomBytes(32).toString('base64'))")
SSH_KEY=$(openssl rand -hex 32 2>/dev/null || node -e "console.log(require('crypto').randomBytes(32).toString('hex'))")
# Update .env file
if [[ "$OS" == "macos" ]]; then
sed -i '' "s|JWT_SECRET=.*|JWT_SECRET=$JWT_SECRET|" .env
sed -i '' "s|SSH_ENCRYPTION_KEY=.*|SSH_ENCRYPTION_KEY=$SSH_KEY|" .env
else
sed -i "s|JWT_SECRET=.*|JWT_SECRET=$JWT_SECRET|" .env
sed -i "s|SSH_ENCRYPTION_KEY=.*|SSH_ENCRYPTION_KEY=$SSH_KEY|" .env
fi
echo -e "${GREEN}✓ Environment file created (.env)${NC}"
echo -e "${GREEN}✓ Generated secure JWT_SECRET${NC}"
echo -e "${GREEN}✓ Generated secure SSH_ENCRYPTION_KEY${NC}"
echo ""
}
# Start services
start_services() {
echo -e "${BLUE}[6/8] Starting services...${NC}"
if [ "$USE_DOCKER" = true ]; then
echo -e "${YELLOW} Starting Docker services (PostgreSQL, Redis, Temporal)...${NC}"
docker compose version > /dev/null
if [ "$OFFLINE_MODE" = true ]; then
docker compose up -d --pull never
else
docker compose up -d
fi
# Wait for services to be ready
echo -e "${YELLOW} Waiting for services to be ready...${NC}"
sleep 5
# Check PostgreSQL
for i in {1..30}; do
if docker compose exec -T postgres pg_isready -U postgres &> /dev/null; then
echo -e "${GREEN}✓ PostgreSQL is ready${NC}"
break
fi
if [ $i -eq 30 ]; then
echo -e "${RED}✗ PostgreSQL failed to start${NC}"
exit 1
fi
sleep 1
done
# Check Redis
if docker compose exec -T redis redis-cli ping &> /dev/null; then
echo -e "${GREEN}✓ Redis is ready${NC}"
fi
echo -e "${GREEN}✓ Temporal Server is starting (port 7233)${NC}"
echo -e "${GREEN}✓ Temporal UI will be available at http://localhost:8080${NC}"
else
echo -e "${YELLOW}⚠ Local services mode${NC}"
echo -e "${YELLOW} Please ensure the following services are running:${NC}"
echo -e "${YELLOW} - PostgreSQL (port 5432)${NC}"
echo -e "${YELLOW} - Redis (port 6379)${NC}"
echo -e "${YELLOW} - Temporal Server (port 7233)${NC}"
echo ""
read -p " Press Enter when services are ready..."
fi
echo ""
}
# Setup database
setup_database() {
echo -e "${BLUE}[7/8] Setting up database...${NC}"
# Run migrations
echo -e "${YELLOW} Running database migrations...${NC}"
if [ -f drizzle/meta/_journal.json ]; then
npm run db:migrate
else
# This development repository does not yet ship migration files.
# Match manage.ts db-setup; never force destructive schema changes.
echo -e "${YELLOW} No migrations found; synchronizing the development schema${NC}"
npm run db:push
fi
echo -e "${GREEN}✓ Database migrations completed${NC}"
# Seed database
echo -e "${YELLOW} Seeding database...${NC}"
npm run db:seed
echo -e "${GREEN}✓ Database seeded with initial data${NC}"
echo ""
}
# Display success message
display_success() {
echo -e "${BLUE}[8/8] Initialization complete!${NC}"
echo ""
echo -e "${GREEN}╔════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ CoreStack is ready! ║${NC}"
echo -e "${GREEN}╚════════════════════════════════════════════╝${NC}"
echo ""
echo -e "${BLUE}Default admin credentials:${NC}"
echo -e " Username: ${GREEN}root${NC}"
echo -e " Password: ${YELLOW}Must-Changed${NC}"
echo -e " ${RED}⚠ Please change the password after first login!${NC}"
echo ""
echo -e "${BLUE}To start the application:${NC}"
echo -e " ${GREEN}./run.sh${NC} # Start all services in tmux"
echo -e " ${GREEN}npm run dev${NC} # Start Next.js dev server only"
echo ""
echo -e "${BLUE}Useful commands:${NC}"
echo -e " ${GREEN}docker compose logs -f${NC} # View Docker service logs"
echo -e " ${GREEN}npm run db:studio${NC} # Open database GUI"
echo -e " ${GREEN}npm run lint${NC} # Check code quality"
echo ""
echo -e "${BLUE}Access points:${NC}"
echo -e " Next.js App: ${GREEN}http://localhost:3000${NC}"
echo -e " WebSocket: ${GREEN}ws://localhost:3001${NC}"
echo -e " Temporal UI: ${GREEN}http://localhost:8080${NC}"
echo ""
}
# Main execution
main() {
detect_os
check_node
detect_docker
install_dependencies
setup_env
start_services
setup_database
display_success
}
# Run main function
main