Skip to content

brickpop/webtrigger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Web Trigger

Web Trigger is a Go service that listens for authenticated requests to trigger server commands on demand.

Get started

Copy the binary to a server folder like /usr/local/bin.

Service definition

Create a config file like the following and adapt it to suit your tasks, tokens and commands.

port: 5000
triggers:
  - id: my-action-1
    token: my-token-1
    command: /home/brickpop/deploy-prod.sh --param-1
    timeout: 20 # seconds
  - id: my-action-2
    token: my-token-2
    command: /home/brickpop/deploy-dev.sh "CLI arguments go here"
  # ...
tls: # optional
  certificate: ./fullchain.pem
  key: ./privkey.pem

Create the scripts for your triggers and make sure that they are executable.

Start the service

Start the service:

$ webtrigger my-config.yaml

Call a URL

Following the example config from above:

Trigger the task

Trigger a task by performing a POST request to its path with the Authorization header including the appropriate token.

$ curl -X POST -H "Authorization: Bearer my-token-1" http://localhost:5000/my-action-1
OK
$ curl -X POST -H "Authorization: Bearer my-token-2" http://localhost:5000/my-action-2
OK
$ curl -X POST -H "Authorization: Bearer bad-token" http://localhost:5000/my-action-2
Invalid token
$ curl -X POST -H "Authorization: Bearer my-token-2" http://localhost:5000/does-not-exist
Not found

Note: invoking a task that is already running will wait to start it again until the current execution has completed

Get the task status

A task can be in 4 different states:

$ curl -H "Authorization: Bearer my-token-1" http://localhost:5000/my-action-1
{"id":"my-action-1","status":"unstarted"}
$ curl -H "Authorization: Bearer my-token-1" http://localhost:5000/my-action-1
{"id":"my-action-1","status":"running"}
$ curl -H "Authorization: Bearer my-token-1" http://localhost:5000/my-action-1
{"id":"my-action-1","status":"done"}
$ curl -H "Authorization: Bearer my-token-1" http://localhost:5000/my-action-1
{"id":"my-action-1","status":"failed"}

Make it persistent

To make the service a system-wide daemon, create /etc/systemd/system/webtrigger.service

[Unit]
Description=Web Trigger service to allow running scripts from CI/CD jobs
After=network.target

[Service]
ExecStart=/usr/local/bin/webtrigger /path/to/config.yaml
# Required on some systems
#WorkingDirectory=/usr/local/bin
Restart=always
# Restart service after 10 seconds if the service crashes
RestartSec=10
# Output to syslog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=webtrigger
Type=simple
#User=<alternate user>
#Group=<alternate group>
Environment=

[Install]
WantedBy=multi-user.target
  • Specify User and Group to drop root privileges

Reload Systemd's config:

$ sudo systemctl daemon-reload

Enable the service:

$ sudo systemctl enable webtrigger.service

Start the service:

$ sudo systemctl start webtrigger.service

TO DO

  • Handle timeouts
  • Allow TLS certificates
  • Prevent blocking 3+ requests while still ongoing

About

Go service that listens for authenticated requests and triggers server scripts on demand.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages