Files
ad-tunnel/install_service.py
2024-06-14 19:02:43 -05:00

56 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
import subprocess
from pathlib import Path
from rich import print
def create_symlink(source, target):
try:
target_path = Path(target)
# Check if the target already exists and remove it if it does
if target_path.exists() or target_path.is_symlink():
target_path.unlink()
# Create the symbolic link
target_path.symlink_to(source)
print(f'Created symlink: {source} -> {target}')
except OSError as e:
print(f'Error creating symlink: {e}')
def run_command(command):
try:
subprocess.run(command, check=True, shell=True)
except subprocess.CalledProcessError as e:
print(f'Error running command: {command}\n{e}')
def main():
repo_dir = Path(__file__).resolve().parent
systemd_dir = Path('/etc/systemd/system')
# Define the source and target paths
socket_file = 'example.socket'
service_file = 'example@.service'
# Create symlinks
create_symlink(repo_dir / socket_file, systemd_dir / socket_file)
create_symlink(repo_dir / service_file, systemd_dir / service_file)
# Reload systemd, start and enable the socket
run_command('sudo systemctl daemon-reload')
print('Reloaded systemd services')
run_command('sudo systemctl start example.socket')
print(f'Started [blue]{socket_file}[/]')
run_command('sudo systemctl enable example.socket')
print(f'Enabled [blue]{socket_file}[/] to start at boot')
if __name__ == '__main__':
main()