-
Notifications
You must be signed in to change notification settings - Fork 22
/
addSystemUser.py
46 lines (41 loc) · 1.86 KB
/
addSystemUser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
##############################################################################
#
# Copyright (C) Zenoss, Inc. 2013, all rights reserved.
#
# This content is made available according to terms specified in
# License.zenoss under the directory where your Zenoss product is installed.
#
##############################################################################
# this script is meant to be ran inside of zendmd
# e.g. zendmd --script addSystemUser.py
import re
import logging
from ZODB.transact import transact
from Products.ZenUtils.Utils import zenPath
from subprocess import call
log = logging.getLogger("zenoss.addsystemuser")
username = 'zenoss_system'
@transact
def addSystemUser(dmd, username):
password = dmd.ZenUsers.generatePassword()
dmd.ZenUsers.manage_addUser(username, password=password, roles=('Manager',))
return username, password
if dmd.ZenUsers._getOb(username, None) is None:
username, password = addSystemUser(dmd, username)
cmd = zenPath('bin', 'zenglobalconf') + " -u zauth-username=%s -u zauth-password=%s" % (username, password)
rc = call(cmd, shell=True)
if rc != 0:
log.error("Unable to set the system username and password please update %s with these lines\nzauth-username=%s\nzauth-password=%s", zenPath('etc', 'global.conf'), username, password)
# Now do the shipper, which is YAML
shippercfg = zenPath('etc', 'metricshipper', 'metricshipper.yaml')
with open(shippercfg, 'r') as f:
cfg = f.read()
# Could use YAML to parse/dump this, but that would remove helpful comments.
# Wimp out with regex for now.
cfg = re.sub(r'^\s*#?\s*username:.*', '\nusername: %s' % username, cfg,
flags=re.MULTILINE)
cfg = re.sub(r'^\s*#?\s*password:.*', '\npassword: %s' % password, cfg,
flags=re.MULTILINE)
with open(shippercfg, 'w') as f:
f.write(cfg)