import channel script should be idempotent (#47)

This commit is contained in:
Rok Garbas 2020-05-22 14:58:38 +02:00 committed by GitHub
parent 2600984760
commit c45a1581b1
Failed to generate hash of commit

View file

@ -271,10 +271,11 @@ def get_options(evaluation):
return len(options), gen
def create_index(es, index, mapping):
def ensure_index(es, index, mapping):
if es.indices.exists(index):
logger.debug(f"create_index: index '{index}' already exists")
return
logger.debug(f"ensure_index: index '{index}' already exists")
return False
es.indices.create(
index=index,
body={
@ -282,10 +283,12 @@ def create_index(es, index, mapping):
"mappings": mapping,
},
)
logger.debug(f"create_index: index '{index}' was created")
logger.debug(f"ensure_index: index '{index}' was created")
return True
def create_index_name(type_, channel, evaluation):
def ensure_index_name(type_, channel, evaluation):
return (
f"latest-{channel}-{type_}",
f"evaluation-{INDEX_SCHEMA_VERSION}-{channel}-{evaluation['revisions_since_start']}-{evaluation['git_revision']}-{type_}",
@ -317,12 +320,13 @@ def main(es_url, channel, verbose):
es = elasticsearch.Elasticsearch([es_url])
# ensure indexes exist
packages_alias, packages_index = create_index_name("packages", channel, evaluation)
options_alias, options_index = create_index_name("options", channel, evaluation)
create_index(es, packages_index, PACKAGES_MAPPING)
create_index(es, options_index, OPTIONS_MAPPING)
options_alias, options_index = ensure_index_name("options", channel, evaluation)
packages_alias, packages_index = ensure_index_name("packages", channel, evaluation)
packages_index_created = ensure_index(es, packages_index, PACKAGES_MAPPING)
options_index_created = ensure_index(es, options_index, OPTIONS_MAPPING)
# write packages
if packages_index_created:
number_of_packages, gen_packages = get_packages(evaluation)
if number_of_packages:
click.echo("Indexing packages...")
@ -336,6 +340,7 @@ def main(es_url, channel, verbose):
click.echo("Indexed %d/%d packages" % (successes, number_of_packages))
# write options
if options_index_created:
number_of_options, gen_options = get_options(evaluation)
if number_of_options:
click.echo("Indexing options...")
@ -349,7 +354,9 @@ def main(es_url, channel, verbose):
print("Indexed %d/%d options" % (successes, number_of_options))
# update alias
if packages_index_created:
update_alias(es, packages_alias, packages_index)
if options_index_created:
update_alias(es, options_alias, options_index)