1
- # !/bin/bash
2
-
3
- # Enable error checking and debugging
4
- set -e # Exit on any error
5
- set -x # Print each command before executing
6
-
7
- # Log startup for debugging
8
- echo "Starting initialization script..."
9
-
10
- # Check if directory exists first and show current permissions
11
- echo "Checking current state..."
12
- ls -la /mnt || true
13
- ls -la /mnt/stateful_partition || true
14
-
15
- # Try creating base directory first
16
- echo "Creating base directory..."
17
- if ! sudo mkdir -p /mnt/stateful_partition; then
18
- echo "Failed to create /mnt/stateful_partition"
19
- # Try to identify the issue
20
- df -h
21
- mount | grep /mnt
22
- ls -la /mnt
23
- exit 1
24
- fi
25
-
26
- # Try alternative locations if stateful_partition isn't working
27
- if [ ! -d "/mnt/stateful_partition" ]; then
28
- echo "Using alternative directory /var/lib/qi-agents"
29
- sudo mkdir -p /var/lib/qi-agents || {
30
- echo "Failed to create alternative directory"
31
- exit 1
32
- }
33
- AGENT_DATA_DIR="/var/lib/qi-agents"
34
- else
35
- AGENT_DATA_DIR="/mnt/stateful_partition/qi-agents"
36
- # Create qi-agents directory
37
- sudo mkdir -p "$AGENT_DATA_DIR" || {
38
- echo "Failed to create qi-agents directory"
39
- exit 1
40
- }
41
- fi
42
-
43
- # Create data directory
44
- echo "Creating data directory in $AGENT_DATA_DIR..."
45
- sudo mkdir -p "$AGENT_DATA_DIR/data" || {
46
- echo "Failed to create data directory"
47
- exit 1
48
- }
49
-
50
- # Set permissions with verbose output
51
- echo "Setting directory permissions..."
52
- sudo chmod -Rv 777 "$AGENT_DATA_DIR" || {
53
- echo "Failed to set permissions"
54
- exit 1
55
- }
56
-
57
- # Verify directory structure and permissions
58
- echo "Verifying directory structure..."
59
- ls -la "$AGENT_DATA_DIR"
60
- ls -la "$AGENT_DATA_DIR/data"
61
-
62
- # Pull latest image with verification
63
- echo "Pulling latest image..."
64
- if ! docker pull us-central1-docker.pkg.dev/${PROJECT_ID}/${FULL_NAME}:${version}; then
65
- echo "Failed to pull image"
66
- exit 1
67
- fi
68
-
69
- # Get secrets from Secret Manager
70
- echo "Fetching secrets..."
71
- SMALL_GOOGLE_MODEL=$(gcloud secrets versions access latest --secret="small-google-model" ) || {
72
- echo "Failed to fetch small-google-model secret"
73
- exit 1
74
- }
75
- MEDIUM_GOOGLE_MODEL=$(gcloud secrets versions access latest --secret="medium-google-model" ) || {
76
- echo "Failed to fetch medium-google-model secret"
77
- exit 1
78
- }
79
- GOOGLE_GENERATIVE_AI_API_KEY=$(gcloud secrets versions access latest --secret="google-generative-ai-key" ) || {
80
- echo "Failed to fetch google-generative-ai-key secret"
81
- exit 1
82
- }
83
-
84
- # Debug existing environment variables
85
- echo "Debug: Checking existing environment variables:"
86
- echo "AGENTS_BUCKET_NAME from environment: ${AGENTS_BUCKET_NAME}"
87
- echo "CHARACTER_FILE from environment: ${CHARACTER_FILE}"
88
-
89
- # Run container with persistent storage and restart policy
90
- echo "Starting container with environment variables..."
91
- docker run -d \
92
- --name ${FULL_NAME} \
93
- --restart=always \
94
- -v "$AGENT_DATA_DIR/data" :/app/agent/data \
95
- -e AGENTS_BUCKET_NAME="${AGENTS_BUCKET_NAME}" \
96
- -e CHARACTER_FILE="${CHARACTER_FILE}" \
97
- -e SMALL_GOOGLE_MODEL="${SMALL_GOOGLE_MODEL}" \
98
- -e MEDIUM_GOOGLE_MODEL="${MEDIUM_GOOGLE_MODEL}" \
99
- -e GOOGLE_GENERATIVE_AI_API_KEY="${GOOGLE_GENERATIVE_AI_API_KEY}" \
100
- -e PORT="8080" \
101
- -e SERVER_PORT="8080" \
102
- us-central1-docker.pkg.dev/${PROJECT_ID}/${FULL_NAME}:${version}
103
-
104
- # Verify container is running and check logs
105
- echo "Checking container status and logs..."
106
- sleep 5 # Give container a moment to start
107
- if ! docker ps | grep ${FULL_NAME}; then
108
- echo "Container failed to start. Checking logs:"
109
- docker logs ${FULL_NAME}
110
- exit 1
111
- fi
112
-
113
- # Show container logs and verify environment variables
114
- echo "Container environment variables:"
115
- docker exec ${FULL_NAME} env | grep -E 'AGENTS_BUCKET_NAME|CHARACTER_FILE'
116
-
117
- echo "Initial container logs:"
118
- docker logs ${FULL_NAME}
119
-
120
- echo "Startup script completed successfully"
1
+ # Use Node.js 23.3.0 as specified in package.json
2
+ FROM node:23.3.0-slim AS builder
3
+
4
+ # Install pnpm globally and install necessary build tools
5
+ RUN npm install -g pnpm@9.4.0 && \
6
+ apt-get update && \
7
+ apt-get install -y git python3 make g++ && \
8
+ apt-get clean && \
9
+ rm -rf /var/lib/apt/lists/*
10
+
11
+ # Set Python 3 as the default python
12
+ RUN ln -s /usr/bin/python3 /usr/bin/python
13
+
14
+ # Set the working directory
15
+ WORKDIR /app
16
+
17
+ # Copy all workspace files first
18
+ COPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc turbo.json ./
19
+ COPY packages ./packages
20
+ COPY agent ./agent
21
+ COPY scripts ./scripts
22
+
23
+ # Install dependencies and build the project
24
+ RUN pnpm install && \
25
+ pnpm build-docker && \
26
+ pnpm prune --prod
27
+
28
+ # Create a new stage for the final image
29
+ FROM node:23.3.0-slim
30
+
31
+ # Install runtime dependencies and Google Cloud SDK
32
+ RUN npm install -g pnpm@9.4.0 && \
33
+ apt-get update && \
34
+ apt-get install -y git python3 curl gnupg && \
35
+ echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
36
+ curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key --keyring /usr/share/keyrings/cloud.google.gpg add - && \
37
+ apt-get update && \
38
+ apt-get install -y google-cloud-sdk && \
39
+ apt-get clean && \
40
+ rm -rf /var/lib/apt/lists/*
41
+
42
+ WORKDIR /app
43
+
44
+ # Copy built artifacts and production dependencies from the builder stage
45
+ COPY --from=builder /app/package.json ./
46
+ COPY --from=builder /app/pnpm-workspace.yaml ./
47
+ COPY --from=builder /app/.npmrc ./
48
+ COPY --from=builder /app/turbo.json ./
49
+ COPY --from=builder /app/node_modules ./node_modules
50
+ COPY --from=builder /app/agent ./agent
51
+ COPY --from=builder /app/packages ./packages
52
+ COPY --from=builder /app/scripts ./scripts
53
+
54
+ # Create characters directory
55
+ RUN mkdir -p characters
56
+
57
+ # Add debugging and error handling to startup command
58
+ CMD sh -c '\
59
+ echo "Debug: Starting container" && \
60
+ echo "Debug: AGENTS_BUCKET_NAME=${AGENTS_BUCKET_NAME}" && \
61
+ echo "Debug: CHARACTER_FILE=${CHARACTER_FILE}" && \
62
+ echo "Debug: Full GCS path=gs://${AGENTS_BUCKET_NAME}/${CHARACTER_FILE}" && \
63
+ if [ -z "${AGENTS_BUCKET_NAME}" ]; then \
64
+ echo "Error: AGENTS_BUCKET_NAME is empty" && exit 1; \
65
+ fi && \
66
+ if [ -z "${CHARACTER_FILE}" ]; then \
67
+ echo "Error: CHARACTER_FILE is empty" && exit 1; \
68
+ fi && \
69
+ echo "Debug: Attempting to copy character file..." && \
70
+ gsutil cp gs://${AGENTS_BUCKET_NAME}/${CHARACTER_FILE} characters/${CHARACTER_FILE} && \
71
+ echo "Debug: Starting application..." && \
72
+ pnpm start --non-interactive --characters=characters/${CHARACTER_FILE}'
0 commit comments