def test_when_ping_interval_is_set_the_callback_should_be_added_to_periodic_callbacks( self, ): with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch( "salt.minion.Minion.sync_connect_master", MagicMock(side_effect=RuntimeError("stop execution")), ), patch( "salt.utils.process.SignalHandlingProcess.start", MagicMock(return_value=True), ), patch( "salt.utils.process.SignalHandlingProcess.join", MagicMock(return_value=True), ): mock_opts = self.get_config("minion", from_scratch=True) mock_opts["ping_interval"] = 10 io_loop = salt.ext.tornado.ioloop.IOLoop() io_loop.make_current() minion = salt.minion.Minion(mock_opts, io_loop=io_loop) try: try: minion.connected = MagicMock(side_effect=(False, True)) minion._fire_master_minion_start = MagicMock() minion.tune_in(start=False) except RuntimeError: pass # Make sure the scheduler is initialized but the beacons are not self.assertTrue("ping" in minion.periodic_callbacks) finally: minion.destroy()
def start(self): ''' Execute this method to start up a minion. ''' verify_env([self.opts['pki_dir'], self.opts['cachedir'], self.opts['extension_modules'], 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 log = logging.getLogger(__name__) if check_user(self.opts['user'], log): try: if self.cli['daemon']: # Late import so logging works correctly import salt.utils salt.utils.daemonize() set_pidfile(self.cli['pidfile']) minion = salt.minion.Minion(self.opts) minion.tune_in() except KeyboardInterrupt: log.warn('Stopping the Salt Minion') raise SystemExit('\nExiting on Ctrl-c')
def test_scheduler_before_connect(self): """ Tests that the 'scheduler_before_connect' option causes the scheduler to be initialized before connect. """ with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch( "salt.minion.Minion.sync_connect_master", MagicMock(side_effect=RuntimeError("stop execution")), ), patch( "salt.utils.process.SignalHandlingProcess.start", MagicMock(return_value=True), ), patch( "salt.utils.process.SignalHandlingProcess.join", MagicMock(return_value=True), ): mock_opts = self.get_config("minion", from_scratch=True) mock_opts["scheduler_before_connect"] = True io_loop = salt.ext.tornado.ioloop.IOLoop() io_loop.make_current() minion = salt.minion.Minion(mock_opts, io_loop=io_loop) try: try: minion.tune_in(start=True) except RuntimeError: pass # Make sure the scheduler is initialized but the beacons are not self.assertTrue("schedule" in minion.periodic_callbacks) self.assertTrue("beacons" not in minion.periodic_callbacks) finally: minion.destroy()
def test_beacons_before_connect(self): ''' Tests that the 'beacons_before_connect' option causes the beacons to be initialized before connect. ''' with patch('salt.minion.Minion.ctx', MagicMock(return_value={})), \ patch('salt.minion.Minion.sync_connect_master', MagicMock(side_effect=RuntimeError('stop execution'))), \ patch('salt.utils.process.SignalHandlingProcess.start', MagicMock(return_value=True)), \ patch('salt.utils.process.SignalHandlingProcess.join', MagicMock(return_value=True)): mock_opts = self.get_config('minion', from_scratch=True) mock_opts['beacons_before_connect'] = True io_loop = tornado.ioloop.IOLoop() io_loop.make_current() minion = salt.minion.Minion(mock_opts, io_loop=io_loop) try: try: minion.tune_in(start=True) except RuntimeError: pass # Make sure beacons are initialized but the sheduler is not self.assertTrue('beacons' in minion.periodic_callbacks) self.assertTrue('schedule' not in minion.periodic_callbacks) finally: minion.destroy()
def test_beacons_before_connect(): """ Tests that the 'beacons_before_connect' option causes the beacons to be initialized before connect. """ with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch( "salt.minion.Minion.sync_connect_master", MagicMock(side_effect=RuntimeError("stop execution")), ), patch( "salt.utils.process.SignalHandlingProcess.start", MagicMock(return_value=True), ), patch( "salt.utils.process.SignalHandlingProcess.join", MagicMock(return_value=True), ): mock_opts = salt.config.DEFAULT_MINION_OPTS.copy() mock_opts["beacons_before_connect"] = True io_loop = salt.ext.tornado.ioloop.IOLoop() io_loop.make_current() minion = salt.minion.Minion(mock_opts, io_loop=io_loop) try: try: minion.tune_in(start=True) except RuntimeError: pass # Make sure beacons are initialized but the sheduler is not assert "beacons" in minion.periodic_callbacks assert "schedule" not in minion.periodic_callbacks finally: minion.destroy()
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()
def start(self): ''' Execute this method to start up a minion. ''' verify_env([self.opts['pki_dir'], self.opts['cachedir'], self.opts['extension_modules'], 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 log = logging.getLogger(__name__) if self.cli['daemon']: # Late import so logging works correctly import salt.utils # If the minion key has not been accepted, then Salt enters a loop # waiting for it, if we daemonize later then the minion cound halt # the boot process waiting for a key to be accepted on the master. # This is the latest safe place to daemonize salt.utils.daemonize() minion = salt.minion.Minion(self.opts) set_pidfile(self.cli['pidfile']) if check_user(self.opts['user'], log): try: minion.tune_in() except KeyboardInterrupt: log.warn('Stopping the Salt Minion') raise SystemExit('\nExiting on Ctrl-c')
def start(self): ''' Execute this method to start up a minion. ''' verify_env([ self.opts['pki_dir'], self.opts['cachedir'], self.opts['extension_modules'], 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 log = logging.getLogger(__name__) if self.cli['daemon']: # Late import so logging works correctly import salt.utils # If the minion key has not been accepted, then Salt enters a loop # waiting for it, if we daemonize later then the minion cound halt # the boot process waiting for a key to be accepted on the master. # This is the latest safe place to daemonize salt.utils.daemonize() minion = salt.minion.Minion(self.opts) set_pidfile(self.cli['pidfile']) if check_user(self.opts['user'], log): try: minion.tune_in() except KeyboardInterrupt: log.warn('Stopping the Salt Minion') raise SystemExit('\nExiting on Ctrl-c')
def start(self): ''' Execute this method to start up a minion. ''' verify_env([ self.opts['pki_dir'], self.opts['cachedir'], self.opts['extension_modules'], 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 log = logging.getLogger(__name__) try: 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() except KeyboardInterrupt: log.warn('Stopping the Salt Minion') raise SystemExit('\nExiting on Ctrl-c')
def start(self): ''' Execute this method to start up a minion. ''' verify_env([self.opts['pki_dir'], self.opts['cachedir']]) if self.cli['daemon']: salt.utils.daemonize() minion = salt.minion.Minion(self.opts) minion.tune_in()
def start(self): ''' Execute this method to start up a minion. ''' 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'], os.path.dirname(self.config['log_file']), ], self.config['user'], permissive=self.config['permissive_pki_access'], pki_dir=self.config['pki_dir'], ) except OSError as err: sys.exit(err.errno) self.setup_logfile_logger() log = logging.getLogger(__name__) log.warn( 'Setting up the Salt Minion "{0}"'.format( self.config['id']) ) migrations.migrate_paths(self.config) # Late import so logging works correctly import salt.minion # If the minion key has not been accepted, then Salt enters a loop # waiting for it, if we daemonize later then the minion could halt # the boot process waiting for a key to be accepted on the master. # This is the latest safe place to daemonize self.daemonize_if_required() try: minion = salt.minion.Minion(self.config) self.set_pidfile() if check_user(self.config['user']): minion.tune_in() except KeyboardInterrupt: log.warn('Stopping the Salt Minion') raise SystemExit('\nExiting on Ctrl-c')
def start(self): ''' Execute this method to start up a minion. ''' 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'], os.path.dirname(self.config['log_file']), ], self.config['user'], permissive=self.config['permissive_pki_access'], pki_dir=self.config['pki_dir'], ) except OSError as err: sys.exit(err.errno) self.setup_logfile_logger() log = logging.getLogger(__name__) log.warn('Setting up the Salt Minion "{0}"'.format(self.config['id'])) migrations.migrate_paths(self.config) # Late import so logging works correctly import salt.minion # If the minion key has not been accepted, then Salt enters a loop # waiting for it, if we daemonize later then the minion could halt # the boot process waiting for a key to be accepted on the master. # This is the latest safe place to daemonize self.daemonize_if_required() try: minion = salt.minion.Minion(self.config) self.set_pidfile() if check_user(self.config['user']): minion.tune_in() except KeyboardInterrupt: log.warn('Stopping the Salt Minion') raise SystemExit('\nExiting on Ctrl-c')
def test_scheduler_before_connect(self): ''' Tests that the 'scheduler_before_connect' option causes the scheduler to be initialized before connect. ''' with patch('salt.minion.Minion.ctx', MagicMock(return_value={})), \ patch('salt.minion.Minion.sync_connect_master', MagicMock(side_effect=RuntimeError('stop execution'))), \ patch('salt.utils.process.SignalHandlingMultiprocessingProcess.start', MagicMock(return_value=True)), \ patch('salt.utils.process.SignalHandlingMultiprocessingProcess.join', MagicMock(return_value=True)): mock_opts = copy.copy(salt.config.DEFAULT_MINION_OPTS) mock_opts['scheduler_before_connect'] = True minion = salt.minion.Minion(mock_opts, io_loop=tornado.ioloop.IOLoop()) try: try: minion.tune_in(start=True) except RuntimeError: pass # Make sure the scheduler is initialized but the beacons are not self.assertTrue('schedule' in minion.periodic_callbacks) self.assertTrue('beacons' not in minion.periodic_callbacks) finally: minion.destroy()
def start(self): ''' Execute this method to start up a minion. ''' 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 verify_env([self.opts['pki_dir'], self.opts['cachedir']]) if self.cli['daemon']: # Late import so logging works correctly import salt.utils salt.utils.daemonize() # Late import so logging works correctly import salt.minion minion = salt.minion.Minion(self.opts) minion.tune_in()
class Minion(object): ''' Create a minion server ''' def __init__(self): self.cli = self.__parse_cli() # command line overrides config if self.cli['user']: self.opts['user'] = self.cli['user'] 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('-u', '--user', dest='user', help='Specify user to run minion') parser.add_option('--pid-file', dest='pidfile', default='/var/run/salt-minion.pid', help=('Specify the location of the pidfile. Default' ' %default')) parser.add_option( '-l', '--log-level', dest='log_level', choices=list(salt.log.LOG_LEVELS), help='Console log level. One of %s. For the logfile settings ' 'see the config file. Default: \'warning\'.' % ', '.join([repr(l) for l in salt.log.SORTED_LEVEL_NAMES])) options, args = parser.parse_args() self.opts = salt.config.minion_config(options.config) if not options.log_level: options.log_level = self.opts['log_level'] salt.log.setup_console_logger(options.log_level, log_format=self.opts['log_fmt_console'], date_format=self.opts['log_datefmt']) cli = { 'daemon': options.daemon, 'config': options.config, 'user': options.user, 'pidfile': options.pidfile } return cli def start(self): ''' Execute this method to start up a minion. ''' try: verify_env([ self.opts['pki_dir'], self.opts['cachedir'], self.opts['sock_dir'], self.opts['extension_modules'], os.path.dirname(self.opts['log_file']), ], self.opts['user'], permissive=self.opts['permissive_pki_access']) except OSError, err: sys.exit(err.errno) import salt.log salt.log.setup_logfile_logger(self.opts['log_file'], self.opts['log_level_logfile'] or self.opts['log_level'], log_format=self.opts['log_fmt_logfile'], date_format=self.opts['log_datefmt']) for name, level in self.opts['log_granular_levels'].items(): salt.log.set_logger_level(name, level) import logging # Late import so logging works correctly import salt.minion log = logging.getLogger(__name__) if self.cli['daemon']: # Late import so logging works correctly import salt.utils # If the minion key has not been accepted, then Salt enters a loop # waiting for it, if we daemonize later then the minion could halt # the boot process waiting for a key to be accepted on the master. # This is the latest safe place to daemonize salt.utils.daemonize() try: minion = salt.minion.Minion(self.opts) set_pidfile(self.cli['pidfile']) if check_user(self.opts['user'], log): minion.tune_in() except KeyboardInterrupt: log.warn('Stopping the Salt Minion') raise SystemExit('\nExiting on Ctrl-c')