Simplify sub-classing Master, Minion and Syndic + PEP-8.

* Since we no longer suffer from too early logger initiation, replace any `logging.getLogger(__name__)` calls with an initial defined `logger`(not `log` so it does not clash with `salt.log`.
This commit is contained in:
Pedro Algarvio 2013-01-13 15:28:23 +00:00
parent f54c7a13fb
commit 24a1f21fd2
2 changed files with 113 additions and 54 deletions

View File

@ -9,7 +9,7 @@ import logging
# Import salt libs, the try block bypasses an issue at build time so that
# modules don't cause the build to fail
from salt.version import __version__
from salt.version import __version__ # pylint: disable-msg=W402
from salt.utils import migrations
try:
@ -21,12 +21,15 @@ except ImportError as e:
raise
logger = logging.getLogger(__name__)
class Master(parsers.MasterOptionParser):
'''
Creates a master server
'''
def start(self):
def run(self):
'''
Run the sequence to start a salt master server
'''
@ -34,20 +37,22 @@ class Master(parsers.MasterOptionParser):
try:
if self.config['verify_env']:
verify_env([
self.config['pki_dir'],
os.path.join(self.config['pki_dir'], 'minions'),
os.path.join(self.config['pki_dir'], 'minions_pre'),
os.path.join(self.config['pki_dir'], 'minions_rejected'),
self.config['cachedir'],
os.path.join(self.config['cachedir'], 'jobs'),
os.path.join(self.config['cachedir'], 'proc'),
self.config['sock_dir'],
self.config['token_dir'],
],
self.config['user'],
permissive=self.config['permissive_pki_access'],
pki_dir=self.config['pki_dir'],
verify_env(
[
self.config['pki_dir'],
os.path.join(self.config['pki_dir'], 'minions'),
os.path.join(self.config['pki_dir'], 'minions_pre'),
os.path.join(self.config['pki_dir'],
'minions_rejected'),
self.config['cachedir'],
os.path.join(self.config['cachedir'], 'jobs'),
os.path.join(self.config['cachedir'], 'proc'),
self.config['sock_dir'],
self.config['token_dir'],
],
self.config['user'],
permissive=self.config['permissive_pki_access'],
pki_dir=self.config['pki_dir'],
)
if (not self.config['log_file'].startswith('tcp://') or
not self.config['log_file'].startswith('udp://') or
@ -61,7 +66,7 @@ class Master(parsers.MasterOptionParser):
sys.exit(err.errno)
self.setup_logfile_logger()
logging.getLogger(__name__).warn('Setting up the Salt Master')
logger.warn('Setting up the Salt Master')
if not verify_socket(self.config['interface'],
self.config['publish_port'],
@ -71,22 +76,39 @@ class Master(parsers.MasterOptionParser):
# Late import so logging works correctly
import salt.master
master = salt.master.Master(self.config)
self.master = salt.master.Master(self.config)
self.daemonize_if_required()
self.set_pidfile()
if check_user(self.config['user']):
try:
master.start()
self.start()
except salt.master.MasterExit:
self.stop()
finally:
sys.exit()
def start(self):
'''
Start the actual master. If sub-classed, don't **ever** forget to run:
super(YourSubClass, self).start()
NOTE: Run your start-up code before calling `super()`.
'''
self.master.start()
def stop(self):
'''
If sub-classed, run any shutdown operations on this method.
'''
class Minion(parsers.MinionOptionParser):
'''
Create a minion server
'''
def start(self):
def run(self):
'''
Execute this method to start up a minion.
'''
@ -95,18 +117,20 @@ class Minion(parsers.MinionOptionParser):
try:
if self.config['verify_env']:
confd = os.path.join(
os.path.dirname(self.config['conf_file']),
'minion.d')
verify_env([
self.config['pki_dir'],
self.config['cachedir'],
self.config['sock_dir'],
self.config['extension_modules'],
confd,
],
self.config['user'],
permissive=self.config['permissive_pki_access'],
pki_dir=self.config['pki_dir'],
os.path.dirname(self.config['conf_file']),
'minion.d'
)
verify_env(
[
self.config['pki_dir'],
self.config['cachedir'],
self.config['sock_dir'],
self.config['extension_modules'],
confd,
],
self.config['user'],
permissive=self.config['permissive_pki_access'],
pki_dir=self.config['pki_dir'],
)
if (not self.config['log_file'].startswith('tcp://') or
not self.config['log_file'].startswith('udp://') or
@ -123,9 +147,10 @@ class Minion(parsers.MinionOptionParser):
sys.exit(err.errno)
self.setup_logfile_logger()
log = logging.getLogger(__name__)
log.warn(
'Setting up the Salt Minion "{0}"'.format( self.config['id'])
logger.warn(
'Setting up the Salt Minion "{0}"'.format(
self.config['id']
)
)
migrations.migrate_paths(self.config)
# Late import so logging works correctly
@ -136,36 +161,54 @@ class Minion(parsers.MinionOptionParser):
# This is the latest safe place to daemonize
self.daemonize_if_required()
try:
minion = salt.minion.Minion(self.config)
self.minion = salt.minion.Minion(self.config)
self.set_pidfile()
if check_user(self.config['user']):
minion.tune_in()
self.start()
except KeyboardInterrupt:
log.warn('Stopping the Salt Minion')
self.stop()
finally:
raise SystemExit('\nExiting on Ctrl-c')
def start(self):
'''
Start the actual minion. If sub-classed, don't **ever** forget to run:
super(YourSubClass, self).start()
NOTE: Run your start-up code before calling `super()`.
'''
self.minion.tune_in()
def stop(self):
'''
If sub-classed, run any shutdown operations on this method.
'''
class Syndic(parsers.SyndicOptionParser):
'''
Create a syndic server
'''
def start(self):
def run(self):
'''
Execute this method to start up a syndic.
'''
self.parse_args()
try:
if self.config['verify_env']:
verify_env([
self.config['pki_dir'],
self.config['cachedir'],
self.config['sock_dir'],
self.config['extension_modules'],
],
self.config['user'],
permissive=self.config['permissive_pki_access'],
pki_dir=self.config['pki_dir'],
verify_env(
[
self.config['pki_dir'],
self.config['cachedir'],
self.config['sock_dir'],
self.config['extension_modules'],
],
self.config['user'],
permissive=self.config['permissive_pki_access'],
pki_dir=self.config['pki_dir'],
)
if (not self.config['log_file'].startswith('tcp://') or
not self.config['log_file'].startswith('udp://') or
@ -179,8 +222,7 @@ class Syndic(parsers.SyndicOptionParser):
sys.exit(err.errno)
self.setup_logfile_logger()
log = logging.getLogger(__name__)
log.warn(
logger.warn(
'Setting up the Salt Syndic Minion "{0}"'.format(
self.config['id']
)
@ -193,8 +235,25 @@ class Syndic(parsers.SyndicOptionParser):
if check_user(self.config['user']):
try:
syndic = salt.minion.Syndic(self.config)
syndic.tune_in()
self.syndic = salt.minion.Syndic(self.config)
self.start()
except KeyboardInterrupt:
log.warn('Stopping the Salt Syndic Minion')
self.stop()
finally:
raise SystemExit('\nExiting on Ctrl-c')
def start(self):
'''
Start the actual syndic. If sub-classed, don't **ever** forget to run:
super(YourSubClass, self).start()
NOTE: Run your start-up code before calling `super()`.
'''
self.syndic.tune_in()
def stop(self):
'''
If sub-classed, run any shutdown operations on this method.
'''

View File

@ -16,7 +16,7 @@ def salt_master():
Start the salt-master.
'''
master = salt.Master()
master.start()
master.run()
def salt_minion():
@ -26,7 +26,7 @@ def salt_minion():
if '' in sys.path:
sys.path.remove('')
minion = salt.Minion()
minion.start()
minion.run()
def salt_syndic():
@ -36,7 +36,7 @@ def salt_syndic():
pid = os.getpid()
try:
syndic = salt.Syndic()
syndic.start()
syndic.run()
except KeyboardInterrupt:
os.kill(pid, 15)