Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(app): update security dependencies #124

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions command/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/docker/compose/v2/pkg/api"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/local-deploy/dl/project"
"github.com/local-deploy/dl/utils/docker"
Expand Down Expand Up @@ -76,21 +77,21 @@ func runPs() error {

func getProjectContainers(ctx context.Context, cli *docker.Client, projectName string) ([]docker.ContainerSummary, error) {
containerFilter := filters.NewArgs(filters.Arg("label", fmt.Sprintf("%s=%s", api.ProjectLabel, projectName)))
containers, _ := cli.DockerCli.Client().ContainerList(ctx, types.ContainerListOptions{Filters: containerFilter, All: true})
containers, _ := cli.DockerCli.Client().ContainerList(ctx, container.ListOptions{Filters: containerFilter, All: true})

netFilters := filters.NewArgs(filters.Arg("name", projectName+"_default"))
network, _ := cli.DockerCli.Client().NetworkList(ctx, types.NetworkListOptions{Filters: netFilters})

summary := make([]docker.ContainerSummary, len(containers))
eg, ctx := errgroup.WithContext(ctx)
for i, container := range containers {
i, container := i, container
for i, c := range containers {
i, con := i, c
eg.Go(func() error {
var publishers []docker.PortPublisher
sort.Slice(container.Ports, func(i, j int) bool {
return container.Ports[i].PrivatePort < container.Ports[j].PrivatePort
sort.Slice(con.Ports, func(i, j int) bool {
return con.Ports[i].PrivatePort < con.Ports[j].PrivatePort
})
for _, p := range container.Ports {
for _, p := range con.Ports {
publishers = append(publishers, docker.PortPublisher{
URL: p.IP,
TargetPort: int(p.PrivatePort),
Expand All @@ -99,7 +100,7 @@ func getProjectContainers(ctx context.Context, cli *docker.Client, projectName s
})
}

inspect, err := cli.DockerCli.Client().ContainerInspect(ctx, container.ID)
inspect, err := cli.DockerCli.Client().ContainerInspect(ctx, con.ID)
if err != nil {
return err
}
Expand All @@ -120,16 +121,16 @@ func getProjectContainers(ctx context.Context, cli *docker.Client, projectName s
}
}

for _, n := range container.NetworkSettings.Networks {
for _, n := range con.NetworkSettings.Networks {
if network[0].ID == n.NetworkID {
ip = n.IPAddress
}
}

summary[i] = docker.ContainerSummary{
ID: container.ID,
Name: docker.GetCanonicalContainerName(container),
State: container.State,
ID: con.ID,
Name: docker.GetCanonicalContainerName(con),
State: con.State,
Health: health,
ExitCode: exitCode,
Publishers: publishers,
Expand Down
4 changes: 2 additions & 2 deletions command/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package command
import (
"context"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
dockerTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
Expand Down Expand Up @@ -63,7 +63,7 @@ func checkOldNetwork(ctx context.Context, client *docker.Client) {

for _, con := range inspect.Containers {
_ = client.DockerCli.Client().ContainerStop(ctx, con.Name, container.StopOptions{})
_ = client.DockerCli.Client().ContainerRemove(ctx, con.Name, dockerTypes.ContainerRemoveOptions{Force: true})
_ = client.DockerCli.Client().ContainerRemove(ctx, con.Name, container.RemoveOptions{Force: true})
}

err = client.DockerCli.Client().NetworkRemove(ctx, "dl_default")
Expand Down
4 changes: 2 additions & 2 deletions command/service_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/api"
"github.com/local-deploy/dl/containers"
"github.com/local-deploy/dl/utils/docker"
Expand Down Expand Up @@ -38,7 +38,7 @@ func downServiceRun(ctx context.Context) error {
services := types.Services{}
servicesContainers := getServicesContainer()
for _, service := range servicesContainers {
services = append(services, service)
services[service.Name] = service
}

project := &types.Project{
Expand Down
19 changes: 17 additions & 2 deletions command/service_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package command
import (
"context"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/local-deploy/dl/containers"
"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/utils"
Expand Down Expand Up @@ -36,6 +36,21 @@ func upServiceCommand() *cobra.Command {
return cmd
}

func MapsAppend[T comparable, U any](target map[T]U, source map[T]U) map[T]U {
if target == nil {
return source
}
if source == nil {
return target
}
for key, value := range source {
if _, ok := target[key]; !ok {
target[key] = value
}
}
return target
}

func upServiceRun(ctx context.Context) error {
if !helper.WpdeployCheck() {
return nil
Expand All @@ -47,7 +62,7 @@ func upServiceRun(ctx context.Context) error {
services := types.Services{}
servicesContainers := getServicesContainer()
for _, service := range servicesContainers {
services = append(services, service)
services[service.Name] = service
}

project := &types.Project{
Expand Down
35 changes: 18 additions & 17 deletions command/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/docker/compose/v2/pkg/api"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/utils/docker"
Expand Down Expand Up @@ -79,7 +80,7 @@ func getServices(ctx context.Context, cli *docker.Client) ([]docker.ContainerSum
containerFilter := filters.NewArgs(
filters.Arg("label", fmt.Sprintf("%s=%s", api.ProjectLabel, "dl-services")),
)
containers, _ := cli.DockerCli.Client().ContainerList(ctx, types.ContainerListOptions{Filters: containerFilter, All: true})
containers, _ := cli.DockerCli.Client().ContainerList(ctx, container.ListOptions{Filters: containerFilter, All: true})

return calculate(ctx, cli, containers)
}
Expand All @@ -88,22 +89,22 @@ func getProjects(ctx context.Context, cli *docker.Client) ([]docker.ContainerSum
containerFilter := filters.NewArgs(
filters.Arg("label", fmt.Sprintf("%s=%s", api.WorkingDirLabel, helper.TemplateDir())),
)
containers, _ := cli.DockerCli.Client().ContainerList(ctx, types.ContainerListOptions{Filters: containerFilter, All: true})
containers, _ := cli.DockerCli.Client().ContainerList(ctx, container.ListOptions{Filters: containerFilter, All: true})

return calculate(ctx, cli, containers)
}

func calculate(ctx context.Context, cli *docker.Client, containers []types.Container) ([]docker.ContainerSummary, error) {
summary := make([]docker.ContainerSummary, len(containers))
eg, ctx := errgroup.WithContext(ctx)
for i, container := range containers {
i, container := i, container
for i, c := range containers {
i, con := i, c
eg.Go(func() error {
var publishers []docker.PortPublisher
sort.Slice(container.Ports, func(i, j int) bool {
return container.Ports[i].PrivatePort < container.Ports[j].PrivatePort
sort.Slice(con.Ports, func(i, j int) bool {
return con.Ports[i].PrivatePort < con.Ports[j].PrivatePort
})
for _, port := range container.Ports {
for _, port := range con.Ports {
publishers = append(publishers, docker.PortPublisher{
URL: port.IP,
TargetPort: int(port.PrivatePort),
Expand All @@ -112,7 +113,7 @@ func calculate(ctx context.Context, cli *docker.Client, containers []types.Conta
})
}

inspect, err := cli.DockerCli.Client().ContainerInspect(ctx, container.ID)
inspect, err := cli.DockerCli.Client().ContainerInspect(ctx, con.ID)
if err != nil {
return err
}
Expand All @@ -133,9 +134,9 @@ func calculate(ctx context.Context, cli *docker.Client, containers []types.Conta
}

summary[i] = docker.ContainerSummary{
ID: container.ID,
Name: docker.GetCanonicalContainerName(container),
State: container.State,
ID: con.ID,
Name: docker.GetCanonicalContainerName(con),
State: con.State,
Health: health,
ExitCode: exitCode,
Publishers: publishers,
Expand All @@ -158,14 +159,14 @@ func render(cli *docker.Client, title string, containers []docker.ContainerSumma

data := make([][]string, len(containers)+1)
data[0] = []string{"ID", "Name", "State", "Ports"}
for _, container := range containers {
status := container.State
if status == "running" && container.Health != "" {
status = fmt.Sprintf("%s (%s)", container.State, container.Health)
for _, con := range containers {
status := con.State
if status == "running" && con.Health != "" {
status = fmt.Sprintf("%s (%s)", con.State, con.Health)
} else if status == "exited" || status == "dead" {
status = fmt.Sprintf("%s (%d)", container.State, container.ExitCode)
status = fmt.Sprintf("%s (%d)", con.State, con.ExitCode)
}
con := []string{container.ID[:12], container.Name, status, cli.DisplayablePorts(container)}
con := []string{con.ID[:12], con.Name, status, cli.DisplayablePorts(con)}
data = append(data, con)
}

Expand Down
44 changes: 12 additions & 32 deletions containers/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package containers
import (
"path/filepath"

"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/v2/types"
"github.com/local-deploy/dl/helper"
)

Expand All @@ -30,34 +30,10 @@ func Traefik() types.ServiceConfig {
},
Environment: nil,
Ports: []types.ServicePortConfig{
{
Mode: "ingress",
HostIP: "0.0.0.0",
Target: 80,
Published: "80",
Protocol: "tcp",
},
{
Mode: "ingress",
HostIP: "0.0.0.0",
Target: 443,
Published: "443",
Protocol: "tcp",
},
{
Mode: "ingress",
HostIP: "0.0.0.0",
Target: 8081,
Published: "8081",
Protocol: "tcp",
},
{
Mode: "ingress",
HostIP: "0.0.0.0",
Target: 8082,
Published: "8082",
Protocol: "tcp",
},
{Mode: "ingress", HostIP: "0.0.0.0", Target: 80, Published: "80", Protocol: "tcp"},
{Mode: "ingress", HostIP: "0.0.0.0", Target: 443, Published: "443", Protocol: "tcp"},
{Mode: "ingress", HostIP: "0.0.0.0", Target: 8081, Published: "8081", Protocol: "tcp"},
{Mode: "ingress", HostIP: "0.0.0.0", Target: 8082, Published: "8082", Protocol: "tcp"},
},
Labels: types.Labels{
"traefik.enable": "true",
Expand All @@ -71,7 +47,7 @@ func Traefik() types.ServiceConfig {
Networks: map[string]*types.ServiceNetworkConfig{
ServicesNetworkName: nil,
},
Scale: 1,
Scale: intPtr(1),
PullPolicy: types.PullPolicyIfNotPresent,
Restart: types.RestartPolicyAlways,
Volumes: []types.ServiceVolumeConfig{
Expand Down Expand Up @@ -116,7 +92,7 @@ func Mail() types.ServiceConfig {
Networks: map[string]*types.ServiceNetworkConfig{
ServicesNetworkName: {},
},
Scale: 1,
Scale: intPtr(1),
PullPolicy: types.PullPolicyIfNotPresent,
Restart: types.RestartPolicyAlways,
}
Expand Down Expand Up @@ -155,8 +131,12 @@ func Portainer() types.ServiceConfig {
Networks: map[string]*types.ServiceNetworkConfig{
ServicesNetworkName: {},
},
Scale: 1,
Scale: intPtr(1),
PullPolicy: types.PullPolicyIfNotPresent,
Restart: types.RestartPolicyAlways,
}
}

func intPtr(i int) *int {
return &i
}
Loading