|
2 |
# -*- coding: utf-8 -*- |
2 |
# -*- coding: utf-8 -*- |
3 |
# |
3 |
# |
4 |
# Univention Updater |
4 |
# Univention Updater |
5 |
# read the repository server |
5 |
""" |
|
|
6 |
Read the repository server from LDAP policy. |
7 |
""" |
6 |
# |
8 |
# |
7 |
# Copyright 2004-2012 Univention GmbH |
9 |
# Copyright 2004-2012 Univention GmbH |
8 |
# |
10 |
# |
|
32 |
# <http://www.gnu.org/licenses/>. |
34 |
# <http://www.gnu.org/licenses/>. |
33 |
|
35 |
|
34 |
import os |
36 |
import os |
35 |
import sys, subprocess |
37 |
import sys |
36 |
import univention.config_registry |
38 |
import subprocess |
|
|
39 |
from univention.config_registry import ConfigRegistry, handler_set |
37 |
|
40 |
|
38 |
configRegistry = univention.config_registry.ConfigRegistry() |
|
|
39 |
configRegistry.load() |
40 |
|
41 |
|
41 |
ldap_hostdn = configRegistry.get('ldap/hostdn') |
42 |
def terminate(result, message=None): |
42 |
|
43 |
""" |
43 |
def exit(result, message = None): |
44 |
Exit with result and optional message. |
44 |
script = os.path.basename(sys.argv[0]) |
45 |
""" |
45 |
if message: |
46 |
if message: |
46 |
print '%s: %s' % (script, message) |
47 |
script = os.path.basename(sys.argv[0]) |
|
|
48 |
print >> sys.stderr, '%s: %s' % (script, message) |
47 |
sys.exit(result) |
49 |
sys.exit(result) |
48 |
|
50 |
|
49 |
def query_policy(update): |
|
|
50 |
server = None |
51 |
p1 = subprocess.Popen(['univention_policy_result', '-D', ldap_hostdn, '-y', '/etc/machine.secret', '-s', ldap_hostdn], stdout=subprocess.PIPE) |
52 |
result = p1.communicate()[0] |
53 |
|
51 |
|
54 |
if p1.returncode != 0: |
52 |
def query_policy(ldap_hostdn): |
55 |
exit(result, "FAIL: failed to execute `univention_policy_result'") |
53 |
""" |
|
|
54 |
Retrive policy result for host. |
55 |
""" |
56 |
cmd = ( |
57 |
'univention_policy_result', |
58 |
'-D', ldap_hostdn, |
59 |
'-y', '/etc/machine.secret', |
60 |
'-s', ldap_hostdn |
61 |
) |
62 |
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
63 |
stdout, _stderr = proc.communicate() |
64 |
|
65 |
if proc.returncode != 0: |
66 |
terminate(stdout, "FAIL: failed to execute `univention_policy_result'") |
56 |
|
67 |
|
57 |
for line in result.split('\n'): |
68 |
server = update = None |
58 |
line = line.strip() |
69 |
for line in stdout.splitlines(): |
59 |
if line.startswith('univentionRepositoryServer='): |
70 |
key, value = line.split('=', 1) |
60 |
server = line.split('=', 1)[1].split('"',2)[1] |
71 |
if key == 'univentionRepositoryServer': |
61 |
elif line.startswith('univentionUpdateVersion='): |
72 |
server = value[1:-1] |
62 |
update = line.split('=', 1)[1].split('"',2)[1] |
73 |
if server.startswith('http://'): |
63 |
if server and server.startswith('http://'): |
74 |
server = server[len('http://'):] |
64 |
server = server.replace('http://', '', 1) |
75 |
elif key == 'univentionUpdateVersion': |
|
|
76 |
update = value[1:-1] |
65 |
return (server, update) |
77 |
return (server, update) |
66 |
|
78 |
|
|
|
79 |
|
67 |
def main(): |
80 |
def main(): |
68 |
restore_server = None |
81 |
""" |
69 |
online_server = configRegistry.get( 'repository/online/server') |
82 |
Retrieve repository servr from LDAP policy. |
70 |
mirror_server = configRegistry.get( 'repository/mirror/server') |
83 |
""" |
71 |
local_repo = configRegistry.is_true( 'local/repository') |
84 |
ucr = ConfigRegistry() |
72 |
fqdn = '%s.%s' % (configRegistry['hostname'], configRegistry['domainname']) |
85 |
ucr.load() |
73 |
update = '%s-%s' % (configRegistry['version/version'], configRegistry['version/patchlevel']) |
|
|
74 |
# ldap_host_dn, old_server, local_server, local_repo, fqdn, update = query_baseconfig() |
75 |
|
86 |
|
76 |
ucr_variables = [] |
87 |
ldap_hostdn = ucr.get('ldap/hostdn') |
77 |
if not ldap_hostdn: |
88 |
if not ldap_hostdn: |
78 |
# can't query policy without host-dn |
89 |
# can't query policy without host-dn |
79 |
exit(0) |
90 |
return |
80 |
|
91 |
|
81 |
new_server, update = query_policy(update) |
92 |
new_server, _update = query_policy(ldap_hostdn) |
82 |
# without a local repository |
93 |
if not _update: |
83 |
if not local_repo: |
94 |
_update = '%(version/version)s-%(version/patchlevel)s' % ucr |
84 |
if not new_server or new_server == online_server: |
95 |
|
85 |
# no new server specified |
96 |
changes = [] |
86 |
exit(0) |
97 |
if ucr.is_true('local/repository'): |
87 |
else: |
|
|
88 |
ucr_variables.append( 'repository/online/server=%s' % new_server ) |
89 |
else: |
90 |
# on a repository server |
98 |
# on a repository server |
91 |
if not new_server: |
99 |
if new_server and new_server != ucr.get('repository/mirror/server'): |
92 |
ucr_variables.append( 'repository/online/server?%s' % fqdn ) |
100 |
changes.append('repository/mirror/server=%s' % new_server) |
93 |
elif new_server != mirror_server: |
101 |
elif not new_server: |
94 |
ucr_variables.append( 'repository/mirror/server=%s' % new_server ) |
102 |
fqdn = '%(hostname)s.%(domainname)s' % ucr |
|
|
103 |
changes.append('repository/online/server?%s' % fqdn) |
104 |
else: |
105 |
# without a local repository |
106 |
if new_server and new_server != ucr.get('repository/online/server'): |
107 |
changes.append('repository/online/server=%s' % new_server) |
108 |
|
109 |
if changes: |
110 |
handler_set(changes) |
95 |
|
111 |
|
96 |
univention.config_registry.handler_set( ucr_variables ) |
|
|
97 |
|
112 |
|
98 |
if __name__ == '__main__': |
113 |
if __name__ == '__main__': |
99 |
main() |
114 |
main() |