Skip to content

Commit

Permalink
Merge pull request #816 from untergeek/feature/812
Browse files Browse the repository at this point in the history
Add missing "ignore_empty_list" option to actions
  • Loading branch information
untergeek committed Nov 21, 2016
2 parents 34c7efe + 9478427 commit d979fb5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
96 changes: 85 additions & 11 deletions curator/singletons.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def filter_schema_check(action, filter_dict):
return validate_filters(action, valid_filters)

def _actionator(action, action_obj, dry_run=True):
logger = logging.getLogger(__name__)
logger.debug('Doing the singleton "{0}" action here.'.format(action))
try:
if dry_run:
Expand All @@ -80,6 +81,28 @@ def _actionator(action, action_obj, dry_run=True):
sys.exit(1)
logger.info('Singleton "{0}" action completed.'.format(action))

def _check_empty_list(list_object, ignore=False):
logger = logging.getLogger(__name__)
logger.debug('Testing for empty list object')
try:
list_object.empty_list_check()
except (NoIndices, NoSnapshots) as e:
if str(type(e)) == "<class 'curator.exceptions.NoIndices'>":
otype = 'index'
else:
otype = 'snapshot'
if ignore:
logger.info(
'Singleton action not performed: empty {0} list'.format(otype)
)
sys.exit(0)
else:
logger.error(
'Singleton action failed due to empty {0} list'.format(otype)
)
sys.exit(1)


def _prune_excluded(option_dict):
for k in list(option_dict.keys()):
if k in EXCLUDED_OPTIONS:
Expand Down Expand Up @@ -133,13 +156,18 @@ def config_override(ctx, config_dict):
@click.option(
'--wait_for_completion', is_flag=True, help='Wait for operation to complete'
)
@click.option(
'--ignore_empty_list', is_flag=True,
help='Do not raise exception if there are no actionable indices'
)
@click.option(
'--filter_list', callback=validate_filter_json,
help='JSON string representing an array of filters.', required=True
)
@click.pass_context
def allocation_singleton(
ctx, key, value, allocation_type, wait_for_completion, filter_list):
ctx, key, value, allocation_type, wait_for_completion, ignore_empty_list,
filter_list):
"""
Shard Routing Allocation
"""
Expand All @@ -165,6 +193,7 @@ def allocation_singleton(
}
ilo = IndexList(client)
ilo.iterate_filters(clean_filters)
_check_empty_list(ilo, ignore_empty_list)
action_obj = action_class(ilo, **mykwargs)
### Do the action
_actionator(action, action_obj, dry_run=ctx.parent.params['dry_run'])
Expand All @@ -175,13 +204,17 @@ def allocation_singleton(
'--delete_aliases', is_flag=True,
help='Delete all aliases from indices to be closed'
)
@click.option(
'--ignore_empty_list', is_flag=True,
help='Do not raise exception if there are no actionable indices'
)
@click.option(
'--filter_list', callback=validate_filter_json,
help='JSON string representing an array of filters.', required=True
)
@click.pass_context
def close_singleton(
ctx, delete_aliases, filter_list):
ctx, delete_aliases, ignore_empty_list, filter_list):
"""
Close indices
"""
Expand All @@ -199,18 +232,23 @@ def close_singleton(
}
ilo = IndexList(client)
ilo.iterate_filters(clean_filters)
_check_empty_list(ilo, ignore_empty_list)
action_obj = action_class(ilo, **mykwargs)
### Do the action
_actionator(action, action_obj, dry_run=ctx.parent.params['dry_run'])


@click.command(name='delete_indices')
@click.option(
'--ignore_empty_list', is_flag=True,
help='Do not raise exception if there are no actionable indices'
)
@click.option(
'--filter_list', callback=validate_filter_json,
help='JSON string representing an array of filters.', required=True
)
@click.pass_context
def delete_indices_singleton(ctx, filter_list):
def delete_indices_singleton(ctx, ignore_empty_list, filter_list):
"""
Delete indices
"""
Expand All @@ -228,6 +266,7 @@ def delete_indices_singleton(ctx, filter_list):
}
ilo = IndexList(client)
ilo.iterate_filters(clean_filters)
_check_empty_list(ilo, ignore_empty_list)
action_obj = action_class(ilo, **mykwargs)
### Do the action
_actionator(action, action_obj, dry_run=ctx.parent.params['dry_run'])
Expand All @@ -243,13 +282,18 @@ def delete_indices_singleton(ctx, filter_list):
@click.option(
'--retry_interval', type=int, help='Time in seconds between retries'
)
@click.option(
'--ignore_empty_list', is_flag=True,
help='Do not raise exception if there are no actionable snapshots'
)
@click.option(
'--filter_list', callback=validate_filter_json,
help='JSON string representing an array of filters.', required=True
)
@click.pass_context
def delete_snapshots_singleton(
ctx, repository, retry_count, retry_interval, filter_list):
ctx, repository, retry_count, retry_interval, ignore_empty_list,
filter_list):
"""
Delete snapshots
"""
Expand All @@ -272,19 +316,24 @@ def delete_snapshots_singleton(
}
slo = SnapshotList(client, repository=repository)
slo.iterate_filters(clean_filters)
_check_empty_list(slo, ignore_empty_list)
action_obj = action_class(slo, **mykwargs)
### Do the action
_actionator(action, action_obj, dry_run=ctx.parent.params['dry_run'])


@click.command(name='open')
@click.option(
'--ignore_empty_list', is_flag=True,
help='Do not raise exception if there are no actionable indices'
)
@click.option(
'--filter_list', callback=validate_filter_json,
help='JSON string representing an array of filters.', required=True
)
@click.pass_context
def open_singleton(
ctx, filter_list):
ctx, ignore_empty_list, filter_list):
"""
Open indices
"""
Expand All @@ -299,6 +348,7 @@ def open_singleton(
}
ilo = IndexList(client)
ilo.iterate_filters(clean_filters)
_check_empty_list(ilo, ignore_empty_list)
action_obj = action_class(ilo)
### Do the action
_actionator(action, action_obj, dry_run=ctx.parent.params['dry_run'])
Expand All @@ -313,13 +363,17 @@ def open_singleton(
'--delay', type=float,
help='Time in seconds to delay between operations. Default 0, maximum 3600'
)
@click.option(
'--ignore_empty_list', is_flag=True,
help='Do not raise exception if there are no actionable indices'
)
@click.option(
'--filter_list', callback=validate_filter_json,
help='JSON string representing an array of filters.', required=True
)
@click.pass_context
def forcemerge_singleton(
ctx, max_num_segments, delay, filter_list):
ctx, max_num_segments, delay, ignore_empty_list, filter_list):
"""
forceMerge index/shard segments
"""
Expand All @@ -340,6 +394,7 @@ def forcemerge_singleton(
}
ilo = IndexList(client)
ilo.iterate_filters(clean_filters)
_check_empty_list(ilo, ignore_empty_list)
action_obj = action_class(ilo, **mykwargs)
### Do the action
_actionator(action, action_obj, dry_run=ctx.parent.params['dry_run'])
Expand All @@ -352,13 +407,17 @@ def forcemerge_singleton(
@click.option(
'--wait_for_completion', is_flag=True, help='Wait for operation to complete'
)
@click.option(
'--ignore_empty_list', is_flag=True,
help='Do not raise exception if there are no actionable indices'
)
@click.option(
'--filter_list', callback=validate_filter_json,
help='JSON string representing an array of filters.', required=True
)
@click.pass_context
def replicas_singleton(
ctx, count, wait_for_completion, filter_list):
ctx, count, wait_for_completion, ignore_empty_list, filter_list):
"""
Change replica count
"""
Expand All @@ -379,6 +438,7 @@ def replicas_singleton(
}
ilo = IndexList(client)
ilo.iterate_filters(clean_filters)
_check_empty_list(ilo, ignore_empty_list)
action_obj = action_class(ilo, **mykwargs)
### Do the action
_actionator(action, action_obj, dry_run=ctx.parent.params['dry_run'])
Expand Down Expand Up @@ -411,14 +471,18 @@ def replicas_singleton(
'--skip_repo_fs_check', is_flag=True, expose_value=True,
help='Skip repository filesystem access validation.'
)
@click.option(
'--ignore_empty_list', is_flag=True,
help='Do not raise exception if there are no actionable indices'
)
@click.option(
'--filter_list', callback=validate_filter_json,
help='JSON string representing an array of filters.', required=True
)
@click.pass_context
def snapshot_singleton(
ctx, repository, name, ignore_unavailable, include_global_state, partial,
skip_repo_fs_check, wait_for_completion, filter_list):
skip_repo_fs_check, wait_for_completion, ignore_empty_list, filter_list):
"""
Snapshot indices
"""
Expand All @@ -444,6 +508,7 @@ def snapshot_singleton(
}
ilo = IndexList(client)
ilo.iterate_filters(clean_filters)
_check_empty_list(ilo, ignore_empty_list)
action_obj = action_class(ilo, **mykwargs)
### Do the action
_actionator(action, action_obj, dry_run=ctx.parent.params['dry_run'])
Expand All @@ -453,13 +518,17 @@ def snapshot_singleton(
@click.option('--verbose', help='Show verbose output.', is_flag=True)
@click.option('--header', help='Print header if --verbose', is_flag=True)
@click.option('--epoch', help='Print time as epoch if --verbose', is_flag=True)
@click.option(
'--ignore_empty_list', is_flag=True,
help='Do not raise exception if there are no actionable indices'
)
@click.option(
'--filter_list', callback=validate_filter_json,
help='JSON string representing an array of filters.', required=True
)
@click.pass_context
def show_indices_singleton(
ctx, epoch, header, verbose, filter_list):
ctx, epoch, header, verbose, ignore_empty_list, filter_list):
"""
Show indices
"""
Expand All @@ -477,7 +546,7 @@ def show_indices_singleton(
}
ilo = IndexList(client)
ilo.iterate_filters(clean_filters)
ilo.empty_list_check()
_check_empty_list(ilo, ignore_empty_list)
indices = sorted(ilo.indices)
# Do some calculations to figure out the proper column sizes
allbytes = []
Expand Down Expand Up @@ -530,13 +599,17 @@ def show_indices_singleton(
@click.option(
'--repository', type=str, required=True, help='Snapshot repository name'
)
@click.option(
'--ignore_empty_list', is_flag=True,
help='Do not raise exception if there are no actionable snapshots'
)
@click.option(
'--filter_list', callback=validate_filter_json,
help='JSON string representing an array of filters.', required=True
)
@click.pass_context
def show_snapshots_singleton(
ctx, repository, filter_list):
ctx, repository, ignore_empty_list, filter_list):
"""
Show snapshots
"""
Expand All @@ -550,6 +623,7 @@ def show_snapshots_singleton(
}
slo = SnapshotList(client, repository=repository)
slo.iterate_filters(clean_filters)
_check_empty_list(slo, ignore_empty_list)
snapshots = sorted(slo.snapshots)
for idx in snapshots:
click.secho('{0}'.format(idx))
Expand Down
5 changes: 5 additions & 0 deletions docs/Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Changelog
4.2.2 (? ? ?)
-------------

**General**

* Added ``--ignore_empty_list`` option to singleton actions. Requested in
#812 (untergeek)

**Documentation**

* Add a FAQ entry regarding the click module's need for Unicode when using
Expand Down

0 comments on commit d979fb5

Please sign in to comment.