Skip to content

Commit 5036d90

Browse files
Vladimir Kotalahornace
authored andcommitted
check mirror config in Docker
fixes #3675
1 parent c57b4ef commit 5036d90

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

docker/start.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
add_project, delete_project, get_configuration
4848
from opengrok_tools.utils.readconfig import read_config
4949
from opengrok_tools.utils.exitvals import SUCCESS_EXITVAL
50+
from opengrok_tools.utils.mirror import check_configuration
5051

5152

5253
fs_root = os.path.abspath('.').split(os.path.sep)[0] + os.path.sep
@@ -70,6 +71,8 @@
7071
OPENGROK_WEBAPPS_DIR = os.path.join(tomcat_root, "webapps")
7172
OPENGROK_JAR = os.path.join(OPENGROK_LIB_DIR, 'opengrok.jar')
7273

74+
NOMIRROR_ENV_NAME = 'NOMIRROR'
75+
7376
expected_token = None
7477

7578
sleep_event = threading.Event()
@@ -485,8 +488,8 @@ def main():
485488
if extra_indexer_options:
486489
logger.info("extra indexer options: {}".format(extra_indexer_options))
487490
env['OPENGROK_INDEXER_OPTIONAL_ARGS'] = extra_indexer_options
488-
if os.environ.get('NOMIRROR'):
489-
env['OPENGROK_NO_MIRROR'] = os.environ.get('NOMIRROR')
491+
if os.environ.get(NOMIRROR_ENV_NAME):
492+
env['OPENGROK_NO_MIRROR'] = os.environ.get(NOMIRROR_ENV_NAME)
490493
logger.debug('Extra environment: {}'.format(env))
491494

492495
use_projects = True
@@ -531,15 +534,25 @@ def main():
531534
if out_file:
532535
os.remove(out_file)
533536

537+
sync_enabled = True
534538
if use_projects:
535539
num_workers = get_num_from_env(logger, 'WORKERS',
536540
multiprocessing.cpu_count())
537541
logger.info('Number of sync workers: {}'.format(num_workers))
538542

539-
mirror_config = os.path.join(OPENGROK_CONFIG_DIR, "mirror.yml")
540-
if not os.path.exists(mirror_config):
541-
with open(mirror_config, 'w') as fp:
542-
fp.write("# Empty config file for opengrok-mirror\n")
543+
if not os.environ.get(NOMIRROR_ENV_NAME):
544+
mirror_config = os.path.join(OPENGROK_CONFIG_DIR, "mirror.yml")
545+
if not os.path.exists(mirror_config):
546+
with open(mirror_config, 'w') as fp:
547+
fp.write("# Empty config file for opengrok-mirror\n")
548+
else:
549+
conf = read_config(logger, mirror_config)
550+
logger.info("Checking mirror configuration in '{}'".
551+
format(mirror_config))
552+
if not check_configuration(conf):
553+
logger.error("Mirror configuration in '{}' is invalid, "
554+
"disabling sync".format(mirror_config))
555+
sync_enabled = False
543556

544557
worker_function = project_syncer
545558
syncer_args = (logger, log_level, uri,
@@ -550,14 +563,16 @@ def main():
550563
syncer_args = (logger, uri, OPENGROK_CONFIG_FILE,
551564
extra_indexer_options)
552565

553-
logger.debug("Starting sync thread")
554-
sync_thread = threading.Thread(target=worker_function, name="Sync thread",
555-
args=syncer_args, daemon=True)
556-
sync_thread.start()
566+
if sync_enabled:
567+
logger.debug("Starting sync thread")
568+
sync_thread = threading.Thread(target=worker_function,
569+
name="Sync thread",
570+
args=syncer_args, daemon=True)
571+
sync_thread.start()
557572

558-
start_rest_thread(logger)
559-
if sync_period > 0:
560-
start_timeout_thread(logger, sync_period)
573+
start_rest_thread(logger)
574+
if sync_period > 0:
575+
start_timeout_thread(logger, sync_period)
561576

562577
# Start Tomcat last. It will be the foreground process.
563578
logger.info("Starting Tomcat")

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def main():
9090
help='mirror all indexed projects', default=False)
9191
parser.add_argument('-c', '--config',
9292
help='config file in JSON/YAML format')
93+
parser.add_argument('--check_config', action='store_true',
94+
help='check configuration and exit')
9395
parser.add_argument('-U', '--uri', default='http://localhost:8080/source',
9496
help='uri of the webapp with context path')
9597
parser.add_argument('-b', '--batch', action='store_true',
@@ -116,7 +118,21 @@ def main():
116118

117119
logger = get_console_logger(get_class_basename(), args.loglevel)
118120

119-
headers = get_headers(args.header)
121+
if args.config:
122+
config = read_config(logger, args.config)
123+
if config is None:
124+
return fatal("Cannot read config file from {}".
125+
format(args.config), False)
126+
else:
127+
config = {}
128+
129+
if not check_configuration(config):
130+
logger.error("Configuration check failed, exiting")
131+
return 1
132+
133+
if args.check_config:
134+
logger.debug("Configuration check passed, exiting")
135+
return 0
120136

121137
nomirror = os.environ.get("OPENGROK_NO_MIRROR")
122138
if nomirror and len(nomirror) > 0:
@@ -130,21 +146,12 @@ def main():
130146
if not args.all and len(args.project) == 0:
131147
return fatal("Need at least one project or --all", False)
132148

133-
if args.config:
134-
config = read_config(logger, args.config)
135-
if config is None:
136-
return fatal("Cannot read config file from {}".
137-
format(args.config), False)
138-
else:
139-
config = {}
140-
141149
uri = args.uri
142150
if not is_web_uri(uri):
143151
return fatal("Not a URI: {}".format(uri), False)
144152
logger.debug("web application URI = {}".format(uri))
145153

146-
if not check_configuration(config):
147-
return 1
154+
headers = get_headers(args.header)
148155

149156
# Save the source root to avoid querying the web application.
150157
source_root = get_config_value(logger, 'sourceRoot', uri,

0 commit comments

Comments
 (0)