Skip to content

Commit 7a2f04d

Browse files
committed
handle detach for swarm containers without replica
1 parent fef20c2 commit 7a2f04d

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

app/main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,14 @@ async def execute_docker_commands():
122122
for service_json in services_json:
123123
image = service_json.Image
124124
name = service_json.Name
125+
replicas_is = service_json.ReplicasIs
126+
detach = "true" if replicas_is == 0 else "false"
127+
125128
await run_command(
126129
(
127130
"docker service update "
128-
f"--image {image} {registry_auth} --force {name}"
131+
f"--image {image} {registry_auth} "
132+
f"--force {name} --detach={detach}"
129133
)
130134
)
131135

app/src/types.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import Optional
44

5-
from pydantic import BaseModel
5+
from pydantic import BaseModel, Field, model_validator
66

77

88
class ReturnMessage(BaseModel):
@@ -27,11 +27,27 @@ class SwarmRequestData(BaseModel):
2727

2828

2929
class ServiceJsonType(BaseModel):
30-
"""describes a response type for services"""
30+
"""Describes a response type for services"""
3131

3232
ID: str
3333
Image: str
3434
Mode: str
3535
Name: str
3636
Ports: str
3737
Replicas: str
38+
39+
ReplicasIs: Optional[int] = Field(default=None, exclude=True)
40+
ReplicasShould: Optional[int] = Field(default=None, exclude=True)
41+
42+
@model_validator(mode="after")
43+
def parse_replicas(self):
44+
"""split replicas into ints"""
45+
try:
46+
is_, should = self.Replicas.split("/")
47+
self.ReplicasIs = int(is_)
48+
self.ReplicasShould = int(should)
49+
except Exception as exc:
50+
raise ValueError(
51+
f"Invalid Replicas format: {self.Replicas}"
52+
) from exc
53+
return self

0 commit comments

Comments
 (0)