100 Commits

Author SHA1 Message Date
7b0073cc90 modify print() calls 2019-07-18 17:04:01 -07:00
813c6d5540 fix path variable name typo 2019-07-17 15:51:40 -07:00
5f4e1417bd fix typos/bugs in pages pull script 2019-07-17 15:49:35 -07:00
e661c3a89e bring pages pull and pages init into harmony 2019-07-17 15:47:17 -07:00
cf5a54134f pages init setup script now working! 2019-07-17 15:41:34 -07:00
87e29c7ec8 get two init scripts (pages and subdomains) in sync 2019-07-17 15:34:39 -07:00
cf72be381e fix variables and typos in subdomains pull script 2019-07-17 15:16:36 -07:00
05be4a3922 fix typos and use executioner in subdomains pull script 2019-07-17 15:10:08 -07:00
1e43264923 check for existing clone path in subdomains init setup script 2019-07-17 15:09:44 -07:00
f682d0a85d update nginx-subdomains submodule 2019-07-17 15:00:08 -07:00
c4e76c7ca9 add executor and convert subdomains init setup to use executor 2019-07-17 14:59:04 -07:00
acc8ac8793 fix/finish pull scripts 2019-07-17 10:49:32 -07:00
d5c3f68406 include *_pull scripts for subdomains 2019-07-17 00:16:36 -07:00
43368d07bb updating nginx-subdomains submodule 2019-07-17 00:13:00 -07:00
7b06256767 Merge branch 'master' of github.com:charlesreid1-docker/pod-webhooks
* 'master' of github.com:charlesreid1-docker/pod-webhooks:
  remove docker-compose.yml from version control
  new nginx subdomains module commits
  update gitignore
  update how output script templates are organized
  fix template var values and typos
  fix dot git directory name
  fix typos and template variable values
2019-07-16 15:08:42 -07:00
c27af44804 add pull scripts for subdomains and pages 2019-07-16 15:08:16 -07:00
ef4a11f823 remove docker-compose.yml from version control
use template docker-compose.yml.j2 + ansible instead
2019-07-16 12:20:09 -07:00
6e7535fe05 new nginx subdomains module commits 2019-07-16 12:19:43 -07:00
d34dfb5a89 update gitignore 2019-07-16 12:17:36 -07:00
af9c6133ed update how output script templates are organized 2019-07-16 12:17:13 -07:00
f5e7045539 fix template var values and typos 2019-07-16 12:16:34 -07:00
957fc63936 fix dot git directory name 2019-07-16 12:16:18 -07:00
439ff53688 fix typos and template variable values 2019-07-16 12:15:57 -07:00
74e16651f6 add scripts to apply templates and create files for testing 2019-07-16 09:03:28 -07:00
be1c3cf67d Update captain hook pull host script
- remove non-templated version
2019-07-16 09:02:33 -07:00
32ab437d39 no clone command for source directory (not gonna do it) 2019-07-16 08:50:58 -07:00
85706f9e63 add clone of -src (master branch) to /www init setup 2019-07-15 16:36:31 -07:00
8773b5f365 template-ify all scripts 2019-07-15 16:10:40 -07:00
5a24540569 stormy captain hook container should wait for stormy nginx subdomains container 2019-07-14 11:49:33 -07:00
feff156691 update subdomains submodule (config file) 2019-07-13 08:07:52 -07:00
8ab5ab5bc7 update subdomains submodule 2019-07-13 07:13:59 -07:00
c084237bb1 quote integers to prevent type error 2019-07-13 07:13:22 -07:00
888c9a708c update gitignore 2019-07-13 00:35:19 -07:00
63cd836d63 add quotes around numbers 2019-07-13 00:34:24 -07:00
fabf35fbd7 update readme 2019-07-12 23:55:03 -07:00
0c647c4040 add pod-webhooks service template 2019-07-12 23:54:53 -07:00
a3d529a955 remove dockerpod-webhooks service 2019-07-12 23:51:42 -07:00
cf500132cf removing captn hook scripts, replaced with templates 2019-07-12 23:47:30 -07:00
7ac9e6bcac fix name of pod-webhooks service 2019-07-12 23:45:44 -07:00
a5ec57689d fix typo in subdomains init setup script 2019-07-12 23:16:53 -07:00
c3b5db300f transform captain hook canary scripts to templates 2019-07-12 23:10:39 -07:00
1d65e16f90 remove docker-compose and use template instead 2019-07-12 23:10:02 -07:00
a2a3b302ca update d-nginx-subdomains submodule/configuration file layout 2019-07-12 23:09:07 -07:00
7b9cf6ce71 update captain hook submodule 2019-07-12 23:08:48 -07:00
ddb8a78cc7 fix mkdocs page titles 2019-03-25 21:43:30 -07:00
b4a82bf3d1 update d-nginx-subdomains to latest 2019-03-25 13:20:10 -07:00
6207e0cae9 fix filenames 2019-03-23 22:35:05 -07:00
8e303efbd6 update all docs - rename, and fix up 2019-03-23 22:33:51 -07:00
3bf863d3c4 rename 2019-03-23 22:15:52 -07:00
10b839aafa update docs of pod-webhooks 2019-03-23 16:00:37 -07:00
97d6805567 update docs on captain hook 2019-03-23 15:36:17 -07:00
12ca200284 change www to /www 2019-03-23 14:46:27 -07:00
ee3c242b71 expand on logging options in docker-compose 2019-03-23 11:55:06 -07:00
b64e79ec5f subdomains /www init setup uses git.charlesreid1.com and github.com repos 2019-03-23 11:44:33 -07:00
4f6f848f4d add scripts to do initial setup of subdomains/pages dir structure 2019-02-20 15:36:02 -08:00
b4828fa3b5 Merge branch 'master' of https://git.charlesreid1.com/docker/pod-webhooks
* 'master' of https://git.charlesreid1.com/docker/pod-webhooks:
  add json logging to pod-webhooks
  update captain hook submodule pointer
  add canary scripts & pull host scripts
2019-02-20 15:35:26 -08:00
abb106878d add json logging to pod-webhooks 2019-02-20 15:33:58 -08:00
a6a528aa73 update captain hook submodule pointer 2019-02-20 15:33:35 -08:00
ba40354bdf Merge branch 'master' of https://git.charlesreid1.com/docker/pod-webhooks
* 'master' of https://git.charlesreid1.com/docker/pod-webhooks:
  add init pages setup script (untested) for pod-webhooks role
  add script to make dir structure pod expects
2019-02-20 15:31:51 -08:00
309166757e add canary scripts & pull host scripts 2019-02-20 15:30:43 -08:00
3b8ecc1495 add mor info about init pages setup 2019-02-20 14:29:41 -08:00
01261d0555 add init pages setup script (untested) for pod-webhooks role 2019-02-20 01:15:32 -08:00
dfd4c126c3 add script to make dir structure pod expects 2019-01-27 12:08:04 -08:00
354c30c5bc reorder docker-compose, no changes 2018-09-08 19:35:20 -07:00
7fc0b506ef update captain hook and nginx subdomains module 2018-09-08 19:35:02 -07:00
1bb5e20f5c note on 403 2018-09-08 16:54:02 -07:00
4ed9203956 add startup script to launch pod webhooks 2018-09-08 16:45:15 -07:00
2682b54f13 Update mkdocs.yml for mkdocs 1.0 2018-08-11 12:34:21 -07:00
d1ba594cff Update mkdocs-material for mkdocs 1.0 2018-08-11 12:21:54 -07:00
1534435add fix link, and add custom css 2018-07-22 14:14:58 -07:00
60432dd805 update links 2018-07-22 14:12:20 -07:00
5b4f409e22 blue uniform 2018-07-22 14:12:13 -07:00
bab7cb0c94 bump captain hook submodule pointer 2018-07-22 14:07:38 -07:00
c61f0500ea mount git directories too, not just htdocs. 2018-07-22 12:28:25 -07:00
a3e05595b2 fix executabilification issue 2018-07-22 10:41:50 -07:00
1e1e5e34c9 update captain hook submodule pointer 2018-07-22 01:29:31 -07:00
9b793eabf8 bringing captain hook submodule pointer up to the latest 2018-07-22 01:13:09 -07:00
b4bb9cee95 update captain hook submodule to fix bugs 2018-07-22 00:45:49 -07:00
769abb65a8 update captain hook submodule pointer 2018-07-21 17:11:30 -07:00
e1db6e9695 Merge branch 'docs-dir' of docker/pod-webhooks into master 2018-07-22 00:07:32 +00:00
23f283a135 update gitignore, add mkdocs toc, and move readme to index 2018-07-21 17:04:17 -07:00
88d1f72637 Merge branch 'docs-dir' of docker/pod-webhooks into master 2018-07-22 00:03:10 +00:00
3247017f27 add mkdocs material theme for mkdocs 2018-07-21 16:58:35 -07:00
1fb1aebfe2 update layout of docs to use docs/, readme is shorter 2018-07-21 16:50:46 -07:00
dcd390ccd4 bring captain hook up to latest version, which fixes the hook server 2018-07-21 16:22:22 -07:00
ffe4ced8fb update start/stop/enable/disable info 2018-07-21 16:21:54 -07:00
223c74c6cf update captain hook submodule 2018-07-21 16:10:32 -07:00
8cb3cdfee4 remove :ro flag for captain hook 2018-07-21 15:59:54 -07:00
eb5fa15800 update captain hook submdoule pointer 2018-07-21 15:59:04 -07:00
2ee0e8eb71 update port 7777+ 2018-07-21 15:35:44 -07:00
1100088f5f fix bind-mounting directories for pages, hooks, and bots 2018-07-21 15:34:56 -07:00
667cee6494 add systemctl commands, and port numbering info 2018-07-21 15:33:41 -07:00
b2687f5f38 more updates: mention blackbeard scripts volumes 2018-07-21 15:17:12 -07:00
25ad617241 clarify volumes info 2018-07-21 15:09:39 -07:00
5c040783d4 add description of captain hooks canary, plus services. 2018-07-21 14:57:39 -07:00
3b0f845ca6 update with info on services, adding hooks, subdomains, captain hook new hooks, network setup, etc. 2018-07-21 14:13:31 -07:00
522995f512 add adding file to cover adding hooks 2018-07-21 13:32:37 -07:00
5fc1c4f19e update docker-compose to mount hooks and triggers directory 2018-07-21 13:27:57 -07:00
a5423ebe5b Merge branch 'fix-submodules' of docker/pod-webhooks into master 2018-07-21 18:55:38 +00:00
423b6cf9a7 Merge branch 'fix-submodules' of docker/pod-webhooks into master 2018-07-20 18:49:40 +00:00
29 changed files with 1561 additions and 107 deletions

12
.gitignore vendored Normal file
View File

@@ -0,0 +1,12 @@
#mkdocs
site/
docker-compose.yml
# ignore scripts generated from templates
scripts/pages_init_setup.py
scripts/subdomains_init_setup.py
scripts/captain-hook-canary.service
scripts/captain_hook_canary.sh
scripts/captain_hook_pull_host.py
scripts/pod-webhooks.service
scripts/output/

3
.gitmodules vendored
View File

@@ -4,3 +4,6 @@
[submodule "d-nginx-subdomains"]
path = d-nginx-subdomains
url = https://git.charlesreid1.com/docker/d-nginx-subdomains.git
[submodule "mkdocs-material"]
path = mkdocs-material
url = https://git.charlesreid1.com/charlesreid1/mkdocs-material

View File

@@ -7,40 +7,34 @@ This docker pod runs two services:
* Static content server for subdomain pages (nginx)
* [d-nginx-subdomains](https://git.charlesreid1.com/docker/d-nginx-subdomains)
These two services are in this repo as submodules.
## How It Works
See [Running.md](/Running.md) for info about running this docker pod.
* Running the Docker Pod from Comand Line
* Workflow for Docker Pod Updates
See [Services.md](/Services.md) for info about running startup services.
* Running the Docker Pod as a Startup Service
* Running Docker-Host Canary Script
## Volumes
This docker container needs to share a few folders:
This uses one shared volume between the two containers.
Captain Hook updates the static content based on web hooks that it receives,
while the nginx container serves up the static content.
## Servers
This pod runs on blackbeard.
The nginx service is reverse-proxied HTTP with krash,
and accessible at ports 7777-7779 and up.
The Captain Hook webhook server is also reverse-proxied HTTP.
The krash nginx server will handle all traffic to
`https://hooks.charlesreid1.com` except URLs prefixed
with `webhook`, which are forwarded on to Captain Hook
on port 5000.
These two services are in this repo as submodules.
## Links
See documentation page here: <https://pages.charlesreid1.com/pod-webhooks>
Or visit [docs/index.md](/docs/index.md)
Source code on git.charlesreid1.com: <https://git.charlesreid1.com/docker/pod-webhooks>
Source code on github.com: <https://github.com/charlesreid1-docker/pod-webhooks>
## If you get a 403
If you visit the IP of the page running things and you get a 403,
it's probably because you haven't created the `/www/` directory
structure yet.
To do this, you need a folder for each subdomain at `/www/`, along
with subdirectories for `htdocs` and for the `.git` directory
(see `scripts/` directory for scripts to do this automatically):
```
/www/pages.charlesreid1.com/
/www/bots.charlesreid1.com/
/www/hooks.charlesreid1.com/
```

View File

@@ -1,58 +0,0 @@
## Running Hooks-Subdomains Docker Pod as Startup Service
The webhooks-subdomains docker pod requires two startup
services - one keeps the docker pod running, the other
watches a folder shared between the host and container
for signals from the container, and uses that to trigger
updates to the subdomains web content.
Also see the `services/` folder of the
[dotfiles/debian repository](https://git.charlesreid1.com/dotfiles/debian),
repository for the systemd services.
### Service 1: Webhooks Docker Pod
This service keeps the webhooks docker pod service running
continuously. If the pod stops, this service will restart it.
(This service should not be running if you are troubleshooting
the docker pod.)
**`dockerpod-webhooks.service:`**
```
[Unit]
Description=webhooks and subdomains docker pod
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/local/bin/docker-compose -f /home/charles/codes/docker/pod-webhooks/docker-compose.yml up
ExecStop=/usr/local/bin/docker-compose -f /home/charles/codes/docker/pod-webhooks/docker-compose.yml down
[Install]
WantedBy=default.target
```
Now install the service to `/etc/systemd/system/dockerpod-webhooks.servce`,
and activate it:
```
sudo systemctl enable dockerpod-webhooks.service
```
Now you can start/stop the service with:
```
sudo systemctl (start|stop) dockerpod-webhooks.service
```
As mentioned above, stop the serivce with
`sudo systemctl stop dockerpod-webhooks.service` before
doing a `docker-compose stop` or a `docker-compose up --build`
to keep the pod from respawning in the middle of the task.

View File

@@ -1,4 +1,4 @@
version: "3.1"
version: "3.3"
services:
stormy_nginx_subs:
@@ -7,18 +7,36 @@ services:
command: /bin/bash -c "nginx -g 'daemon off;'"
volumes:
- "./d-nginx-subdomains/conf.d:/etc/nginx/conf.d"
- "stormy_webhooks_data:/www"
- "/www/pages.charlesreid1.red/htdocs:/www/pages.charlesreid1.red/htdocs:ro"
- "/www/hooks.charlesreid1.red/htdocs:/www/hooks.charlesreid1.red/htdocs:ro"
- "/www/bots.charlesreid1.red/htdocs:/www/bots.charlesreid1.red/htdocs:ro"
logging:
driver: "json-file"
options:
max-size: "1m"
max-file: "10"
ports:
- "7777:7777"
- "7778:7778"
- "7779:7779"
stormy_captain_hook:
restart: always
build: b-captain-hook
volumes:
- "/www/pages.charlesreid1.red:/www/pages.charlesreid1.red"
- "/www/hooks.charlesreid1.red:/www/hooks.charlesreid1.red"
- "/www/bots.charlesreid1.red:/www/bots.charlesreid1.red"
- "/tmp/triggers:/tmp/triggers"
- "./b-captain-hook/config.json:/app/config.json"
- "./b-captain-hook/hooks:/app/hooks"
logging:
driver: "json-file"
options:
max-size: "1m"
max-file: "10"
ports:
- "5000:5000"
volumes:
- "stormy_webhooks_data:/www"
depends_on:
- stormy_nginx_subs
volumes:
stormy_webhooks_data:

42
docker-compose.yml.j2 Normal file
View File

@@ -0,0 +1,42 @@
version: "3.3"
services:
stormy_nginx_subs:
restart: always
image: nginx
command: /bin/bash -c "nginx -g 'daemon off;'"
volumes:
- "./d-nginx-subdomains/conf.d:/etc/nginx/conf.d"
- "/www/pages.{{ server_name_default }}/htdocs:/www/pages.{{ server_name_default }}/htdocs:ro"
- "/www/hooks.{{ server_name_default }}/htdocs:/www/hooks.{{ server_name_default }}/htdocs:ro"
- "/www/bots.{{ server_name_default }}/htdocs:/www/bots.{{ server_name_default }}/htdocs:ro"
logging:
driver: "json-file"
options:
max-size: "1m"
max-file: "10"
ports:
- "7777:7777"
- "7778:7778"
- "7779:7779"
stormy_captain_hook:
restart: always
build: b-captain-hook
volumes:
- "/www/pages.{{ server_name_default }}:/www/pages.{{ server_name_default }}"
- "/www/hooks.{{ server_name_default }}:/www/hooks.{{ server_name_default }}"
- "/www/bots.{{ server_name_default }}:/www/bots.{{ server_name_default }}"
- "/tmp/triggers:/tmp/triggers"
- "./b-captain-hook/config.json:/app/config.json"
- "./b-captain-hook/hooks:/app/hooks"
logging:
driver: "json-file"
options:
max-size: "1m"
max-file: "10"
ports:
- "5000:5000"
depends_on:
- stormy_nginx_subs

44
docs/adding.md Normal file
View File

@@ -0,0 +1,44 @@
## Adding New Hooks
To add a hook to Captain Hook:
1. Create an executable script in [bots/b-captain-hook](https://git.charlesreid1.com/bots/b-captain-hook)
on git.charlesreid1.com with the name of the action, the name of the repo (not the owner),
and the name of the branch in the filename. For example, `push-my-dotfiles-master` would be
matched every time I `push` changes to the `master` branch of any repository named `my-dotfiles`.
2. Add, commit, and push your hook to the master branch of Captain Hook
3. Wait about 15 seconds for the canary script to run (it has to update
the Captain Hook git repo running on the remote server to the latest version
and restart the webhooks-subdomains docker pod.)
4. Open the `my-dotfiles` repository on git.charlesreid1.com, go to the
Settings > Webhooks page, and add a Gitea webhook.
5. Enter info:
a. Payload URL is the Captain Hook server, which is `https://hooks.charlesreid1.com/webhook`.
b. Content type is `application/json`
c. Secret is (that's my little secret)
d. Pick what events you would like to trigger webhooks, usually "just the push event"
6. Save the webhook, then click on the webhook again to open it back up.
Scroll down to the bottom right and click "Test Delivery".
You should see a green success sign.
## Debugging Failed Hooks
If you see a red warning sign:
* Ensure the webhooks docker pod is actually running okay (`docker ps` on the host machine)
* Ensure port 5000 is open in the Captain Hook container, and on the host machine
* Ensure you can see port 5000 of the `pod-webhooks` host machine from the `pod-charlesreid1` host machine
* Ensure there is actually a hook in the `hooks/` directory of the Captain Hook repo
Captain Hook repo: <https://git.charlesreid1.com/bots/b-captain-hook>
Captain Hook repo (Github mirror): <https://github.com/charlesreid1-docker/b-captain-hook>

148
docs/canary.md Normal file
View File

@@ -0,0 +1,148 @@
# Captain Hook's Canary
First things first: Captain Hook is the webhook server that
is part of the webhooks docker pod. It receives webhooks
from Github and Gitea and uses them to trigger scrips.
Links to [documentation](https://pages.charlesreid1.com/b-captain-hook)
and [code](https://git.charlesreid1.com/bots/b-captain-hook)
for Captain Hook.
Captain Hook's Canary is a mechanism by which the Captain Hook
webhooks server (running in a docker container) can trigger an action
on the host machine (running the pod). In this case the action is to
update Captain Hook and restart the docker pod anytime a webhook is
received indicating the Captain Hook repo has changed.
This is done by bind-mounting a host directory at `/tmp/triggers/`
inside the Captain Hook docker container. When Captain Hook receives
a webhook from Github or Gitea that indicates the Captain Hook
repo (<https://git.charlesreid1.com/bots/b-captain-hook> or
<https://github.com/charlesreid1/captain-hook>), it creates a
trigger file.
Meanwhile, on the host that is running the docker pod, a service
script is running continuously to check for that trigger file
every 10 seconds. If the trigger file is seen, it updates the
Captain Hook git repository on the host machine and then restarts
the docker pod.
Sections below cover the following scripts, all run on the host:
* The canary bash script
* The docker host pull script
* The canary statup service
## The Canary Bash Script
Note: this needs an associated systemd service.
See the services directory of the dotfiles repo.
This is a canary script for connecting
the Captain Hook container to the host
machine, and triggering tasks on the
host machine with webhooks.
The Captain Hook container mounts the
following host directory inside the
container (same location for host/container):
```
/tmp/triggers/
```
When a webhook in Captain Hook wants to
trigger an event on the host (blackbeard),
it puts a file in `/tmp/triggers/`.
Meanwhile, on the host, this script checks
every 10 seconds for trigger files.
Each webhook can create its own trigger file,
and this script processes each trigger differently.
```bash
#!/bin/bash
TRIGGER="/tmp/triggers/push-b-captain-hook-master"
UPDATE_SCRIPT="${HOME}/pod-webhooks/scripts/captain_hook_pull_host.py"
while true
do
# bootstrap-pull captain hook
if [ -f "$TRIGGER" ]; then
echo "CAPTAIN HOOK'S CANARY:"
echo "Running trigger to update Captain Hook on the host machine (user charles)"
sudo -H -u charles python $UPDATE_SCRIPT
echo "All done."
rm -f ${TRIGGER}
fi
sleep 10;
done
```
## The Pull Host Captain Hook Script
Next we have a python script that actually updates the host's
version of Captain Hook:
```python
#!/usr/bin/env python3
import subprocess
import os
import time
"""
Captain Hook: Pull Captain Hook on the Host
This script is called by the host machine
(blackbeard) running the Captain Hook container.
This is triggered by push actions to the
master branch of b-captain-hook.
The action is to update (git pull) the copy
of Captain Hook running on the host, and
restart the container pod.
"""
work_dir = os.path.join('/home','charles','pod-webhooks','b-captain-hook')
# Step 1:
# Update Captain Hook
pull_cmd = ['git','pull','origin','master']
subprocess.call(pull_cmd, cwd=work_dir)
time.sleep(5)
# Step 2:
# Restart Captain Hook pod
pod_restart = ['docker-compose','restart']
subprocess.call(pod_restart, cwd=work_dir)
```
## The Canary Startup Script
Here is the startup file that runs the Captain Hook's Canary bash script.
The stop directive uses pgrep to find the process id and stops any PIDs returned.
```
[Unit]
Description=captain hook canary script
Requires=pod-webhooks.service
After=pod-webhooks.service
[Service]
Restart=always
ExecStart=/home/charles/pod-webhooks/scripts/captain_hook_canary.sh
ExecStop=/usr/bin/pgrep -f captain_hook_canary | /usr/bin/xargs /bin/kill
[Install]
WantedBy=default.target
```
See [Services](services.md) for more info on what to do with this file.

11
docs/css/custom.css Normal file
View File

@@ -0,0 +1,11 @@
.md-typeset h1 { font-weight: 600; }
.md-typeset h2 { font-weight: 600; }
.md-typeset h3 { font-weight: 600; }
.md-typeset h4 { font-weight: 600; }
body {
background-color: #FAFAFA;
}
div.body {
background-color: #FAFAFA;
}

176
docs/index.md Normal file
View File

@@ -0,0 +1,176 @@
# pod-webhooks
This docker pod runs two services:
* Captain Hook webhook server (python + flask)
* [b-captain-hook](https://git.charlesreid1.com/bots/b-captain-hook)
* Static content server for subdomain pages (nginx)
* [d-nginx-subdomains](https://git.charlesreid1.com/docker/d-nginx-subdomains)
These two services are in this repo as submodules.
## Links
See documentation page here: <https://pages.charlesreid1.com/pod-webhooks>
Source code on git.charlesreid1.com: <https://git.charlesreid1.com/docker/pod-webhooks>
Source code on github.com: <https://github.com/charlesreid1-docker/pod-webhooks>
## Initial Setup of Pages Subdomain
The pages.charlesreid1.com subdomain is served from content
in the directory `/www/pages.charlesreid1.com`.
To set this up with pages and sites that should exist already,
use the set up script at `scripts/pages_init_setup.py` and
run it on the pages.charlesreid1.com server:
```
python scripts/pages_init_setup.py
```
This will create the `/www/pages.charlesreid1.com/` directory
structure and will clone several repositories to populate it
with content.
## Adding Hooks
Since this is probably the only thing you'll care about once everything
is actually running... until it breaks.
[How To Add A Hook](adding.md)
## How It Works
See [Running.md](running.md) for info about running this docker pod.
* Running the Docker Pod from Comand Line
* Workflow for Docker Pod Updates
See [Services.md](services.md) for info about running startup services.
* Running the Docker Pod as a Startup Service
* Running Captain Hook's Canary (Script)
Enable/disable service (installs/uninstalls, but does not start):
```
sudo systemctl (enable|disable) pod-webhooks.service
sudo systemctl (enable|disable) captain-hook-canary.service
```
Start/stop:
```
sudo systemctl (start|stop) pod-webhooks.service
sudo systemctl (start|stop) captain-hook-canary.service
```
See [Captain Hook's Canary](canary.md) for details on the canary script that allows the
webhooks docker pod to trigger itself to be re-loaded when there are new hooks
added to captain hook.
## Volumes and Files
### Subdomains
The static files hosted on charlesreid1.com subdomains are contained in
subdirectories of `/www/*.charlesreid1.com/` and this is
mounted by the subdomains docker container that has rules
set up for which subdomains to serve.
### Captain Hook
Captain Hook mounts the `/www` folder, which is served by the subdomains
nginx server, as well as the hooks folder in the Captain Hook repository
(that's at `b-captain-hook/hooks`).
When there is a change pushed to a particular branch on git.charlesreid1.com,
the git.charlesreid1.com server will check if there is a corresponding hook that's
been added to Captain Hook for that repo and branch. If so, git.charlesreid1.com
runs that script. For pages.charlesreid1.com, that's usually just a git pull
on the contents of `/www/pages.charlesreid1.com/my-page`.
### Captain Hook's Canary
Captain Hook presents a bit of a paradox: what happens when Captain Hook installs
a hook for itself, and detects that Captain Hook itself has changed?
In this case, the webhooks docker pod needs to be able to tell the host machine
to restart the webhooks docker pod.
This is done by Captain Hook's Canary. This is a service script that checks every
10 seconds for a trigger file in a directory mounted between the host and container.
If the trigger file is present, the host will update its copy of Captain Hook,
then restart the webhooks-subdomains docker pod.
As per the [dotfiles/debian](https://git.charlesreid1.com/dotfiles/debian) repo,
the `captain_hook_canary.sh` canary will restart the webhooks docker pod if it
detects the presence following file:
```
/tmp/triggers/push-b-captain-hook-master
```
(The canary script will remove this file once it has restarted the webhooks docker pod.)
Now, a hook can be added to Captain Hook that will be run when there is a push event
on the master branch of [bots/b-captain-hook](https://git.charlesreid1.com/bots/b-captain-hook).
By creating a hook named `push-b-captain-hook-master` in the
`hooks/` directory of Captain Hook, and having it simply run
`touch /tmp/triggers/push-b-captain-hook-master`,
this webhook can trigger the Captain Hook's Canary service script,
which triggers a restart of the webhooks docker pod.
Code: <https://git.charlesreid1.com/bots/b-captain-hook/src/branch/master/hooks/push-b-captain-hook-master>
## Network
The `d-subodomains-nginx` container opens different ports for different
subdomains, and reverse-proxies requests from charlesreid1.com.
The port numbering starts at 7777 for pages.charlesreid1.com
and goes up from there, one port per subdomain.
Also see [pod-charlesreid1](https://git.charlesreid1.com/docker/pod-charlesreid1)
on git.charlesreid1.com for the nginx reverse proxy configuration.
Captain Hook runs a Flask server on port 5000 and listens for triggers
from git.charlesreid1.com (gitea) web hooks. These web hooks must have
the correct secret or the trigger will be ignored.
## Servers
[pod-webhooks](https://pages.charlesreid1.com/pod-webhooks/)
runs on a virtual server somewhere.
This pod's nginx service provides a backend that is
reverse-proxied by the machine running
[pod-charlesreid1](https://pages.charlesreid1.com/pod-charlesreid1)
(and the whole <https://charlesreid1.com> frontend).
It opens ports 7777+ and up (one per subdomain).
The [pod-charlesreid1](https://pages.charlesreid1.com/pod-charlesreid1)
docker pod (actually the
[d-nginx-charlesreid1](https://pages.charlesreid1.com/d-nginx-charlesreid1/)
submodule in that pod) contains the nginx
config files that control the reverse proxy
behavior of <https://charlesreid1.com>.
Like the nginx service in this webhooks pod, the Captain Hook
webhook service is also reverse-proxied by the main nginx frontend
running <https://charlesreid1.com> with the
[pod-charlesreid1](https://pages.charlesreid1.com/pod-charlesreid1)
docker pod. If a URL request for <https://hooks.charlesreid1.com>
is received by the <https://charlesreid1.com> server,
it is forwarded to the machine running the [pod-webhooks](https://git.charlesreid1.com/pod-webhooks)
docker pod, which decides whether it is a POST request
(that should be sent to this pod's Captain Hook) or a GET request
(that should be sent to this pod's nginx).

View File

@@ -1,12 +1,13 @@
## The Docker Compose File
The `docker-compose.yml` file contains all the directives needed
to run a docker pod for captain hook and the subdomains nginx server.
The `docker-compose.yml` file contains everything needed to
run the webhooks docker pod: a Captain Hook webhook server, and
an nginx server to serve up static pages for each subdomain.
Why use docker-compose instead of docker?
docker-compose is the preferred way to run multiple containers.
## Running Webhooks-Subdomains Docker Pod from Command Line
## Running Webhooks Docker Pod from Command Line
Run the pod in the foreground or background by running these
commands from the directory containing `docker-compose.yml`:
@@ -16,13 +17,15 @@ docker-compose up # interactive
docker-compose up -d # detached
```
If you want to rebuild all the containers before running up,
If you want to rebuild all the containers before bringing
the pod up, add the `--build` flag:
```
docker-compose up --build
```
If you just want to rebuild the containers,
If you just want to rebuild the containers without bringing
them up,
```
docker-compose build
@@ -35,7 +38,7 @@ docker-compose build --no-cache
```
***WARNING:*** this will re-download all aptitude packages,
which can be extremely slow. Use with caution.)
which can be extremely slow. Use with caution.
You can restart all containers in a pod using the restart command:

93
docs/services.md Normal file
View File

@@ -0,0 +1,93 @@
## Running Hooks-Subdomains Docker Pod as Startup Service
The webhooks-subdomains docker pod requires two startup
services:
* Docker pod service - this startup service keeps the pod running,
and will restart it if it crashes
* Captain Hook's canary - this watches a folder that is shared between
the docker pod host and the Captain Hook container, which allows the
pod to send triggers to the host.
Also see the `scripts/` folder of this repo,
[pod-webhooks](https://git.charlesreid1.com/docker/pod-webhooks)
([Github mirror](https://github.com/charlesreid1-docker/pod-webhooks)).
### Docker Pod Startup Service
This service keeps the webhooks docker pod service running
continuously. If the pod stops, this service will restart it.
(This service should not be running if you are troubleshooting
the docker pod, otherwise every time you try and stop the pod
it will respawn.)
**`pod-webhooks.service:`**
```
[Unit]
Description=webhooks and subdomains docker pod
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/local/bin/docker-compose -f /home/charles/codes/docker/pod-webhooks/docker-compose.yml up
ExecStop=/usr/local/bin/docker-compose -f /home/charles/codes/docker/pod-webhooks/docker-compose.yml down
[Install]
WantedBy=default.target
```
### Captain Hook's Canary Startup Service
This service just watches a folder for a particular
watchfile, and runs a script if it sees the watchfile
appear.
**`captain-hook-canary.service:`**
```
[Unit]
Description=captain hook canary script
Requires=pod-webhooks.service
After=pod-webhooks.service
[Service]
Restart=always
ExecStart=/home/charles/pod-webhooks/scripts/captain_hook_canary.sh
ExecStop=/usr/bin/pgrep -f captain_hook_canary | /usr/bin/xargs /bin/kill
[Install]
WantedBy=default.target
```
### Installing Services
Install the services by copying the `*.service` files
to `/etc/systemd/system/dockerpod-webhooks.servce`
and `/etc/systemd/system/captain-hook-canary.servce`,
and activate the startup services:
```
sudo systemctl enable pod-webhooks.service
sudo systemctl enable captain-hook-canary.service
```
Now you can start/stop the services with:
```
sudo systemctl (start|stop) pod-webhooks.service
sudo systemctl (start|stop) captain-hook-canary.service
```
As mentioned above, these services should be stopped before
doing a `docker-compose stop` or a `docker-compose up --build`
to keep the pod from respawning in the middle of the task.

1
mkdocs-material Submodule

Submodule mkdocs-material added at b0c6890853

38
mkdocs.yml Normal file
View File

@@ -0,0 +1,38 @@
site_name: pod-webhooks
site_url: https://pages.charlesreid1.com/pod-webhooks
repo_name: pod-webhooks
repo_url: https://git.charlesreid1.com/docker/pod-webhooks
edit_uri: ""
copyright: 'Copyright &copy; 2018 <a href="https://charlesreid1.com">Charles Reid</a>, released under the <a href="https://opensource.org/licenses/MIT">MIT license</a>'
docs_dir: docs
site_dir: site
extra_css:
- css/custom.css
theme:
name: null
custom_dir: 'mkdocs-material/material'
palette:
primary: 'blue'
accent: 'blue'
logo:
icon: 'dns'
font:
text: 'Roboto'
code: 'Roboto Mono'
nav:
- "Home": "index.md"
- "Adding Webhooks": "adding.md"
- "Running Captain Hook": "running.md"
- "Captain Hook Startup Services": "services.md"
- "Captain Hook's Canary": "canary.md"
# Extensions
markdown_extensions:
- admonition
- codehilite:
guess_lang: false
- toc:
permalink: true
strict: true

24
scripts/Readme.md Normal file
View File

@@ -0,0 +1,24 @@
# pod-webhooks scripts
Copy this `pod-webhooks.service` service script to
`/etc/systemd/system/pod-webhooks.service`:
```
sudo cp pod-webhooks.service /etc/systemd/system/pod-webhooks.service
```
Enable/disable the service:
```
sudo systemctl enable pod-webhooks
sudo systemctl disable pod-webhooks
```
Start/restart/stop the service:
```
sudo systemctl start pod-webhooks
sudo systemctl restart pod-webhooks
sudo systemctl stop pod-webhooks
```

View File

@@ -0,0 +1,89 @@
import os, re, sys
import glob
from jinja2 import Environment, FileSystemLoader, select_autoescape
"""
Apply Default Values to
Captain Hook Jinja Templates
This script applies default values to
templates in this folder.
The templates are used by Ansible,
but this script uses the same template
engine as Ansible to apply template
variable values to the template files
and make real files.
variables are:
- `webhooks_install_dir` - location of pod-webhooks repo
- `username` - user/group name to change ownership to
- `server_name_default` - name of server
(e.g., charlesreid1.com or charlesreid1.red)
"""
# Where templates live
TEMPLATEDIR = '.'
# Where rendered templates will go
OUTDIR = 'output'
# Should existing (destination) files
# be overwritten if they exist?
OVERWRITE = True
# Template variables
TV = {
'server_name_default': 'charlesreid1.red',
'username': 'charles',
'webhooks_install_dir' : '/home/charles/pod-webhooks'
}
def apply_templates(template_dir, output_dir, template_vars, overwrite=False):
"""Apply the template variables
to the template files.
"""
if not os.path.exists(output_dir):
msg = "Error: output dir %s does not exist!"%(output_dir)
raise Exception(msg)
if not os.path.exists(template_dir):
msg = "Error: template dir %s does not exist!"%(output_dir)
raise Exception(msg)
# Jinja env
env = Environment(loader=FileSystemLoader('.'))
# Render templates
template_files = glob.glob('captain*.j2')
render_files = [re.sub('\.j2','',s) for s in template_files]
for rfile,tfile in zip(render_files,template_files):
# Get rendered template content
content = env.get_template(tfile).render(**template_vars)
# Write to file
dest = os.path.join(output_dir,rfile)
if os.path.exists(dest) and overwrite is False:
msg = "Error: template rendering destination %s already exists!"%(dest)
raise Exception(msg)
with open(dest,'w') as f:
f.write(content)
print("Rendered the following templates:%s\nOutput files:%s\n"%(
"".join(["\n- "+os.path.join(template_dir,j) for j in template_files]),
"".join(["\n- "+os.path.join(output_dir,j) for j in render_files])
))
if __name__=="__main__":
apply_templates(TEMPLATEDIR,OUTDIR,TV,OVERWRITE)

View File

@@ -0,0 +1,84 @@
import os, re, sys
import glob
from jinja2 import Environment, FileSystemLoader, select_autoescape
"""
Apply Default Values to
pod-webhooks Jinja Templates
This script applies default values to
templates in this folder.
The templates are used by Ansible,
but this script uses the same template
engine as Ansible to apply template
variable values to the template files
and make real files.
variables are:
- `webhooks_install_dir` - location of pod-webhooks repo
(i.e., location of pod-webhooks docker-compose file)
"""
# Where templates live
TEMPLATEDIR = '.'
# Where rendered templates will go
OUTDIR = 'output'
# Should existing (destination) files
# be overwritten if they exist?
OVERWRITE = True
# Template variables
TV = {
'webhooks_install_dir' : '/home/charles/pod-webhooks'
}
def apply_templates(template_dir, output_dir, template_vars, overwrite=False):
"""Apply the template variables
to the template files.
"""
if not os.path.exists(output_dir):
msg = "Error: output dir %s does not exist!"%(output_dir)
raise Exception(msg)
if not os.path.exists(template_dir):
msg = "Error: template dir %s does not exist!"%(output_dir)
raise Exception(msg)
# Jinja env
env = Environment(loader=FileSystemLoader('.'))
# Render templates
template_files = glob.glob('*.service.j2')
render_files = [re.sub('\.j2','',s) for s in template_files]
for rfile,tfile in zip(render_files,template_files):
# Get rendered template content
content = env.get_template(tfile).render(**template_vars)
# Write to file
dest = os.path.join(output_dir,rfile)
if os.path.exists(dest) and overwrite is False:
msg = "Error: template rendering destination %s already exists!"%(dest)
raise Exception(msg)
with open(dest,'w') as f:
f.write(content)
print("Rendered the following templates:%s\nOutput files:%s\n"%(
"".join(["\n- "+os.path.join(template_dir,j) for j in template_files]),
"".join(["\n- "+os.path.join(output_dir,j) for j in render_files])
))
if __name__=="__main__":
apply_templates(TEMPLATEDIR,OUTDIR,TV,OVERWRITE)

View File

@@ -0,0 +1,90 @@
import os, re, sys
import glob
import subprocess
from jinja2 import Environment, FileSystemLoader, select_autoescape
"""
Apply Default Values to
SUBDOMAINS Jinja Templates
This script applies default values to
templates in this folder.
The templates are used by Ansible,
but this script uses the same template
engine as Ansible to apply template
variable values to the template files
and make real files.
only variables are:
- `username` - user/group name to change ownership to
- `server_name_default` - name of server
(e.g., charlesreid1.com or charlesreid1.red)
"""
# Where templates live
TEMPLATEDIR = '.'
# Where rendered templates will go
OUTDIR = 'output'
# Should existing (destination) files
# be overwritten if they exist?
OVERWRITE = True
# Template variables
TV = {
'server_name_default': 'charlesreid1.red',
'username': 'charles'
}
def apply_templates(template_dir, output_dir, template_vars, overwrite=False):
"""Apply the template variables
to the template files.
"""
if not os.path.exists(output_dir):
msg = "Error: output dir %s does not exist!"%(output_dir)
raise Exception(msg)
if not os.path.exists(template_dir):
msg = "Error: template dir %s does not exist!"%(output_dir)
raise Exception(msg)
# Jinja env
env = Environment(loader=FileSystemLoader('.'))
# Render templates
template_files = glob.glob('*_setup.py.j2') + glob.glob('*_pull.py.j2')
render_files = [re.sub('\.j2','',s) for s in template_files]
for rfile,tfile in zip(render_files,template_files):
# Get rendered template content
content = env.get_template(tfile).render(**template_vars)
# Write to file
dest = os.path.join(output_dir,rfile)
if os.path.exists(dest) and overwrite is False:
msg = "Error: template rendering destination %s already exists!"%(dest)
raise Exception(msg)
with open(dest,'w') as f:
f.write(content)
x = 'executioner.py'
subprocess.call(['cp',x,os.path.join(output_dir,x)])
print("Rendered the following templates:%s\nOutput files:%s\n"%(
"".join(["\n- "+os.path.join(template_dir,j) for j in template_files]),
"".join(["\n- "+os.path.join(output_dir,j) for j in render_files])
))
if __name__=="__main__":
apply_templates(TEMPLATEDIR,OUTDIR,TV,OVERWRITE)

View File

@@ -0,0 +1,12 @@
[Unit]
Description=captain hook canary script
Requires=pod-webhooks.service
After=pod-webhooks.service
[Service]
Restart=always
ExecStart={{ webhooks_install_dir }}/scripts/captain_hook_canary.sh
ExecStop=/usr/bin/pgrep -f captain_hook_canary | /usr/bin/xargs /bin/kill
[Install]
WantedBy=default.target

View File

@@ -0,0 +1,47 @@
#!/bin/bash
: '
Captain Hook Canary Script (Template)
Note: this needs an associated systemd service.
See the services directory of the dotfiles repo.
This is a canary script for connecting
the Captain Hook container to the host
machine, and triggering tasks on the
host machine with webhooks.
The Captain Hook container mounts the
following host directory inside the
container (same location for host/container):
/tmp/triggers/
When a webhook in Captain Hook wants to
trigger an event on the host (blackbeard),
it puts a file in /tmp/triggers/.
Meanwhile, on the host, this script checks
every 10 seconds for trigger files.
Each webhook can create its own trigger file,
and this script processes each trigger differently.
'
while true
do
# bootstrap-pull captain hook
if [ -f "/tmp/triggers/push-b-captain-hook-master" ]; then
echo "CAPTAIN HOOK'S CANARY:"
echo "Running trigger to update Captain Hook on the host machine (user charles)"
sudo -H -u charles python {{ webhooks_install_dir }}/scripts/captain_hook_pull_host.py
echo "All done."
rm -f "/tmp/triggers/push-b-captain-hook-master"
touch /tmp/canary-yup-host-works
fi
sleep 10;
done

View File

@@ -0,0 +1,110 @@
#!/usr/bin/env python3
import subprocess
import os
import time
"""
Captain Hook: Pull Captain Hook on the Host
This script is called by the host machine
(blackbeard) running the Captain Hook container.
This is triggered by push actions to the
master branch of b-captain-hook.
The action is to update (git pull) the copy
of Captain Hook running on the host, and
restart the container pod.
"""
pod_dir = '{{ webhooks_install_dir }}'
work_dir = os.path.join(pod_dir,'b-captain-hook')
from datetime import datetime
d = datetime.now().strftime('%Y-m-%d')
with open('/tmp/captain_hook_pull_host_%s.log'%(d),'w') as f:
# Step 1:
# Update Captain Hook
co_cmd = ['git','checkout','master']
proc = subprocess.call(co_cmd, cwd=work_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
o = stdout.decode('utf-8')
e = stderr.decode('utf-8')
f.write(" ".join(co_cmd))
f.write("\n")
f.write("-"*40)
f.write("\n")
f.write(o)
f.write("\n")
f.write(e)
f.write("\n\n")
f_cmd = ['git','fetch','--all']
proc = subprocess.call(f_cmd, cwd=work_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
o = stdout.decode('utf-8')
e = stderr.decode('utf-8')
f.write(" ".join(f_cmd))
f.write("\n")
f.write("-"*40)
f.write("\n")
f.write(o)
f.write("\n")
f.write(e)
f.write("\n\n")
time.sleep(5)
pull_cmd = ['git','pull','origin','master']
proc = subprocess.call(pull_cmd, cwd=work_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
o = stdout.decode('utf-8')
e = stderr.decode('utf-8')
f.write(" ".join(pull_cmd))
f.write("\n")
f.write("-"*40)
f.write("\n")
f.write(o)
f.write("\n")
f.write(e)
f.write("\n\n")
time.sleep(10)
submod_cmd = ['git','submodule','update','--remote']
proc = subprocess.call(submod_cmd, cwd=work_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
o = stdout.decode('utf-8')
e = stderr.decode('utf-8')
f.write(" ".join(pull_cmd))
f.write("\n")
f.write("-"*40)
f.write("\n")
f.write(o)
f.write("\n")
f.write(e)
f.write("\n\n")
time.sleep(10)
# Step 2:
# Restart Captain Hook pod
pod_restart = ['systemctl','restart','pod-webhooks']
#pod_restart = ['docker-compose','restart']
proc = subprocess.call(pod_restart, cwd=pod_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
o = stdout.decode('utf-8')
e = stderr.decode('utf-8')
f.write(" ".join(pod_restart))
f.write("\n")
f.write("-"*40)
f.write("\n")
f.write(o)
f.write("\n")
f.write(e)
f.write("\n\n")

21
scripts/executioner.py Normal file
View File

@@ -0,0 +1,21 @@
#!/usr/bin/env python3
import subprocess
import os
def execute(cmd):
"""
A function to run a command and return the
lines of output as they are generated,
allowing the calling function to "stream"
the output of the command to print() or etc.
"""
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
for stdout_line in iter(p.stdout.readline, ""):
yield stdout_line
p.stdout.close()
err = "".join([j for j in iter(p.stderr.readline,"")])
return_code = p.wait()
if return_code:
yield err
raise subprocess.CalledProcessError(return_code, cmd)

125
scripts/pages_init_setup.py.j2 Executable file
View File

@@ -0,0 +1,125 @@
#!/usr/bin/env python3
import subprocess
import os
from executioner import execute
"""
Pages Initial Setup
This script sets up the initial pages.charlesreid1.XYZ
folder structure and checks out all pages that should
be present initially at pages.charlesreid1.XYZ.
This clones the page for a given project to
/www/pages.charlesreid1.XYZ/htdocs/project-name
(where project = repo)
"""
SERVER_NAME_DEFAULT = '{{ server_name_default }}'
USERNAME = '{{ username }}'
# Repos must have a gh-pages branch
repo_names = '''bots/b-apollo
bots/b-captain-hook
bots/b-ginsberg
bots/b-milton
bots/uncle-archie
bots/boring-mind-machine
docker/d-gitea
docker/d-mediawiki
docker/d-mysql
docker/d-nginx-charlesreid1
docker/d-nginx-subdomains
docker/d-phpmyadmin
docker/d-python-files
docker/d-python-helium
charlesreid1/dont-sudo-pip
bots/embarcadero-mind-machine
charlesreid1/git-commit-ectomy
charlesreid1/git-subway-maps
charlesreid1/github-heroku-attack-rabbits
charlesreid1/how-do-i-heroku
charlesreid1/how-do-i-pandoc
charlesreid1/how-do-i-pelican
charlesreid1/how-do-i-pyenv
charlesreid1/how-do-i-snakemake
docker/pod-bots
docker/pod-charlesreid1
docker/pod-webhooks
bots/russian-rainbow-mind-machine
charlesreid1/scurvy-knave-theme
charlesreid1/translate-yer-docs
charlesreid1/wisko-manual'''.split('\n')
repo_urls = ['https://git.charlesreid1.com/%s.git'%(j) for j in repo_names]
root = '/www'
pages = 'pages.%s'%(SERVER_NAME_DEFAULT)
basedir = os.path.join(root,pages)
if not os.path.exists(basedir):
raise Exception("Error: base directory %s does not exist!"%(basedir))
for name,url in zip(repo_names,repo_urls):
# for a hypothetical repo "project":
#
# base dir: /www/pages.charlesreid1.com
# .git dir: /www/pages.charlesreid1.com/git.project
# htdocs dir: /www/pages.charlesreid1.com/htdocs/project
# Step 1: clone
# Construct the clone command to set up
# live htdocs directory for this project
repo_owner, repo_name = name.split("/")
clonepth = os.path.join(basedir,"htdocs",repo_name)
dotgitpth = os.path.join(basedir,"git.%s"%(repo_name))
if os.path.exists(clonepth):
print("Git repository has already been cloned, %s already exists on disk!"%(clonepth))
else:
clonecmd = ["git","clone",
"--separate-git-dir=%s"%(dotgitpth),
"-b","gh-pages",
url,clonepth]
print("About to clone gh-pages branch for %s using command:\n"%(name))
print(" $ " + " ".join(clonecmd))
print("\n")
try:
for loo in execute(clonecmd):
print(loo)
print("\n")
print("Done.")
except subprocess.CalledProcessError:
print("Failed on clone command for project %s"%(name))
print("Continuing...")
# Step 2: chown everybody
# Construct chown command
chowncmd = ['chown','-R',
USERNAME+":"+USERNAME,
basedir]
print("About to change owner of %s using command:\n"%(basedir))
print(" $ " + " ".join(chowncmd))
print("\n")
for loo in execute(chowncmd):
print(loo)
print("\n")
print("Done.")

129
scripts/pages_pull.py.j2 Executable file
View File

@@ -0,0 +1,129 @@
#!/usr/bin/env python3
import subprocess
import os
from executioner import execute
"""
Pages Pull Script
This script pulls chnages to all repos in the
pages.charlesreid1.XYZ folder structure
This clones the page for a given project to
/www/pages.charlesreid1.XYZ/htdocs/project-name
(where project = repo)
"""
SERVER_NAME_DEFAULT = '{{ server_name_default }}'
USERNAME = '{{ username }}'
# Repos must have a gh-pages branch
repo_names = '''bots/b-apollo
bots/b-captain-hook
bots/b-ginsberg
bots/b-milton
bots/uncle-archie
bots/boring-mind-machine
docker/d-gitea
docker/d-mediawiki
docker/d-mysql
docker/d-nginx-charlesreid1
docker/d-nginx-subdomains
docker/d-phpmyadmin
docker/d-python-files
docker/d-python-helium
charlesreid1/dont-sudo-pip
bots/embarcadero-mind-machine
charlesreid1/git-commit-ectomy
charlesreid1/git-subway-maps
charlesreid1/github-heroku-attack-rabbits
charlesreid1/how-do-i-heroku
charlesreid1/how-do-i-pandoc
charlesreid1/how-do-i-pelican
charlesreid1/how-do-i-pyenv
charlesreid1/how-do-i-snakemake
docker/pod-bots
docker/pod-charlesreid1
docker/pod-webhooks
bots/russian-rainbow-mind-machine
charlesreid1/scurvy-knave-theme
charlesreid1/translate-yer-docs
charlesreid1/wisko-manual'''.split('\n')
repo_urls = ['https://git.charlesreid1.com/%s.git'%(j) for j in repo_names]
root = '/www'
pages = 'pages.%s'%(SERVER_NAME_DEFAULT)
basedir = os.path.join(root,pages)
htdocsdir = os.path.join(basedir,"htdocs")
if not os.path.exists(basedir):
raise Exception("Error: base directory %s does not exist!"%(basedir))
if not os.path.exists(htdocsdir):
raise Exception("Error: htdocs directory %s does not exist!"%(htdocsdir))
for name,url in zip(repo_names,repo_urls):
# for a hypothetical repo "project":
#
# base dir: /www/pages.charlesreid1.com
# .git dir: /www/pages.charlesreid1.com/git.project
# htdocs dir: /www/pages.charlesreid1.com/htdocs/project
# Step 1: pull
# Construct the git pull command to update
# the live htdocs directory for this project
repo_owner, repo_name = name.split("/")
workdir = os.path.join(basedir,"htdocs",repo_name)
dotgitdir = os.path.join(basedir,"git.%s"%(repo_name))
if not os.path.exists(workdir):
print("Git repository has not yet been cloned, %s does not exist on disk!"%(workdir))
else:
pullcmd = ['git',
'-C', basedir,
'--git-dir=%s'%(dotgitdir),
'--work-tree=%s'%(workdir),
'pull','origin','gh-pages']
print("About to pull gh-pages branch for %s using command:\n"%(name))
print(" $ " + " ".join(pullcmd))
print("\n")
try:
for loo in execute(pullcmd):
print(loo)
print("\n")
print("Done.")
except subprocess.CalledProcessError:
print("Failed on pull command for project %s"%(name))
print("Continuing...")
# Step 2: chown everybody
# Construct chown command
chowncmd = ['chown','-R',
USERNAME+":"+USERNAME,
basedir]
print("About to change owner of %s using command:\n"%(basedir))
print(" $ " + " ".join(chowncmd))
print("\n")
for loo in execute(chowncmd):
print(loo)
print("\n")
print("Done.")

View File

@@ -0,0 +1,14 @@
# Hopefully this comment works
[Unit]
Description=webhooks and subdomains docker pod
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/local/bin/docker-compose -f {{ webhooks_install_dir }}/docker-compose.yml up
ExecStop=/usr/local/bin/docker-compose -f {{ webhooks_install_dir }}/docker-compose.yml down
[Install]
WantedBy=default.target

View File

@@ -0,0 +1,89 @@
#!/usr/bin/env python3
import subprocess
import os
from executioner import execute
"""
Subdomains Initial Setup
This script sets up the initial folder structure
for the charlesreid1.com subdomains and their content.
"""
SERVER_NAME_DEFAULT = '{{ server_name_default }}'
USERNAME = '{{ username }}'
# Each subdomain page has an htdocs dir at
#
# /www/SUBDOMAIN.charlesreid1.XYZ/htdocs
#
# and a .git directory at
#
# /www/SUBDOMAIN.charlesreid1.XYZ/git.SUBDOMAIN.charlesreid1.XYZ
# Name of all subdomains to set up
sub_names = ['bots','pages','hooks']
subs = [n+'.'+SERVER_NAME_DEFAULT for n in sub_names]
# Use git.charlesreid1.com
# (Note we have to hard-code the suffix
# charlesreid1.com b/c that is the name
# of the repo for ALL subdomains)
urls = ['https://git.charlesreid1.com/charlesreid1/%s.charlesreid1.com'%(n) for n in sub_names]
#
# Use github.com (alternative)
# (Note we have to hard-code the suffix charlesreid1.com)
#urls = ['https://github.com/charlesreid1-docker/%s.charlesreid1.com'%(n) for n in sub_names]
# Base directory for all htdocs folders
pth = '/www'
# Create the base directory
subprocess.call(['mkdir','-p',pth])
# Step 1: clone htdocs dir for each subdomain
for subname, sub, url in zip(sub_names,subs,urls):
# Construct the clone command for the live
# htdocs directory for this subdomain
clonepth = os.path.join(pth,sub,'htdocs')
dotgitpth = os.path.join(pth,sub,'git.%s'%(subname))
if os.path.exists(clonepth):
print("Git repository has already been cloned, %s already exists on disk!"%(clonepth))
else:
clonecmd = ['git','clone',
'--separate-git-dir=%s'%(dotgitpth),
'-b','gh-pages',
url,clonepth]
print("About to clone htdocs dir for %s using command:\n"%(sub))
print(" $ " + " ".join(clonecmd))
print("\n")
for loo in execute(clonecmd):
print(loo)
print("\n")
print("Done.")
# Step 2: chown
# Construct chown command to chown everybody
chowncmd = ['chown','-R',USERNAME+":"+USERNAME,pth]
print("About to change owner of %s using command:\n"%(pth))
print(" $ " + " ".join(chowncmd))
print("\n")
for loo in execute(chowncmd):
print(loo)
print("\n")
print("Done.")

95
scripts/subdomains_pull.py.j2 Executable file
View File

@@ -0,0 +1,95 @@
#!/usr/bin/env python3
import subprocess
import os
from executioner import execute
"""
Subdomains Pull Script
This script sets up the initial folder structure
for the charlesreid1.com subdomains and their content.
"""
SERVER_NAME_DEFAULT = '{{ server_name_default }}'
USERNAME = '{{ username }}'
# Each subdomain page has an htdocs dir at
#
# /www/SUBDOMAIN.charlesreid1.XYZ/htdocs
#
# and a .git directory at
#
# /www/SUBDOMAIN.charlesreid1.XYZ/git.SUBDOMAIN.charlesreid1.XYZ
# Name of all subdomains to set up
sub_names = ['bots','pages','hooks']
subs = [n+'.'+SERVER_NAME_DEFAULT for n in sub_names]
# Use git.charlesreid1.com
# (Note we have to hard-code the suffix
# charlesreid1.com b/c that is the name
# of the repo for ALL subdomains)
urls = ['https://git.charlesreid1.com/charlesreid1/%s.charlesreid1.com'%(n) for n in sub_names]
#
# Use github.com (alternative)
# (Note we have to hard-code the suffix charlesreid1.com)
#urls = ['https://github.com/charlesreid1-docker/%s.charlesreid1.com'%(n) for n in sub_names]
# Base directory for all htdocs folders
pth = '/www'
# Create the base directory
subprocess.call(['mkdir','-p',pth])
# Step 1: clone htdocs dir for each subdomain
for subname, sub, url in zip(sub_names,subs,urls):
# Step 1: pull
# Construct the git pull command to update
# the live htdocs directory for this project
basedir = os.path.join(pth,sub)
htdocsdir = os.path.join(basedir,"htdocs")
dotgitdir = os.path.join(basedir,"git.%s"%(subname))
if not os.path.exists(htdocsdir):
print("Git repository has not been cloned, %s does not exist on disk!"%(htdocsdir))
else:
# git pull, respecting the
# unusual .git/repo folder layout
pullcmd = ['git',
'-C', basedir,
'--git-dir=%s'%(dotgitdir),
'--work-tree=%s'%(htdocsdir),
'pull','origin','gh-pages']
print("About to pull htdocs dir for %s using command:\n"%(sub))
print(" $ " + " ".join(pullcmd))
print("\n")
for loo in execute(pullcmd):
print(loo)
print("\n")
print("Done.")
# Step 2: chown
# Construct chown command to chown everybody
chowncmd = ['chown','-R',USERNAME+":"+USERNAME,basedir]
print("About to change owner of %s using command:\n"%(basedir))
print(" $ " + " ".join(chowncmd))
print("\n")
for loo in execute(chowncmd):
print(loo)
print("\n")
print("Done.")