-
Notifications
You must be signed in to change notification settings - Fork 63
/
down.go
155 lines (129 loc) · 3.81 KB
/
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package down
import (
"context"
"os"
"github.com/MakeNowJust/heredoc"
"github.com/compose-spec/compose-go/v2/types"
"github.com/spf13/cobra"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/compose"
"kraftkit.sh/internal/cli/kraft/compose/utils"
networkremove "kraftkit.sh/internal/cli/kraft/net/remove"
machineremove "kraftkit.sh/internal/cli/kraft/remove"
"kraftkit.sh/log"
"kraftkit.sh/packmanager"
machineapi "kraftkit.sh/api/machine/v1alpha1"
networkapi "kraftkit.sh/api/network/v1alpha1"
mnetwork "kraftkit.sh/machine/network"
mplatform "kraftkit.sh/machine/platform"
)
type DownOptions struct {
composefile string
RemoveOrphans bool `long:"remove-orphans" usage:"Remove machines for services not defined in the Compose file."`
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&DownOptions{}, cobra.Command{
Short: "Stop and remove a compose project",
Use: "down [FLAGS]",
Aliases: []string{"dw"},
Long: "Stop and remove a compose project.",
Example: heredoc.Doc(`
# Stop and remove a compose project
$ kraft compose down
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "compose",
},
})
if err != nil {
panic(err)
}
return cmd
}
func (opts *DownOptions) Pre(cmd *cobra.Command, _ []string) error {
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}
cmd.SetContext(ctx)
if cmd.Flag("file").Changed {
opts.composefile = cmd.Flag("file").Value.String()
}
log.G(cmd.Context()).WithField("composefile", opts.composefile).Debug("using")
return nil
}
func (opts *DownOptions) Run(ctx context.Context, args []string) error {
workdir, err := os.Getwd()
if err != nil {
return err
}
project, err := compose.NewProjectFromComposeFile(ctx, workdir, opts.composefile)
if err != nil {
return err
}
if err := project.Validate(ctx); err != nil {
return err
}
if opts.RemoveOrphans {
if err := utils.RemoveOrphans(ctx, project); err != nil {
return err
}
}
machineController, err := mplatform.NewMachineV1alpha1ServiceIterator(ctx)
if err != nil {
return err
}
machines, err := machineController.List(ctx, &machineapi.MachineList{})
if err != nil {
return err
}
orderedServices := project.ServicesReversedByDependencies(ctx, project.Services, false)
for _, service := range orderedServices {
for _, machine := range machines.Items {
if service.ContainerName == machine.Name {
if err := removeService(ctx, service); err != nil {
return err
}
}
}
}
networkController, err := mnetwork.NewNetworkV1alpha1ServiceIterator(ctx)
if err != nil {
return err
}
networks, err := networkController.List(ctx, &networkapi.NetworkList{})
if err != nil {
return err
}
for _, projectNetwork := range project.Networks {
if projectNetwork.External {
continue
}
for _, network := range networks.Items {
if projectNetwork.Name == network.Name {
if err := removeNetwork(ctx, projectNetwork); err != nil {
return err
}
}
}
}
return nil
}
func removeService(ctx context.Context, service types.ServiceConfig) error {
log.G(ctx).Infof("removing service %s...", service.Name)
removeOptions := machineremove.RemoveOptions{Platform: "auto"}
return removeOptions.Run(ctx, []string{service.ContainerName})
}
func removeNetwork(ctx context.Context, network types.NetworkConfig) error {
log.G(ctx).Infof("removing network %s...", network.Name)
driver := "bridge"
if network.Driver != "" {
driver = network.Driver
}
removeOptions := networkremove.RemoveOptions{Driver: driver}
return removeOptions.Run(ctx, []string{network.Name})
}