-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal
executable file
·139 lines (112 loc) · 3.43 KB
/
global
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
#---------------------------------------------------------------
# Project : adminkit
# File : global
# Copyright : (C) 2011,2012 by Frederic Lepied
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Author : Frederic Lepied
# Created On : Sun Feb 20 14:28:08 2011
#---------------------------------------------------------------
"""
Entry point for global rules that need to process the config as a
whole.
"""
import os
import sys
import commands
try:
import adminkit
except ImportError:
sys.path = ['/usr/share/adminkit'] + sys.path
import adminkit
from plan import Plan
SUBDIR = sys.argv[1]
del sys.argv[1]
plan = Plan()
CFGFILE = adminkit.init(plan)
# safety check
if not os.path.isfile(CFGFILE):
sys.exit(0)
# Implement the same functions as if it was adminkit itself
_HOSTS = []
_ROLES = {}
define_domain = plan.define_domain
def check_host(host):
"""Add host to the list of known hosts."""
if '.' not in host and plan.default_domain():
host = host + '.' + plan.default_domain()
if host not in _HOSTS:
_HOSTS.append(host)
return host
def add_var_host(host, name, val):
"""Define a variable named name to the value val if host is the current host."""
check_host(host)
plan.add_var_host(host, name, val)
def add_to_list_host(host, name, val):
"""Add val to the list named name if host if the current host."""
check_host(host)
plan.add_to_list_host(host, name, val)
get_var = plan.get_var
def add_roles(h, *rls):
"""Add roles for the host."""
host = check_host(h)
try:
roles = _ROLES[host]
except KeyError:
roles = _ROLES[host] = []
for role in rls:
if role not in roles:
_ROLES[host].append(role)
glbls = {'define_domain': define_domain,
'add_var': add_var_host,
'add_to_list': add_to_list_host,
'get_var': get_var,
'add_roles': add_roles,
}
execfile(CFGFILE, glbls)
(path, _name) = os.path.split(sys.argv[0])
if path != '':
cmd0 = '%s/adminkit ' % path
else:
cmd0 = 'adminkit '
if adminkit.debug():
cmd0 = cmd0 + ' -d '
ret = 0
if adminkit.debug():
print 'HOSTS:', _HOSTS
print 'ROLES:', _ROLES
for hst in _HOSTS:
cmd = [cmd0, '-H', hst, '-R', os.path.join(adminkit.root(), SUBDIR), '-D', adminkit.dest()]
try:
for r in _ROLES[hst]:
cmd.append('-r')
cmd.append(r)
except KeyError:
pass
cmd.append(CFGFILE)
cmd = ' '.join(cmd)
if adminkit.debug():
print 'Launching', cmd
(res, out) = commands.getstatusoutput(cmd)
if res != 0:
print 'Error running "%s":' % cmd
print out
ret = 1
else:
print out,
sys.exit(ret)
# global ends here