Skip to content

Commit b936133

Browse files
author
Vladimir Kotal
committed
allow to use read-only configuration in Docker
fixes #3531
1 parent 5483932 commit b936133

File tree

4 files changed

+71
-18
lines changed

4 files changed

+71
-18
lines changed

docker/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ The volume mounted to `/opengrok/src` should contain the projects you want to ma
8383
`AVOID_PROJECTS` | empty | run in project less configuration. Set to non empty value disables projects.
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.
86+
`READONLY_CONFIG_FILE` | None | if set, the configuration will be merged with configuration from this file. This is run when the container starts.
8687

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

docker/start.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import multiprocessing
2727
import shutil
2828
import subprocess
29+
import tempfile
2930
import threading
3031
import time
3132
from pathlib import Path
@@ -39,6 +40,7 @@
3940
from opengrok_tools.deploy import deploy_war
4041
from opengrok_tools.utils.indexer import Indexer
4142
from opengrok_tools.sync import do_sync
43+
from opengrok_tools.config_merge import merge_config_files
4244
from opengrok_tools.utils.opengrok import list_projects, \
4345
add_project, delete_project, get_configuration, set_config_value
4446
from opengrok_tools.utils.readconfig import read_config
@@ -444,6 +446,28 @@ def main():
444446
os.path.getsize(OPENGROK_CONFIG_FILE) == 0:
445447
create_bare_config(logger, extra_indexer_options.split())
446448

449+
#
450+
# If there is read-only configuration file, merge it with current
451+
# configuration.
452+
#
453+
read_only_config_file = os.environ.get('READONLY_CONFIG_FILE')
454+
if read_only_config_file and os.path.exists(read_only_config_file):
455+
logger.info('Merging read-only configuration from \'{}\' with current '
456+
'configuration in \'{}\''.format(read_only_config_file,
457+
OPENGROK_CONFIG_FILE))
458+
out_file = None
459+
with tempfile.NamedTemporaryFile(mode='w+', delete=False,
460+
prefix='merged_config') as tmp_out:
461+
merge_config_files(read_only_config_file, OPENGROK_CONFIG_FILE,
462+
tmp_out, jar=OPENGROK_JAR, loglevel=log_level)
463+
out_file = tmp_out.name
464+
465+
if out_file and os.path.getsize(out_file) > 0:
466+
shutil.move(tmp_out.name, OPENGROK_CONFIG_FILE)
467+
else:
468+
logger.warning('Failed to merge read-only configuration, '
469+
'leaving the original in place')
470+
447471
if use_projects:
448472
num_workers = get_num_from_env(logger, 'WORKERS',
449473
multiprocessing.cpu_count())

tools/src/main/python/opengrok_tools/config_merge.py

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# CDDL HEADER END
1919

2020
#
21-
# Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2222
#
2323

2424
import argparse
@@ -37,34 +37,55 @@
3737
"""
3838

3939

40-
def main():
41-
parser = argparse.ArgumentParser(description='Java wrapper for project '
42-
'configuration merging',
43-
parents=[get_java_parser()])
40+
def merge_config_files(read_only, current, out_file, jar,
41+
loglevel=logging.INFO):
42+
43+
return config_merge_wrapper([read_only, current], jar=jar,
44+
out_file=out_file, loglevel=loglevel)
4445

45-
try:
46-
args = parser.parse_args()
47-
except ValueError as e:
48-
fatal(e)
46+
47+
def config_merge_wrapper(options, loglevel=logging.INFO,
48+
jar=None, java=None, java_opts=None,
49+
doprint=False, out_file=None):
4950

5051
# Avoid using utils.log.get_console_level() since the stdout of the program
5152
# is interpreted as data.
5253
logger = logging.getLogger(__name__)
53-
logger.setLevel(args.loglevel)
54+
logger.setLevel(loglevel)
5455

55-
cmd = Java(args.options, classpath=args.jar, java=args.java,
56-
java_opts=args.java_opts, redirect_stderr=False,
56+
cmd = Java(options, classpath=jar, java=java,
57+
java_opts=java_opts, redirect_stderr=False,
5758
main_class='org.opengrok.indexer.configuration.ConfigMerge',
58-
logger=logger, doprint=args.doprint)
59+
logger=logger, doprint=doprint)
5960
cmd.execute()
6061
ret = cmd.getretcode()
6162
if ret is None or ret != SUCCESS_EXITVAL:
6263
logger.error(cmd.geterroutput())
6364
logger.error("command failed (return code {})".format(ret))
64-
sys.exit(FAILURE_EXITVAL)
65+
return FAILURE_EXITVAL
6566
else:
66-
print(cmd.getoutputstr())
67+
if out_file:
68+
out_file.write(cmd.getoutputstr())
69+
else:
70+
print(cmd.getoutputstr())
71+
72+
return SUCCESS_EXITVAL
73+
74+
75+
def main():
76+
parser = argparse.ArgumentParser(description='Java wrapper for project '
77+
'configuration merging',
78+
parents=[get_java_parser()])
79+
80+
try:
81+
args = parser.parse_args()
82+
except ValueError as e:
83+
fatal(e)
84+
85+
return config_merge_wrapper(options=args.options, loglevel=args.loglevel,
86+
jar=args.jar, java=args.java,
87+
java_opts=args.java_opts, doprint=args.doprint)
6788

6889

6990
if __name__ == '__main__':
70-
main()
91+
sys.exit(main())

tools/src/main/python/opengrok_tools/utils/java.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
# CDDL HEADER END
1919

2020
#
21-
# Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
2222
# Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
2323
#
2424

25+
import os
2526
import platform
27+
import shutil
28+
2629
from .command import Command
2730
from .utils import is_exe
28-
import os
2931

3032

3133
class Java(Command):
@@ -105,4 +107,9 @@ def FindJava(self, logger):
105107
if os.path.isdir(java_home):
106108
java = os.path.join(java_home, 'bin', 'java')
107109

110+
if not java:
111+
logger.debug("Could not detemine java executable using standard "
112+
"means, trying system path")
113+
java = shutil.which('java')
114+
108115
return java

0 commit comments

Comments
 (0)