-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirhooker.py
138 lines (100 loc) · 4.52 KB
/
dirhooker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# DirHooker - Send webhook request on directory change
# Copyright (C) 2023 Jason Greenbaum (https://github.com/jgbaum)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
import threading
import time
import subprocess
import argparse
import yaml
import os
from copy import deepcopy
parser = argparse.ArgumentParser()
# add arguments to the parser
parser.add_argument('--generate-docker-command',
help='Generate the docker run command',
action='store_true',
default=False,
required=False)
parser.add_argument('--config',
help='Path to the config file',
required=False)
parser.add_argument('--image-name',
help='The name of the image to run',
required=False,
default='ghcr.io/jgbaum/dirhooker:latest')
args = parser.parse_args()
def generate_docker_command(config_file):
"""Using the template config file, generate the docker run command"""
# read in the yaml config file
with open(config_file, 'r') as file:
config = yaml.safe_load(file)
# record the volumes to be mapped
volume_maps = list()
for d in config['directories']:
volume_maps.append(f"{d['path']}:{d['path']}")
# create the docker run command as a list
# we'll join these together later
docker_run_cmds = list()
docker_run_cmds.append('docker run -d')
# add the name and restart condition
docker_run_cmds.append("--name dirhooker")
docker_run_cmds.append("--restart on-failure")
# now add the volume mounts
for v in volume_maps:
docker_run_cmds.append(f"-v {v}")
# mount the config file
runtime_config_full_path = os.path.abspath(config_file)
docker_run_cmds.append(f"-v {runtime_config_full_path}:/config.yaml")
# add the name of the image
docker_run_cmds.append(f"{args.image_name}")
# join into a string
docker_cmd = " \\\n".join(docker_run_cmds)
print(docker_cmd)
def watch_dir(dir_info):
# default to watching CREATE actions, but this can be overridden
# by adding a list of 'watched_actions' item to the dir info
watched_actions = ['CREATE']
if 'watched_actions' in dir_info:
watched_actions = dir_info['watched_actions']
events_string = ','.join(watched_actions)
watch_cmd = f'inotifywait -e {events_string} {dir_info["path"]};'
webhook_url = dir_info['webhook_url']
curl_cmd = f"curl '{webhook_url}'"
logging.info(f"Thread {dir_info['name']}: watch command: {watch_cmd}")
logging.info(f"Thread {dir_info['name']}: curl command: {curl_cmd}")
# loop infinitely until the process quits
while True:
logging.info(f"Thread {dir_info['name']}: starting")
watch_result = subprocess.run(watch_cmd, stdout=subprocess.PIPE, shell=True)
logging.info(f"Thread {dir_info['name']}: watch response: " + watch_result.stdout.decode())
# If we pass ctrl-c, sometimes this block executes, so we need to first
# check that CREATE exists in the watch result
if 'CREATE' in watch_result.stdout.decode():
curl_result = subprocess.run(curl_cmd, stdout=subprocess.PIPE, shell=True)
logging.info(f"Thread {dir_info['name']} curl response: " + curl_result.stdout.decode())
time.sleep(30)
if __name__ == "__main__":
config_file = args.config
gen_config = args.generate_docker_command
if gen_config:
generate_docker_command(config_file)
else:
# read in the yaml config file
with open(config_file, 'r') as file:
config = yaml.safe_load(file)
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO,
datefmt="%H:%M:%S")
for d in config['directories']:
x = threading.Thread(target=watch_dir, args=(d,))
x.start()