2023-04-21 16:00:50 +00:00
#!/usr/bin/python3
import pathlib
from typing import List , Union
try :
from ansible_collections . sebastian . systemd . plugins . module_utils . generic import SYSTEMD_SERVICE_CONFIG , Types
2023-04-23 08:32:42 +00:00
from ansible_collections . sebastian . systemd . plugins . module_utils . module import SystemdReloadMixin , SystemdUnitModule , installable
2023-04-21 16:00:50 +00:00
except ImportError :
from plugins . module_utils . generic import SYSTEMD_SERVICE_CONFIG , Types
2023-04-23 08:32:42 +00:00
from plugins . module_utils . module import SystemdReloadMixin , SystemdUnitModule , installable
2023-04-21 16:00:50 +00:00
@installable
2023-04-23 08:32:42 +00:00
class Module ( SystemdUnitModule , SystemdReloadMixin ) :
2023-04-21 16:00:50 +00:00
""" Creates System Services units """
name = " system_service "
module_spec = dict (
argument_spec = dict (
2023-04-23 08:32:42 +00:00
name = Types . str ( required = True , help = " Name of the service " ) ,
serviceuser = Types . str ( help = " Username of under which the commands run at. " , default = " root " ) ,
servicegroup = Types . str ( help = " Group of under which the commands run at. " , default = " root " ) ,
2023-04-21 16:00:50 +00:00
type = Types . str (
choices = ( " simple " , " exec " , " forking " , " oneshot " , " dbus " , " notify " , " notify-reload " , " idle " ) ,
default = " simple " ,
help = " Type of the systemd service. \n "
" simple and exec start long running services that run in the same process over the whole time, exec is waiting until the process was started completly. \n "
" forking does some things in the foreground, starts an background process and then exits to leave the work to the background process. \n "
" oneshot processes are started by systemd, do their work and then exit, similar to cronjobs. \n "
" dbus services will be considered started up once they aquire the specified dbus bus "
" notify and notify-reload notify systemd about the start up via sd_notify. notify-reload needs also inform systemd on reloads and when it is ready again after an reload. \n "
" idle is similar to simple, but it can delay the start up by a few seconds. " ,
) ,
pre = Types . list ( str , help = " command or list of commands that are started before the main command(Types.str) " ) ,
start = Types . list (
str ,
True ,
help = " command or list of commands that are started as main programm. Multiple commands are only allowed in a oneshot command " ,
) ,
post = Types . list ( str , help = " Command or list of commands that are started after the main command(s) stopped without problems. " ) ,
2023-04-23 08:32:42 +00:00
environmentfile = Types . list (
str , help = " List of file that are containing environment variables. They are evaluated before each pre/start/post command "
) ,
environment = Types . list (
Types . dict (
name = Types . str ( help = " name of the Environment variable " , required = True ) ,
value = Types . str ( help = " value of the Environment variable " , required = True ) ,
) ,
help = " List of environment variables that are set to each command before they run " ,
) ,
workingdirectory = Types . str ( help = " The Directory that is used for the processes as current working directory " ) ,
2023-04-21 16:00:50 +00:00
) ,
)
def prepare ( self ) :
2023-04-23 08:32:42 +00:00
self . unitfile = ( SYSTEMD_SERVICE_CONFIG / self . get ( " name " ) ) . with_suffix ( " .service " )
2023-04-21 18:56:16 +00:00
self . __unit = None
2023-04-21 16:00:50 +00:00
if self . get ( " type " , " simple " ) != " oneshot " and len ( self . get ( " start " ) ) > 1 :
self . module . fail_json ( " only oneshot services are allowed to have multiple start commands " , * * self . result )
def service ( self ) :
section = " [Service] \n "
2023-04-23 08:32:42 +00:00
section + = " \n " . join (
self . map_param (
type = " Type " ,
pre = " ExecStartPre " ,
start = " ExecStart " ,
post = " ExecStartPost " ,
serviceuser = " User " ,
servicegroup = " Group " ,
workingdirectory = " WorkingDirectory " ,
)
)
2023-04-21 16:00:50 +00:00
return section
def unit ( self ) - > str :
if self . __unit is None :
self . __unit = " \n " . join (
(
self . header ( ) ,
self . service ( ) ,
self . install ( ) ,
)
)
return self . __unit
DOCUMENTATION = """ ---
description :
- Creates System Services units
module : system_service
options :
after :
default : [ ]
description :
- list of units that this unit wants to be started after this unit
elements : str
required : false
type : list
before :
default : [ ]
description :
- list of units that this unit needs to be started before this unit .
elements : str
required : false
type : list
description :
description :
- An description for programs that access systemd
required : false
type : str
documentation :
default : [ ]
description :
- Paths where documentation can be found
elements : str
required : false
type : list
2023-04-23 08:32:42 +00:00
environment :
default : [ ]
description :
- List of environment variables that are set to each command before they run
elements : dict
options :
name :
description :
- name of the Environment variable
required : true
type : str
value :
description :
- value of the Environment variable
required : true
type : str
2023-04-21 16:00:50 +00:00
required : false
2023-04-23 08:32:42 +00:00
type : list
environmentfile :
default : [ ]
description :
- List of file that are containing environment variables . They are evaluated before
each pre / start / post command
elements : str
required : false
type : list
2023-04-21 16:00:50 +00:00
name :
2023-04-23 08:32:42 +00:00
description :
- Name of the service
2023-04-21 16:00:50 +00:00
required : true
type : str
partof :
default : [ ]
description :
- list of units that this unit is part of .
- If the restart this unit does it too , but if this restarts it does not affect
the other units .
elements : str
required : false
type : list
post :
default : [ ]
description :
- Command or list of commands that are started after the main command ( s ) stopped
without problems .
elements : str
required : false
type : list
pre :
default : [ ]
description :
- command or list of commands that are started before the main command ( Types . str )
elements : str
required : false
type : list
required_by :
default : [ ]
description :
- systemd units that require this mount
elements : str
required : false
type : list
requires :
default : [ ]
description :
- list of units that this unit requires . If it fails or can ' t be started this
unit fails . without before / after this is started at the same time
elements : str
required : false
type : list
2023-04-23 08:32:42 +00:00
servicegroup :
default : root
description :
- Group of under which the commands run at .
required : false
type : str
serviceuser :
default : root
description :
- Username of under which the commands run at .
required : false
type : str
2023-04-21 16:00:50 +00:00
start :
description :
- command or list of commands that are started as main programm . Multiple commands
are only allowed in a oneshot command
elements : str
required : true
type : list
type :
choices :
- simple
- exec
- forking
- oneshot
- dbus
- notify
- notify - reload
- idle
default : simple
description :
- Type of the systemd service .
- simple and exec start long running services that run in the same process over
the whole time , exec is waiting until the process was started completly .
- forking does some things in the foreground , starts an background process and
then exits to leave the work to the background process .
- oneshot processes are started by systemd , do their work and then exit , similar
to cronjobs .
- dbus services will be considered started up once they aquire the specified dbus
busnotify and notify - reload notify systemd about the start up via sd_notify .
notify - reload needs also inform systemd on reloads and when it is ready again
after an reload .
- idle is similar to simple , but it can delay the start up by a few seconds .
required : false
type : str
wanted_by :
default : [ ]
description :
- systemd units that want the mount , but not explicitly require it . Commonly used
for target if not service explicitly require it .
elements : str
required : false
type : list
wants :
default : [ ]
description :
- list of units that this unit wants . If it fails or can ' t be started it does
not affect this unit
elements : str
required : false
type : list
2023-04-23 08:32:42 +00:00
workingdirectory :
description :
- The Directory that is used for the processes as current working directory
required : false
type : str
2023-04-21 16:00:50 +00:00
short_description : Creates System Services units
"""
if __name__ == " __main__ " :
Module ( ) ( )