-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from shizunge/readme
Add a webhook example
- Loading branch information
Showing
8 changed files
with
235 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# cronjob | ||
|
||
Use [*swarm-cronjob*](https://github.com/crazy-max/swarm-cronjob) to launch *gantry* at a given time. | ||
Use [*swarm-cronjob*](https://github.com/crazy-max/swarm-cronjob) to launch [*Gantry*](https://github.com/shizunge/gantry) at a specific time. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# webhook | ||
|
||
This example describes how to launch [*Gantry*](https://github.com/shizunge/gantry) via [adnanh/webhook](https://github.com/adnanh/webhook). | ||
|
||
## Setup | ||
|
||
We leverage a dockerized webhook image [lwlook/webhook](https://hub.docker.com/r/lwlook/webhook) which is based on the offical Docker image. This allows us to launch the *Gantry* service with simple docker commands. | ||
|
||
[hooks.json](./hooks.json) defines the webhook's behavior. It parses incoming payloads and transforms them into environment variables like `GANTRY_SERVICES_EXCLUDED`, `GANTRY_SERVICES_EXCLUDED_FILTERS` and `GANTRY_SERVICES_FILTERS`. These variables are then used by [run_gantry.sh](./run_gantry.sh) to control *Gantry* behaviors. which means you can update different services by passing different payloads to the webhook. Refer to the [adnanh/webhook](https://github.com/adnanh/webhook) repository for more advanced webhook configurations, including securing the webhook with `trigger-rule`. | ||
|
||
[run_gantry.sh](./run_gantry.sh) is responsible for launching the *Gantry* service. | ||
|
||
## Test | ||
|
||
Use the following command to deploy the Docker Compose stack that includes the webhook service. | ||
|
||
``` | ||
docker stack deploy --detach=true --prune --with-registry-auth --compose-file ./docker-compose.yml webhook | ||
``` | ||
|
||
Use curl to send a POST request to the webhook endpoint. This request tells the *Gantry* to only update the service named "webhook_webhook". | ||
|
||
``` | ||
curl -X POST localhost:9000/hooks/run-gantry -H "Content-Type: application/json" -d '{"GANTRY_SERVICES_FILTERS":"name=webhook_webhook"}' | ||
``` | ||
|
||
Check the webhook service logs to confirm if the webhook was triggered correctly. | ||
|
||
``` | ||
docker service logs webhook_webhook | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
version: "3.8" | ||
|
||
services: | ||
webhook: | ||
image: lwlook/webhook:latest | ||
command: | ||
- -verbose | ||
- -hooks=/hooks.json | ||
- -hotreload | ||
ports: | ||
- "9000:9000" | ||
configs: | ||
- source: hooks_json | ||
target: /hooks.json | ||
- source: run_gantry | ||
target: /run_gantry.sh | ||
mode: 0550 | ||
volumes: | ||
- /var/run/docker.sock:/var/run/docker.sock | ||
deploy: | ||
placement: | ||
constraints: | ||
- node.role==manager | ||
|
||
# Note: run_gantry.sh does not use this service by default. | ||
# This service is left here to demonstrate a potential approach for reusing the same service | ||
# by scaling its replicas instead of starting a new service each webhook request. | ||
# See function resume_gantry in run_gantry.sh. | ||
# Pros: | ||
# * This approach can work together with other launching methods like crazymax/swarm-cronjob. | ||
# Cons: | ||
# * Concurrency Issues: Sending webhook requests too frequently can increase the chance of the | ||
# webhook failing to launch Gantry correctly for some requests due to the existing service | ||
# potentially handling a previous command. | ||
gantry: | ||
image: shizunge/gantry:latest | ||
volumes: | ||
- /var/run/docker.sock:/var/run/docker.sock | ||
environment: | ||
- "GANTRY_NODE_NAME={{.Node.Hostname}}" | ||
- "GANTRY_SLEEP_SECONDS=0" | ||
deploy: | ||
replicas: 0 | ||
placement: | ||
constraints: | ||
- node.role==manager | ||
restart_policy: | ||
condition: none | ||
labels: | ||
# The label can be used to find this service. | ||
# This can be used together with resume_gantry function in the run_gantry.sh | ||
- webhook.run-gantry=true | ||
|
||
configs: | ||
hooks_json: | ||
name: hooks_json | ||
file: ./hooks.json | ||
run_gantry: | ||
name: run_gantry | ||
file: ./run_gantry.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[ | ||
{ | ||
"id": "run-gantry", | ||
"execute-command": "/run_gantry.sh", | ||
"command-working-directory": "/", | ||
"pass-environment-to-command": | ||
[ | ||
{ | ||
"source": "payload", | ||
"name": "GANTRY_SERVICES_EXCLUDED", | ||
"envname": "GANTRY_SERVICES_EXCLUDED" | ||
}, | ||
{ | ||
"source": "payload", | ||
"name": "GANTRY_SERVICES_EXCLUDED_FILTERS", | ||
"envname": "GANTRY_SERVICES_EXCLUDED_FILTERS" | ||
}, | ||
{ | ||
"source": "payload", | ||
"name": "GANTRY_SERVICES_FILTERS", | ||
"envname": "GANTRY_SERVICES_FILTERS" | ||
} | ||
] | ||
} | ||
] |
Oops, something went wrong.