Skip to content

Commit

Permalink
Merge branch 'python-reformatting' of https://github.com/astoorangi/b…
Browse files Browse the repository at this point in the history
…areos into astoorangi-python-reformatting
  • Loading branch information
sduehr committed Nov 28, 2019
2 parents ec88b5b + 0ea8d3d commit 27eb39d
Show file tree
Hide file tree
Showing 23 changed files with 2,227 additions and 1,695 deletions.
85 changes: 44 additions & 41 deletions core/platforms/univention/univention-bareos.py
Expand Up @@ -2,7 +2,7 @@
#
"""Bareos Client Configuration Listener Module."""

__package__ = '' # workaround for PEP 366
__package__ = "" # workaround for PEP 366
import grp
import os
import stat
Expand All @@ -13,35 +13,35 @@
from listener import setuid, unsetuid
import univention.debug as ud

name = 'bareos'
description = 'Generate Bareos Configuration for Clients'
filter = '(objectClass=bareosClientHost)'
name = "bareos"
description = "Generate Bareos Configuration for Clients"
filter = "(objectClass=bareosClientHost)"
attributes = []

PATH_PREFIX = '/etc/bareos/autogenerated'
JOBS_PATH = PATH_PREFIX + '/clients'
INCLUDES_PATH = PATH_PREFIX + '/clients.include'
BCONSOLE_CMD = ['/usr/bin/bconsole']
PATH_PREFIX = "/etc/bareos/autogenerated"
JOBS_PATH = PATH_PREFIX + "/clients"
INCLUDES_PATH = PATH_PREFIX + "/clients.include"
BCONSOLE_CMD = ["/usr/bin/bconsole"]

JOB_DISABLED = 'Not'
JOB_DISABLED = "Not"

bareos_gid = grp.getgrnam('bareos').gr_gid
bareos_gid = grp.getgrnam("bareos").gr_gid


def getFqdn(entry):
if not entry.has_key('cn'):
if not entry.has_key("cn"):
return None

name = entry['cn'][0]
if entry.has_key('associatedDomain'):
name = name + '.' + entry['associatedDomain'][0]
name = entry["cn"][0]
if entry.has_key("associatedDomain"):
name = name + "." + entry["associatedDomain"][0]

return name


def initialize():
"""Initialize the module once on first start or after clean."""
ud.debug(ud.LISTENER, ud.INFO, 'BAREOS: Initialize')
ud.debug(ud.LISTENER, ud.INFO, "BAREOS: Initialize")


def handler(dn, new, old):
Expand Down Expand Up @@ -87,16 +87,16 @@ def processClient(client_name, entry, delete=False):
if client_name is None:
return

client_type = 'generic'
if 'univentionWindows' in entry['objectClass']:
client_type = 'windows'
client_type = "generic"
if "univentionWindows" in entry["objectClass"]:
client_type = "windows"

if delete == True:
removeClient(client_name, client_type)
return

if entry.has_key('bareosEnableJob'):
if entry['bareosEnableJob'][0] == JOB_DISABLED:
if entry.has_key("bareosEnableJob"):
if entry["bareosEnableJob"][0] == JOB_DISABLED:
removeClient(client_name, client_type)
return

Expand All @@ -121,7 +121,7 @@ def getClientSecret(client_name):
password = None

try:
f = open(path, 'r')
f = open(path, "r")
password = f.read().strip()
except:
password = createClientSecret(client_name)
Expand All @@ -135,56 +135,59 @@ def exportBareosFdDirectorResource(client_name, client_type):
# additional reload required, to gurantee that client is known to director
out = process.communicate(
b'reload\nconfigure export client="{client_name}-fd"\n'.format(
client_name=client_name))[0]
client_name=client_name
)
)[0]
ud.debug(ud.LISTENER, ud.INFO, "bareos export output:\n" + str(out))


def createClientSecret(client_name):
path = getClientSecretPath(client_name)

char_set = string.ascii_uppercase + string.digits + string.ascii_lowercase
password = ''.join(random.sample(char_set * 40, 40))
password = "".join(random.sample(char_set * 40, 40))
with os.fdopen(
os.open(path, os.O_CREAT | os.O_WRONLY,
stat.S_IRUSR | stat.S_IWUSR), 'w') as f:
os.open(path, os.O_CREAT | os.O_WRONLY, stat.S_IRUSR | stat.S_IWUSR), "w"
) as f:
f.write(password)
os.chown(path, -1, 0)

return password


def removeClientJob(client_name):
path = JOBS_PATH + '/' + client_name + '.include'
path = JOBS_PATH + "/" + client_name + ".include"
os.remove(path)


def createClientJob(client_name, client_type, enable='Yes'):
def createClientJob(client_name, client_type, enable="Yes"):
password = getClientSecret(client_name)
path = JOBS_PATH + '/' + client_name + '.include'
templatefile = JOBS_PATH + '/' + client_type + '.template'
with open(templatefile, 'r') as f:
path = JOBS_PATH + "/" + client_name + ".include"
templatefile = JOBS_PATH + "/" + client_type + ".template"
with open(templatefile, "r") as f:
content = f.read()

t = string.Template(content)
with os.fdopen(
os.open(path, os.O_CREAT | os.O_WRONLY,
stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP), 'w') as f:
f.write(
t.substitute(
enable=enable, password=password, client_name=client_name))
os.open(
path, os.O_CREAT | os.O_WRONLY, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP
),
"w",
) as f:
f.write(t.substitute(enable=enable, password=password, client_name=client_name))
os.chown(path, -1, bareos_gid)


def disableClientJob(client_name, client_type):
createClientJob(client_name, client_type, 'No')
createClientJob(client_name, client_type, "No")


def getClientIncludePath(client_name):
return '@' + JOBS_PATH + '/' + client_name + '.include'
return "@" + JOBS_PATH + "/" + client_name + ".include"


def getClientSecretPath(client_name):
return JOBS_PATH + '/' + client_name + '.secret'
return JOBS_PATH + "/" + client_name + ".secret"


def addClientInclude(client_name):
Expand All @@ -197,14 +200,14 @@ def addClientInclude(client_name):
return

# if not, add it at the end of the file
with open(INCLUDES_PATH, 'a') as f:
with open(INCLUDES_PATH, "a") as f:
f.write(getClientIncludePath(client_name))
f.write('\n')
f.write("\n")


def isClientIncluded(client_name):
want = getClientIncludePath(client_name)
with open(INCLUDES_PATH, 'r') as f:
with open(INCLUDES_PATH, "r") as f:
for l in f.readlines():
if want in l:
return True
Expand Down

0 comments on commit 27eb39d

Please sign in to comment.