Skip to content

Commit 78569c9

Browse files
authored
Refactor to allow Murfey servers to operate for any instrument in configuration (#352)
Also allows one server to use another for auth if configured. This will allow the use of only a single central server for auth
1 parent 16cf40c commit 78569c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1324
-609
lines changed

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ developer = [
5555
server = [
5656
# "matplotlib", # For visual statistical analysis of images
5757
"aiohttp",
58+
"backports.entry_points_selectable",
5859
"cryptography",
5960
"fastapi[standard]",
6061
"ispyb", # Responsible for setting requirements for SQLAlchemy and mysql-connector-python; v10.0.0: sqlalchemy <2, mysql-connector-python >=8.0.32
@@ -84,14 +85,15 @@ murfey = "murfey.client:run"
8485
"murfey.decrypt_password" = "murfey.cli.decrypt_db_password:run"
8586
"murfey.generate_key" = "murfey.cli.generate_crypto_key:run"
8687
"murfey.generate_password" = "murfey.cli.generate_db_password:run"
87-
"murfey.hub" = "murfey.hub:run"
8888
"murfey.instrument_server" = "murfey.instrument_server:run"
8989
"murfey.server" = "murfey.server:run"
9090
"murfey.sessions" = "murfey.cli.db_sessions:run"
9191
"murfey.simulate" = "murfey.cli.dummy:run"
9292
"murfey.spa_inject" = "murfey.cli.inject_spa_processing:run"
9393
"murfey.spa_ispyb_entries" = "murfey.cli.spa_ispyb_messages:run"
9494
"murfey.transfer" = "murfey.cli.transfer:run"
95+
[project.entry-points."murfey.auth.token_validation"]
96+
"password" = "murfey.server.api.auth:password_token_validation"
9597
[project.entry-points."murfey.workflows"]
9698
"lif_to_stack" = "murfey.workflows.lif_to_stack:zocalo_cluster_request"
9799
"tiff_to_stack" = "murfey.workflows.tiff_to_stack:zocalo_cluster_request"

src/murfey/cli/add_user.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import argparse
2-
import os
32

43
from sqlmodel import Session, create_engine
54

65
from murfey.server.api.auth import hash_password
76
from murfey.server.murfey_db import url
8-
from murfey.util.config import get_machine_config
7+
from murfey.util.config import get_security_config
98
from murfey.util.db import MurfeyUser as User
109

1110

@@ -16,23 +15,13 @@ def run():
1615

1716
parser.add_argument("-u", "--username", type=str, help="User name for new user")
1817
parser.add_argument("-p", "--password", type=str, help="Password for new user")
19-
parser.add_argument(
20-
"-m",
21-
"--microscope",
22-
dest="microscope",
23-
type=str,
24-
default="",
25-
help="Microscope as specified in the Murfey machine configuration",
26-
)
2718

2819
args = parser.parse_args()
29-
if args.microscope:
30-
os.environ["BEAMLINE"] = args.microscope
3120

3221
new_user = User(
3322
username=args.username, hashed_password=hash_password(args.password)
3423
)
35-
_url = url(get_machine_config())
24+
_url = url(get_security_config())
3625
engine = create_engine(_url)
3726
with Session(engine) as murfey_db:
3827
murfey_db.add(new_user)

src/murfey/cli/create_db.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import argparse
2-
import os
32

43
from murfey.util.db import clear, setup
54

@@ -16,18 +15,8 @@ def run():
1615
action="store_false",
1716
help="Do not clear current database tables before creating specified tables",
1817
)
19-
parser.add_argument(
20-
"-m",
21-
"--microscope",
22-
dest="microscope",
23-
type=str,
24-
default="",
25-
help="Microscope as specified in the Murfey machine configuration",
26-
)
2718

2819
args = parser.parse_args()
29-
if args.microscope:
30-
os.environ["BEAMLINE"] = args.microscope
3120

3221
from murfey.server.murfey_db import url
3322

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
import argparse
2-
import os
32

43
from cryptography.fernet import Fernet
54

6-
from murfey.util.config import get_machine_config
5+
from murfey.util.config import get_security_config
76

87

98
def run():
109
parser = argparse.ArgumentParser(description="Decrypt Murfey database password")
1110

1211
parser.add_argument(nargs="?", dest="password", help="Password to decrypt")
13-
parser.add_argument(
14-
"-m",
15-
"--microscope",
16-
dest="microscope",
17-
type=str,
18-
default="",
19-
help="Microscope as specified in the Murfey machine configuration",
20-
)
2112

2213
args = parser.parse_args()
2314

24-
if args.microscope:
25-
os.environ["BEAMLINE"] = args.microscope
26-
27-
machine_config = get_machine_config()
28-
f = Fernet(machine_config.crypto_key.encode("ascii"))
15+
security_config = get_security_config()
16+
f = Fernet(security_config.crypto_key.encode("ascii"))
2917
print(f.decrypt(args.password.encode("ascii")).decode())

src/murfey/cli/generate_db_password.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
from cryptography.fernet import Fernet
55

6-
from murfey.util.config import get_machine_config
6+
from murfey.util.config import get_security_config
77

88

99
def run():
10-
machine_config = get_machine_config()
11-
f = Fernet(machine_config.crypto_key.encode("ascii"))
10+
security_config = get_security_config()
11+
f = Fernet(security_config.crypto_key.encode("ascii"))
1212
alphabet = string.ascii_letters + string.digits
1313
password = "".join(secrets.choice(alphabet) for i in range(32))
1414
print(f.encrypt(password.encode("ascii")).decode())

src/murfey/client/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def _check_for_updates(
100100

101101
def run():
102102
config = read_config()
103+
instrument_name = config["Murfey"]["instrument_name"]
103104
try:
104105
server_routing = config["ServerRouter"]
105106
except KeyError:
@@ -257,7 +258,7 @@ def run():
257258
break
258259
if not ongoing_visits:
259260
print("Ongoing visits:")
260-
ongoing_visits = _get_visit_list(murfey_url)
261+
ongoing_visits = _get_visit_list(murfey_url, instrument_name)
261262
pprint(ongoing_visits)
262263
ongoing_visits = [v.name for v in ongoing_visits]
263264

@@ -293,6 +294,7 @@ def run():
293294
instance_environment = MurfeyInstanceEnvironment(
294295
url=murfey_url,
295296
client_id=ws.id,
297+
instrument_name=instrument_name,
296298
software_versions=machine_data.get("software_versions", {}),
297299
# sources=[Path(args.source)],
298300
# watchers=source_watchers,

src/murfey/client/analyser.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ def __init__(
7373
self._stopping = False
7474
self._halt_thread = False
7575
self._murfey_config = (
76-
get_machine_config(str(environment.url.geturl()), demo=environment.demo)
76+
get_machine_config(
77+
str(environment.url.geturl()),
78+
instrument_name=environment.instrument_name,
79+
demo=environment.demo,
80+
)
7781
if environment
7882
else {}
7983
)
@@ -135,6 +139,7 @@ def _find_context(self, file_path: Path) -> bool:
135139
created_directories = set(
136140
get_machine_config(
137141
str(self._environment.url.geturl()),
142+
instrument_name=self._environment.instrument_name,
138143
demo=self._environment.demo,
139144
).get("analyse_created_directories", [])
140145
)
@@ -154,6 +159,7 @@ def _find_context(self, file_path: Path) -> bool:
154159
try:
155160
cfg = get_machine_config(
156161
str(self._environment.url.geturl()),
162+
instrument_name=self._environment.instrument_name,
157163
demo=self._environment.demo,
158164
)
159165
except Exception as e:

src/murfey/client/contexts/fib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def post_transfer(
9898
).name
9999
# post gif list to gif making API call
100100
requests.post(
101-
f"{str(environment.url.geturl())}/visits/{datetime.now().year}/{environment.visit}/make_milling_gif",
101+
f"{str(environment.url.geturl())}/visits/{datetime.now().year}/{environment.visit}/{environment.murfey_session}/make_milling_gif",
102102
json={
103103
"lamella_number": lamella_number,
104104
"images": gif_list,

src/murfey/client/contexts/spa.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ def _file_transferred_to(
106106
environment: MurfeyInstanceEnvironment, source: Path, file_path: Path
107107
):
108108
machine_config = get_machine_config(
109-
str(environment.url.geturl()), demo=environment.demo
109+
str(environment.url.geturl()),
110+
instrument_name=environment.instrument_name,
111+
demo=environment.demo,
110112
)
111113
if environment.visit in environment.default_destinations[source]:
112114
return (
@@ -160,7 +162,7 @@ def _grid_square_data(xml_path: Path, grid_square: int, session_id: int) -> Grid
160162
)
161163
if image_paths:
162164
image_paths.sort(key=lambda x: x.stat().st_ctime)
163-
image_path = image_paths[0]
165+
image_path = image_paths[-1]
164166
with open(Path(image_path).with_suffix(".xml")) as gs_xml:
165167
gs_xml_data = xmltodict.parse(gs_xml.read())
166168
readout_area = gs_xml_data["MicroscopeImage"]["microscopeData"]["acquisition"][
@@ -175,8 +177,8 @@ def _grid_square_data(xml_path: Path, grid_square: int, session_id: int) -> Grid
175177
session_id=session_id,
176178
readout_area_x=full_size[0] if image_path else None,
177179
readout_area_y=full_size[1] if image_path else None,
178-
thumbnail_size_x=None,
179-
thumbnail_size_y=None,
180+
thumbnail_size_x=int((512 / max(full_size)) * full_size[0]),
181+
thumbnail_size_y=int((512 / max(full_size)) * full_size[1]),
180182
pixel_size=float(pixel_size) if image_path else None,
181183
image=str(image_path),
182184
)
@@ -417,7 +419,7 @@ def gather_metadata(
417419
binning_factor = 1
418420
if environment:
419421
server_config_response = capture_get(
420-
f"{str(environment.url.geturl())}/machine"
422+
f"{str(environment.url.geturl())}/instruments/{environment.instrument_name}/machine"
421423
)
422424
if server_config_response is None:
423425
return None
@@ -677,7 +679,9 @@ def post_transfer(
677679
if self._acquisition_software == "epu":
678680
if environment:
679681
machine_config = get_machine_config(
680-
str(environment.url.geturl()), demo=environment.demo
682+
str(environment.url.geturl()),
683+
instrument_name=environment.instrument_name,
684+
demo=environment.demo,
681685
)
682686
else:
683687
machine_config = {}
@@ -720,7 +724,7 @@ def post_transfer(
720724
eer_fractionation_file = None
721725
if file_transferred_to.suffix == ".eer":
722726
response = capture_post(
723-
f"{str(environment.url.geturl())}/visits/{environment.visit}/eer_fractionation_file",
727+
f"{str(environment.url.geturl())}/visits/{environment.visit}/{environment.murfey_session}/eer_fractionation_file",
724728
json={
725729
"eer_path": str(file_transferred_to),
726730
"fractionation": environment.data_collection_parameters[
@@ -863,7 +867,9 @@ def _register_processing_job(
863867
environment.id_tag_registry["processing_job"].append(tag)
864868
proc_url = f"{str(environment.url.geturl())}/visits/{environment.visit}/{environment.murfey_session}/register_processing_job"
865869
machine_config = get_machine_config(
866-
str(environment.url.geturl()), demo=environment.demo
870+
str(environment.url.geturl()),
871+
instrument_name=environment.instrument_name,
872+
demo=environment.demo,
867873
)
868874
image_directory = str(
869875
Path(machine_config.get("rsync_basepath", "."))

src/murfey/client/contexts/tomo.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def _complete_process_file(
290290
eer_fractionation_file = None
291291
if environment.data_collection_parameters.get("num_eer_frames"):
292292
response = requests.post(
293-
f"{str(environment.url.geturl())}/visits/{environment.visit}/eer_fractionation_file",
293+
f"{str(environment.url.geturl())}/visits/{environment.visit}/{environment.murfey_session}/eer_fractionation_file",
294294
json={
295295
"num_frames": environment.data_collection_parameters[
296296
"num_eer_frames"
@@ -342,7 +342,9 @@ def _file_transferred_to(
342342
self, environment: MurfeyInstanceEnvironment, source: Path, file_path: Path
343343
):
344344
machine_config = get_machine_config(
345-
str(environment.url.geturl()), demo=environment.demo
345+
str(environment.url.geturl()),
346+
instrument_name=environment.instrument_name,
347+
demo=environment.demo,
346348
)
347349
if environment.visit in environment.default_destinations[source]:
348350
return (
@@ -576,7 +578,7 @@ def _add_tilt(
576578
eer_fractionation_file = None
577579
if environment.data_collection_parameters.get("num_eer_frames"):
578580
response = requests.post(
579-
f"{str(environment.url.geturl())}/visits/{environment.visit}/eer_fractionation_file",
581+
f"{str(environment.url.geturl())}/visits/{environment.visit}/{environment.murfey_session}/eer_fractionation_file",
580582
json={
581583
"num_frames": environment.data_collection_parameters[
582584
"num_eer_frames"
@@ -786,7 +788,9 @@ def post_transfer(
786788
if self._acquisition_software == "tomo":
787789
if environment:
788790
machine_config = get_machine_config(
789-
str(environment.url.geturl()), demo=environment.demo
791+
str(environment.url.geturl()),
792+
instrument_name=environment.instrument_name,
793+
demo=environment.demo,
790794
)
791795
else:
792796
machine_config = {}
@@ -927,7 +931,7 @@ def gather_metadata(
927931
binning_factor = 1
928932
if environment:
929933
server_config = requests.get(
930-
f"{str(environment.url.geturl())}/machine"
934+
f"{str(environment.url.geturl())}/instruments/{environment.instrument_name}/machine"
931935
).json()
932936
if (
933937
server_config.get("superres")

0 commit comments

Comments
 (0)