Skip to main content

Helps create non-templated systemd services.

Project description

service-config-foundry

service-config-foundry is a Python-based library that simplifies the process of creating, updating, replacing, and deleting systemd service files for Linux users. With this tool, you can easily manage systemd services programmatically, reducing the complexity of writing and maintaining service configuration files manually.

Features

  • Create and manage non-templated systemd service files
  • Support for various systemd file types including .service, .timer, .socket, .mount, and more
  • Programmatic control over file attributes and configurations
  • Replace and update existing services without conflicts
  • Fully integrated Python API with no external dependencies

Installation

You can install the library using pip directly from the GitHub repository:

pip install git+https://github.com/yushdotkapoor/service-config-foundry.git

Alternatively, clone the repository to get started:

git clone https://github.com/yushdotkapoor/service-config-foundry.git
cd service-config-foundry

Usage

Below are detailed examples of how to use service-config-foundry to create and manage systemd files of various types.

Example: Creating a Service, Timer, and Socket

from service import Service
from service_location import ServiceLocation

# Create a new service instance
service = Service("example", service_location=ServiceLocation.GLOBAL, auto_start=False, enable_at_startup=False, force_overwrite=False)

# Configure the service file
service_file = service.service_file
service_file.unit.description = "Example service for demonstration purposes"
service_file.service.user = "yushrajkapoor"
service_file.service.exec_start = "echo Hello, world >> /tmp/example.log"
service_file.install.wanted_by = "multi-user.target"

# Configure the timer file
timer_file = service.timer_file
timer_file.unit.description = "Example timer for demonstration purposes"
timer_file.timer.on_calendar = "*-*-* *:00:00"
timer_file.install.wanted_by = "timers.target"

# Configure the socket file
socket_file = service.socket_file
socket_file.unit.description = "Example socket for demonstration purposes"
socket_file.socket.listen_stream = "/run/example.sock"
socket_file.install.wanted_by = "sockets.target"

# Create or update the service, timer, and socket files
service.update()

# Enable the service to start at boot time
service.enable_service_at_startup()

# Start the service
service.start_service()

# Display the status of the service
service.status()

Output Files

example.service

[Unit]
Description = Example service for demonstration purposes

[Service]
User = yushrajkapoor
ExecStart = echo Hello, world >> /tmp/example.log

[Install]
WantedBy = multi-user.target

example.timer

[Unit]
Description = Example timer for demonstration purposes

[Timer]
OnCalendar = *-*-* *:00:00

[Install]
WantedBy = timers.target

example.socket

[Unit]
Description = Example socket for demonstration purposes

[Socket]
ListenStream = /run/example.sock

[Install]
WantedBy = sockets.target

Example: Replacing an Existing Service

If you want to replace an existing service configuration, you can use the replace() method. This ensures that any old files are removed and replaced with the new configuration.

# Replace the existing service configuration
service.replace()

This is particularly useful when you want to ensure that your updates overwrite any conflicting configurations from previous versions of the service.

Example: Creating Mount and Automount Files

# Configure the mount file
mount_file = service.mount_file
mount_file.unit.description = "Example mount for demonstration purposes"
mount_file.mount.what = "/dev/sda1"
mount_file.mount.where = "/mnt/example"
mount_file.mount.type = "ext4"
mount_file.install.wanted_by = "multi-user.target"

# Configure the automount file
automount_file = service.automount_file
automount_file.unit.description = "Example automount for demonstration purposes"
automount_file.automount.where = "/mnt/example"
automount_file.install.wanted_by = "multi-user.target"

# Create or update the files
service.update()

example.mount

[Unit]
Description = Example mount for demonstration purposes

[Mount]
What = /dev/sda1
Where = /mnt/example
Type = ext4

[Install]
WantedBy = multi-user.target

example.automount

[Unit]
Description = Example automount for demonstration purposes

[Automount]
Where = /mnt/example

[Install]
WantedBy = multi-user.target

Example: Creating Swap and Path Files

# Configure the swap file
swap_file = service.swap_file
swap_file.unit.description = "Example swap for demonstration purposes"
swap_file.swap.what = "/swapfile"
swap_file.install.wanted_by = "multi-user.target"

# Configure the path file
path_file = service.path_file
path_file.unit.description = "Example path for demonstration purposes"
path_file.path.path_exists = "/tmp/example"
path_file.install.wanted_by = "multi-user.target"

# Create or update the files
service.update()

example.swap

[Unit]
Description = Example swap for demonstration purposes

[Swap]
What = /swapfile

[Install]
WantedBy = multi-user.target

example.path

[Unit]
Description = Example path for demonstration purposes

[Path]
PathExists = /tmp/example

[Install]
WantedBy = multi-user.target

Example: Creating Slice and Scope Files

# Configure the slice file
slice_file = service.slice_file
slice_file.unit.description = "Example slice for demonstration purposes"

# Configure the scope file
scope_file = service.scope_file
scope_file.unit.description = "Example scope for demonstration purposes"
scope_file.scope.slice = "example.slice"

# Create or update the files
service.update()

example.slice

[Unit]
Description = Example slice for demonstration purposes

example.scope

[Unit]
Description = Example scope for demonstration purposes

[Scope]
Slice = example.slice

Deleting a Service

To delete a service and its associated files:

service.delete()

This will remove all files matching the service name in the systemd directory.

Configuration Options

service-config-foundry supports multiple systemd file types, including:

  • .service - Main service configuration
  • .socket - Socket configuration
  • .timer - Timer configuration
  • .mount - Mount point configuration
  • .automount - Automount configuration
  • .swap - Swap space configuration
  • .path - Path configuration
  • .slice - Slice configuration
  • .scope - Scope configuration

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvement, please open an issue or submit a pull request.

Contact

Acknowledgments

Special thanks to the Linux and Python communities for their inspiration and support in creating this project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

service_config_foundry-0.2.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

service_config_foundry-0.2-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file service_config_foundry-0.2.tar.gz.

File metadata

  • Download URL: service_config_foundry-0.2.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.20 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for service_config_foundry-0.2.tar.gz
Algorithm Hash digest
SHA256 34b8fc5ccba239a0ebc8976827f04807cba7248a51e02621a905a3435a02e395
MD5 b814df136b24e6fc36991b866f8828c5
BLAKE2b-256 444ae799819cab15e43810b984294125d6949f292c46a3c65cf999c217a1015f

See more details on using hashes here.

File details

Details for the file service_config_foundry-0.2-py3-none-any.whl.

File metadata

  • Download URL: service_config_foundry-0.2-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.20 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

File hashes

Hashes for service_config_foundry-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7efe3b709bebddb4a4e944b29a4d1fcd0f1623d8fdc6bee27337ab0791821beb
MD5 c97624422da64877c67045f9d9ec2a5d
BLAKE2b-256 bd9e1eefa5549bc04fb8c40f4d158e65d22f08ec012c6c40d125ec0ea8397ffc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page