-
Notifications
You must be signed in to change notification settings - Fork 64
/
start.go
116 lines (95 loc) · 2.62 KB
/
start.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
// 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 start
import (
"context"
"os"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/compose"
"kraftkit.sh/log"
"kraftkit.sh/packmanager"
machineapi "kraftkit.sh/api/machine/v1alpha1"
kernelstart "kraftkit.sh/internal/cli/kraft/start"
mplatform "kraftkit.sh/machine/platform"
)
type StartOptions struct {
Composefile string `noattribute:"true"`
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&StartOptions{}, cobra.Command{
Short: "Start a compose project",
Use: "start [FLAGS]",
Aliases: []string{},
Example: heredoc.Doc(`
# Start a compose project
$ kraft compose start
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "compose",
},
})
if err != nil {
panic(err)
}
return cmd
}
func (opts *StartOptions) 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 *StartOptions) 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
}
machineController, err := mplatform.NewMachineV1alpha1ServiceIterator(ctx)
if err != nil {
return err
}
machines, err := machineController.List(ctx, &machineapi.MachineList{})
if err != nil {
return err
}
services, err := project.GetServices(args...)
if err != nil {
return err
}
orderedServices := project.ServicesOrderedByDependencies(ctx, services, true)
machinesToStart := []string{}
for _, service := range orderedServices {
for _, machine := range machines.Items {
if service.ContainerName == machine.Name {
if machine.Status.State == machineapi.MachineStateCreated || machine.Status.State == machineapi.MachineStateExited {
machinesToStart = append(machinesToStart, machine.Name)
}
}
}
}
kernelStartOptions := kernelstart.StartOptions{
Detach: true,
Platform: "auto",
}
if err := kernelStartOptions.Run(ctx, machinesToStart); err != nil {
return err
}
return nil
}