This commit is contained in:
John Lancaster
2024-06-14 23:34:27 -05:00
parent ca7f62afe3
commit 1cae18670a
7 changed files with 75 additions and 11 deletions

6
cleanup_services.sh Executable file
View File

@@ -0,0 +1,6 @@
#!/bin/bash
for service in $(systemctl list-units --type=service --no-legend | grep example | awk '{print $1}'); do
sudo systemctl stop $service
echo "Stopped $service"
done

12
example.service Normal file
View File

@@ -0,0 +1,12 @@
[Unit]
Description=Example Service
Requires=example.socket
After=example.socket
[Service]
User=root
Group=root
ExecStart=/root/.pyenv/versions/venv/bin/python /home/john/ad-tunnel/run_service.py
[Install]
WantedBy=multi-user.target

View File

@@ -1,9 +1,10 @@
[Unit] [Unit]
Description=Example Unix Socket Description=Example Unix Socket
StopWhenUnneeded=true
[Socket] [Socket]
ListenStream=/run/example.sock ListenStream=/run/example.sock
Accept=yes Accept=no
[Install] [Install]
WantedBy=sockets.target WantedBy=sockets.target

View File

@@ -1,6 +0,0 @@
[Unit]
Description=Example Service
[Service]
ExecStart=/bin/bash -c 'echo "Hello, world!" > /tmp/example_output.txt'
StandardInput=socket

View File

@@ -16,9 +16,9 @@ def create_symlink(source, target):
# Create the symbolic link # Create the symbolic link
target_path.symlink_to(source) target_path.symlink_to(source)
print(f'Created symlink: {source} -> {target}') print(f'Created symlink: [magenta]{source}[/] -> [magenta]{target}[/]')
except OSError as e: except OSError as e:
print(f'Error creating symlink: {e}') print(f'Error creating symlink: [bold red]{e}[/]')
def run_command(command): def run_command(command):
@@ -34,7 +34,7 @@ def main():
# Define the source and target paths # Define the source and target paths
socket_file = 'example.socket' socket_file = 'example.socket'
service_file = 'example@.service' service_file = 'example.service'
# Create symlinks # Create symlinks
create_symlink(repo_dir / socket_file, systemd_dir / socket_file) create_symlink(repo_dir / socket_file, systemd_dir / socket_file)

27
run_service.py Executable file
View File

@@ -0,0 +1,27 @@
#!/usr/bin/env python3
import os
import subprocess
from rich import print
os.chdir('/root')
cmd = ['code', 'tunnel']
# cmd = ['echo', '"Hello world"']
print('PATH:\n' + '\n'.join(f' - {p}' for p in os.environ['PATH'].split(':')))
try:
result = subprocess.run(
cmd,
shell=True,
env=os.environ,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
result.check_returncode()
except subprocess.CalledProcessError as e:
print(e)
finally:
print(result.stdout)

24
test_socket.py Executable file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env python
import socket
from time import sleep
from rich.console import Console
console = Console()
print = console.print
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
try:
sock.connect('/run/example.sock')
except Exception:
raise
else:
with console.status('Connected...'):
while True:
try:
sleep(0.1)
except KeyboardInterrupt:
break
finally:
print('Exiting')