Skip to content

Commit 1ba4d93

Browse files
committed
Updates intelmqsetup and remove unnecessary code.
1 parent aaeb1b6 commit 1ba4d93

File tree

1 file changed

+12
-205
lines changed

1 file changed

+12
-205
lines changed

intelmq/bin/intelmqsetup.py

Lines changed: 12 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,16 @@
2727
from grp import getgrnam
2828
from pathlib import Path
2929
from pwd import getpwnam
30-
from subprocess import run, CalledProcessError
31-
from tempfile import NamedTemporaryFile
3230
from typing import Optional
3331

34-
try:
35-
import intelmq_api
36-
import intelmq_api.version
37-
except ImportError:
38-
intelmq_api = None
39-
40-
try:
41-
import intelmq_manager
42-
except ImportError:
43-
intelmq_manager = None
44-
else:
45-
try:
46-
import intelmq_manager.build
47-
except ImportError:
48-
intelmq_manager_has_build = False
49-
else:
50-
intelmq_manager_has_build = True
51-
5232
from termstyle import red
5333
from intelmq import (CONFIG_DIR, DEFAULT_LOGGING_PATH, ROOT_DIR, VAR_RUN_PATH,
54-
VAR_STATE_PATH, STATE_FILE_PATH)
34+
VAR_STATE_PATH, STATE_FILE_PATH, VAR_SERVER_PATH)
5535
from intelmq.bin.intelmqctl import IntelMQController
5636

57-
5837
FILE_OUTPUT_PATH = Path(VAR_STATE_PATH) / 'file-output/'
5938
ETC_INTELMQ = Path('/etc/intelmq/')
6039
ETC_INTELMQ_MANAGER = ETC_INTELMQ / 'manager/'
61-
WEBSERVER_CONFIG_DIR = None # "cache" for the webserver configuration directory
62-
NOTE_WEBSERVER_RELOAD = False # if the webserver needs to be reloaded
6340

6441

6542
def basic_checks(skip_ownership):
@@ -108,57 +85,19 @@ def change_owner(file: str, owner: Optional[str] = None, group: Optional[str] =
10885
shutil.chown(file, group=group)
10986

11087

111-
def find_webserver_user():
112-
candidates = ('www-data', 'wwwrun', 'httpd', 'apache')
113-
for candidate in candidates:
114-
try:
115-
getpwnam(candidate)
116-
except KeyError:
117-
pass
118-
else:
119-
print(f'Detected Apache username {candidate!r}.')
120-
return candidate
121-
else:
122-
sys.exit(red("Unable to detect Apache user name. "
123-
"Please re-run this program and give the Apache user name with '--webserver-user'."))
124-
125-
126-
def find_webserver_configuration_directory():
127-
global WEBSERVER_CONFIG_DIR
128-
if WEBSERVER_CONFIG_DIR:
129-
return WEBSERVER_CONFIG_DIR
130-
webserver_configuration_dir_candidates = (Path('/etc/apache2/conf-available/'),
131-
Path('/etc/apache2/conf.d/'),
132-
Path('/etc/httpd/conf.d/'))
133-
for webserver_configuration_dir_candidate in webserver_configuration_dir_candidates:
134-
if webserver_configuration_dir_candidate.exists():
135-
print(f'Detected Apache configuration directory {webserver_configuration_dir_candidate!s}.')
136-
WEBSERVER_CONFIG_DIR = webserver_configuration_dir_candidate
137-
webserver_configuration_dir_candidate.as_posix
138-
return webserver_configuration_dir_candidate
139-
else:
140-
sys.exit(red("Unable to detect Apache configuration directory. "
141-
"Please re-run this program and give the Apache configuration directory with '--webserver-configuration-directory'."))
142-
143-
144-
def debian_activate_apache_config(config_name: str):
145-
if 'available' not in WEBSERVER_CONFIG_DIR.as_posix():
146-
return # not a Debian system
147-
available = WEBSERVER_CONFIG_DIR / config_name
148-
enabled = Path(WEBSERVER_CONFIG_DIR.as_posix().replace('available', 'enabled')) / config_name
149-
if not enabled.exists():
150-
enabled.symlink_to(available)
151-
print(f'Created symbolic link {enabled!s} pointing to {available!s}.')
152-
153-
15488
def intelmqsetup_core(ownership=True, state_file=STATE_FILE_PATH):
15589
create_directory(FILE_OUTPUT_PATH, 0o40755)
15690
create_directory(VAR_RUN_PATH, 0o40755)
15791
create_directory(DEFAULT_LOGGING_PATH, 0o40755)
15892
create_directory(CONFIG_DIR, 0o40775)
93+
create_directory(VAR_SERVER_PATH, 0o40775)
15994

16095
example_path = Path(pkg_resources.resource_filename('intelmq', 'etc'))
161-
example_confs = [example_path / 'runtime.yaml', example_path / 'harmonization.conf']
96+
example_confs = [
97+
example_path / 'runtime.yaml',
98+
example_path / 'harmonization.conf',
99+
example_path / 'intelmq.yaml',
100+
]
162101
for example_conf in example_confs:
163102
fname = Path(example_conf).name
164103
destination_file = Path(CONFIG_DIR) / fname
@@ -186,117 +125,6 @@ def intelmqsetup_core(ownership=True, state_file=STATE_FILE_PATH):
186125
change_owner(STATE_FILE_PATH, owner='intelmq', group='intelmq')
187126

188127

189-
def intelmqsetup_api(ownership: bool = True, webserver_user: Optional[str] = None):
190-
intelmq_group = getgrnam('intelmq')
191-
webserver_user = webserver_user or find_webserver_user()
192-
193-
create_directory(ETC_INTELMQ, 0o40775)
194-
if ownership:
195-
change_owner(CONFIG_DIR, group='intelmq')
196-
change_owner(ETC_INTELMQ, owner='intelmq', group='intelmq')
197-
198-
# Manager configuration directory
199-
create_directory(ETC_INTELMQ_MANAGER, 0o40775)
200-
if ownership:
201-
change_owner(ETC_INTELMQ_MANAGER, group='intelmq')
202-
203-
base = Path(pkg_resources.resource_filename('intelmq_api', '')).parent
204-
api_config = base / 'etc/intelmq/api-config.json'
205-
etc_intelmq_config = ETC_INTELMQ / 'api-config.json'
206-
api_sudoers = base / 'etc/intelmq/api-sudoers.conf'
207-
etc_sudoers_api = Path('/etc/sudoers.d/01_intelmq-api') # same path as used in the packages
208-
api_manager_positions = base / 'etc/intelmq/manager/positions.conf'
209-
etc_intelmq_manager_positions = ETC_INTELMQ_MANAGER / 'positions.conf'
210-
211-
if not base.as_posix().startswith('/usr/'):
212-
# Paths differ in editable installations
213-
print(red("Detected an editable (egg-link) pip-installation of 'intelmq-api'. Some feature of this program may not work."))
214-
215-
if api_config.exists() and not etc_intelmq_config.exists():
216-
shutil.copy(api_config, etc_intelmq_config)
217-
print(f'Copied {api_config!s} to {ETC_INTELMQ!s}.')
218-
elif not api_config.exists() and not etc_intelmq_config.exists():
219-
print(red(f'Unable to install api-config.json: Neither {api_config!s} nor {etc_intelmq_config!s} exists.'))
220-
if api_sudoers.exists() and not etc_sudoers_api.exists():
221-
with open(api_sudoers) as sudoers:
222-
original_sudoers = sudoers.read()
223-
sudoers = original_sudoers.replace('www-data', webserver_user)
224-
with NamedTemporaryFile(mode='w') as tmp_file:
225-
tmp_file.write(sudoers)
226-
tmp_file.flush()
227-
try:
228-
run(('visudo', '-c', tmp_file.name))
229-
except CalledProcessError:
230-
sys.exit(red('Fatal error: Validation of adapted sudoers-file failed. Please report this bug.'))
231-
change_owner(tmp_file.name, owner='root', group='root', log=False)
232-
Path(tmp_file.name).chmod(0o440)
233-
shutil.copy(tmp_file.name, etc_sudoers_api)
234-
print(f'Copied {api_sudoers!s} to {etc_sudoers_api!s}.')
235-
elif not api_sudoers.exists() and not etc_sudoers_api.exists():
236-
print(red(f'Unable to install api-sudoers.conf: Neither {api_sudoers!s} nor {etc_sudoers_api!s} exists.'))
237-
if api_manager_positions.exists() and not etc_intelmq_manager_positions.exists():
238-
shutil.copy(api_manager_positions, etc_intelmq_manager_positions)
239-
print(f'Copied {api_manager_positions!s} to {etc_intelmq_manager_positions!s}.')
240-
etc_intelmq_manager_positions.chmod(0o664)
241-
change_owner(etc_intelmq_manager_positions, owner='intelmq', group='intelmq', log=False)
242-
elif not api_manager_positions.exists() and not etc_intelmq_manager_positions.exists():
243-
print(red(f'Unable to install positions.conf: Neither {api_manager_positions!s} nor {etc_intelmq_manager_positions!s} exists.'))
244-
245-
if webserver_user not in intelmq_group.gr_mem:
246-
sys.exit(red(f"Webserver user {webserver_user} is not a member of the 'intelmq' group. "
247-
f"Please add it with: 'usermod -aG intelmq {webserver_user}'."))
248-
249-
250-
def intelmqsetup_api_webserver_configuration(webserver_configuration_directory: Optional[str] = None):
251-
webserver_configuration_dir = webserver_configuration_directory or find_webserver_configuration_directory()
252-
api_config = Path(pkg_resources.resource_filename('intelmq_api', '')).parent / 'etc/intelmq/api-apache.conf'
253-
apache_api_config = webserver_configuration_dir / 'api-apache.conf'
254-
if api_config.exists() and not apache_api_config.exists():
255-
shutil.copy(api_config, apache_api_config)
256-
print(f'Copied {api_config!s} to {ETC_INTELMQ!s}.')
257-
debian_activate_apache_config('api-apache.conf')
258-
259-
global NOTE_WEBSERVER_RELOAD
260-
NOTE_WEBSERVER_RELOAD = True
261-
elif not api_config.exists() and not apache_api_config.exists():
262-
print(red(f'Unable to install webserver configuration api-config.conf: Neither {api_config!s} nor {apache_api_config!s} exists.'))
263-
264-
print('Setup of intelmq-api successful.')
265-
266-
267-
def intelmqsetup_manager_webserver_configuration(webserver_configuration_directory: Optional[str] = None):
268-
webserver_configuration_dir = webserver_configuration_directory or find_webserver_configuration_directory()
269-
manager_config_1 = Path(pkg_resources.resource_filename('intelmq_manager', '')).parent / 'etc/intelmq/manager-apache.conf'
270-
# IntelMQ Manager >= 3.1.0
271-
manager_config_2 = Path(pkg_resources.resource_filename('intelmq_manager', '')) / 'manager-apache.conf'
272-
manager_config = manager_config_2 if manager_config_2.exists() else manager_config_1
273-
apache_manager_config = webserver_configuration_dir / 'manager-apache.conf'
274-
if manager_config.exists() and not apache_manager_config.exists():
275-
shutil.copy(manager_config, apache_manager_config)
276-
print(f'Copied {manager_config!s} to {apache_manager_config!s}.')
277-
debian_activate_apache_config('manager-apache.conf')
278-
279-
global NOTE_WEBSERVER_RELOAD
280-
NOTE_WEBSERVER_RELOAD = True
281-
elif not manager_config.exists() and not apache_manager_config.exists():
282-
print(red(f'Unable to install webserver configuration manager-config.conf: Neither {manager_config_1!s} nor {manager_config_2!s} nor {apache_manager_config!s} exist.'))
283-
284-
285-
def intelmqsetup_manager_generate():
286-
if not intelmq_manager_has_build:
287-
print('Unable to build intelmq-manager files. Installed version of intelmq-manager is too old, at least version 3.1.0 is required.',
288-
file=sys.stderr)
289-
return
290-
src_dir = Path(pkg_resources.resource_filename('intelmq_manager', ''))
291-
html_dir_destination = Path('/usr/share/intelmq_manager/html')
292-
293-
if not src_dir.as_posix().startswith('/usr/'):
294-
# Paths differ in editable installations
295-
print(red("Detected an editable (egg-link) pip-installation of intelmq-manager. Some features of this program may not work."))
296-
297-
intelmq_manager.build.buildhtml(html_dir_destination)
298-
299-
300128
def main():
301129
parser = argparse.ArgumentParser("Set's up directories and example "
302130
"configurations for IntelMQ.")
@@ -306,44 +134,23 @@ def main():
306134
help='The state file location to use.',
307135
default=STATE_FILE_PATH)
308136
parser.add_argument('--webserver-user',
309-
help='The webserver to use instead of auto-detection.')
137+
help='(DEPRECATED) The webserver to use instead of auto-detection.')
310138
parser.add_argument('--webserver-configuration-directory',
311-
help='The webserver configuration directory to use instead of auto-detection.')
139+
help='(DEPRECATED) The webserver configuration directory to use instead of auto-detection.')
312140
parser.add_argument('--skip-api',
313-
help='Skip set-up of intelmq-api.',
141+
help='(DEPRECATED) Skip set-up of intelmq-api.',
314142
action='store_true')
315143
parser.add_argument('--skip-webserver',
316-
help='Skip all operations on the webserver configuration, affects the API and Manager.',
144+
help='(DEPRECATED) Skip all operations on the webserver configuration, affects the API and Manager.',
317145
action='store_true')
318146
parser.add_argument('--skip-manager',
319-
help='Skip set-up of intelmq-manager.',
147+
help='(DEPRECATED) Skip set-up of intelmq-manager.',
320148
action='store_true')
321149
args = parser.parse_args()
322150

323151
basic_checks(skip_ownership=args.skip_ownership)
324152
intelmqsetup_core(ownership=not args.skip_ownership,
325153
state_file=args.state_file)
326-
if intelmq_api and not args.skip_api:
327-
print(f'Running setup for intelmq-api (version {intelmq_api.version.__version__}).')
328-
intelmqsetup_api(ownership=not args.skip_ownership,
329-
webserver_user=args.webserver_user)
330-
if not args.skip_webserver:
331-
print('Running webserver setup for intelmq-api.')
332-
intelmqsetup_api_webserver_configuration(webserver_configuration_directory=args.webserver_configuration_directory)
333-
else:
334-
print('Skipping set-up of intelmq-api.')
335-
if intelmq_manager and not args.skip_manager and not args.skip_webserver:
336-
print('Running webserver setup for intelmq-manager.')
337-
intelmqsetup_manager_webserver_configuration(webserver_configuration_directory=args.webserver_configuration_directory)
338-
else:
339-
print('Skipping intelmq-manager configuration.')
340-
if intelmq_manager and not args.skip_manager:
341-
manager_version = pkg_resources.get_distribution('intelmq-manager').version
342-
print(f'Generate and save intelmq-manager (version {manager_version}) static files.')
343-
intelmqsetup_manager_generate()
344-
345-
if NOTE_WEBSERVER_RELOAD:
346-
print('Reload the webserver to make the changes effective.')
347154

348155
print("'intelmqsetup' completed.")
349156

0 commit comments

Comments
 (0)