/
service_down.go
67 lines (58 loc) · 1.47 KB
/
service_down.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package command
import (
"context"
"time"
"github.com/compose-spec/compose-go/types"
"github.com/docker/compose/v2/pkg/api"
"github.com/local-deploy/dl/containers"
"github.com/local-deploy/dl/utils/docker"
"github.com/spf13/cobra"
)
func downServiceCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "down",
Short: "Stop and remove services",
Long: `Stops and removes portainer, mailcatcher and traefik containers.
Valid parameters for the "--service" flag: portainer, mail, traefik`,
Example: "dl down\ndl down -s portainer",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
err := downServiceRun(ctx)
if err != nil {
return err
}
return nil
},
}
return cmd
}
func downServiceRun(ctx context.Context) error {
client, _ := docker.NewClient()
checkOldNetwork(ctx, client)
services := types.Services{}
servicesContainers := getServicesContainer()
for _, service := range servicesContainers {
services = append(services, service)
}
project := &types.Project{
Name: "dl-services",
WorkingDir: "",
Services: services,
Networks: map[string]types.NetworkConfig{
containers.ServicesNetworkName: {
Name: containers.ServicesNetworkName,
},
},
}
timeoutValue := 30 * time.Second
err := client.Backend.Down(ctx, project.Name, api.DownOptions{
RemoveOrphans: false,
Project: project,
Timeout: &timeoutValue,
Volumes: false,
})
if err != nil {
return err
}
return nil
}