Skip to content

Commit 5affe86

Browse files
GitHKAndrei Neagu
andauthored
✨ Adds RPC interface for containers routes (#8227)
Co-authored-by: Andrei Neagu <[email protected]>
1 parent 1df610c commit 5affe86

File tree

21 files changed

+1438
-397
lines changed

21 files changed

+1438
-397
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import logging
2+
3+
from models_library.projects_nodes_io import NodeID
4+
from models_library.rabbitmq_basic_types import RPCMethodName
5+
from models_library.services import ServiceOutput
6+
from pydantic import TypeAdapter
7+
8+
from ....logging_utils import log_decorator
9+
from ... import RabbitMQRPCClient
10+
from ._utils import get_rpc_namespace
11+
12+
_logger = logging.getLogger(__name__)
13+
14+
15+
@log_decorator(_logger, level=logging.DEBUG)
16+
async def toggle_ports_io(
17+
rabbitmq_rpc_client: RabbitMQRPCClient,
18+
*,
19+
node_id: NodeID,
20+
enable_outputs: bool,
21+
enable_inputs: bool
22+
) -> None:
23+
rpc_namespace = get_rpc_namespace(node_id)
24+
result = await rabbitmq_rpc_client.request(
25+
rpc_namespace,
26+
TypeAdapter(RPCMethodName).validate_python("toggle_ports_io"),
27+
enable_outputs=enable_outputs,
28+
enable_inputs=enable_inputs,
29+
)
30+
assert result is None # nosec
31+
32+
33+
@log_decorator(_logger, level=logging.DEBUG)
34+
async def create_output_dirs(
35+
rabbitmq_rpc_client: RabbitMQRPCClient,
36+
*,
37+
node_id: NodeID,
38+
outputs_labels: dict[str, ServiceOutput]
39+
) -> None:
40+
rpc_namespace = get_rpc_namespace(node_id)
41+
result = await rabbitmq_rpc_client.request(
42+
rpc_namespace,
43+
TypeAdapter(RPCMethodName).validate_python("create_output_dirs"),
44+
outputs_labels=outputs_labels,
45+
)
46+
assert result is None # nosec
47+
48+
49+
@log_decorator(_logger, level=logging.DEBUG)
50+
async def attach_container_to_network(
51+
rabbitmq_rpc_client: RabbitMQRPCClient,
52+
*,
53+
node_id: NodeID,
54+
container_id: str,
55+
network_id: str,
56+
network_aliases: list[str]
57+
) -> None:
58+
rpc_namespace = get_rpc_namespace(node_id)
59+
result = await rabbitmq_rpc_client.request(
60+
rpc_namespace,
61+
TypeAdapter(RPCMethodName).validate_python("attach_container_to_network"),
62+
container_id=container_id,
63+
network_id=network_id,
64+
network_aliases=network_aliases,
65+
)
66+
assert result is None # nosec
67+
68+
69+
@log_decorator(_logger, level=logging.DEBUG)
70+
async def detach_container_from_network(
71+
rabbitmq_rpc_client: RabbitMQRPCClient,
72+
*,
73+
node_id: NodeID,
74+
container_id: str,
75+
network_id: str
76+
) -> None:
77+
rpc_namespace = get_rpc_namespace(node_id)
78+
result = await rabbitmq_rpc_client.request(
79+
rpc_namespace,
80+
TypeAdapter(RPCMethodName).validate_python("detach_container_from_network"),
81+
container_id=container_id,
82+
network_id=network_id,
83+
)
84+
assert result is None # nosec

packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/dynamic_sidecar/containers.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
2+
from typing import Any
23

34
from models_library.api_schemas_directorv2.dynamic_services import ContainersComposeSpec
5+
from models_library.api_schemas_dynamic_sidecar.containers import ActivityInfoOrNone
46
from models_library.projects_nodes_io import NodeID
57
from models_library.rabbitmq_basic_types import RPCMethodName
68
from pydantic import TypeAdapter
@@ -26,3 +28,60 @@ async def create_compose_spec(
2628
containers_compose_spec=containers_compose_spec,
2729
)
2830
assert result is None # nosec
31+
32+
33+
@log_decorator(_logger, level=logging.DEBUG)
34+
async def containers_docker_inspect(
35+
rabbitmq_rpc_client: RabbitMQRPCClient,
36+
*,
37+
node_id: NodeID,
38+
only_status: bool,
39+
) -> dict[str, Any]:
40+
rpc_namespace = get_rpc_namespace(node_id)
41+
result = await rabbitmq_rpc_client.request(
42+
rpc_namespace,
43+
TypeAdapter(RPCMethodName).validate_python("containers_docker_inspect"),
44+
only_status=only_status,
45+
)
46+
assert isinstance(result, dict) # nosec
47+
return result
48+
49+
50+
@log_decorator(_logger, level=logging.DEBUG)
51+
async def get_containers_activity(
52+
rabbitmq_rpc_client: RabbitMQRPCClient, *, node_id: NodeID
53+
) -> ActivityInfoOrNone:
54+
rpc_namespace = get_rpc_namespace(node_id)
55+
result = await rabbitmq_rpc_client.request(
56+
rpc_namespace,
57+
TypeAdapter(RPCMethodName).validate_python("get_containers_activity"),
58+
)
59+
return TypeAdapter(ActivityInfoOrNone).validate_python(result) if result else None
60+
61+
62+
@log_decorator(_logger, level=logging.DEBUG)
63+
async def get_containers_name(
64+
rabbitmq_rpc_client: RabbitMQRPCClient, *, node_id: NodeID, filters: str
65+
) -> str:
66+
rpc_namespace = get_rpc_namespace(node_id)
67+
result = await rabbitmq_rpc_client.request(
68+
rpc_namespace,
69+
TypeAdapter(RPCMethodName).validate_python("get_containers_name"),
70+
filters=filters,
71+
)
72+
assert isinstance(result, str)
73+
return result
74+
75+
76+
@log_decorator(_logger, level=logging.DEBUG)
77+
async def inspect_container(
78+
rabbitmq_rpc_client: RabbitMQRPCClient, *, node_id: NodeID, container_id: str
79+
) -> dict[str, Any]:
80+
rpc_namespace = get_rpc_namespace(node_id)
81+
result = await rabbitmq_rpc_client.request(
82+
rpc_namespace,
83+
TypeAdapter(RPCMethodName).validate_python("inspect_container"),
84+
container_id=container_id,
85+
)
86+
assert isinstance(result, dict)
87+
return result

services/dynamic-sidecar/openapi.json

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@
7272
"tags": [
7373
"containers"
7474
],
75-
"summary": "Store Compose Spec",
75+
"summary": "Create Compose Spec",
7676
"description": "Validates and stores the docker compose spec for the user services.",
77-
"operationId": "store_compose_spec_v1_containers_compose_spec_post",
77+
"operationId": "create_compose_spec_v1_containers_compose_spec_post",
7878
"requestBody": {
7979
"content": {
8080
"application/json": {
@@ -164,7 +164,8 @@
164164
"tags": [
165165
"containers"
166166
],
167-
"summary": "Starts the containers as defined in ContainerCreate by:\n- cleaning up resources from previous runs if any\n- starting the containers\n\nProgress may be obtained through URL\nProcess may be cancelled through URL",
167+
"summary": "Create Containers",
168+
"description": "Starts the containers as defined in ContainerCreate by:\n- cleaning up resources from previous runs if any\n- starting the containers\n\nProgress may be obtained through URL\nProcess may be cancelled through URL",
168169
"operationId": "create_containers_v1_containers_post",
169170
"requestBody": {
170171
"required": true,
@@ -207,6 +208,7 @@
207208
"containers"
208209
],
209210
"summary": "Get Containers Activity",
211+
"description": "If user service declared an inactivity hook, this endpoint provides\ninformation about how much time has passed since the service became inactive.",
210212
"operationId": "get_containers_activity_v1_containers_activity_get",
211213
"responses": {
212214
"200": {
@@ -336,7 +338,8 @@
336338
"tags": [
337339
"containers"
338340
],
339-
"summary": "Enable/disable ports i/o",
341+
"summary": "Toggle Ports Io",
342+
"description": "Enable/disable ports i/o",
340343
"operationId": "toggle_ports_io_v1_containers_ports_io_patch",
341344
"requestBody": {
342345
"content": {
@@ -370,7 +373,8 @@
370373
"tags": [
371374
"containers"
372375
],
373-
"summary": "Creates the output directories declared by the docker images's labels. It is more convenient to pass the labels from director-v2, since it already has all the machinery to call into director-v0 to retrieve them.",
376+
"summary": "Create Output Dirs",
377+
"description": "Creates the output directories declared by the docker images's labels.\nIt is more convenient to pass the labels from director-v2,\nsince it already has all the machinery to call into director-v0\nto retrieve them.",
374378
"operationId": "create_output_dirs_v1_containers_ports_outputs_dirs_post",
375379
"requestBody": {
376380
"content": {
@@ -404,7 +408,8 @@
404408
"tags": [
405409
"containers"
406410
],
407-
"summary": "attach container to a network, if not already attached",
411+
"summary": "Attach Container To Network",
412+
"description": "attach container to a network, if not already attached",
408413
"operationId": "attach_container_to_network_v1_containers__id__networks_attach_post",
409414
"parameters": [
410415
{
@@ -449,7 +454,8 @@
449454
"tags": [
450455
"containers"
451456
],
452-
"summary": "detach container from a network, if not already detached",
457+
"summary": "Detach Container From Network",
458+
"description": "detach container from a network, if not already detached",
453459
"operationId": "detach_container_from_network_v1_containers__id__networks_detach_post",
454460
"parameters": [
455461
{
@@ -494,7 +500,8 @@
494500
"tags": [
495501
"containers"
496502
],
497-
"summary": "Pulls all the docker container images for the user services",
503+
"summary": "Pull Container Images",
504+
"description": "Pulls all the docker container images for the user services",
498505
"operationId": "pull_container_images_v1_containers_images_pull_post",
499506
"responses": {
500507
"202": {
@@ -516,7 +523,8 @@
516523
"tags": [
517524
"containers"
518525
],
519-
"summary": "Remove the previously started containers",
526+
"summary": "Down Containers",
527+
"description": "Remove the previously started containers",
520528
"operationId": "down_containers_v1_containers_down_post",
521529
"responses": {
522530
"202": {
@@ -538,7 +546,8 @@
538546
"tags": [
539547
"containers"
540548
],
541-
"summary": "Restores the state of the dynamic service",
549+
"summary": "Restore Containers State Paths",
550+
"description": "Restores the state of the dynamic service",
542551
"operationId": "restore_containers_state_paths_v1_containers_state_restore_post",
543552
"responses": {
544553
"202": {
@@ -560,7 +569,8 @@
560569
"tags": [
561570
"containers"
562571
],
563-
"summary": "Stores the state of the dynamic service",
572+
"summary": "Save Containers State Paths",
573+
"description": "Stores the state of the dynamic service",
564574
"operationId": "save_containers_state_paths_v1_containers_state_save_post",
565575
"responses": {
566576
"202": {
@@ -582,7 +592,8 @@
582592
"tags": [
583593
"containers"
584594
],
585-
"summary": "Pull input ports data",
595+
"summary": "Pull Container Port Inputs",
596+
"description": "Pull input ports data",
586597
"operationId": "pull_container_port_inputs_v1_containers_ports_inputs_pull_post",
587598
"requestBody": {
588599
"content": {
@@ -634,7 +645,8 @@
634645
"tags": [
635646
"containers"
636647
],
637-
"summary": "Pull output ports data",
648+
"summary": "Pull Container Port Outputs",
649+
"description": "Pull output ports data",
638650
"operationId": "pull_container_port_outputs_v1_containers_ports_outputs_pull_post",
639651
"requestBody": {
640652
"content": {
@@ -686,7 +698,8 @@
686698
"tags": [
687699
"containers"
688700
],
689-
"summary": "Push output ports data",
701+
"summary": "Push Container Port Outputs",
702+
"description": "Push output ports data",
690703
"operationId": "push_container_port_outputs_v1_containers_ports_outputs_push_post",
691704
"responses": {
692705
"202": {
@@ -708,7 +721,8 @@
708721
"tags": [
709722
"containers"
710723
],
711-
"summary": "Restarts previously started user services",
724+
"summary": "Restart Containers",
725+
"description": "Restarts previously started user services",
712726
"operationId": "restart_containers_v1_containers_restart_post",
713727
"responses": {
714728
"202": {
@@ -730,7 +744,8 @@
730744
"tags": [
731745
"volumes"
732746
],
733-
"summary": "Updates the state of the volume",
747+
"summary": "Put Volume State",
748+
"description": "Updates the state of the volume",
734749
"operationId": "put_volume_state_v1_volumes__id__put",
735750
"parameters": [
736751
{
@@ -774,7 +789,8 @@
774789
"tags": [
775790
"disk"
776791
],
777-
"summary": "Frees up reserved disk space",
792+
"summary": "Free Reserved Disk Space",
793+
"description": "Frees up reserved disk space",
778794
"operationId": "free_reserved_disk_space_v1_disk_reserved_free_post",
779795
"responses": {
780796
"204": {

0 commit comments

Comments
 (0)