Skip to content

Commit

Permalink
fixup! [OPS-1463] Docker networks and volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sereja313 committed Dec 12, 2023
1 parent 4bff8b8 commit bcc1b47
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 13 deletions.
12 changes: 7 additions & 5 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
url = "github:hercules-ci/gitignore.nix";
flake = false;
};

nixpkgs.url = "github:serokell/nixpkgs/sereja/revert-docker-networks";
flake-compat = {
flake = false;
};
Expand Down Expand Up @@ -81,6 +81,9 @@
packages = pkgs.lib.optionalAttrs (! lib.hasInfix "darwin" system) {
inherit (pkgs) benchwrapper;
};
checks = {
docker = import ./tests/docker.nix (inputs // { inherit pkgs; });
};
}
));
}
25 changes: 18 additions & 7 deletions modules/virtualization/docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@ with lib; let
vsuperfluous="$(${pkgs.gnugrep}/bin/grep -vxF -f $vwanted $vexisting || true)"
while read -r net; do
if [[ ! -z "$net" ]]; then
echo -n "Removed superfluous Docker network: "
${pkgs.docker}/bin/docker network rm "$net" || true
rm -f /etc/docker/network-opts/$net
if [[ -f /etc/docker/network-opts/$net ]]; then
echo -n "Removed superfluous Docker network: "
${pkgs.docker}/bin/docker network rm "$net" || true
rm -f /etc/docker/network-opts/$net
else
echo "Skipped deleting Docker network $net as it was manually created (/etc/docker/network-opts/$net is missing)."
fi
fi
done <<< "$nsuperfluous"
while read -r vol; do
if [[ ! -z "$vol" ]]; then
echo -n "Removed superfluous Docker volume: "
${pkgs.docker}/bin/docker volume rm "$vol" || true
if [[ -f /etc/docker/volumes/$vol ]]; then
echo -n "Removed superfluous Docker volume: "
${pkgs.docker}/bin/docker volume rm "$vol" || true
rm -f /etc/docker/volumes/$vol
else
echo "Skipped deleting Docker volume $vol as it was manually created (/etc/docker/volumes/$vol is missing)."
fi
fi
done <<< "$vsuperfluous"
'';
Expand All @@ -50,7 +59,7 @@ with lib; let
'';
in ''
mkdir -p /etc/docker/network-opts/
if [[ $(${pkgs.docker}/bin/docker network ls --quiet --filter name=${name} | wc -c) -eq 0 ]]; then
if [[ $(${pkgs.docker}/bin/docker network ls --quiet --filter name=^${name}$ | wc -c) -eq 0 ]]; then
rm -f /etc/docker/network-opts/${name}
${create}
elif [[ "${toString recreate}" ]]; then
Expand All @@ -69,9 +78,11 @@ with lib; let
'';

mkVolume = name: ''
if [[ $(${pkgs.docker}/bin/docker volume ls --quiet --filter name=${name} | wc -c) -eq 0 ]]; then
mkdir -p /etc/docker/volumes/
if [[ $(${pkgs.docker}/bin/docker volume ls --quiet --filter name=^${name}$ | wc -c) -eq 0 ]]; then
echo "*** docker volume create ${name}"
${pkgs.docker}/bin/docker volume create ${name}
touch /etc/docker/volumes/${name}
fi
'';
in {
Expand Down
44 changes: 44 additions & 0 deletions tests/docker.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-FileCopyrightText: 2023 Serokell <https://serokell.io/>
#
# SPDX-License-Identifier: MPL-2.0

{ self, nixpkgs, pkgs, ... }:
import "${nixpkgs}/nixos/tests/make-test-python.nix" ({...} : {
name = "docker";
nodes = {
docker = {...}:
{
imports = [ self.nixosModules.docker ];
virtualisation.docker = {
enable = true;
volumes = [ "thevolume" ];
networks.thenetwork = {
driver = "bridge";
subnet = "172.28.0.0/16";
ip-range = "172.28.5.0/24";
gateway = "172.28.5.254";
};
};

};
};

testScript = ''
start_all()
docker.wait_for_unit("sockets.target")
docker.wait_for_unit("docker.service")
docker.succeed("docker volume ls | grep thevolume")
docker.succeed("docker network ls | grep thenetwork")
docker.succeed("docker volume create newvolume");
docker.succeed("docker network create newnetwork")
docker.systemctl("restart docker")
docker.wait_for_unit("docker.service")
# don't remove manually created networks and volumes
docker.succeed("docker volume ls | grep newvolume")
docker.succeed("docker network ls | grep newnetwork")
'';
}) { inherit pkgs; }

0 comments on commit bcc1b47

Please sign in to comment.