forked from saltstack/salt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
executable file
·267 lines (237 loc) · 8.76 KB
/
__init__.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
'''
Make me some salt!
'''
__version_info__ = (0, 9, 5, 'pre')
__version__ = '.'.join(map(str, __version_info__))
# Import python libs
import optparse
import os
import sys
# Import salt libs, the try block bypasses an issue at build time so that c
# modules don't cause the build to fail
try:
import salt.config
import salt.utils.verify
except ImportError:
pass
def verify_env(dirs):
'''
Verify that the named directories are in place and that the environment
can shake the salt
'''
for dir_ in dirs:
if not os.path.isdir(dir_):
try:
os.makedirs(dir_)
except OSError, e:
print 'Failed to create directory path "%s" - %s' % (dir_, e)
# Run the extra verification checks
salt.utils.verify.run()
class Master(object):
'''
Creates a master server
'''
def __init__(self):
self.cli = self.__parse_cli()
self.opts = salt.config.master_config(self.cli['config'])
def __parse_cli(self):
'''
Parse the cli for options passed to a master daemon
'''
import salt.log
parser = optparse.OptionParser(version="%%prog %s" % __version__)
parser.add_option('-d',
'--daemon',
dest='daemon',
default=False,
action='store_true',
help='Run the master in a daemon')
parser.add_option('-c',
'--config',
dest='config',
default='/etc/salt/master',
help='Pass in an alternative configuration file')
parser.add_option('-l',
'--log-level',
dest='log_level',
default='warning',
choices=salt.log.LOG_LEVELS.keys(),
help='Console log level. One of %s. For the logfile settings '
'see the config file. Default: \'%%default\'.' %
', '.join([repr(l) for l in salt.log.LOG_LEVELS.keys()])
)
options, args = parser.parse_args()
salt.log.setup_console_logger(options.log_level)
cli = {'daemon': options.daemon,
'config': options.config}
return cli
def start(self):
'''
Run the sequence to start a salt master server
'''
verify_env([os.path.join(self.opts['pki_dir'], 'minions'),
os.path.join(self.opts['pki_dir'], 'minions_pre'),
os.path.join(self.opts['cachedir'], 'jobs'),
os.path.dirname(self.opts['log_file']),
self.opts['sock_dir'],
])
import salt.log
salt.log.setup_logfile_logger(
self.opts['log_file'], self.opts['log_level']
)
for name, level in self.opts['log_granular_levels'].iteritems():
salt.log.set_logger_level(name, level)
import logging
# Late import so logging works correctly
import salt.master
master = salt.master.Master(self.opts)
if self.cli['daemon']:
# Late import so logging works correctly
import salt.utils
salt.utils.daemonize()
master.start()
class Minion(object):
'''
Create a minion server
'''
def __init__(self):
self.cli = self.__parse_cli()
self.opts = salt.config.minion_config(self.cli['config'])
def __parse_cli(self):
'''
Parse the cli input
'''
import salt.log
parser = optparse.OptionParser(version="%%prog %s" % __version__)
parser.add_option('-d',
'--daemon',
dest='daemon',
default=False,
action='store_true',
help='Run the minion as a daemon')
parser.add_option('-c',
'--config',
dest='config',
default='/etc/salt/minion',
help='Pass in an alternative configuration file')
parser.add_option('-l',
'--log-level',
dest='log_level',
default='warning',
choices=salt.log.LOG_LEVELS.keys(),
help='Console log level. One of %s. For the logfile settings '
'see the config file. Default: \'%%default\'.' %
', '.join([repr(l) for l in salt.log.LOG_LEVELS.keys()]))
options, args = parser.parse_args()
salt.log.setup_console_logger(options.log_level)
cli = {'daemon': options.daemon,
'config': options.config}
return cli
def start(self):
'''
Execute this method to start up a minion.
'''
verify_env([self.opts['pki_dir'], self.opts['cachedir'],
os.path.dirname(self.opts['log_file']),
])
import salt.log
salt.log.setup_logfile_logger(
self.opts['log_file'], self.opts['log_level']
)
for name, level in self.opts['log_granular_levels'].iteritems():
salt.log.set_logger_level(name, level)
import logging
# Late import so logging works correctly
import salt.minion
if self.cli['daemon']:
# Late import so logging works correctly
import salt.utils
salt.utils.daemonize()
minion = salt.minion.Minion(self.opts)
minion.tune_in()
class Syndic(object):
'''
Create a syndic server
'''
def __init__(self):
self.cli = self.__parse_cli()
self.opts = self.__prep_opts()
def __prep_opts(self):
'''
Generate the opts used by the syndic
'''
opts = salt.config.master_config(self.cli['master_config'])
opts['_minion_conf_file'] = opts['conf_file']
opts.update(salt.config.minion_config(self.cli['minion_config']))
if 'syndic_master' in opts:
# Some of the opts need to be changed to match the needed opts
# in the minion class.
opts['master'] = opts['syndic_master']
opts['master_ip'] = salt.config.dns_check(opts['master'])
opts['master_uri'] = ('tcp://' + opts['master_ip'] +
':' + str(opts['master_port']))
opts['_master_conf_file'] = opts['conf_file']
opts.pop('conf_file')
return opts
err = ('The syndic_master needs to be configured in the salt master '
'config, EXITING!\n')
sys.stderr.write(err)
sys.exit(2)
def __parse_cli(self):
'''
Parse the cli for options passed to a master daemon
'''
import salt.log
parser = optparse.OptionParser(version="%%prog %s" % __version__)
parser.add_option('-d',
'--daemon',
dest='daemon',
default=False,
action='store_true',
help='Run the master in a daemon')
parser.add_option('--master-config',
dest='master_config',
default='/etc/salt/master',
help='Pass in an alternative master configuration file')
parser.add_option('--minion-config',
dest='minion_config',
default='/etc/salt/minion',
help='Pass in an alternative minion configuration file')
parser.add_option('-l',
'--log-level',
dest='log_level',
default='warning',
choices=salt.log.LOG_LEVELS.keys(),
help=('Console log level. One of %s. For the logfile settings '
'see the config file. Default: \'%%default\'.' %
', '.join([repr(l) for l in salt.log.LOG_LEVELS.keys()]))
)
options, args = parser.parse_args()
salt.log.setup_console_logger(options.log_level)
cli = {'daemon': options.daemon,
'minion_config': options.minion_config,
'master_config': options.master_config,
}
return cli
def start(self):
'''
Execute this method to start up a syndic.
'''
verify_env([self.opts['pki_dir'], self.opts['cachedir'],
os.path.dirname(self.opts['log_file']),
])
import salt.log
salt.log.setup_logfile_logger(
self.opts['log_file'], self.opts['log_level']
)
for name, level in self.opts['log_granular_levels'].iteritems():
salt.log.set_logger_level(name, level)
import logging
# Late import so logging works correctly
import salt.minion
syndic = salt.minion.Syndic(self.opts)
if self.cli['daemon']:
# Late import so logging works correctly
import salt.utils
salt.utils.daemonize()
syndic.tune_in()