Skip to content

Commit f750ae7

Browse files
Vladimir Kotalahornace
authored andcommitted
check index and wipe out data root on mismatch
fixes #3571
1 parent ec9556c commit f750ae7

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

docker/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ The volume mounted to `/opengrok/src` should contain the projects you want to ma
8484
`REST_PORT` | 5000 | TCP port where simple REST app listens for GET requests on `/reindex` to trigger manual reindex.
8585
`REST_TOKEN` | None | if set, the REST app will require this token as Bearer token in order to trigger reindex.
8686
`READONLY_CONFIG_FILE` | None | if set, the configuration will be merged with configuration from this file. This is run when the container starts.
87+
`CHECK_INDEX` | None | if set, the format of the index will be checked first. **If the index is not compatible with the currently running version, the data root will be wiped out and reindex from scratch will be performed.**
8788

8889
To specify environment variable for `docker run`, use the `-e` option, e.g. `-e SYNC_PERIOD_MINUTES=30`
8990

docker/start.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,32 @@ def get_num_from_env(logger, env_name, default_value):
402402
return value
403403

404404

405+
def check_index_and_wipe_out(logger):
406+
"""
407+
Check index by running the indexer. If the index does not match
408+
currently running version and the CHECK_INDEX environment variable
409+
is non empty, wipe out the directories under data root.
410+
"""
411+
check_index = os.environ.get('CHECK_INDEX')
412+
if check_index and os.path.exists(OPENGROK_CONFIG_FILE):
413+
logger.info('Checking if index matches current version')
414+
indexer_options = ['-R', OPENGROK_CONFIG_FILE, '--checkIndex']
415+
indexer = Indexer(indexer_options, logger=logger,
416+
jar=OPENGROK_JAR, doprint=True)
417+
indexer.execute()
418+
if indexer.getretcode() == 1:
419+
logger.info('Wiping out data root')
420+
root = OPENGROK_DATA_ROOT
421+
for entry in os.listdir(root):
422+
path = os.path.join(root, entry)
423+
if os.path.isdir(path):
424+
try:
425+
logger.info("Removing '{}'".format(path))
426+
shutil.rmtree(path)
427+
except Exception as e:
428+
logger.error("cannot delete '{}': {}".format(path, e))
429+
430+
405431
def main():
406432
log_level = os.environ.get('OPENGROK_LOG_LEVEL')
407433
if log_level:
@@ -448,6 +474,12 @@ def main():
448474
os.path.getsize(OPENGROK_CONFIG_FILE) == 0:
449475
create_bare_config(logger, extra_indexer_options.split())
450476

477+
#
478+
# Index check needs read-only configuration so it is placed
479+
# right after create_bare_config().
480+
#
481+
check_index_and_wipe_out(logger)
482+
451483
#
452484
# If there is read-only configuration file, merge it with current
453485
# configuration.

opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ public static void main(String[] argv) {
279279
" under the data root and reindex\n");
280280
System.exit(1);
281281
}
282+
283+
System.exit(0);
282284
}
283285

284286
// Let repository types to add items to ignoredNames.

0 commit comments

Comments
 (0)