Skip to content

Commit

Permalink
update_image enhancement side-car-container image update
Browse files Browse the repository at this point in the history
  • Loading branch information
shantanuduplo committed Feb 5, 2025
1 parent c7179be commit 43bb4a3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
51 changes: 34 additions & 17 deletions src/duplo_resource/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ def update_replicas(self,
@Command()
def update_image(self,
name: args.NAME,
image: args.IMAGE,
container: args.CONTAINER = None,
image: args.IMAGE = None,
wait: args.WAIT = False) -> dict:
"""Update the image of a service.
Expand All @@ -231,37 +232,53 @@ def update_image(self,
Usage: Basic CLI Use
```sh
duploctl service update_image <service-name> <image-name>
duploctl service update_image <service-name> --container <side-car-container> <image-name>
```
Args:
name: The name of the service to update.
image: The new image to use for the service.
container: The name of the sidecar container to update image.
Returns:
message: Success message
"""
service = self.find(name)
current_image = self.image_from_body(service)
data = {
"Name": name,
"Image": image,
"AllocationTags": service["Template"].get("AllocationTags", "")
}

# needed before the update so we know how many pods to wait for
if wait:
service["Replicaset"] = self.current_replicaset(name)
data = {}

if container:
other_docker_config = loads(service["Template"]["OtherDockerConfig"])
additional_containers = other_docker_config.get("additionalContainers", [])

container_found = False
for c in additional_containers:
if c["name"] == container:
c["image"] = image
container_found = True
break

if not container_found:
return {"error": f"Side-car container '{container}' not found in service '{name}'"}

data = {
"Name": name,
"OtherDockerConfig": dumps(other_docker_config),
"AllocationTags": service["Template"].get("AllocationTags", "")
}

# if the tag is the same, reboot to pull new code, otherwise actual update
if(current_image == image):
self.duplo.post(self.endpoint(f"ReplicationControllerReboot/{name}"))
else:
self.duplo.post(self.endpoint("ReplicationControllerChange"), data)

data = {
"Name": name,
"Image": image,
"AllocationTags": service["Template"].get("AllocationTags", "")
}

self.duplo.post(self.endpoint("ReplicationControllerChange"), data)

if wait:
self.wait(service, data)

return {"message": f"Successfully updated image for service '{name}'"}
return {"message": f"Successfully updated image for {'container ' + container if container else 'service'} '{name}'"}

@Command()
def update_env(self,
Expand Down
3 changes: 3 additions & 0 deletions src/duplocloud/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@
CONTAINER_PORT = Arg("container-port", "--container-port",
help='Container port')

CONTAINER = Arg("container", "--container", "-C",
help='The side-car container name')

EXTERNAL_PORT = Arg("external-port", "--external-port",
help='The external port')

Expand Down

0 comments on commit 43bb4a3

Please sign in to comment.