-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·196 lines (167 loc) · 6.81 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·196 lines (167 loc) · 6.81 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
#!/bin/bash
if [ ! -f .env ]; then
cp .env.sample .env
fi
install=0
ngrok=0
cloudflared=0
redis=0
BASEDIR="$(dirname $(realpath $0))"
# Parse arguments:
# --install: Installation of dependencies
# --ngrok: Use ngrok to expose the site
# --ngrok-token=YOUR_NGROK_TOKEN: Override the ngrok token in .env
# --cloudflared: Use cloudflared to expose the site
# --cloudflared-token=YOUR_CLOUDFLARED_TOKEN: Override the cloudflared token in .env
# --redis: Enable Redis persistent object cache
while [[ "$#" -gt 0 ]]; do
if [ "$1" == "--install" ]; then
install=1
elif [ "$1" == "--ngrok" ]; then
ngrok=1
elif [ "$1" == "--cloudflared" ]; then
cloudflared=1
elif [ "$1" == "--redis" ]; then
redis=1
elif [[ "$1" == --ngrok-token=* ]]; then
ngrok_token="${1#*=}"
sed -i.bak "s|NGROK_AUTHTOKEN=.*|NGROK_AUTHTOKEN=$ngrok_token|" .env
rm .env.bak
elif [[ "$1" == --cloudflared-token=* ]]; then
cloudflared_token="${1#*=}"
sed -i.bak "s|CLOUDFLARED_TUNNEL_TOKEN=.*|CLOUDFLARED_TUNNEL_TOKEN=$cloudflared_token|" .env
rm .env.bak
fi
shift
done
# Enable the repo's shared git hooks (pre-commit / pre-push quality gates).
git -C "$BASEDIR" config core.hooksPath .githooks
# Extract WP_URL from .env and set PUBLIC_URL to that value
WP_URL=$(grep '^WP_URL=' .env | cut -d'=' -f2)
sed -i.bak "s|PUBLIC_URL=.*|PUBLIC_URL=$WP_URL|" .env
rm .env.bak
if [ $redis -eq 1 ]; then
# Inject Redis constants into WORDPRESS_CONFIG_EXTRA
sed -i.bak "s|WORDPRESS_CONFIG_EXTRA=\"\(.*\)\"|WORDPRESS_CONFIG_EXTRA=\"\1define('WP_REDIS_HOST', getenv('REDIS_HOST') ?: 'redis');define('WP_REDIS_PORT', (int)(getenv('REDIS_PORT') ?: 6379));\"|" .env
rm .env.bak
# Append redis-cache plugin to WP_PLUGINS
current_plugins=$(grep '^WP_PLUGINS=' .env | cut -d'=' -f2- | sed 's/^"//;s/"$//')
sed -i.bak "s|^WP_PLUGINS=.*|WP_PLUGINS=\"${current_plugins},redis-cache\"|" .env
rm .env.bak
fi
set -o allexport
source .env
set +o allexport
if [ $ngrok -eq 1 ]; then
if [ -z "$NGROK_AUTHTOKEN" ]; then
echo "❌ Please set NGROK_AUTHTOKEN with your ngrok auth token in your .env file (get it from https://dashboard.ngrok.com/)"
exit 1
fi
echo "🔗 Starting ngrok tunnel..."
if [ -z "$NGROK_CONTAINER_NAME" ]; then
NGROK_CONTAINER_NAME=woocommerce-sequra-ngrok
fi
docker run -d -e NGROK_AUTHTOKEN=$NGROK_AUTHTOKEN \
-p $NGROK_PORT:4040 \
--name $NGROK_CONTAINER_NAME \
--add-host=host:host-gateway \
ngrok/ngrok:alpine \
http host:$WP_HTTP_PORT
WP_URL=""
retry=10
timeout=1
start=$(date +%s)
while [ -z "$WP_URL" ]; do
sleep $timeout
WP_URL=$(curl -s http://localhost:$NGROK_PORT/api/tunnels | grep -o '"public_url":"[^"]*"' | sed 's/"public_url":"\(.*\)"/\1/' | head -n 1)
if [ $(($(date +%s) - $start)) -gt $retry ]; then
docker rm -f $NGROK_CONTAINER_NAME || true
echo "❌ Error getting public url from ngrok after ${retry} seconds"
exit 1
fi
done
# Overwrite PUBLIC_URL inside .env
sed -i.bak "s|PUBLIC_URL=.*|PUBLIC_URL=$WP_URL|" .env
rm .env.bak
echo "✅ Ngrok started. Public URL: $WP_URL"
elif [ $cloudflared -eq 1 ]; then
if [ -z "$CLOUDFLARED_TUNNEL_TOKEN" ]; then
echo "❌ Please set CLOUDFLARED_TUNNEL_TOKEN with your cloudflared tunnel token in your .env file (get it from https://developers.cloudflare.com/cloudflare-one/networks/connectors/cloudflare-tunnel/get-started/create-remote-tunnel/)"
exit 1
fi
if [ -z "$CLOUDFLARED_TUNNEL_URL" ]; then
echo "❌ Please set CLOUDFLARED_TUNNEL_URL with your cloudflared tunnel URL in your .env file"
exit 1
fi
echo "🔗 Starting cloudflared tunnel..."
if [ -z "$CLOUDFLARED_CONTAINER_NAME" ]; then
CLOUDFLARED_CONTAINER_NAME=woocommerce-sequra-cloudflared
fi
docker run -d \
--name $CLOUDFLARED_CONTAINER_NAME \
--add-host=host:host-gateway \
cloudflare/cloudflared:latest \
tunnel --no-autoupdate run --token $CLOUDFLARED_TUNNEL_TOKEN
# Overwrite PUBLIC_URL inside .env
WP_URL=$CLOUDFLARED_TUNNEL_URL
sed -i.bak "s|PUBLIC_URL=.*|PUBLIC_URL=$WP_URL|" .env
rm .env.bak
echo "✅ Cloudflared started. Public URL: $WP_URL"
fi
if [ $install -eq 1 ]; then
./bin/composer install
./bin/npm install
./bin/npm run build
else
echo "Skipping installation of dependencies."
fi
IMAGE_EXISTS=$(docker images -q ghcr.io/sequra/woocommerce-sequra:$WP_TAG)
if [ -z "$IMAGE_EXISTS" ]; then
if [ -n "$GITHUB_TOKEN" ]; then
# Log in to GitHub Container Registry
echo "🔐 Logging in to the GitHub Container Registry..."
echo "$GITHUB_TOKEN" | docker login ghcr.io -u sequra --password-stdin || (echo "❌ Cannot log in to GitHub Container Registry" && exit 1)
echo "🔍 Checking if image ghcr.io/sequra/woocommerce-sequra:$WP_TAG exists in the GitHub Container Registry..."
if docker pull ghcr.io/sequra/woocommerce-sequra:$WP_TAG > /dev/null 2>&1; then
echo "🐳 Image ghcr.io/sequra/woocommerce-sequra:$WP_TAG pulled from the registry."
IMAGE_EXISTS=1
else
echo "⚠️ Image ghcr.io/sequra/woocommerce-sequra:$WP_TAG not found in the GitHub Container Registry."
fi
else
echo "⚠️ GITHUB_TOKEN is not set. Skipping check for existing image in the GitHub Container Registry."
fi
if [ -z "$IMAGE_EXISTS" ]; then
echo "🐳 Image ghcr.io/sequra/woocommerce-sequra:$WP_TAG will be built now..."
$BASEDIR/docker/build-image.sh --wp=$WP_TAG || (echo "❌ Docker image build failed" && exit 1)
fi
fi
COMPOSE_FILES="-f docker-compose.yml"
if [ $redis -eq 1 ]; then
COMPOSE_FILES="$COMPOSE_FILES -f docker-compose.redis.yml"
fi
docker compose $COMPOSE_FILES up -d || exit 1
echo "🚀 Waiting for installation to complete..."
retry=120
timeout=1
start=$(date +%s)
while [ $(($(date +%s) - $start)) -lt $retry ]; do
if docker compose exec web ls /var/www/html/.post-install-complete > /dev/null 2>&1; then
seconds=$(($(date +%s) - $start))
echo "✅ Done in ${seconds} seconds."
echo "🔗 Access seQura settings at ${WP_URL}/wp-admin/admin.php?page=wc-settings&tab=checkout§ion=sequra"
echo "🔗 Or browse products at ${WP_URL}/shop/"
echo "User: $WP_ADMIN_USER"
echo "Password: $WP_ADMIN_PASSWORD"
exit 0
elif docker compose exec web ls /var/www/html/.post-install-failed > /dev/null 2>&1; then
seconds=$(($(date +%s) - $start))
echo "❌ Installation failed after ${seconds} seconds."
$BASEDIR/teardown.sh
exit 1
fi
sleep $timeout
done
echo "❌ Timeout after ${retry} seconds"
$BASEDIR/teardown.sh
exit 1