Skip to content

Commit

Permalink
use "before" for networks and volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiksi committed Nov 9, 2023
1 parent c455c4a commit fe6354c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
12 changes: 8 additions & 4 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ func (g *Generator) buildNixContainers(composeProject *types.Project) ([]*NixCon
return containers, nil
}

func (g *Generator) containerNameToService(name string) string {
return fmt.Sprintf("%s-%s.service", g.Runtime, name)
}

func (g *Generator) buildNixNetworks(composeProject *types.Project, containers []*NixContainer) []*NixNetwork {
var networks []*NixNetwork
for name, network := range composeProject.Networks {
Expand All @@ -351,10 +355,10 @@ func (g *Generator) buildNixNetworks(composeProject *types.Project, containers [
Name: g.Project.With(name),
Labels: network.Labels,
}
// Keep track of all containers that are in this network.
// Keep track of all container services that are in this network.
for _, c := range containers {
if slices.Contains(c.Networks, name) {
n.Containers = append(n.Containers, fmt.Sprintf("%s-%s", g.Runtime, c.Name))
n.Containers = append(n.Containers, g.containerNameToService(c.Name))
}
}
networks = append(networks, n)
Expand Down Expand Up @@ -393,10 +397,10 @@ func (g *Generator) buildNixVolumes(composeProject *types.Project, containers []
continue
}

// Keep track of all containers that use this named volume.
// Keep track of all container services that use this named volume.
for _, c := range containers {
if _, ok := c.Volumes[name]; ok {
v.Containers = append(v.Containers, fmt.Sprintf("%s-%s", g.Runtime, c.Name))
v.Containers = append(v.Containers, g.containerNameToService(c.Name))
}
}
volumes = append(volumes, v)
Expand Down
7 changes: 6 additions & 1 deletion templates/network.nix.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ systemd.services."create-{{$runtime}}-network-{{.Name}}" = {
{{- if .Containers}}
wantedBy = [
{{- range .Containers}}
{{. | printf "\"%s.service\"" | indent 2}}
{{. | printf "%q" | indent 2}}
{{- end}}
];
before = [
{{- range .Containers}}
{{. | printf "%q" | indent 2}}
{{- end}}
];
{{- end}}
Expand Down
7 changes: 6 additions & 1 deletion templates/volume.nix.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ systemd.services."create-{{$runtime}}-volume-{{$name}}" = {
{{- if .Containers}}
wantedBy = [
{{- range .Containers}}
{{. | printf "\"%s.service\"" | indent 2}}
{{. | printf "%q" | indent 2}}
{{- end}}
];
before = [
{{- range .Containers}}
{{. | printf "%q" | indent 2}}
{{- end}}
];
{{- end}}
Expand Down
10 changes: 10 additions & 0 deletions testdata/TestDocker_WithProject_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@
"docker-torrent-client.service"
"docker-traefik.service"
];
before = [
"docker-myproject_sabnzbd.service"
"docker-photoprism-mariadb.service"
"docker-torrent-client.service"
"docker-traefik.service"
];
};

# Volumes
Expand Down Expand Up @@ -244,5 +250,9 @@
"docker-myproject_sabnzbd.service"
"docker-torrent-client.service"
];
before = [
"docker-myproject_sabnzbd.service"
"docker-torrent-client.service"
];
};
}
10 changes: 10 additions & 0 deletions testdata/TestDocker_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@
"docker-torrent-client.service"
"docker-traefik.service"
];
before = [
"docker-photoprism-mariadb.service"
"docker-sabnzbd.service"
"docker-torrent-client.service"
"docker-traefik.service"
];
};

# Volumes
Expand Down Expand Up @@ -244,5 +250,9 @@
"docker-sabnzbd.service"
"docker-torrent-client.service"
];
before = [
"docker-sabnzbd.service"
"docker-torrent-client.service"
];
};
}
6 changes: 6 additions & 0 deletions testdata/TestPodman_WithProject_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,11 @@
"podman-torrent-client.service"
"podman-traefik.service"
];
before = [
"podman-myproject_sabnzbd.service"
"podman-photoprism-mariadb.service"
"podman-torrent-client.service"
"podman-traefik.service"
];
};
}
6 changes: 6 additions & 0 deletions testdata/TestPodman_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,11 @@
"podman-torrent-client.service"
"podman-traefik.service"
];
before = [
"podman-photoprism-mariadb.service"
"podman-sabnzbd.service"
"podman-torrent-client.service"
"podman-traefik.service"
];
};
}

0 comments on commit fe6354c

Please sign in to comment.