Skip to content

Commit d77da16

Browse files
committed
draft up, now can handle signals
1 parent 2394e83 commit d77da16

File tree

5 files changed

+42
-13
lines changed

5 files changed

+42
-13
lines changed

config/ansible/config.ini

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ ansible_working_dir = /opt/linode_ansible/project
44
ansible_setup_playbook = setup_linode.yml
55
ansible_inventory = /opt/linode_ansible/inventory/inventory
66
ansible_passwd_template = paswd_template.j2
7+
ansible_out_dump = /tmp/ansible_out_dump
8+

src/Hermes/ansible.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from os import path
2+
from io import FileIO
23
from ansible_runner import Runner, RunnerConfig, run_async
34
from discord import app_commands
45
from discord.app_commands import Group
@@ -20,6 +21,7 @@ def ansible_init(self):
2021
self.ansible_roles = path.join(config_predir['ansible_working_dir'], 'roles')
2122
self.linode_runner = None
2223
self.ansible_passwd_template = config_predir["ansible_passwd_template"]
24+
self.ansible_out_dump = config_predir["ansible_out_dump"]
2325
self.ansible_command_group = AnsibleG(
2426
self, name="ansible", description="ansible module"
2527
)
@@ -71,7 +73,9 @@ def run_ansible(self):
7173
inventory = self.ansible_inventory,
7274
roles_path = self.ansible_roles,
7375
quiet = True,
74-
status_handler= status_handler)
76+
status_handler= status_handler,
77+
_output=FileIO(self.ansible_out_dump, mode = 'w'),
78+
_input=FileIO('/dev/null', mode = 'r'))
7579
# Runner.event_handler -> na progress
7680
# Runner.finished_callback -> na end
7781
except Exception as e:

src/Hermes/signals.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import signal
2+
3+
def signals_init(self):
4+
5+
def hup_handler(signum, frame):
6+
for callback in self.hup_callbacks:
7+
callback()
8+
self.logger.info("Recieved HUP")
9+
10+
self.hup_callbacks = [self.status_hup_handler]
11+
signal.signal(signal.SIGHUP, hup_handler)

src/Hermes/status.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ def status_init(self):
1717
self.status_callbacks = []
1818
self.status_accumulator = {}
1919

20-
def register_status_callback(self, status):
21-
"""
22-
sync_tree docstring
23-
"""
24-
for key in status.keys():
25-
if key not in self.status_accumulator:
26-
self.status_accumulator[key] = [status[key]]
27-
else:
28-
self.status_accumulator[key].append(status[key])
20+
def status_hup_handler(self):
21+
print("Status recieved hup")
22+
23+
# not used
24+
#def register_status_callback(self, status):
25+
# """
26+
# sync_tree docstring
27+
# """
28+
# for key in status.keys():
29+
# if key not in self.status_accumulator:
30+
# self.status_accumulator[key] = [status[key]]
31+
# else:
32+
# self.status_accumulator[key].append(status[key])
2933

3034
class StatusG(Group):
3135
"""

src/HermesBot.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from discord import Intents
33
from apscheduler.schedulers.asyncio import AsyncIOScheduler
44
from discord import app_commands
5+
from os import getpid
56
"""
67
Module docstring
78
"""
@@ -14,9 +15,13 @@ class Hermes(BotBase):
1415
"""
1516

1617
def __init__(self, *args, **kwargs):
17-
18+
print(f"The pid is: {getpid()}")
1819
self.start_logging()
1920
self.logger.info("###########################")
21+
22+
# SIGNALS
23+
self.signals_init()
24+
2025
# CONFIGURATOR
2126
self.configurator_init()
2227
# DISCORD
@@ -36,7 +41,6 @@ def __init__(self, *args, **kwargs):
3641
# ANSIBLE
3742
self.ansible_init()
3843

39-
4044
super().__init__(
4145
command_prefix=self.prefix, intents=Intents.all(), *args, **kwargs
4246
)
@@ -134,5 +138,9 @@ def run(self):
134138
run_ansible
135139
)
136140
from src.Hermes.status import (
137-
status_init
141+
status_init,
142+
status_hup_handler
143+
)
144+
from src.Hermes.signals import (
145+
signals_init
138146
)

0 commit comments

Comments
 (0)