Skip to content

Commit 82d957f

Browse files
authored
Remove the need for Zocalo configuration file (#431)
* Directly pass configuration file from Murfey security configuration to workflows transport object rather than relying on the Zocalo configuration * Setup graylog logging outside of Zocalo configuration This should remove the need for the ZOCALO_CONFIG environment variable to be set when starting a Murfey server * Add rabbitmq_credentials default for tests
1 parent 67e1bdf commit 82d957f

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/murfey/server/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
from threading import Thread
1414
from typing import Any, Dict, List, NamedTuple, Tuple
1515

16+
import graypy
1617
import mrcfile
1718
import numpy as np
1819
import uvicorn
1920
import workflows
20-
import zocalo.configuration
2121
from backports.entry_points_selectable import entry_points
2222
from fastapi import Request
2323
from fastapi.templating import Jinja2Templates
@@ -274,17 +274,19 @@ def run():
274274
default=0,
275275
)
276276

277+
security_config = get_security_config()
277278
# setup logging
278-
zc = zocalo.configuration.from_file()
279-
zc.activate()
279+
if security_config.graylog_host:
280+
handler = graypy.GELFUDPHandler(
281+
security_config.graylog_host, security_config.graylog_port, level_names=True
282+
)
283+
root_logger = logging.getLogger()
284+
root_logger.addHandler(handler)
280285

281286
# Install a log filter to all existing handlers.
282-
# At this stage this will exclude console loggers, but will cover
283-
# any Graylog logging set up by the environment activation
284287
LogFilter.install()
285288

286-
zc.add_command_line_options(parser)
287-
workflows.transport.add_command_line_options(parser, transport_argument=True)
289+
workflows.transport.load_configuration_file(security_config.rabbitmq_credentials)
288290

289291
args = parser.parse_args()
290292

@@ -297,7 +299,6 @@ def run():
297299
# Set up logging now that the desired verbosity is known
298300
_set_up_logging(quiet=args.quiet, verbosity=args.verbose)
299301

300-
security_config = get_security_config()
301302
if not args.temporary and _transport_object:
302303
_transport_object.feedback_queue = security_config.feedback_queue
303304
rabbit_thread = Thread(

src/murfey/util/config.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import yaml
1010
from backports.entry_points_selectable import entry_points
11-
from pydantic import BaseModel, BaseSettings, Extra
11+
from pydantic import BaseModel, BaseSettings, Extra, validator
1212

1313

1414
class MachineConfig(BaseModel, extra=Extra.allow): # type: ignore
@@ -83,6 +83,7 @@ def from_file(config_file_path: Path, instrument: str = "") -> Dict[str, Machine
8383

8484

8585
class Security(BaseModel):
86+
rabbitmq_credentials: str
8687
murfey_db_credentials: str
8788
crypto_key: str
8889
auth_key: str = ""
@@ -94,6 +95,16 @@ class Security(BaseModel):
9495
auth_type: Literal["password", "cookie"] = "password"
9596
cookie_key: str = ""
9697
feedback_queue: str = "murfey_feedback"
98+
graylog_host: str = ""
99+
graylog_port: Optional[int] = None
100+
101+
@validator("graylog_port")
102+
def check_port_present_if_host_is(
103+
cls, v: Optional[int], values: dict, **kwargs
104+
) -> Optional[int]:
105+
if values["graylog_host"] and v is None:
106+
raise ValueError("The Graylog port must be set if the Graylog host is")
107+
return v
97108

98109

99110
def security_from_file(config_file_path: Path) -> Security:
@@ -134,6 +145,7 @@ def get_security_config() -> Security:
134145
if machine_config.security_configuration_path:
135146
return security_from_file(machine_config.security_configuration_path)
136147
return Security(
148+
rabbitmq_credentials="",
137149
session_validation="",
138150
murfey_db_credentials="",
139151
crypto_key="",

0 commit comments

Comments
 (0)