-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-docker.sh
More file actions
executable file
·155 lines (135 loc) · 4.66 KB
/
Copy pathinit-docker.sh
File metadata and controls
executable file
·155 lines (135 loc) · 4.66 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
#!/bin/bash
set -e # Exit on error
# Get a list of all remote names
remotes=$(git remote 2>/dev/null || echo "")
# Rename origin to speedpy if it exists
if echo "$remotes" | grep -q "^origin$"; then
git remote rename origin speedpy
echo "Renamed remote 'origin' to 'speedpy'"
fi
echo "Creating a speedpy branch for future updates. "
git branch speedpy || true
git branch --set-upstream-to=speedpy/master speedpy 2>/dev/null || true
echo "Set speedpy branch to track speedpy/master"
# Unset upstream tracking for master branch
if git rev-parse --verify master >/dev/null 2>&1; then
git branch --unset-upstream master 2>/dev/null || true
echo "Unset upstream tracking for 'master' branch"
fi
# Backup docker-compose.yml before modifications
if [ ! -f "docker-compose.yml.bak" ]; then
cp docker-compose.yml docker-compose.yml.bak
echo "Created backup: docker-compose.yml.bak"
else
# Restore from backup for clean slate
cp docker-compose.yml.bak docker-compose.yml
echo "Restored docker-compose.yml from backup"
fi
# Generate unique suffix based on directory path for Docker image names
# This prevents image name conflicts when running multiple instances in different directories
UNIQUE_SUFFIX="_$(pwd | md5sum | cut -c1-8)"
echo " * Using unique image suffix: $UNIQUE_SUFFIX"
# Replace the placeholder image name in docker-compose.yml with unique suffix
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS sed syntax
sed -i '' "s/-image-suffix/speedpy${UNIQUE_SUFFIX}/g" docker-compose.yml
else
# Linux sed syntax
sed -i "s/-image-suffix/speedpy${UNIQUE_SUFFIX}/g" docker-compose.yml
fi
# Generate random port between 9000 and 9999
WEB_PORT=$((9000 + RANDOM % 501))
echo " * Using random port for Django: $WEB_PORT"
# Update docker-compose.yml with the random port
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "s/9000/$WEB_PORT/g" docker-compose.yml
else
sed -i "s/9000/$WEB_PORT/g" docker-compose.yml
fi
# Generate random port between 9501 and 9999
NGINX_PORT=$((9501 + RANDOM % 499))
echo " * Using random port for nginx: $NGINX_PORT"
# Update docker-compose.yml with the random port
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "s/127.0.0.1:9001:80/127.0.0.1:$NGINX_PORT:80/g" docker-compose.yml
else
sed -i "s/127.0.0.1:9001:80/127.0.0.1:$NGINX_PORT:80/g" docker-compose.yml
fi
# Write dev.sh with the chosen port. docker-compose.yml's web.command runs
# `bash dev.sh`, so this is what the web container actually executes.
cat > dev.sh <<EOF
#!/usr/bin/env bash
exec python manage.py runserver 0.0.0.0:$WEB_PORT
EOF
chmod a+x dev.sh
# Populate the mode-specific commands sheet for AI coding agents.
cp AGENTS-local.docker.md AGENTS-local.md
open_url() {
local url="$1"
case "$(uname -s)" in
Linux*)
if grep -qi microsoft /proc/version; then
# WSL
cmd.exe /c start "$url" >/dev/null 2>&1
else
# Regular Linux
xdg-open "$url" >/dev/null 2>&1
fi
;;
Darwin*)
# macOS
open "$url" >/dev/null 2>&1
;;
*)
echo "Unsupported operating system"
exit 1
;;
esac
}
# Source pre-check script if it exists
if [ -f "./pre_check.sh" ]; then
. ./pre_check.sh
fi
# Configure TTY for docker compose
USE_TTY=""
if [ -t 1 ]; then
USE_TTY="-T"
fi
echo "USE_TTY=${USE_TTY}"
cp .docker.env .env
echo " * docker compose down -v --remove-orphans"
docker compose down -v --remove-orphans
echo " * Building the Docker image for the project (might take a while)"
docker compose build -q --no-cache
echo " * Starting DB and redis"
docker compose up -d db redis
sleep 2
echo " * Generating directories for tailwind config"
docker compose run --rm ${USE_TTY} web python manage.py generate_tailwind_directories
echo " * Installing tailwind and dependencies"
docker compose run --rm ${USE_TTY} web bash -c "npm i && npm run tailwind:build"
echo " * Initializing the project"
docker compose run --rm ${USE_TTY} web python manage.py makemigrations
docker compose run --rm ${USE_TTY} web python manage.py migrate
docker compose run --rm ${USE_TTY} web python manage.py makesuperuser >local_password.txt
cat local_password.txt
echo " * Created local superuser credentials are stored in local_password.txt file for your convenience"
git add .
git commit -q -m "Initial commit"
echo " * Starting the project..."
docker compose up -d
sleep 1
count=0
max_retries=5
until curl -s -f -o /dev/null "http://127.0.0.1:$WEB_PORT" || [ "$count" -ge "$max_retries" ]; do
echo "Waiting... $((count + 1))/$max_retries"
count=$((count + 1))
sleep 1
done
if [ "$count" -lt "$max_retries" ]; then
echo "Site is up!"
else
echo "App seems to be still down after $max_retries retries"
fi
open_url "http://127.0.0.1:$WEB_PORT"
echo "Open your browser at http://127.0.0.1:$WEB_PORT"