|
37 | 37 | import logging |
38 | 38 | import tempfile |
39 | 39 | import shutil |
40 | | -import stat |
41 | 40 | from utils import get_command |
42 | 41 |
|
43 | 42 |
|
@@ -75,61 +74,69 @@ def get_config_file(basedir): |
75 | 74 | return path.join(basedir, "etc", "configuration.xml") |
76 | 75 |
|
77 | 76 |
|
| 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 | + |
78 | 102 | def config_refresh(doit, logger, basedir, messages, configmerge, roconfig): |
79 | 103 | """ |
80 | 104 | 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. |
82 | 107 |
|
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 |
88 | 110 | """ |
89 | 111 |
|
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) |
93 | 116 |
|
94 | | - logger.info('Refreshing configuration and merging with read-only ' |
95 | | - 'configuration') |
96 | 117 | current_config = exec_command(doit, logger, |
97 | 118 | [messages, '-n', 'config', '-t', 'getconf'], |
98 | 119 | "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)) |
101 | 122 | 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)) |
113 | 137 | 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) |
133 | 140 |
|
134 | 141 |
|
135 | 142 | def project_add(doit, logger, project, messages): |
@@ -207,8 +214,9 @@ def project_delete(doit, logger, project, messages): |
207 | 214 | group.add_argument('-d', '--delete', metavar='project', nargs='+', |
208 | 215 | help='Delete project and its data and source code') |
209 | 216 | 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.') |
212 | 220 |
|
213 | 221 | args = parser.parse_args() |
214 | 222 |
|
@@ -240,10 +248,6 @@ def project_delete(doit, logger, project, messages): |
240 | 248 | logger.error("File {} does not exist".format(args.roconfig)) |
241 | 249 | sys.exit(1) |
242 | 250 |
|
243 | | - if args.refresh and not args.roconfig: |
244 | | - logger.error("-r requires -R") |
245 | | - sys.exit(1) |
246 | | - |
247 | 251 | # XXX replace Messages with REST request after issue #1801 |
248 | 252 | messages_file = get_command(logger, args.messages, "Messages") |
249 | 253 | configmerge_file = get_command(logger, args.configmerge, "ConfigMerge") |
|
0 commit comments