Skip to content

Commit 8f44737

Browse files
authored
PHRAS-4149 stack management (#4609)
* wip * compose stack management WIP * Clean output SKIP_CI * Adding latest version info * Adding latest version info SKIP_CI * update readme * update readme * wip * wip * wip * wip
1 parent eca8706 commit 8f44737

File tree

4 files changed

+86
-59
lines changed

4 files changed

+86
-59
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ COMPOSE_PROFILES=app,gateway-classic,db,pma,elasticsearch,redis,redis-session,ra
186186

187187

188188
You should review the default env variables defined in `.env` file.
189-
Use `export` method to override these values
189+
Use `export` method to override these values
190190

191191
i.e:
192192
```bash
@@ -198,24 +198,26 @@ export PHRASEANET_APP_PORT=8082
198198

199199
If you want persit this change you can also save your change in a files named `env.local` or `.env.local`
200200

201-
These files are git ignored and permit stack customization without git conflict generation.
201+
These files are git ignored and permit stack customization without git conflict.
202202

203203
If you are not interested in the development of Phraseanet, you can ignore everything in `.env` after the `DEV Purpose` part.
204204

205205

206206
### stack Management
207207

208-
To start and stop the stack, use the `stack_management.sh start|stop|info|check|log` script at the project root.
208+
209+
To start and stop the stack, use the `stack_management.sh start|stop|info|status|log` script at the project root.
209210
this script work only Linux and MacOS.
210211

211212
This script wrapping standard oprations for the stack, using standard docker compose cmd and take in account `.env` and `env.local | .env.local` files.
212213

214+
213215
The `stop` operation is the most critical function of the script, as it ensures a clean shutdown
214216
of the database container before stopping the entire stack.
215217

216218
This script is useful if you are using the database container within the stack.
217219

218-
see more here ![stack Management](doc/infra/compose/stack_management.md)
220+
see more here ![stack](doc/infra/compose/stack.md)
219221

220222
### Using a env.local method for custom .env values
221223

doc/infra/compose/stack_management.md renamed to doc/infra/compose/stack.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This script is an helper designed to manage a Phraseanet Docker compose stack by
1212
- **Version Checking**: Verify the compatibility of Docker and Docker Compose versions.
1313
- **Environment Information**: Gather detailed information about the environment, including Phraseanet installation status, version details, and internal URLs.
1414
- **Log Viewing**: View logs for all containers or filter logs for a specific container.
15-
- **Environment Variables**: Load and display environment variables from `.env` and either `env.local` or `.env.local` files.
15+
1616

1717
## Usage
1818

@@ -26,32 +26,32 @@ This script is an helper designed to manage a Phraseanet Docker compose stack by
2626

2727
- **Start the Stack**:
2828
```bash
29-
./manage_docker.sh start
29+
./stack.sh start
3030
```
3131

3232
- **Stop the Stack**:
3333
```bash
34-
./manage_docker.sh stop
34+
./stack.sh stop
3535
```
3636

3737
- **Check Versions**:
3838
```bash
39-
./manage_docker.sh check
39+
./stack.sh status
4040
```
4141

4242
- **Display Environment Information**:
4343
```bash
44-
./manage_docker.sh info
44+
./stack.sh info
4545
```
4646

4747
- **View Logs**:
4848
- For all containers:
4949
```bash
50-
./manage_docker.sh log
50+
./stack.sh logs
5151
```
5252
- For a specific container:
5353
```bash
54-
./manage_docker.sh log <container_name>
54+
./stack.sh logs <container_name>
5555
```
5656

5757
### Environment Variables

docker-compose.datastores.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ services:
2929
environment:
3030
- RABBITMQ_DEFAULT_USER
3131
- RABBITMQ_DEFAULT_PASS
32+
- PHRASEANET_RABBITMQ_VHOST
3233
volumes:
3334
- rabbitmq_vol:/var/lib/rabbitmq
3435
networks:

stack_management.sh renamed to stack.sh

Lines changed: 72 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,31 @@ get_env_files() {
2222
}
2323

2424
# Function to check Docker and Docker Compose versions and display additional information
25-
check_versions() {
26-
local required_docker_version="25.0.5"
27-
local required_compose_version="2.29.0"
28-
29-
# Get Docker version
30-
local docker_version=$(docker --version | awk -F'[ ,]' '{print $3}')
31-
if [ "$(printf '%s\n' "$required_docker_version" "$docker_version" | sort -V | head -n1)" != "$required_docker_version" ]; then
32-
echo "Error: Docker version $docker_version is less than the required version $required_docker_version."
33-
exit 1
34-
fi
35-
36-
# Get Docker Compose version
37-
local compose_version=$($DOCKER_COMPSE_CMD version --short)
38-
if [ "$(printf '%s\n' "$required_compose_version" "$compose_version" | sort -V | head -n1)" != "$required_compose_version" ]; then
39-
echo "Error: Docker Compose version $compose_version is less than the required version $required_compose_version."
40-
exit 1
41-
fi
42-
43-
echo "Docker and Docker Compose versions are compatible."
44-
45-
# Get uptime of the stack
46-
echo "Stack Uptime:"
47-
$DOCKER_COMPSE_CMD ps | awk 'NR>1 {print $4}'
48-
echo
49-
50-
# Get internal IP addresses
51-
echo "Internal IP Addresses:"
52-
$DOCKER_COMPSE_CMD exec -T db sh -c 'ip addr show eth0 | grep "inet " | awk "{print \$2}" | cut -d/ -f1'
53-
echo
25+
stack_status() {
26+
check_compose_version
5427

5528
# Get container status
5629
echo "Container Status:"
57-
$DOCKER_COMPSE_CMD ps
30+
$DOCKER_COMPOSE_CMD ps
5831
echo
5932

6033
# Get resource usage
6134
echo "Resource Usage:"
62-
$DOCKER_COMPSE_CMD top
35+
$DOCKER_COMPOSE_CMD top
6336
echo
37+
38+
display_rabbitmq_info
39+
echo
40+
display_db_info
6441
}
6542

6643
# Function to display information about the environment
6744
display_info() {
6845
echo "Checking environment information..."
6946
echo
47+
48+
check_compose_version
7049

71-
# Load environment variables
7250
local env_files=($(get_env_files))
7351
for env_file in "${env_files[@]}"; do
7452
if [ -f "$env_file" ]; then
@@ -77,7 +55,7 @@ display_info() {
7755
set +a
7856
fi
7957
done
80-
58+
8159
# Display Docker tag and registry information
8260
echo "Phraseanet Docker Tag: ${PHRASEANET_DOCKER_TAG:-Not set}"
8361
echo "Phraseanet Docker Registry: ${PHRASEANET_DOCKER_REGISTRY:-Not set}"
@@ -99,20 +77,20 @@ display_info() {
9977

10078
# Get creation date of the configuration file
10179
local creation_date=$(date -r "$(stat -f %B "config/configuration.yml" 2>/dev/null || stat -c %Y "config/configuration.yml" 2>/dev/null)" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || echo "Unknown")
102-
echo "Installation Date: $creation_date"
80+
echo "Installation Date (configuration.yml creation date): $creation_date"
10381

10482
# Check for the compiled configuration file and get its last modification date
10583
if [ -f "config/configuration-compiled.php" ]; then
10684
local last_modified_date=$(date -r "$(stat -f %m "config/configuration-compiled.php" 2>/dev/null || stat -c %Y "config/configuration-compiled.php" 2>/dev/null)" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || echo "Unknown")
107-
echo "Last Update Date: $last_modified_date"
85+
echo "Last Update Date (configuration-compiled.yml update date): $last_modified_date"
10886
echo
10987
else
11088
echo "Last Update unknown, config/configuration-compiled.php not found."
11189
echo
11290
fi
11391

11492
# Check if the Phraseanet container is running
115-
if $DOCKER_COMPSE_CMD ps | grep -q "phraseanet.*Up"; then
93+
if $DOCKER_COMPOSE_CMD ps | grep -q "phraseanet.*Up"; then
11694
echo "Phraseanet container is running. Fetching version information..."
11795

11896
# Extract version from Version.php file
@@ -125,7 +103,7 @@ display_info() {
125103
fi
126104

127105
# Execute the command to get Phraseanet version from console
128-
local version_from_console=$($DOCKER_COMPSE_CMD exec phraseanet sh -c 'bin/console --version | grep -o "KONSOLE KOMMANDER version [^ ]* [^ ]*" | awk "{print \$NF}"')
106+
local version_from_console=$($DOCKER_COMPOSE_CMD exec phraseanet sh -c 'bin/console --version | grep -o "KONSOLE KOMMANDER version [^ ]* [^ ]*" | awk "{print \$NF}"')
129107
echo "Version from console: $version_from_console"
130108

131109
# Compare versions
@@ -164,32 +142,78 @@ display_info() {
164142
display_logs() {
165143
if [ -n "$1" ]; then
166144
echo "Displaying logs for container: $1"
167-
$DOCKER_COMPSE_CMD logs -f "$1"
145+
$DOCKER_COMPOSE_CMD logs -f "$1"
168146
else
169147
echo "Displaying logs for all containers"
170-
$DOCKER_COMPSE_CMD logs -f
148+
$DOCKER_COMPOSE_CMD logs -f
171149
fi
172150
}
173151

174-
DOCKER_COMPSE_CMD=$(detect_docker_compose_command)
152+
DOCKER_COMPOSE_CMD=$(detect_docker_compose_command)
175153

176154
# Function to start the Docker stack
177155
start_stack() {
178156
echo "Starting the Docker stack..."
179157
local env_files=($(get_env_files))
180-
$DOCKER_COMPSE_CMD "${env_files[@]/#/--env-file=}" up -d
158+
$DOCKER_COMPOSE_CMD "${env_files[@]/#/--env-file=}" up -d
181159
}
182160

183161
# Function to stop the Docker stack
184162
stop_stack() {
185163
echo "Stopping the MySQL server in the db container..."
186164
# Execute the mysqladmin command inside the container where the environment variable is defined
187-
$DOCKER_COMPSE_CMD exec db sh -c '/usr/bin/mysqladmin -uroot -p"$MYSQL_ROOT_PASSWORD" shutdown'
165+
$DOCKER_COMPOSE_CMD exec db sh -c '/usr/bin/mysqladmin -uroot -p"$MYSQL_ROOT_PASSWORD" shutdown'
188166
echo
189167

190168
echo "Stopping the Docker stack..."
191169
local env_files=($(get_env_files))
192-
$DOCKER_COMPSE_CMD "${env_files[@]/#/--env-file=}" down
170+
$DOCKER_COMPOSE_CMD "${env_files[@]/#/--env-file=}" down
171+
}
172+
173+
# Function rabbimq queue information
174+
display_rabbitmq_info() {
175+
echo "RabbitMQ queue informations :"
176+
$DOCKER_COMPOSE_CMD exec rabbitmq sh -c 'rabbitmqctl --version & rabbitmqctl list_queues --vhost $PHRASEANET_RABBITMQ_VHOST'
177+
echo
178+
}
179+
180+
# Function check compose version
181+
check_compose_version() {
182+
local compose_version=$($DOCKER_COMPOSE_CMD version --short)
183+
if [[ $compose_version == *"v2"* ]]; then
184+
echo "Docker Compose v2 detected."
185+
else
186+
echo "Docker Compose v1 detected."
187+
fi
188+
189+
local required_docker_version="25.0.5"
190+
local required_compose_version="2.29.0"
191+
192+
# Get Docker version
193+
local docker_version=$(docker --version | awk -F'[ ,]' '{print $3}')
194+
if [ "$(printf '%s\n' "$required_docker_version" "$docker_version" | sort -V | head -n1)" != "$required_docker_version" ]; then
195+
echo "Error: Docker version $docker_version is less than the required version $required_docker_version."
196+
exit 1
197+
fi
198+
199+
# Get Docker Compose version
200+
local compose_version=$($DOCKER_COMPOSE_CMD version --short)
201+
if [ "$(printf '%s\n' "$required_compose_version" "$compose_version" | sort -V | head -n1)" != "$required_compose_version" ]; then
202+
echo "Error: Docker Compose version $compose_version is less than the required version $required_compose_version."
203+
exit 1
204+
fi
205+
206+
echo "Docker and Docker Compose versions are compatible."
207+
echo "Docker Version: $docker_version"
208+
echo "Docker Compose Version: $compose_version"
209+
echo
210+
}
211+
212+
# Function db status
213+
display_db_info () {
214+
echo "DB status"
215+
$DOCKER_COMPOSE_CMD exec db sh -c 'env |grep MYSQL_ & mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "SHOW DATABASES;SHOW PROCESSLIST;"'
216+
echo
193217
}
194218

195219
# Check the argument passed to the script
@@ -200,17 +224,17 @@ case "$1" in
200224
stop)
201225
stop_stack
202226
;;
203-
check)
204-
check_versions
227+
status)
228+
stack_status
205229
;;
206-
info)
230+
version)
207231
display_info
208232
;;
209-
log)
233+
logs)
210234
display_logs "$2"
211235
;;
212236
*)
213-
echo "Usage: $0 {start|stop|check|info|log [container_name]}"
237+
echo "Usage: $0 {start|stop|status|version|logs [container_name]}"
214238
exit 1
215239
esac
216240

0 commit comments

Comments
 (0)