Exemple #1
0
    def apply_config(self, configs, skip_env_dependent=False, reconfigure=False):
        """Apply a configuration. If skip_env_dependent is True we're
        loading this locally to test the config as part of tronfig. We want to
        skip applying some settings because the local machine we're using to
        edit the config may not have the same environment as the live
        trond machine.
        """
        master_config = configs[MASTER_NAMESPACE]
        self.output_stream_dir = master_config.output_stream_dir or self.working_dir
        if not skip_env_dependent:
            ssh_options = self._ssh_options_from_config(configs[MASTER_NAMESPACE].ssh_options)
            state_persistence = configs[MASTER_NAMESPACE].state_persistence
        else:
            ssh_options = config_parse.valid_ssh_options({})
            state_persistence = config_parse.DEFAULT_STATE_PERSISTENCE

        self.state_manager = PersistenceManagerFactory.from_config(
                    state_persistence)
        self.context.base = configs[MASTER_NAMESPACE].command_context
        self.time_zone = configs[MASTER_NAMESPACE].time_zone
        self._apply_nodes(configs[MASTER_NAMESPACE].nodes, ssh_options)
        self._apply_node_pools(configs[MASTER_NAMESPACE].node_pools)
        self._apply_notification_options(configs[MASTER_NAMESPACE].notification_options)

        jobs, services = collate_jobs_and_services(configs)
        self._apply_jobs(jobs, reconfigure=reconfigure)
        self._apply_services(services)
Exemple #2
0
def get_state_manager_from_config(config_path, working_dir):
    """Return a state manager from the configuration.
    """
    config_manager = manager.ConfigManager(config_path)
    config_container = config_manager.load()
    state_config = config_container.get_master().state_persistence
    with tool_utils.working_dir(working_dir):
        return PersistenceManagerFactory.from_config(state_config)
Exemple #3
0
 def test_from_config_shelve(self):
     thefilename = 'thefilename'
     config = Turtle(store_type='shelve', name=thefilename, buffer_size=0)
     manager = PersistenceManagerFactory.from_config(config)
     store = manager._impl
     assert_equal(store.filename, config.name)
     assert isinstance(store, ShelveStateStore)
     os.unlink(thefilename)
Exemple #4
0
def get_state_manager_from_config(config_path, working_dir):
    """Return a state manager from the configuration.
    """
    config_manager = manager.ConfigManager(config_path)
    config_container = config_manager.load()
    state_config = config_container.get_master().state_persistence
    with tool_utils.working_dir(working_dir):
        return PersistenceManagerFactory.from_config(state_config)
Exemple #5
0
def get_state_manager_from_config(config_filename):
    """Return a state manager that is configured in the file at
    config_filename.
    """
    with open(config_filename) as fh:
        config = config_parse.load_config(fh)
    state_config = config.state_persistence

    return PersistenceManagerFactory.from_config(state_config)
Exemple #6
0
 def test_from_config_shelve(self):
     thefilename = 'thefilename'
     config = schema.ConfigState(store_type='shelve',
                                 name=thefilename,
                                 buffer_size=0,
                                 connection_details=None)
     manager = PersistenceManagerFactory.from_config(config)
     store = manager._impl
     assert_equal(store.filename, config.name)
     assert isinstance(store, ShelveStateStore)
     os.unlink(thefilename)
Exemple #7
0
def main():
    # Fetch configs. You can find the arguments in puppet.
    args = parse_cli()
    persistence_config = read_config(args)
    store_type = schema.StatePersistenceTypes(persistence_config.store_type)
    job_name = args.job_name

    # Alert for DynamoDB
    if store_type == schema.StatePersistenceTypes.dynamodb:
        # Fetch job state from dynamodb
        state_manager = PersistenceManagerFactory.from_config(
            persistence_config)
        try:
            job = state_manager.restore(
                job_names=[job_name])['job_state'][job_name]
        except Exception as e:
            logging.exception(
                f'UNKN: Failed to retreive status for job {job_name} due to {e}'
            )
            sys.exit(3)

        # Exit if the job never runs.
        last_run_time = get_last_run_time(job)
        if not last_run_time:
            logging.error(
                f'WARN: No last run for {job_name} found. If the job was just added, it might take some time for it to run'
            )
            sys.exit(1)

        # Alert if timestamp is not updated after staleness_threshold
        stateless_for_secs = time.time() - last_run_time.astimezone(
            pytz.utc).timestamp()
        if stateless_for_secs > args.staleness_threshold:
            logging.error(
                f'CRIT: {job_name} has not been updated in DynamoDB for {stateless_for_secs} seconds'
            )
            sys.exit(2)
        else:
            logging.info(
                f"OK: DynamoDB is up to date. It's last updated at {last_run_time}"
            )
            sys.exit(0)
    # Alert for BerkeleyDB
    elif store_type == schema.StatePersistenceTypes.shelve:
        os.execl('/usr/lib/nagios/plugins/check_file_age',
                 '/nail/tron/tron_state', '-w', str(args.staleness_threshold),
                 '-c', str(args.staleness_threshold))
    else:
        logging.exception(
            f'UNKN: Not designed to check this type of datastore: {store_type}'
        )
        sys.exit(3)
Exemple #8
0
 def test_from_config_shelve(self):
     tmpdir = tempfile.mkdtemp()
     try:
         fname = os.path.join(tmpdir, 'state')
         config = schema.ConfigState(
             store_type='shelve',
             name=fname,
             buffer_size=0,
         )
         manager = PersistenceManagerFactory.from_config(config)
         store = manager._impl
         assert_equal(store.filename, config.name)
         assert isinstance(store, ShelveStateStore)
     finally:
         shutil.rmtree(tmpdir)
Exemple #9
0
 def test_from_config_shelve(self):
     tmpdir = tempfile.mkdtemp()
     try:
         fname = os.path.join(tmpdir, 'state')
         config = schema.ConfigState(
             store_type='shelve',
             name=fname,
             buffer_size=0,
             connection_details=None,
         )
         manager = PersistenceManagerFactory.from_config(config)
         store = manager._impl
         assert_equal(store.filename, config.name)
         assert isinstance(store, ShelveStateStore)
     finally:
         shutil.rmtree(tmpdir)