Skip to content

Commit

Permalink
feat: Swarm cleaner now also cleans up dangling services
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelePasini committed Sep 11, 2024
1 parent 8781a52 commit 0a534ed
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
11 changes: 7 additions & 4 deletions dataplatform/images/docker-cleaner/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
FROM centos:centos7
FROM docker:stable

# Install bash
RUN apk add --no-cache bash

# Install Docker
RUN yum install -y docker
# Run the script
ADD commands.sh /usr/local/bin/shell.sh

RUN chmod 777 /usr/local/bin/shell.sh

CMD /usr/local/bin/shell.sh
CMD ["bash", "/usr/local/bin/shell.sh"]


14 changes: 13 additions & 1 deletion dataplatform/images/docker-cleaner/commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@ docker image prune -f

echo "Removing cache..."
docker builder prune -f
echo "Docker cleanup complete."
echo "Docker cleanup complete."


echo "Cleaning up old services left around the Swarm..."

docker service ls --format "{{.ID}} {{.Replicas}}" | while read service_id replicas; do
# Check if the replication factor starts with 0 (i.e., 0/x)
if [[ "$replicas" =~ ^0/ ]]; then
echo "Removing service: $service_id"
docker service rm $service_id
fi
done
echo "Succesfully cleaned Swarm services"
4 changes: 2 additions & 2 deletions dataplatform/images/docker-cleaner/docker-cleaner-dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
volumes=['/var/run/docker.sock:/var/run/docker.sock'],
mount_tmp_dir=False,
mode=ServiceMode('global'),
placement=Placement(constraints=['node.hostname != CB-Mass-Node1']),
placement=Placement(constraints=['node.hostname != CB-Mass-Node1', 'node.role == manager']),
xcom_all=True, # Enable XCom push for this task
)
)

0 comments on commit 0a534ed

Please sign in to comment.