This commit is contained in:
mwiegand 2021-07-13 14:45:27 +02:00
parent 8584484564
commit 7fd587e51a
10 changed files with 208 additions and 109 deletions

View file

@ -4,7 +4,7 @@ downloads['/usr/local/bin/gitea'] = {
'url': f'https://dl.gitea.io/gitea/{version}/gitea-{version}-linux-amd64', 'url': f'https://dl.gitea.io/gitea/{version}/gitea-{version}-linux-amd64',
'sha256': node.metadata.get('gitea/sha256'), 'sha256': node.metadata.get('gitea/sha256'),
'triggers': { 'triggers': {
'svc_systemd:gitea:restart', 'svc_systemd:gitea.service:restart',
}, },
'preceded_by': { 'preceded_by': {
'action:stop_gitea', 'action:stop_gitea',
@ -17,7 +17,7 @@ directories['/var/lib/gitea'] = {
'owner': 'git', 'owner': 'git',
'mode': '0700', 'mode': '0700',
'triggers': { 'triggers': {
'svc_systemd:gitea:restart', 'svc_systemd:gitea.service:restart',
}, },
} }
@ -40,6 +40,6 @@ files['/etc/gitea/app.ini'] = {
'owner': 'git', 'owner': 'git',
'context': node.metadata['gitea'], 'context': node.metadata['gitea'],
'triggers': { 'triggers': {
'svc_systemd:gitea:restart', 'svc_systemd:gitea.service:restart',
}, },
} }

View file

@ -28,8 +28,8 @@ defaults = {
}, },
}, },
'systemd': { 'systemd': {
'services': { 'units': {
'gitea': { 'gitea.service': {
'content': { 'content': {
'Unit': { 'Unit': {
'Description': 'gitea', 'Description': 'gitea',
@ -51,12 +51,13 @@ defaults = {
'WantedBy': 'multi-user.target', 'WantedBy': 'multi-user.target',
}, },
}, },
'needs': [ 'item': {
'action:chmod_gitea', 'needs': [
'download:/usr/local/bin/gitea', 'action:chmod_gitea',
'file:/etc/systemd/system/gitea.service', 'download:/usr/local/bin/gitea',
'file:/etc/gitea/app.ini', 'file:/etc/gitea/app.ini',
], ],
},
}, },
}, },
}, },

View file

@ -7,31 +7,33 @@ defaults = {
@metadata_reactor.provides( @metadata_reactor.provides(
'systemd-networkd/networks', 'systemd/units',
) )
def systemd_networkd(metadata): def units(metadata):
units = {} units = {}
for type, network in metadata.get('network').items(): for type, network in metadata.get('network').items():
units[type] = { units[f'{type}.network'] = {
'Match': { 'content': {
'Name': network['interface'], 'Match': {
}, 'Name': network['interface'],
'Network': { },
'DHCP': 'no', 'Network': {
'IPv6AcceptRA': 'no', 'DHCP': 'no',
'IPv6AcceptRA': 'no',
}
} }
} }
for i in [4, 6]: for i in [4, 6]:
if network.get(f'ipv{i}', None): if network.get(f'ipv{i}', None):
units[type].update({ units[f'{type}.network']['content'].update({
f'Address#ipv{i}': { f'Address#ipv{i}': {
'Address': network[f'ipv{i}'], 'Address': network[f'ipv{i}'],
}, },
}) })
if f'gateway{i}' in network: if f'gateway{i}' in network:
units[type].update({ units[f'{type}.network']['content'].update({
f'Route#ipv{i}': { f'Route#ipv{i}': {
'Gateway': network[f'gateway{i}'], 'Gateway': network[f'gateway{i}'],
'GatewayOnlink': 'yes', 'GatewayOnlink': 'yes',
@ -40,7 +42,7 @@ def systemd_networkd(metadata):
return { return {
'systemd-networkd': { 'systemd': {
'networks': units, 'units': units,
} }
} }

View file

@ -16,21 +16,21 @@ directories = {
}, },
} }
for type, path in { # for type, path in {
'networks': '/etc/systemd/network/{}.network', # 'networks': '/etc/systemd/network/{}.network',
'netdevs': '/etc/systemd/network/{}.netdev', # 'netdevs': '/etc/systemd/network/{}.netdev',
}.items(): # }.items():
for name, config in node.metadata.get(f'systemd-networkd/{type}').items(): # for name, config in node.metadata.get(f'systemd-networkd/{type}').items():
files[path.format(name)] = { # files[path.format(name)] = {
'content': repo.libs.systemd.generate_unitfile(config), # 'content': repo.libs.systemd.generate_unitfile(config),
'needed_by': { # 'needed_by': {
'svc_systemd:systemd-networkd', # 'svc_systemd:systemd-networkd',
}, # },
'triggers': { # 'triggers': {
'svc_systemd:systemd-networkd:restart', # 'svc_systemd:systemd-networkd:restart',
}, # },
} # }
#
svc_systemd = { svc_systemd = {
'systemd-networkd': {}, 'systemd-networkd': {},
} }

View file

@ -1,26 +1,27 @@
# svc_systemd['cron'] = { # # svc_systemd['cron'] = {
# 'enabled': False, # # 'enabled': False,
# } # # }
#
for name, config in node.metadata.get('systemd-timers').items(): # for name, config in node.metadata.get('systemd-timers').items():
files[f'/etc/systemd/system/{name}.timer'] = { # files[f'/etc/systemd/system/{name}.timer'] = {
'content': repo.libs.systemd.generate_unitfile({ # 'content': repo.libs.systemd.generate_unitfile({
'Unit':{ # 'Unit':{
'Description': f'{name} timer', # 'Description': f'{name} timer',
}, # },
'Timer': { # 'Timer': {
'OnCalendar': config['when'], # 'OnCalendar': config['when'],
'Persistent': config.get('persistent', False), # 'Persistent': config.get('persistent', False),
'Unit': f'{name}.service', # 'Unit': f'{name}.service',
}, # },
'Install': { # 'Install': {
'WantedBy': 'multi-user.target', # 'WantedBy': 'multi-user.target',
} # }
}), # }),
'triggers': [ # 'triggers': [
'action:systemd-reload', # 'action:systemd-reload',
f'svc_systemd:{name}:restart', # f'svc_systemd:{name}:restart',
], # ],
} # }
#
svc_systemd[f'{name}.timer'] = {} # svc_systemd[f'{name}.timer'] = {}
# #

View file

@ -3,14 +3,41 @@ defaults = {
} }
@metadata_reactor.provides(
'systemd/services',
)
def timers(metadata):
return {
'systemd': {
'units': {
f'{name}.timer': {
'content': {
'Unit':{
'Description': f'{name} timer',
},
'Timer': {
'OnCalendar': config['when'],
'Persistent': config.get('persistent', False),
'Unit': f'{name}.service',
},
'Install': {
'WantedBy': 'multi-user.target',
}
},
} for name, config in metadata.get('systemd-timers').items()
},
},
}
@metadata_reactor.provides( @metadata_reactor.provides(
'systemd/services', 'systemd/services',
) )
def services(metadata): def services(metadata):
return { return {
'systemd': { 'systemd': {
'services': { 'units': {
name: { f'{name}.service': {
'content': { 'content': {
'Unit':{ 'Unit':{
'Description': f'{name} timer service', 'Description': f'{name} timer service',
@ -19,9 +46,11 @@ def services(metadata):
'ExecStart': config['command'], 'ExecStart': config['command'],
}, },
}, },
'enabled': False, 'item': {
'running': False, 'enabled': False,
} for name, config in metadata.get('systemd-timers').items() 'running': False,
},
} for name, config in metadata.get('systemd-timers').items()
}, },
}, },
} }

23
bundles/systemd/README.md Normal file
View file

@ -0,0 +1,23 @@
# SYSTEMD
## metadata
```python
{
'systemd': {
'units': {
'test.service': {
# optional: will be derived from unit extension
'path': '/etc/systemd/system/test.service',
# content of the unit file
'content': {
},
# bw item data
# applies to unitfile and svc_systemd aswell, if present
'item': {
},
}
}
},
}
```

View file

@ -6,39 +6,18 @@ actions = {
'command': 'systemctl daemon-reload', 'command': 'systemctl daemon-reload',
'cascade_skip': False, 'cascade_skip': False,
'triggered': True, 'triggered': True,
'needed_by': {
'svc_systemd:',
},
}, },
} }
for name, service in node.metadata.get('systemd/services').items(): for name, config in node.metadata.get('systemd/units').items():
# dont call a service 'service' explicitly files[config['path']] = {
if name.endswith('.service'): 'content': repo.libs.systemd.generate_unitfile(config['content']),
raise Exception(name) **config['item'],
# split unit file content data from item data
content_data = service.pop('content')
# default WantedBy=multi-user.target
content_data\
.setdefault('Install', {})\
.setdefault('WantedBy', {'multi-user.target'})
# create unit file
unit_path = f'/etc/systemd/system/{name}.service'
files[unit_path] = {
'content': repo.libs.systemd.generate_unitfile(content_data),
'triggers': [
'action:systemd-reload',
f'svc_systemd:{name}:restart',
],
} }
files[config['path']].setdefault('triggers', []).append("action:systemd-reload")
# service depends on unit file for name, config in node.metadata.get('systemd/services').items():
service\ svc_systemd[name] = {
.setdefault('needs', [])\ **config,
.append(f'file:{unit_path}') }
svc_systemd[name].setdefault('needs', []).append("action:systemd-reload")
# service
svc_systemd[name] = service

View file

@ -1,5 +1,65 @@
defaults = { defaults = {
'systemd': { 'systemd': {
'units': {},
'services': {}, 'services': {},
} }
} }
# create a svc_sytemd item for each .service and .timer unit
@metadata_reactor#.provides(
# 'systemd/services',
#)
def unit_services(metadata):
services = {}
for name, config in metadata.get('systemd/units').items():
if name.split('.')[-1] not in ['timer', 'service']:
continue
services[name] = config['item']
services[name].setdefault('needs', []).append(f"file:{config.get('path')}")
return {
'systemd': {
'services': services,
}
}
# add defaults to units
@metadata_reactor#.provides(
# 'systemd/units',
#)
def unit_defaults(metadata):
units = {}
for name in metadata.get('systemd/units').keys():
extension = name.split('.')[-1]
if extension in ['netdev', 'network']:
units[name] = {
'path': f'/etc/systemd/network/{name}',
'item': {
'triggers': [
'svc_systemd:systemd-networkd:restart',
]
}
}
elif extension in ['timer', 'service']:
units[name] = {
'path': f'/etc/systemd/system/{name}',
'item': {
'triggers': [
f'svc_systemd:{name}:restart',
]
},
}
else:
raise Exception(f'unknown unit extension: "{extension}"')
return {
'systemd': {
'units': units,
}
}

View file

@ -26,7 +26,7 @@ defaults = {
@metadata_reactor.provides( @metadata_reactor.provides(
'systemd-networkd/networks', 'systemd/units',
) )
def systemd_networkd_networks(metadata): def systemd_networkd_networks(metadata):
network = { network = {
@ -60,16 +60,18 @@ def systemd_networkd_networks(metadata):
}) })
return { return {
'systemd-networkd': { 'systemd': {
'networks': { 'units': {
'wireguard': network, 'wireguard.network': {
'content': network,
},
}, },
}, },
} }
@metadata_reactor.provides( @metadata_reactor.provides(
'systemd-networkd/netdevs', 'systemd/units',
) )
def systemd_networkd_netdevs(metadata): def systemd_networkd_netdevs(metadata):
netdev = { netdev = {
@ -99,9 +101,11 @@ def systemd_networkd_netdevs(metadata):
}) })
return { return {
'systemd-networkd': { 'systemd': {
'netdevs': { 'units': {
'wireguard': netdev, 'wireguard.netdev': {
'content': netdev,
},
}, },
}, },
} }