Skip to content

Commit a21c58b

Browse files
author
Vladimir Kotal
committed
use opengrok-sync
fixes #3221
1 parent a629d4a commit a21c58b

File tree

7 files changed

+129
-53
lines changed

7 files changed

+129
-53
lines changed

docker/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ The mirroring step works by going through all projects and attempting to
3636
synchronize all its repositories (e.g. it will do `git pull` for Git
3737
repositories).
3838

39+
Projects are enabled in this setup and there is no way how to change that.
40+
3941
### Indexer logs
4042

4143
The indexer/mirroring is set so that it does not log into files.

docker/start.sh

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,24 @@ fi
4040

4141
URI="http://localhost:8080/${URL_ROOT}"
4242
OPS=${INDEXER_FLAGS:='-H -P -S -G'}
43-
BODY_INCLUDE_FILE="/opengrok/data/body_include"
43+
44+
OPENGROK_BASE_DIR="/opengrok"
45+
OPENGROK_DATA_ROOT="$OPENGROK_BASE_DIR/data"
46+
OPENGROK_SRC_ROOT="$OPENGROK_BASE_DIR/src"
47+
BODY_INCLUDE_FILE="$OPENGROK_DATA_ROOT/body_include"
48+
OPENGROK_CONFIG_FILE="$OPENGROK_BASE_DIR/etc/configuration.xml"
49+
50+
export OPENGROK_INDEXER_OPTIONAL_ARGS=$INDEXER_OPT
51+
export OPENGROK_NO_MIRROR=$NOMIRROR
4452

4553
function deploy {
4654
if [[ ! -f "/usr/local/tomcat/webapps/${WAR_NAME}" ]]; then
4755
date +"%F %T Deployment path does not exist. Deploying..."
4856

4957
# Delete old deployment and (re)deploy.
5058
rm -rf /usr/local/tomcat/webapps/*
51-
opengrok-deploy -c /opengrok/etc/configuration.xml \
52-
/opengrok/lib/source.war "/usr/local/tomcat/webapps/${WAR_NAME}"
59+
opengrok-deploy -c "$OPENGROK_CONFIG_FILE" \
60+
"$OPENGROK_BASE_DIR/lib/source.war" "/usr/local/tomcat/webapps/${WAR_NAME}"
5361

5462
# Set up redirect from /source
5563
mkdir "/usr/local/tomcat/webapps/source"
@@ -64,42 +72,62 @@ function bare_config {
6472
fi
6573
echo '<p><h1>Waiting on the initial reindex to finish.. Stay tuned !</h1></p>' > "$BODY_INCLUDE_FILE"
6674

75+
echo "Creating bare configuration"
6776
opengrok-indexer \
6877
-a /opengrok/lib/opengrok.jar -- \
69-
-s /opengrok/src \
70-
-d /opengrok/data \
78+
-s "$OPENGROK_SRC_ROOT" \
79+
-d "$OPENGROK_DATA_ROOT" \
7180
--remote on \
72-
-W /opengrok/etc/configuration.xml \
81+
-P -H \
82+
-W "$OPENGROK_CONFIG_FILE" \
7383
--noIndex
84+
echo "Done creating bare config"
7485
}
7586

7687
function wait_for_tomcat {
77-
# Wait for Tomcat startup.
78-
date +"%F %T Waiting for Tomcat startup..."
79-
while [ "`curl --silent --write-out '%{response_code}' -o /dev/null \"http://localhost:8080/${URL_ROOT}\"`" == "000" ]; do
88+
# Wait for Tomcat startup and web app deploy.
89+
date +"%F %T Waiting for Tomcat startup and web app deployment..."
90+
while [ "`curl --silent --write-out '%{response_code}' -o /dev/null \"http://localhost:8080/${URL_ROOT}\"`" != "200" ]; do
8091
sleep 1;
8192
done
82-
date +"%F %T Tomcat startup finished"
93+
date +"%F %T Tomcat startup finished and web app deployed"
94+
}
95+
96+
function save_config {
97+
echo "Saving configuration"
98+
# Note: URI ends with a slash
99+
# TODO: check result of the API call and move only if successful
100+
curl -s -o "$OPENGROK_CONFIG_FILE" -X GET "${URI}api/v1/configuration"
101+
echo "Done saving configuration"
83102
}
84103

104+
function add_projects {
105+
# Add each directory under source root as a project.
106+
# For https://github.com/oracle/opengrok/issues/3403 this should be replaced
107+
# with query to get all projects and add only those that are not already present.
108+
echo "Adding projects"
109+
find $OPENGROK_SRC_ROOT/* -maxdepth 0 -type d -print | xargs -n 1 basename | \
110+
while read dir; do
111+
echo "Adding $dir"
112+
# Note: URI ends with a slash
113+
curl -s -X POST -H 'Content-Type: text/plain' -d "$dir" "${URI}api/v1/projects"
114+
done
115+
echo "Done adding projects"
116+
}
117+
118+
# Perform mirroring and indexing of all projects.
85119
function data_sync {
86-
if [ -z $NOMIRROR ]; then
87-
date +"%F %T Mirroring starting"
88-
opengrok-mirror --all --uri "$URI"
89-
date +"%F %T Mirroring finished"
90-
fi
120+
add_projects
91121

92-
date +"%F %T Indexing starting"
93-
opengrok-indexer \
94-
-a /opengrok/lib/opengrok.jar -- \
95-
-s /opengrok/src \
96-
-d /opengrok/data \
97-
--remote on \
98-
-W /opengrok/etc/configuration.xml \
99-
-U "$URI" \
100-
$OPS \
101-
$INDEXER_OPT "$@"
102-
date +"%F %T Indexing finished"
122+
date +"%F %T Sync starting"
123+
# TODO: $URI vs sync.yml
124+
opengrok-sync -U "$URI" --driveon --config /scripts/sync.yml
125+
126+
# Workaround for https://github.com/oracle/opengrok/issues/1670
127+
touch $OPENGROK_DATA_ROOT/timestamp
128+
129+
date +"%F %T Sync finished"
130+
save_config
103131
}
104132

105133
function indexer {
@@ -108,18 +136,21 @@ function indexer {
108136
while true; do
109137
data_sync
110138

111-
# If this was a case of initial indexing, move the include away.
139+
# If this was the case of initial indexing, move the include away.
112140
rm -f "$BODY_INCLUDE_FILE"
113141
if [[ -f $BODY_INCLUDE_FILE.orig ]]; then
114142
mv "$BODY_INCLUDE_FILE.orig" "$BODY_INCLUDE_FILE"
115143
fi
116144

117-
# Index every $REINDEX minutes.
145+
# Want to reload the includes anyway in case the user modified them.
146+
curl -s -X PUT "${URI}api/v1/system/includes/reload"
147+
148+
# Sync every $REINDEX minutes.
118149
if [ "$REINDEX" == "0" ]; then
119-
date +"%F %T Automatic reindexing disabled"
150+
date +"%F %T Automatic sync disabled"
120151
return
121152
else
122-
date +"%F %T Automatic reindexing in $REINDEX minutes..."
153+
date +"%F %T Automatic sync in $REINDEX minutes..."
123154
fi
124155
sleep `expr 60 \* $REINDEX`
125156
done
@@ -131,7 +162,7 @@ deploy
131162
# Create empty configuration to avoid the non existent file exception
132163
# during the first web app startup.
133164
#
134-
if [[ ! -f /opengrok/etc/configuration.xml ]]; then
165+
if [[ ! -f "$OPENGROK_CONFIG_FILE" ]]; then
135166
bare_config
136167
fi
137168

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,15 @@ def main():
9595
lock = FileLock(os.path.join(tempfile.gettempdir(), lockfile + ".lock"))
9696
try:
9797
with lock.acquire(timeout=0):
98-
indexer = Indexer(args.options, logger=logger, java=args.java,
98+
optional_args = os.environ.get("OPENGROK_INDEXER_OPTIONAL_ARGS")
99+
options = args.options
100+
if optional_args and len(optional_args) > 0 and options:
101+
logger.debug("adding optional indexer arguments: {}".
102+
format(optional_args))
103+
if options:
104+
options.extend(optional_args)
105+
106+
indexer = Indexer(options, logger=logger, java=args.java,
99107
jar=args.jar, java_opts=args.java_opts,
100108
env_vars=args.environment, doprint=doprint)
101109
indexer.execute()

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ def main():
9393
help='uri of the webapp with context path')
9494
parser.add_argument('-b', '--batch', action='store_true',
9595
help='batch mode - will log into a file')
96+
parser.add_argument('-L', '--logdir',
97+
help='log directory')
9698
parser.add_argument('-B', '--backupcount', default=8,
9799
help='how many log files to keep around in batch mode')
98100
parser.add_argument('-I', '--check-changes', action='store_true',
@@ -110,6 +112,12 @@ def main():
110112

111113
logger = get_console_logger(get_class_basename(), args.loglevel)
112114

115+
nomirror = os.environ.get("OPENGROK_NO_MIRROR")
116+
if nomirror and len(nomirror) > 0:
117+
logger.debug("skipping mirror based on the OPENGROK_NO_MIRROR " +
118+
"environment variable")
119+
return SUCCESS_EXITVAL
120+
113121
if len(args.project) > 0 and args.all:
114122
return fatal("Cannot use both project list and -a/--all", False)
115123

@@ -156,10 +164,13 @@ def main():
156164
logdir = None
157165
# Log messages to dedicated log file if running in batch mode.
158166
if args.batch:
159-
logdir = config.get(LOGDIR_PROPERTY)
160-
if not logdir:
161-
return fatal("The {} property is required in batch mode".
162-
format(LOGDIR_PROPERTY), False)
167+
if args.logdir:
168+
logdir = args.logdir
169+
else:
170+
logdir = config.get(LOGDIR_PROPERTY)
171+
if not logdir:
172+
return fatal("The {} property is required in batch mode".
173+
format(LOGDIR_PROPERTY), False)
163174

164175
projects = args.project
165176
if len(projects) == 1:

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,50 +75,61 @@ def get_config_file(logger, uri):
7575
def main():
7676
parser = argparse.ArgumentParser(description='OpenGrok indexer wrapper '
7777
'for indexing single project',
78-
parents=[get_java_parser()])
79-
parser.add_argument('-t', '--template', required=True,
78+
parents=[get_java_parser()],
79+
prog=sys.argv[0])
80+
parser.add_argument('-t', '--template',
8081
help='Logging template file')
81-
parser.add_argument('-p', '--pattern', required=True,
82+
parser.add_argument('-p', '--pattern',
8283
help='Pattern to substitute in logging template with'
8384
'project name')
8485
parser.add_argument('-P', '--project', required=True,
8586
help='Project name')
86-
parser.add_argument('-d', '--directory', required=True,
87+
parser.add_argument('-d', '--directory',
8788
help='Logging directory')
8889
parser.add_argument('-U', '--uri', default='http://localhost:8080/source',
8990
help='URI of the webapp with context path')
9091

92+
cmd_args = sys.argv[1:]
93+
extra_opts = os.environ.get("OPENGROK_INDEXER_OPTIONAL_ARGS")
94+
if extra_opts:
95+
cmd_args.extend(extra_opts.split())
96+
9197
try:
92-
args = parser.parse_args()
98+
args = parser.parse_args(cmd_args)
9399
except ValueError as e:
94100
fatal(e)
95101

96102
logger = get_console_logger(get_class_basename(), args.loglevel)
97103

98104
# Make sure the log directory exists.
99-
if not os.path.isdir(args.directory):
100-
os.makedirs(args.directory)
105+
if args.directory:
106+
if not os.path.isdir(args.directory):
107+
os.makedirs(args.directory)
101108

102109
# Get files needed for per-project reindex.
103110
conf_file = get_config_file(logger, args.uri)
104-
logprop_file = get_logprop_file(logger, args.template, args.pattern,
105-
args.project)
111+
logprop_file = None
112+
if args.template and args.pattern:
113+
logprop_file = get_logprop_file(logger, args.template, args.pattern,
114+
args.project)
106115

107116
# Reindex with the modified logging.properties file and read-only config.
108117
command = ['-R', conf_file]
109118
command.extend(args.options)
110119
java_opts = []
111120
if args.java_opts:
112121
java_opts.extend(args.java_opts)
113-
java_opts.append("-Djava.util.logging.config.file={}".
114-
format(logprop_file))
122+
if logprop_file:
123+
java_opts.append("-Djava.util.logging.config.file={}".
124+
format(logprop_file))
115125
indexer = Indexer(command, logger=logger, jar=args.jar,
116126
java=args.java, java_opts=java_opts,
117127
env_vars=args.environment, doprint=args.doprint)
118128
indexer.execute()
119129
ret = indexer.getretcode()
120130
os.remove(conf_file)
121-
os.remove(logprop_file)
131+
if logprop_file:
132+
os.remove(logprop_file)
122133
if ret is None or ret != SUCCESS_EXITVAL:
123134
logger.error(indexer.getoutputstr())
124135
logger.error("Indexer command for project {} failed (return code {})".

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@ def do_sync(args, commands, config, directory, dirs_to_process, ignore_errors,
9090
for entry in os.listdir(directory):
9191
if path.isdir(path.join(directory, entry)):
9292
dirs_to_process.append(entry)
93+
9394
logger.debug("to process: {}".format(dirs_to_process))
9495
cmds_base = []
9596
for d in dirs_to_process:
9697
cmd_base = CommandSequenceBase(d, commands, args.loglevel,
9798
config.get("cleanup"),
9899
args.driveon)
99100
cmds_base.append(cmd_base)
101+
100102
# Map the commands into pool of workers so they can be processed.
101103
retval = SUCCESS_EXITVAL
102104
with Pool(processes=int(args.workers)) as pool:

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,22 @@ def run(self):
149149
format(command, self.name))
150150
continue
151151
else:
152-
self.logger.error("command '{}' for project {} failed "
153-
"with code {}, breaking".
154-
format(command, self.name, retcode))
155-
self.failed = True
156-
self.run_cleanup()
152+
if self.driveon:
153+
do = "driving on"
154+
self.logger.debug("command '{}' for project '{}'' failed "
155+
"with code {}, {}".
156+
format(command, self.name, retcode, do))
157+
else:
158+
do = "breaking"
159+
self.logger.error("command '{}' for project '{}'' failed "
160+
"with code {}, {}".
161+
format(command, self.name, retcode, do))
162+
163+
if self.driveon:
164+
continue
165+
else:
166+
self.failed = True
167+
self.run_cleanup()
157168

158169
break
159170

0 commit comments

Comments
 (0)