-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartup-script.sh
More file actions
131 lines (104 loc) · 5.51 KB
/
startup-script.sh
File metadata and controls
131 lines (104 loc) · 5.51 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
#!/bin/bash
set -e
echo "Setup started." >/var/log/startup-script.log
DIFY_VERSION="${dify_version}"
# =============================================================================
# Configure Dify
# =============================================================================
# Create .env file from .env.example
cd /opt/dify-$DIFY_VERSION/docker
cp .env.example .env
# Replace configuration values using sed
# Database Configuration
sed -i "s|^DB_HOST=.*|DB_HOST=${db_host}|" .env
sed -i "s|^DB_USERNAME=.*|DB_USERNAME=${database_user}|" .env
sed -i "s|^DB_PASSWORD=.*|DB_PASSWORD='${database_password}'|" .env
sed -i "s|^DB_DATABASE=.*|DB_DATABASE=${database_name}|" .env
# pgvector Configuration
sed -i "s|^VECTOR_STORE=.*|VECTOR_STORE=pgvector|" .env
sed -i "s|^PGVECTOR_HOST=.*|PGVECTOR_HOST=${pgvector_private_ip}|" .env
sed -i "s|^PGVECTOR_USER=.*|PGVECTOR_USER=${pgvector_database_user}|" .env
sed -i "s|^PGVECTOR_PGUSER=.*|PGVECTOR_PGUSER=${pgvector_database_user}|" .env
sed -i "s|^PGVECTOR_PASSWORD=.*|PGVECTOR_PASSWORD='${pgvector_database_password}'|" .env
sed -i "s|^PGVECTOR_POSTGRES_PASSWORD=.*|PGVECTOR_POSTGRES_PASSWORD='${pgvector_database_password}'|" .env
sed -i "s|^PGVECTOR_DATABASE=.*|PGVECTOR_DATABASE=${pgvector_database_name}|" .env
sed -i "s|^PGVECTOR_POSTGRES_DB=.*|PGVECTOR_POSTGRES_DB=${pgvector_database_name}|" .env
# Redis Configuration
sed -i "s|^REDIS_HOST=.*|REDIS_HOST=${redis_host}|" .env
sed -i "s|^REDIS_PORT=.*|REDIS_PORT=${redis_port}|" .env
sed -i "s|^REDIS_PASSWORD=.*|REDIS_PASSWORD='${redis_auth_string}'|" .env
sed -i "s|^CELERY_BROKER_URL=.*|CELERY_BROKER_URL='redis://:${redis_auth_string}@${redis_host}:${redis_port}/1'|" .env
# Disable Default DB
# ETL / Unstructured Configuration
if [ "${enable_unstructured_api}" = "true" ]; then
sed -i "s|^COMPOSE_PROFILES=.*|COMPOSE_PROFILES=unstructured|" .env
sed -i "s|^ETL_TYPE=.*|ETL_TYPE=Unstructured|" .env
sed -i "s|^UNSTRUCTURED_API_URL=.*|UNSTRUCTURED_API_URL=http://unstructured:8000/general/v0/general|" .env
else
sed -i "s|^COMPOSE_PROFILES=.*|COMPOSE_PROFILES=|" .env
fi
# Password for admin user initialization.
sed -i "s|^INIT_PASSWORD=.*|INIT_PASSWORD='${initial_password}'|" .env
# Comma-separated list of file extensions blocked from upload for security reasons.
sed -i "s|^UPLOAD_FILE_EXTENSION_BLACKLIST=.*|UPLOAD_FILE_EXTENSION_BLACKLIST=exe,bat,cmd,com,scr,vbs,ps1,msi,dll|" .env
chown -R ubuntu:ubuntu /opt/dify-$DIFY_VERSION
echo "Dify was configured." >>/var/log/startup-script.log
# =============================================================================
# Filestore Setup
# =============================================================================
# Mount Filestore to Dify volumes directory
FILESTORE_IP="${filestore_ip}"
FILESTORE_SHARE="${filestore_share_name}"
DIFY_DIR="/opt/dify-$DIFY_VERSION"
VOLUMES_DIR="$DIFY_DIR/docker/volumes"
TEMP_MOUNT="/mnt/filestore_temp"
# Create temporary mount point and mount Filestore
mkdir -p "$TEMP_MOUNT"
mount -t nfs -o rw,intr "$FILESTORE_IP:/$FILESTORE_SHARE" "$TEMP_MOUNT"
# Check if Filestore is empty (only lost+found exists)
ITEM_COUNT=$(ls -A "$TEMP_MOUNT" | grep -v '^lost+found$' | wc -l)
if [ "$ITEM_COUNT" -eq 0 ]; then
echo "Filestore is empty (only lost+found exists). Copying initial volumes data..." >>/var/log/startup-script.log
# Copy contents from source volumes directory to Filestore
if [ -d "$VOLUMES_DIR" ]; then
cp -a "$VOLUMES_DIR/." "$TEMP_MOUNT/"
echo "Initial volumes data copied to Filestore." >>/var/log/startup-script.log
else
echo "Warning: Source volumes directory not found." >>/var/log/startup-script.log
fi
# Additional python packages to be installed in sandbox container.
mkdir -p "$TEMP_MOUNT/sandbox/dependencies"
else
echo "Filestore already contains data. Skipping initial copy." >>/var/log/startup-script.log
fi
# Get python-requirements.txt from metadata and save to sandbox volume
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/python-requirements" \
-H "Metadata-Flavor: Google" >"$TEMP_MOUNT/sandbox/dependencies/python-requirements.txt"
echo "Python requirements copied to sandbox volume" >>/var/log/startup-script.log
# Get sandbox config from metadata and save to sandbox volume only if python-requirements.txt is not empty
if [ -s "$TEMP_MOUNT/sandbox/dependencies/python-requirements.txt" ]; then
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/sandbox-config" \
-H "Metadata-Flavor: Google" >"$TEMP_MOUNT/sandbox/conf/config.yaml"
echo "Sandbox config copied to sandbox volume" >>/var/log/startup-script.log
else
echo "Python requirements is empty. Skipping sandbox config." >>/var/log/startup-script.log
fi
# Unmount temporary mount
umount "$TEMP_MOUNT"
rmdir "$TEMP_MOUNT"
# Remove existing volumes directory if it exists
if [ -d "$VOLUMES_DIR" ]; then
rm -rf "$VOLUMES_DIR"
fi
# Create mount point and mount Filestore
mkdir -p "$VOLUMES_DIR"
mount -t nfs -o rw,intr "$FILESTORE_IP:/$FILESTORE_SHARE" "$VOLUMES_DIR"
# Add to /etc/fstab for automatic mounting on reboot
echo "$FILESTORE_IP:/$FILESTORE_SHARE $VOLUMES_DIR nfs rw,intr 0 0" >>/etc/fstab
echo "Filestore mounted successfully." >>/var/log/startup-script.log
# =============================================================================
# Start Dify
# =============================================================================
# Start Dify with Docker Compose
sudo -u ubuntu docker compose up -d
echo "Setup completed successfully!" >>/var/log/startup-script.log