Skip to content

Commit b0ac441

Browse files
author
Vladimir Kotal
committed
make read-only config optional
1 parent 93dc5b4 commit b0ac441

File tree

1 file changed

+54
-50
lines changed

1 file changed

+54
-50
lines changed

tools/sync/projadm.py

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import logging
3838
import tempfile
3939
import shutil
40-
import stat
4140
from utils import get_command
4241

4342

@@ -75,61 +74,69 @@ def get_config_file(basedir):
7574
return path.join(basedir, "etc", "configuration.xml")
7675

7776

77+
def install_config(doit, src, dst):
78+
"""
79+
Copy the data of src to dst. Exit on failure.
80+
"""
81+
if not doit:
82+
logger.debug("Not copying {} to {}".format(src, dst))
83+
return
84+
85+
#
86+
# Copy the file so that close() triggered unlink()
87+
# does not fail.
88+
#
89+
logger.debug("Copying {} to {}".format(src, dst))
90+
try:
91+
shutil.copyfile(src, dst)
92+
except PermissionError:
93+
logger.error('Failed to copy {} to {} (permissions)'.
94+
format(src, dst))
95+
sys.exit(1)
96+
except OSError:
97+
logger.error('Failed to copy {} to {} (I/O)'.
98+
format(src, dst))
99+
sys.exit(1)
100+
101+
78102
def config_refresh(doit, logger, basedir, messages, configmerge, roconfig):
79103
"""
80104
Refresh current configuration file with configuration retrieved
81-
from webapp merged with readonly configuration.
105+
from webapp. If roconfig is not None, the current config is merged with
106+
readonly configuration first.
82107
83-
1. retrieves current configuration from the webapp,
84-
stores it into temporary file
85-
2. merge the read-only config with the config from previous step
86-
- this is done as a workaround for
87-
https://github.com/oracle/opengrok/issues/2002
108+
The merge of the current config from the webapp with the read-only config
109+
is done as a workaround for https://github.com/oracle/opengrok/issues/2002
88110
"""
89111

90-
if not roconfig:
91-
logger.debug("No read-only configuration specified, not refreshing")
92-
return
112+
main_config = get_config_file(basedir)
113+
if not path.isfile(main_config):
114+
logger.error("file {} does not exist".format(main_config))
115+
sys.exit(1)
93116

94-
logger.info('Refreshing configuration and merging with read-only '
95-
'configuration')
96117
current_config = exec_command(doit, logger,
97118
[messages, '-n', 'config', '-t', 'getconf'],
98119
"getting configuration failed")
99-
with tempfile.NamedTemporaryFile() as fc:
100-
logger.debug("Temporary file for current config: {}".format(fc.name))
120+
with tempfile.NamedTemporaryFile() as fcur:
121+
logger.debug("Temporary file for current config: {}".format(fcur.name))
101122
if doit:
102-
fc.write(bytearray(''.join(current_config), "UTF-8"))
103-
merged_config = exec_command(doit, logger,
104-
[configmerge, roconfig, fc.name],
105-
"cannot merge configuration")
106-
with tempfile.NamedTemporaryFile() as fm:
107-
logger.debug("Temporary file for merged config: {}".
108-
format(fm.name))
109-
if doit:
110-
fm.write(bytearray(''.join(merged_config), "UTF-8"))
111-
main_config = get_config_file(basedir)
112-
if path.isfile(main_config):
123+
fcur.write(bytearray(''.join(current_config), "UTF-8"))
124+
125+
if not roconfig:
126+
logger.info('Refreshing configuration')
127+
install_config(doit, fcur.name, main_config)
128+
else:
129+
logger.info('Refreshing configuration '
130+
'(merging with read-only config)')
131+
merged_config = exec_command(doit, logger,
132+
[configmerge, roconfig, fcur.name],
133+
"cannot merge configuration")
134+
with tempfile.NamedTemporaryFile() as fmerged:
135+
logger.debug("Temporary file for merged config: {}".
136+
format(fmerged.name))
113137
if doit:
114-
#
115-
# Copy the file so that close() triggered unlink()
116-
# does not fail.
117-
#
118-
logger.debug("Copying {} to {}".
119-
format(fm.name, main_config))
120-
try:
121-
shutil.copyfile(fm.name, main_config)
122-
except PermissionError:
123-
logger.error('Failed to copy {} to {} (permissions)'.
124-
format(fm.name, main_config))
125-
sys.exit(1)
126-
except OSError:
127-
logger.error('Failed to copy {} to {} (I/O)'.
128-
format(fm.name, main_config))
129-
sys.exit(1)
130-
else:
131-
logger.error("file {} does not exist".format(main_config))
132-
sys.exit(1)
138+
fmerged.write(bytearray(''.join(merged_config), "UTF-8"))
139+
install_config(doit, fmerged.name, main_config)
133140

134141

135142
def project_add(doit, logger, project, messages):
@@ -207,8 +214,9 @@ def project_delete(doit, logger, project, messages):
207214
group.add_argument('-d', '--delete', metavar='project', nargs='+',
208215
help='Delete project and its data and source code')
209216
group.add_argument('-r', '--refresh', action='store_true',
210-
help='Refresh configuration from read-only '
211-
'configuration')
217+
help='Refresh configuration. If read-only '
218+
'configuration is supplied, it is merged with current '
219+
'configuration.')
212220

213221
args = parser.parse_args()
214222

@@ -240,10 +248,6 @@ def project_delete(doit, logger, project, messages):
240248
logger.error("File {} does not exist".format(args.roconfig))
241249
sys.exit(1)
242250

243-
if args.refresh and not args.roconfig:
244-
logger.error("-r requires -R")
245-
sys.exit(1)
246-
247251
# XXX replace Messages with REST request after issue #1801
248252
messages_file = get_command(logger, args.messages, "Messages")
249253
configmerge_file = get_command(logger, args.configmerge, "ConfigMerge")

0 commit comments

Comments
 (0)