· 7 years ago · Sep 12, 2018, 09:02 AM
1-c/--schema: Just loads a different template configuration to use
2for the interactive "asking" process. Takes the path to that template.
3
4-s/--short: Uses a default schema (template) that is shorter than the
5original one (has less and more basic options)
6
7-x/--extended: Uses a default schema (template) that is longer than the
8original one (has more options that are more complex)
9
10-d/--directory: The directory in which the script should
11save the service unit file, by default "/etc/systemd/system".
12
13--delete: Deletes the service unit file of the specified service name and
14disables/stops that service. If used, should be the only argument used
15along with service_name.
16
17--edit: Edits the service unit file (opens an editor)
18of the specified service name. If used, should be the only argument used
19along with service_name.
20
21-b/--build: Builds a default service configuration unit file as an example.
22Should be the ONLY argument used, even the
23positional argument service_name should not be used.
24
25--info: Outputs information about the script - purpose, maintainer, etc.
26Again should be the ONLY argument used.
27
28service_name: The positional argument which tells what is the name
29of the service that has to be configured/edited/deleted.
30Doesn't have to be used with --build/--info.
31
32#!/usr/bin/env python3
33
34"""
35This is a helper script that semi-automates the process
36of configuring/editing systemd services.
37
38Examples --
39Create a service using a short preset configuration:
40sudo ./service-config.py --short some_service_name
41
42Create a service using a custom preset configuration template:
43sudo ./service-config.py -c some_schema some_service_name
44
45Edit an existing service:
46sudo ./service-config.py --edit some_service_name
47
48# To do list:
491. Document the script.
502. Fix permission issues with some of the arguments.
513. Add a configuration file for the script for
52 some of the default values and options. (?)
534. Allow the user to choose an editor.
54
55NOTES:
561. This script requires root privileges for most use-cases.
572. Skipping a configuration option (just pressing enter) without
58 supplying any value will just tell the script to skip that option.
59"""
60
61# IMPORTS:
62
63import argparse
64import configparser
65import datetime
66import os
67import subprocess
68import sys
69import time
70
71from collections import OrderedDict
72
73# DEFINING CONSTANTS:
74
75VERSION = "0.4"
76MAINTAINER_NICK = "..."
77MAINTAINER_EMAIL = "...@gmail.com"
78TRACE = True
79SCHEMA = "schemas/service-config"
80SCHEMA_SHORT = "schemas/short_service-config"
81SCHEMA_EXTENDED = "schemas/extended_service-config"
82CONFIG = None # for future uses
83OUTPUT_DIR = "/etc/systemd/system"
84
85# ERRORS:
86
87USER_ABORT = 5
88ARGPARSE_ERR = 6
89CONFIGURATION_ERR = 7
90GLOBAL_ERR = 8
91SCHEMA_ERR = 9
92SYSTEMD_ERR = 10
93UID_ERROR = 11
94FINISH_ERROR = 12
95
96# DEFAULTS:
97
98DEFAULT_EDITOR = "vim"
99DEFAULT_BUILD_SCHEMA = "schemas/default-schema"
100
101DEFAULT_DESCRIPTION = "Example"
102DEFAULT_AFTER = "network.target"
103DEFAULT_TYPE = "simple"
104DEFAULT_USER = "root"
105DEFAULT_GROUP = "root"
106DEFAULT_EXEC_START = "/bin/true"
107DEFAULT_EXEC_STOP = "/bin/true"
108DEFAULT_KILL_MODE = "control-group"
109DEFAULT_KILL_SIGNAL = "SIGTERM"
110DEFAULT_PID_FILE = "/run/service.pid"
111DEFAULT_RESTART = "on-failure"
112DEFAULT_RESTART_SEC = "2"
113DEFAULT_TIMEOUT_STOP_SEC = "5"
114DEFAULT_DYNAMIC_USER = "no"
115DEFAULT_ENVIRONMENT_FILE = "/etc/service/env"
116DEFAULT_STANDARD_OUTPUT = "journal"
117DEFAULT_STANDARD_ERROR = "journal"
118DEFAULT_WANTED_BY = "multi-user.target"
119
120# COLORS AND Formatting:
121
122def tty_supports_ansi():
123 """Checks whether the terminal used supports ANSI codes."""
124
125 for handle in [sys.stdout, sys.stderr]:
126 if ((hasattr(handle, "isatty") and handle.isatty()) or
127 ('TERM' in os.environ and os.environ['TERM'] == "ANSI")):
128 return True
129 else:
130 return False
131
132class Formatting:
133 """
134 A class containing constants with most Formatting/basic colors
135 for Unix-based terminals and vtys.
136 Does NOT work on Windows cmd, PowerShell, and their varieties!
137 """
138
139 RESET = "