def __init__(self, home): """ Load configuration object """ AutoConf.autoconf(home) self.configuration = AutoConf.get_warden_conf() # Setup logger # this is the stdout log level loglevel = getattr(logging, self.configuration.get('warden', {}).get('loglevel', 'INFO')) log.setLevel(loglevel) self.startuptime = None self.shutdowntime = None self.smtp_forwarder_enabled = str(self.configuration.get('smtp_forwarder', {}).get('enabled', '')).lower() in ('true', 't', '1', 'yes', 'y') log.info('Initialising Warden..') try: # initialise Carbon, daemon services are setup here, but the event reactor is not yet run self.carbon = CarbonManager() # initialise Gentry, this will also perform database manipulation for Sentry self.gentry = GentryManager() # initialise Diamond, not much is required here self.diamond = DiamondManager() if self.smtp_forwarder_enabled: self.smtpforward = SMTPForwarderManager(dict(self.configuration.get('smtp_forwarder', {}))) except Exception: log.exception("An error occured during initialisation.") sys.exit(1)
def _startup(self): """ Start the warden instance Carbon, Diamond and Gentry are started in order, and this method will only exit once all are bound to their correct ports """ log.info('Starting Warden..') try: self.carbon.start() self._wait_for_start(self.carbon) log.debug('1. Carbon Started') self.diamond.start() self._wait_for_start(self.diamond) log.debug('2. Diamond Started') self.gentry.start() self._wait_for_start(self.gentry) log.debug('3. Gentry Started') if self.configuration.getboolean('smtp_forwarder', 'enabled'): self.smtpforward.start() log.debug('4. Graphite SMTP forwarder Started') # blocking log.info('Started Warden.') self.startuptime = self.shutdowntime = datetime.datetime.now() except Exception, e: raise StartupException(e)
def create_service(home): if 'win' in sys.platform: if hasattr(sys, "frozen"): svc_exe = os.path.join(os.path.dirname(sys.executable), 'warden-svc.exe') if os.path.exists(svc_exe): log.info('Attempting to create service') log.info('Output: \n%s', subprocess.check_output([svc_exe, '-h', home, 'install'])) else: pass
def start(self): try: self._startup() while True: time.sleep(5) if not self._is_active(): log.error("Something caused one of the services to stop!") break # need some way to pickup errors at runtime. should check after each sleep whether any of the # services have picked up an error except KeyboardInterrupt: log.info("Keyboard interrupt received.") self._shutdown() except StartupException: log.exception("An error occured during startup.") self._shutdown() except Exception: log.exception("An error occured while running.") self._shutdown()
def __init__(self, home): """ Load configuration object """ AutoConf.autoconf(home) self.configuration = AutoConf.get_warden_conf() # Setup logger # this is the stdout log level loglevel = getattr( logging, self.configuration.get('warden', {}).get('loglevel', 'INFO')) log.setLevel(loglevel) self.startuptime = None self.shutdowntime = None self.smtp_forwarder_enabled = str( self.configuration.get('smtp_forwarder', {}).get('enabled', '')).lower() in ('true', 't', '1', 'yes', 'y') log.info('Initialising Warden..') try: # initialise Carbon, daemon services are setup here, but the event reactor is not yet run self.carbon = CarbonManager() # initialise Gentry, this will also perform database manipulation for Sentry self.gentry = GentryManager() # initialise Diamond, not much is required here self.diamond = DiamondManager() if self.smtp_forwarder_enabled: self.smtpforward = SMTPForwarderManager( dict(self.configuration.get('smtp_forwarder', {}))) except Exception: log.exception("An error occured during initialisation.") sys.exit(1)
def main(): import argparse parser = argparse.ArgumentParser(description='Warden Server') parser.add_argument('home', nargs='?', help="the warden home folder") parser.add_argument( '--pid-file', help= "PID file for Daemon mode. This causes Warden to run in Daemon mode", dest='pid_file') parser.add_argument('--stop', help='Stop Warden running in Daemon mode', action='store_true', default=False) args = parser.parse_args() if args.stop and not args.pid_file: log.error( 'Warden cannot stop daemon mode unless the pid-file is specified') sys.exit(1) if args.stop: pid_file = os.path.abspath(os.path.expanduser(args.pid_file)) if not os.path.exists(pid_file): log.error('Warden cannot find pid-file %s', pid_file) sys.exit(1) pid = int(open(pid_file, 'r').readline()) log.info('Killing pid %d', pid) os.kill(pid, signal.SIGINT) # Check if we've managed for 10 seconds for i in range(10): try: os.kill(pid, 0) log.info('Waiting for %d to die', pid) except OSError: log.info('Stop complete') return time.sleep(1) log.warning("Could not end warden process - killing manually") os.kill(pid, signal.SIGHUP) if os.path.exists(pid_file): os.remove(pid_file) return home = AutoConf.get_home(args.home) if not os.path.exists(home): log.error('The warden home specified ("%s") does not exist!' % home) sys.exit(1) if args.pid_file: pid_file = os.path.abspath(os.path.expanduser(args.pid_file)) import daemon from lockfile import pidlockfile context = daemon.DaemonContext( pidfile=pidlockfile.PIDLockFile(pid_file)) with context: warden_server = WardenServer(home) warden_server.start() return warden_server = WardenServer(home) warden_server.start()
def setup( home, super_user, project_name ): """ Warden uses values from its default settings file UNLESS explicitely defined here in the constructor. """ os.environ['DJANGO_SETTINGS_MODULE'] = 'gentry.settings' log.info ('$DJANGO_SETTINGS_MODULE = %s' % os.environ['DJANGO_SETTINGS_MODULE']) from django.conf import settings as gsetts database = gsetts.DATABASES['default']['NAME'] if not os.path.exists(os.path.dirname(database)): os.makedirs(os.path.dirname(database)) management.execute_from_command_line(['manage.py', 'syncdb','--noinput']) management.execute_from_command_line(['manage.py', 'migrate', '--noinput']) # add a super user if super_user: username = super_user[0] password = super_user[1] email = super_user[2] from sentry.models import User try: auser = User.objects.using('default').get(username=username) except User.DoesNotExist: auser = User.objects.db_manager('default').create_superuser(username, email, password) log.info('Added Sentry superuser "%s" with password like "%s%s"' % (username, password[:3], '*'*(len(password)-3))) else: log.error('Username "%s" is already taken.' % username) if project_name: project_slug = project_name.lower().replace(' ','_') try: # add a project from sentry.models import Project, Team team = Team.objects.create(name=project_name + ' Team', slug=project_slug + '_team', owner=auser) project = Project.objects.create(name=project_name, slug=project_slug, owner=auser, team=team) key = project.key_set.filter(user=auser)[0] dsn = "http://%s:%s@localhost:%s/%s" % (key.public_key, key.secret_key, gsetts.SENTRY_WEB_PORT, key.project_id) log.info('Added "%s" project to Sentry with dsn: %s' % (project_name, dsn)) except Exception: log.error('Failed to create project.')
def main(): import argparse parser = argparse.ArgumentParser(description='Warden Server') parser.add_argument('home', nargs='?', help="the warden home folder") parser.add_argument('--pid-file', help="PID file for Daemon mode. This causes Warden to run in Daemon mode", dest='pid_file') parser.add_argument('--stop', help='Stop Warden running in Daemon mode', action='store_true', default=False) args = parser.parse_args() if args.stop and not args.pid_file: log.error('Warden cannot stop daemon mode unless the pid-file is specified') sys.exit(1) if args.stop: pid_file = os.path.abspath(os.path.expanduser(args.pid_file)) if not os.path.exists(pid_file): log.error('Warden cannot find pid-file %s',pid_file) sys.exit(1) pid = int(open(pid_file, 'r').readline()) log.info('Killing pid %d', pid) os.kill(pid, signal.SIGINT) # Check if we've managed for 10 seconds for i in range(10): try: os.kill(pid, 0) log.info('Waiting for %d to die', pid) except OSError: log.info('Stop complete') return time.sleep(1) log.warning("Could not end warden process - killing manually") os.kill(pid, signal.SIGHUP) if os.path.exists(pid_file): os.remove(pid_file) return home = AutoConf.get_home(args.home) if not os.path.exists(home): log.error('The warden home specified ("%s") does not exist!' % home) sys.exit(1) if args.pid_file: pid_file = os.path.abspath(os.path.expanduser(args.pid_file)) import daemon from lockfile import pidlockfile context = daemon.DaemonContext(pidfile=pidlockfile.PIDLockFile(pid_file)) with context: warden_server = WardenServer(home) warden_server.start() return warden_server = WardenServer(home) warden_server.start()
def _shutdown(self): """ Shutdown in order, some threading may be wrong here, make sure of inidividual .join() """ self.shutdowntime = datetime.datetime.now() elapsed = self.shutdowntime - self.startuptime log.info('Warden was active for %s' % str(elapsed)) log.info('Shutting down Warden..') if self.configuration.getboolean('smtp_forwarder', 'enabled'): try: self.smtpforward.stop() log.debug('4. Graphite SMTP forwarder stopped') except Exception: log.exception( 'An error occured while shutting down Graphite SMTP forwarder' ) try: self.gentry.stop() log.debug('3. Gentry Stopped.') except Exception: log.exception("An error occured while shutting down Gentry") try: self.diamond.stop() log.debug('2. Diamond Stopped.') except Exception: log.exception("An error occured while shutting down Diamond") try: self.carbon.stop() log.debug('1. Carbon Stopped.') except Exception: log.exception("An error occured while shutting down Carbon") log.info('Shut down Warden.')
def _shutdown(self): """ Shutdown in order, some threading may be wrong here, make sure of inidividual .join() """ self.shutdowntime = datetime.datetime.now() elapsed = self.shutdowntime - self.startuptime log.info('Warden was active for %s' % str(elapsed)) log.info('Shutting down Warden..') if self.configuration.getboolean('smtp_forwarder', 'enabled'): try: self.smtpforward.stop() log.debug('4. Graphite SMTP forwarder stopped') except Exception: log.exception('An error occured while shutting down Graphite SMTP forwarder') try: self.gentry.stop() log.debug('3. Gentry Stopped.') except Exception: log.exception("An error occured while shutting down Gentry") try: self.diamond.stop() log.debug('2. Diamond Stopped.') except Exception: log.exception("An error occured while shutting down Diamond") try: self.carbon.stop() log.debug('1. Carbon Stopped.') except Exception: log.exception("An error occured while shutting down Carbon") log.info('Shut down Warden.')
def __init__( self, warden_configuration_file, new_graphite_root=None, # does the graphite root variable need to be changed carbon_config_path=None, # where are the carbon config files diamond_config_path=None, # where is the diamond config file gentry_settings_path=None, # the name of the gentry settings module start_stmp_forwarder=True, smtp_forwarder_config_path=None, ): """ Load configuration object """ # Otherwise there may be a config argument if warden_configuration_file is None: log.critical( 'No Warden configuration file supplied! Please use the "warden_configuration_file" parameter.' ) sys.exit() warden_configuration_file = os.path.abspath( os.path.expanduser(warden_configuration_file)) try: with open(warden_configuration_file) as f: pass except IOError: log.error( 'The warden config file specified ("%s") does not exist!' % warden_configuration_file) raise self.configuration = ConfigParser.RawConfigParser() self.configuration.read(warden_configuration_file) # Setup logger # this is the stdout log level loglevel = getattr(logging, self.configuration.get('warden', 'loglevel')) log.setLevel(loglevel) self.startuptime = None self.shutdowntime = None # pull new config values into configuration object if new_graphite_root is not None: self.configuration.set('carbon', 'graphite_root', str(new_graphite_root)) if carbon_config_path is not None: self.configuration.set('carbon', 'configuration', str(carbon_config_path)) if diamond_config_path is not None: self.configuration.set('diamond', 'configuration', str(diamond_config_path)) if gentry_settings_path is not None: self.configuration.set('gentry', 'gentry_settings_py_path', str(gentry_settings_path)) if start_stmp_forwarder is False: self.configuration.set('smtp_forwarder', 'enabled', str(start_stmp_forwarder)) if smtp_forwarder_config_path is not None: self.configuration.set('smtp_forwarder', 'configuration', str(smtp_forwarder_config_path)) log.info('Initialising Warden..') try: # initialise Carbon, daemon services are setup here, but the event reactor is not yet run self.carbon = CarbonManager( self.configuration.get('carbon', 'graphite_root'), self.configuration.get('carbon', 'configuration')) # initialise Gentry, this will also perform database manipulation for Sentry self.gentry = GentryManager( self.configuration.get('gentry', 'gentry_settings_py_path')) # initialise Diamond, not much is required here self.diamond = DiamondManager( self.configuration.get('diamond', 'diamond_root'), self.configuration.get('diamond', 'configuration'), getattr(logging, self.configuration.get('diamond', 'loglevel'))) if self.configuration.getboolean('smtp_forwarder', 'enabled'): self.smtpforward = SMTPForwarderManager( dict(self.configuration.items('smtp_forwarder'))) except Exception: log.exception("An error occured during initialisation.") sys.exit(1)
def __init__(self, warden_configuration_file, new_graphite_root=None, # does the graphite root variable need to be changed carbon_config_path=None, # where are the carbon config files diamond_config_path=None, # where is the diamond config file gentry_settings_path=None, # the name of the gentry settings module start_stmp_forwarder=True, smtp_forwarder_config_path=None, ): """ Load configuration object """ # Otherwise there may be a config argument if warden_configuration_file is None: log.critical('No Warden configuration file supplied! Please use the "warden_configuration_file" parameter.') sys.exit() warden_configuration_file = os.path.abspath(os.path.expanduser(warden_configuration_file)) try: with open(warden_configuration_file) as f: pass except IOError: log.error('The warden config file specified ("%s") does not exist!' % warden_configuration_file) raise self.configuration = ConfigParser.RawConfigParser() self.configuration.read(warden_configuration_file) # Setup logger # this is the stdout log level loglevel = getattr(logging, self.configuration.get('warden','loglevel')) log.setLevel(loglevel) self.startuptime = None self.shutdowntime = None # pull new config values into configuration object if new_graphite_root is not None: self.configuration.set('carbon', 'graphite_root', str(new_graphite_root)) if carbon_config_path is not None: self.configuration.set('carbon', 'configuration', str(carbon_config_path)) if diamond_config_path is not None: self.configuration.set('diamond', 'configuration', str(diamond_config_path)) if gentry_settings_path is not None: self.configuration.set('gentry', 'gentry_settings_py_path', str(gentry_settings_path)) if start_stmp_forwarder is False: self.configuration.set('smtp_forwarder', 'enabled', str(start_stmp_forwarder)) if smtp_forwarder_config_path is not None: self.configuration.set('smtp_forwarder', 'configuration', str(smtp_forwarder_config_path)) log.info('Initialising Warden..') try: # initialise Carbon, daemon services are setup here, but the event reactor is not yet run self.carbon = CarbonManager( self.configuration.get('carbon', 'graphite_root'), self.configuration.get('carbon', 'configuration')) # initialise Gentry, this will also perform database manipulation for Sentry self.gentry = GentryManager( self.configuration.get('gentry', 'gentry_settings_py_path')) # initialise Diamond, not much is required here self.diamond = DiamondManager( self.configuration.get('diamond', 'diamond_root'), self.configuration.get('diamond', 'configuration'), getattr(logging, self.configuration.get('diamond','loglevel'))) if self.configuration.getboolean('smtp_forwarder', 'enabled'): self.smtpforward = SMTPForwarderManager(dict(self.configuration.items('smtp_forwarder'))) except Exception: log.exception("An error occured during initialisation.") sys.exit(1)
def setup( carbon_conf, diamond_conf, gentry_settings, super_user, project_name ): """ Warden uses values from its default settings file UNLESS explicitely defined here in the constructor. """ # GENTRY sentry_key = base64.b64encode(os.urandom(40)) # write key into settings file try: new_lines = [] with open(gentry_settings) as f: old_lines = f.readlines() for line in old_lines: if line.startswith('SENTRY_KEY'): nline = 'SENTRY_KEY=\'' + str(sentry_key) + '\'\n' log.info( 'Rewriting "%s" -> "%s"' % (line.strip(), nline.strip())) else: nline = line new_lines.append(nline) if len(new_lines) > 0: log.info('Writing new Sentry_key into settings module "%s"' % gentry_settings) with open(gentry_settings, 'wb') as f: f.writelines(new_lines) f.flush() f.close() except IOError: log.exception('Could not write gentry_settings module: "%s"' % gentry_settings) if gentry_settings is None: os.environ['DJANGO_SETTINGS_MODULE'] = 'gentry.settings' else: n = 'j5_warden_gentry_settings' os.environ['DJANGO_SETTINGS_MODULE'] = n if not sys.modules.has_key(n): imp.load_source(n, os.path.abspath(os.path.expanduser(gentry_settings))) log.info ('$DJANGO_SETTINGS_MODULE = %s' % os.environ['DJANGO_SETTINGS_MODULE']) from django.conf import settings as gsetts database = gsetts.DATABASES['default']['NAME'] if file_exists(database): os.remove(database) management.execute_from_command_line(['manage.py', 'syncdb','--noinput']) management.execute_from_command_line(['manage.py', 'migrate', '--noinput']) # add a super user if super_user is not None: username = super_user[0] password = super_user[1] email = super_user[2] else: username, password, email = '', '', '' log.info('Creating new Superuser for Sentry:') while True: username = raw_input('Enter username: '******' ' in username: continue password = raw_input('Enter password: '******' ' in password: continue email = raw_input('Enter email: ').strip() if len(email) == 0 or ' ' in email or '@' not in email: continue break from sentry.models import User try: auser = User.objects.using('default').get(username=username) except User.DoesNotExist: auser = User.objects.db_manager('default').create_superuser(username, email, password) log.info('Added Sentry superuser "%s" with password like "%s%s"' % (username, password[:3], '*'*(len(password)-3))) else: log.error('Username "%s" is already taken.' % username) if project_name is None: yesno = raw_input('Would you like to create a new project for Sentry? (yes/no): ' ) if yesno == 'yes' or yesno == 'y': while True: project_name = raw_input('Enter Project Name: ').strip() if len(project_name) == 0: continue break if project_name is not None: project_slug = project_name.lower().replace(' ','_') try: # add a project from sentry.models import Project, Team team = Team.objects.create(name=project_name + ' Team', slug=project_slug + '_team', owner=auser) project = Project.objects.create(name=project_name, slug=project_slug, owner=auser, team=team) key = project.key_set.filter(user=auser)[0] dsn = "http://%s:%s@localhost:%s/%s" % (key.public_key, key.secret_key, gsetts.SENTRY_WEB_PORT, key.project_id) log.info('Added "%s" project to Sentry with dsn: %s' % (project_name, dsn)) except Exception: log.error('Failed to create project.')
def setup(carbon_conf, diamond_conf, gentry_settings, super_user, project_name): """ Warden uses values from its default settings file UNLESS explicitely defined here in the constructor. """ # GENTRY sentry_key = base64.b64encode(os.urandom(40)) # write key into settings file try: new_lines = [] with open(gentry_settings) as f: old_lines = f.readlines() for line in old_lines: if line.startswith('SENTRY_KEY'): nline = 'SENTRY_KEY=\'' + str(sentry_key) + '\'\n' log.info('Rewriting "%s" -> "%s"' % (line.strip(), nline.strip())) else: nline = line new_lines.append(nline) if len(new_lines) > 0: log.info('Writing new Sentry_key into settings module "%s"' % gentry_settings) with open(gentry_settings, 'wb') as f: f.writelines(new_lines) f.flush() f.close() except IOError: log.exception('Could not write gentry_settings module: "%s"' % gentry_settings) if gentry_settings is None: os.environ['DJANGO_SETTINGS_MODULE'] = 'gentry.settings' else: n = 'j5_warden_gentry_settings' os.environ['DJANGO_SETTINGS_MODULE'] = n if not sys.modules.has_key(n): imp.load_source( n, os.path.abspath(os.path.expanduser(gentry_settings))) log.info('$DJANGO_SETTINGS_MODULE = %s' % os.environ['DJANGO_SETTINGS_MODULE']) from django.conf import settings as gsetts database = gsetts.DATABASES['default']['NAME'] if file_exists(database): os.remove(database) management.execute_from_command_line(['manage.py', 'syncdb', '--noinput']) management.execute_from_command_line(['manage.py', 'migrate', '--noinput']) # add a super user if super_user is not None: username = super_user[0] password = super_user[1] email = super_user[2] else: username, password, email = '', '', '' log.info('Creating new Superuser for Sentry:') while True: username = raw_input('Enter username: '******' ' in username: continue password = raw_input('Enter password: '******' ' in password: continue email = raw_input('Enter email: ').strip() if len(email) == 0 or ' ' in email or '@' not in email: continue break from sentry.models import User try: auser = User.objects.using('default').get(username=username) except User.DoesNotExist: auser = User.objects.db_manager('default').create_superuser( username, email, password) log.info('Added Sentry superuser "%s" with password like "%s%s"' % (username, password[:3], '*' * (len(password) - 3))) else: log.error('Username "%s" is already taken.' % username) if project_name is None: yesno = raw_input( 'Would you like to create a new project for Sentry? (yes/no): ') if yesno == 'yes' or yesno == 'y': while True: project_name = raw_input('Enter Project Name: ').strip() if len(project_name) == 0: continue break if project_name is not None: project_slug = project_name.lower().replace(' ', '_') try: # add a project from sentry.models import Project, Team team = Team.objects.create(name=project_name + ' Team', slug=project_slug + '_team', owner=auser) project = Project.objects.create(name=project_name, slug=project_slug, owner=auser, team=team) key = project.key_set.filter(user=auser)[0] dsn = "http://%s:%s@localhost:%s/%s" % ( key.public_key, key.secret_key, gsetts.SENTRY_WEB_PORT, key.project_id) log.info('Added "%s" project to Sentry with dsn: %s' % (project_name, dsn)) except Exception: log.error('Failed to create project.')