Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(run): custom script runner #17

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -41,6 +41,10 @@ services:

| **⚠️ Note:** your scripts **MUST** be executable, and have a [shebang](<https://en.wikipedia.org/wiki/Shebang_(Unix)>) to be run by flashlight at startup. Otherwise they would be ignored.

## Examples

A few examples are available in the [examples](./examples) folder.

## Run environment variables

| Variable | Description | Required | Default value |
Expand Down
16 changes: 9 additions & 7 deletions assets/run.sh
Expand Up @@ -146,17 +146,19 @@ fi
# Custom init scripts
if [ ! -f $INIT_SCRIPTS_LOCK ] || [ "$INIT_SCRIPTS_ON_RESTART" = "true" ]; then
if [ -d /tmp/init-scripts/ ]; then
echo "* Running init script(s)..."
find '/tmp/init-scripts' -maxdepth 1 -executable -type f -exec sh -c '
echo " --> Running $1..."
printf "* Running init script(s)..."
# shellcheck disable=SC2016
find '/tmp/init-scripts' -maxdepth 1 -executable -type f -print0 | sort -z | xargs -0 -n1 sh -c '
printf "\n--> Running $1...\n"
if [ "$ON_INIT_SCRIPT_FAILURE" = "continue" ]; then
( sudo -g www-data -u www-data -- $1 ) || { echo "x $1 execution failed. Skipping."; }
(sudo -E -g www-data -u www-data -- $1) || { echo "x $1 execution failed. Skipping."; }
else
( sudo -g www-data -u www-data -- $1 ) || { echo "x $1 execution failed. Sleep and exit."; sleep 10; exit 6; }
(sudo -E -g www-data -u www-data -- $1) || { echo "x $1 execution failed. Sleep and exit."; sleep 10; exit 6; }
fi
' sh {} \;
' sh | awk 'BEGIN{RS="\n";ORS="\n "}1';
printf "\n";
else
echo "* No init script found, let's continue..."
echo "* No init script(s) found"
fi
touch $INIT_SCRIPTS_LOCK
else
Expand Down
7 changes: 7 additions & 0 deletions examples/README.md
@@ -0,0 +1,7 @@
# Examples

- [With custom scripts](./with-custom-scripts/)

## Contributing

Feel free to contribute to this project to add more documentation and examples!
35 changes: 35 additions & 0 deletions examples/with-custom-scripts/README.md
@@ -0,0 +1,35 @@
# Example: with custom scripts

This is an example of a running PrestaShop Flashlight with custom scripts.
See [./init-scripts](./init-scripts).

## Test the example

The expected output of this example is:

```
❯ docker-compose up prestashop
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
❯ docker-compose up prestashop
❯ docker compose up prestashop

[+] Building 0.0s (0/0) docker:desktop-linux
[+] Running 3/2
✔ Network with-custom-scripts_default Created 0.0s
✔ Container with-custom-scripts-mysql-1 Created 0.0s
✔ Container with-custom-scripts-prestashop-1 Created 0.0s
Attaching to with-custom-scripts-prestashop-1
with-custom-scripts-prestashop-1 | * Applying PS_DOMAIN (localhost:8000) to the dump...
with-custom-scripts-prestashop-1 | * Checking MySQL connectivity...
with-custom-scripts-prestashop-1 | * PHP PDO connectivity checked
with-custom-scripts-prestashop-1 | * PrestaShop MySQL client configuration set
with-custom-scripts-prestashop-1 | * Restoring MySQL dump...
with-custom-scripts-prestashop-1 | * MySQL dump restored!
with-custom-scripts-prestashop-1 | * Running init script(s)...
with-custom-scripts-prestashop-1 | --> Running /tmp/init-scripts/01-bretzel.sh...
with-custom-scripts-prestashop-1 | * 🥨 01 bretzel here
with-custom-scripts-prestashop-1 | CUSTOM_FOO contains bar
with-custom-scripts-prestashop-1 |
with-custom-scripts-prestashop-1 | --> Running /tmp/init-scripts/02-tarte-flambee.sh...
with-custom-scripts-prestashop-1 | * 🍕 02 tarte flambée here
with-custom-scripts-prestashop-1 | CUSTOM_MUSH contains room
with-custom-scripts-prestashop-1 |
with-custom-scripts-prestashop-1 | * Starting php-fpm...
with-custom-scripts-prestashop-1 | * Starting nginx...
```
44 changes: 44 additions & 0 deletions examples/with-custom-scripts/docker-compose.yml
@@ -0,0 +1,44 @@
services:
prestashop:
image: prestashop/prestashop-flashlight:latest
healthcheck:
test: ["CMD-SHELL", "curl -Isf http://localhost:80/robots.txt || exit 1"]
interval: 30s
timeout: 10s
retries: 10
depends_on:
mysql:
condition: service_healthy
environment:
- PS_DOMAIN=localhost:8000
- CUSTOM_FOO=bar
- CUSTOM_MUSH=room
volumes:
- ./init-scripts:/tmp/init-scripts
ports:
- 8000:80

mysql:
image: mariadb:lts
healthcheck:
test:
[
"CMD",
"mysqladmin",
"ping",
"--host=localhost",
"--user=root",
"--password=prestashop",
]
interval: 10s
timeout: 10s
retries: 5
environment:
- MYSQL_HOST=mysql
- MYSQL_USER=prestashop
- MYSQL_PASSWORD=prestashop
- MYSQL_ROOT_PASSWORD=prestashop
- MYSQL_PORT=3306
- MYSQL_DATABASE=prestashop
ports:
- 3306:3306
4 changes: 4 additions & 0 deletions examples/with-custom-scripts/init-scripts/01-bretzel.sh
@@ -0,0 +1,4 @@
#!/bin/sh
echo "* 🥨 01 bretzel here"
echo "CUSTOM_FOO contains $CUSTOM_FOO"

3 changes: 3 additions & 0 deletions examples/with-custom-scripts/init-scripts/02-tarte-flambee.sh
@@ -0,0 +1,3 @@
#!/bin/sh
echo "* 🍕 02 tarte flambée here"
echo "CUSTOM_MUSH contains $CUSTOM_MUSH"