Skip to content

Commit 28e7f79

Browse files
committed
[] Initial commit
0 parents  commit 28e7f79

File tree

11 files changed

+301
-0
lines changed

11 files changed

+301
-0
lines changed

README.md

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Systemd
2+
3+
Puppet module to generate systemd unit files and manage services
4+
5+
## Creating Unit Files
6+
7+
*systemd::resources::init* resource will create the unit files and manage service using puppet resource.
8+
9+
For common unit directives refer to man(5) systemd.unit, man(7) systemd.directives
10+
11+
### Service unit
12+
13+
man(5) systemd.service
14+
15+
```
16+
systemd::units:
17+
'hello_service':
18+
type: 'service'
19+
ensure: 'running'
20+
enable: 'true'
21+
unit_params:
22+
'Unit':
23+
Description: "program to print $MESSAGE"
24+
'Service':
25+
ExecStart: "/bin/echo $MESSAGE"
26+
Environment: "MESSAGE=HELLOWORLD"
27+
Slice: "hello.slice"
28+
'Install':
29+
WantedBy: 'multi-user.target'
30+
31+
```
32+
33+
### Slice unit
34+
35+
man(5) systemd.slice
36+
37+
```
38+
systemd::units:
39+
'hello_slice':
40+
type: 'slice'
41+
ensure: 'running'
42+
enable: 'true'
43+
unit_params:
44+
'Unit':
45+
Description: "Slice for hello service"
46+
'Slice':
47+
CPUQuota: "300%"
48+
MemoryMax: "5G"
49+
'Install':
50+
WantedBy: 'multi-user.target'
51+
'hello_timer':
52+
type: 'slice'
53+
ensure: 'running'
54+
enable: 'true'
55+
```
56+
57+
### Timer unit
58+
59+
man(5) systemd.timer
60+
61+
```
62+
systemd::units:
63+
'hello_timer':
64+
type: 'slice'
65+
ensure: 'running'
66+
enable: 'true'
67+
unit_params:
68+
'Unit':
69+
Description: "Run hello service periodically"
70+
'Timer':
71+
Unit: 'hello.service'
72+
OnCalendar: '*-*-* 0/8:00:00'
73+
'Install':
74+
WantedBy: 'timers.target'
75+
```
76+
77+
78+
## Managing Journald config
79+
80+
man(5) journald.conf
81+
82+
```
83+
systemd::journald_conf:
84+
Compress: 'yes'
85+
SyncIntervalSec: '5m'
86+
```
87+
88+
## Managing Logind config
89+
90+
man(5) logind.conf
91+
92+
```
93+
systemd::logind_conf:
94+
NAutoVTs: 6
95+
KillUserProcesses: 'yes'
96+
```
97+
98+
99+
## Enabling/Disabling Linger for user
100+
101+
man loginctl
102+
103+
```
104+
systemd::linger:
105+
'unprivileged_user': {} # enable
106+
'deploy_user':
107+
enable: false # disable
108+
```

manifests/init.pp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# @summary
2+
# This module manages systemd configuration and unit files.
3+
#
4+
# @example Basic usage
5+
# include systemd
6+
#
7+
# @param template
8+
# Specifies a custom template. A template takes precedence over `content`. Valid options: '/mymodule/mytemplate.erb'.
9+
#
10+
class systemd(
11+
# Start Hiera Lookups
12+
Hash $units = {},
13+
Hash $linger = {},
14+
Hash $logind_conf = {},
15+
Hash $journald_conf = {},
16+
) {
17+
18+
include ::systemd::systemctl::daemon_reload
19+
if !empty($logind_conf) {
20+
contain 'systemd::logind'
21+
}
22+
if !empty($journald_conf) {
23+
contain 'systemd::journald'
24+
}
25+
26+
if $facts['os']['name'] != 'Debian' {
27+
notify {"[systemd] systemd module is not tested on non debian systems":}
28+
}
29+
else{
30+
notify {"[systemd] On Debian system. Applying unit configurations":}
31+
create_resources('systemd::resources::unit', $units)
32+
create_resources('systemd::resources::linger', $linger)
33+
}
34+
35+
}

manifests/journald.pp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class systemd::journald {
2+
3+
assert_private()
4+
# Start Hiera Lookups
5+
$journald_conf = $systemd::journald_conf
6+
# Generate ini file
7+
if $journald_conf != undef {
8+
notify {"[systemd] serving jornald.conf file":}
9+
$journald_conf.each |$option, $value| {
10+
ini_setting{$option:
11+
path => '/etc/systemd/journald.conf',
12+
section => 'Journal',
13+
setting => $option,
14+
value => $value,
15+
}
16+
}
17+
}
18+
}

manifests/logind.pp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# @summary
2+
# This module manages systemd configuration and unit files.
3+
#
4+
# @example Basic usage
5+
# include systemd
6+
#
7+
# @param template
8+
# Specifies a custom template. A template takes precedence over `content`. Valid options: '/mymodule/mytemplate.erb'.
9+
#
10+
class systemd::logind {
11+
12+
assert_private()
13+
# Start Hiera Lookups
14+
$logind_conf = $systemd::logind_conf
15+
# Generate ini file
16+
if $logind_conf != undef {
17+
notify {"[systemd] serving logind.conf file":}
18+
$logind_conf.each |$option, $value| {
19+
ini_setting{$option:
20+
path => '/etc/systemd/logind.conf',
21+
section => 'Login',
22+
setting => $option,
23+
value => $value
24+
}
25+
}
26+
}
27+
}

manifests/resources/linger.pp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
define systemd::resources::linger (
2+
$enable = true,
3+
) {
4+
5+
if $enable == true {
6+
# Enabling linger for user
7+
notify {"[systemd] Enabling linger for ${name}":}
8+
exec { "Enabling linger for ${name}":
9+
command => "loginctl enable-linger ${name}",
10+
path => "/usr/bin:/bin:",
11+
unless => "/usr/bin/test -f /var/lib/systemd/linger/${name}",
12+
}
13+
}
14+
else {
15+
# Disabling linger for user
16+
notify {"[systemd] Disabling linger for ${name}":}
17+
exec { "Disabling linger for ${name}":
18+
command => "loginctl disable-linger ${name}",
19+
path => "/usr/bin:/bin:",
20+
onlyif => "/usr/bin/test -f /var/lib/systemd/linger/${name}",
21+
}
22+
}
23+
24+
}

manifests/resources/unit.pp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
define systemd::resources::unit (
2+
$type = 'mount',
3+
Stdlib::Absolutepath $path = '/etc/systemd/system',
4+
$enable = 'true',
5+
$ensure = 'running',
6+
$template_basepath = 'systemd',
7+
$unit_params = undef,
8+
$owner = 'root',
9+
$group = 'root',
10+
$mode = '0644',
11+
) {
12+
13+
case $type {
14+
'service', 'slice', 'timer', 'path': {
15+
# For debugging
16+
notify {"[systemd] Serving content for service unit in ${name}": }
17+
# Generate the absolute path for unit file (this may be useful in future)
18+
$file_path = "${path}/${name}.${type}"
19+
# Serve unit file and reload systemd daemon if there is any change
20+
file { "${file_path}":
21+
owner => "$owner",
22+
group => "$group",
23+
mode => "$mode",
24+
content => template("${template_basepath}/${type}.erb"),
25+
notify => [
26+
Class['systemd::systemctl::daemon_reload'],
27+
Service["${name}.${type}"],
28+
],
29+
}
30+
# Manage the state of unit using puppet service resource
31+
service { "${name}.${type}":
32+
ensure => "$ensure",
33+
enable => "$enable",
34+
provider => "systemd",
35+
require => Class['systemd::systemctl::daemon_reload'],
36+
}
37+
}
38+
default: {
39+
# For debugging
40+
notify {"[systemd] Unit type does not exists, or not supported by the module. Failed to create unit $name": }
41+
}
42+
}
43+
44+
}

manifests/systemctl/daemon_reload.pp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Summary
2+
# Reload systemd daemon
3+
#
4+
class systemd::systemctl::daemon_reload {
5+
6+
exec { 'systemctl daemon-reload':
7+
refreshonly => true,
8+
path => '/usr/bin:/bin',
9+
}
10+
11+
}

templates/path.erb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Unit]
2+
<% if @unit_params['Unit'] %><% @unit_params['Unit'].each do |key, value| %><%= key %>=<%= value %><%= "\n" %><% end %><% end %>
3+
[Path]
4+
<% if @unit_params['Path'] %><% @unit_params['Path'].each do |key, value| %><%= key %>=<%= value %><%= "\n" %><% end %><% end %>
5+
<% if @unit_params['Install'] %>[Install]
6+
<% @unit_params['Install'].each do |key, value| %><%= key %>=<%= value %><%= "\n" %><% end %>
7+
<% end %>

templates/service.erb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<% if @unit_params['Unit'] %>[Unit]
2+
<% @unit_params['Unit'].each do |key, value| %><%= key %>=<%= value %><%= "\n" %><% end %>
3+
<% end %>
4+
<% if @unit_params['Service'] %>[Service]
5+
<% @unit_params['Service'].each do |key, value| %><%= key %>=<%= value %><%= "\n" %><% end %>
6+
<% end %>
7+
<% if @unit_params['Install'] %>[Install]
8+
<% @unit_params['Install'].each do |key, value| %><%= key %>=<%= value %><%= "\n" %><% end %>
9+
<% end %>

templates/slice.erb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<% if @unit_params['Unit'] %>[Unit]
2+
<% @unit_params['Unit'].each do |key, value| %><%= key %>=<%= value %><%= "\n" %><% end %>
3+
<% end %>
4+
<% if @unit_params['Slice'] %>[Slice]
5+
<% @unit_params['Slice'].each do |key, value| %><%= key %>=<%= value %><%= "\n" %><% end %>
6+
<% end %>
7+
<% if @unit_params['Install'] %>[Install]
8+
<% @unit_params['Install'].each do |key, value| %><%= key %>=<%= value %><%= "\n" %><% end %>
9+
<% end %>

templates/timer.erb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<% if @unit_params['Unit'] %>[Unit]
2+
<% @unit_params['Unit'].each do |key, value| %><%= key %>=<%= value %><%= "\n" %><% end %>
3+
<% end %>
4+
<% if @unit_params['Timer'] %>[Timer]
5+
<% @unit_params['Timer'].each do |key, value| %><%= key %>=<%= value %><%= "\n" %><% end %>
6+
<% end %>
7+
<% if @unit_params['Install'] %>[Install]
8+
<% @unit_params['Install'].each do |key, value| %><%= key %>=<%= value %><%= "\n" %><% end %>
9+
<% end %>

0 commit comments

Comments
 (0)