Exemplo n.º 1
0
def lambda_handler(event, context):

    if not SNAPSHOT_INDEX_FILTERS and not DELETE_INDEX_FILTERS:
        raise ValueError(
            'No value for delete_index_filters or snapshot_index_filters found - aborting'
        )

    if SNAPSHOT_INDEX_FILTERS:
        if not SNAPSHOT_BUCKET or not SNAPSHOT_BUCKET_REGION or not SNAPSHOT_NAME:
            raise ValueError(
                'Some required snapshot parameters have no values - aborting')

    es = elasticsearch_client()

    result = {}

    if SNAPSHOT_INDEX_FILTERS:
        snapshot_name = datetime.now().strftime(SNAPSHOT_NAME)
        indices = filter_indices(es, SNAPSHOT_INDEX_FILTERS)

        if TEST_MODE:
            result['snapshots'] = indices.working_list()
            result['test_mode'] = True
        else:
            if indices.working_list():

                if not curator.repository_exists(es,
                                                 repository=SNAPSHOT_BUCKET):
                    print('Registering snapshot repository in s3://{}'.format(
                        SNAPSHOT_BUCKET))
                    response = curator.create_repository(
                        client=es,
                        repository=SNAPSHOT_BUCKET,
                        repo_type='s3',
                        bucket=SNAPSHOT_BUCKET,
                        region=SNAPSHOT_BUCKET_REGION)
                    print('Response: ' + str(response))

                print('Creating a snapshot of indices')
                snapshot_indices = curator.Snapshot(indices,
                                                    repository=SNAPSHOT_BUCKET,
                                                    name=snapshot_name)
                snapshot_indices.do_action()
                result['snapshot'] = indices.working_list()

    if DELETE_INDEX_FILTERS:
        indices = filter_indices(es, DELETE_INDEX_FILTERS)

        if TEST_MODE:
            result['deleted'] = indices.working_list()
            result['test_mode'] = True
        else:
            if indices.working_list():
                print('Deleting indices')
                delete_indices = curator.DeleteIndices(indices)
                delete_indices.do_action()
                result['deleted'] = indices.working_list()

    return result
Exemplo n.º 2
0
 def test_repo_not_in_results(self):
     client = Mock()
     client.snapshot.get_repository.return_value = {
         'not_your_repo': {
             'foo': 'bar'
         }
     }
     self.assertFalse(curator.repository_exists(client, repository="repo"))
Exemplo n.º 3
0
 def test_delete_repository_success(self):
     self.create_repository()
     self.write_config(
         self.args['configfile'],
         testvars.client_conf_logfile.format(host, port, os.devnull))
     test = clicktest.CliRunner()
     result = test.invoke(
         curator.repo_mgr_cli,
         [
             '--config',
             self.args['configfile'],
             'delete',
             '--yes',  # This ensures no prompting will happen
             '--repository',
             self.args['repository']
         ])
     self.assertFalse(
         curator.repository_exists(self.client, self.args['repository']))
Exemplo n.º 4
0
 def test_delete_repository_success(self):
     self.create_repository()
     self.write_config(
         self.args['configfile'],
         testvars.client_conf_logfile.format(host, port, os.devnull)
     )
     test = clicktest.CliRunner()
     _ = test.invoke(
                 curator.repo_mgr_cli,
                 [
                     '--config', self.args['configfile'],
                     'delete',
                     '--yes', # This ensures no prompting will happen
                     '--repository', self.args['repository']
                 ]
     )
     self.assertFalse(
         curator.repository_exists(self.client, self.args['repository'])
     )
Exemplo n.º 5
0
 def test_repo_not_in_results(self):
     client = Mock()
     client.snapshot.get_repository.return_value = {'not_your_repo':{'foo':'bar'}}
     self.assertFalse(curator.repository_exists(client, repository="repo"))