-
Notifications
You must be signed in to change notification settings - Fork 62
/
push.go
197 lines (166 loc) · 4.59 KB
/
push.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// 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 push
import (
"context"
"fmt"
"os"
"strings"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
kraftcloud "sdk.kraft.cloud"
kcinstances "sdk.kraft.cloud/instances"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/compose"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
"kraftkit.sh/log"
"kraftkit.sh/packmanager"
"kraftkit.sh/tui/processtree"
)
type PushOptions struct {
Auth *config.AuthConfig `noattribute:"true"`
Client kcinstances.InstancesService `noattribute:"true"`
Composefile string `noattribute:"true"`
Metro string `noattribute:"true"`
Project *compose.Project `noattribute:"true"`
Token string `noattribute:"true"`
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&PushOptions{}, cobra.Command{
Short: "Push a KraftCloud deployment service",
Use: "push [FLAGS] [COMPONENT]",
Args: cobra.ArbitraryArgs,
Aliases: []string{"p"},
Long: heredoc.Doc(`
Push a KraftCloud deployment service.
`),
Example: heredoc.Doc(`
# Push a KraftCloud deployment service.
$ kraft cloud compose push nginx
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "kraftcloud-compose",
},
})
if err != nil {
panic(err)
}
return cmd
}
func Push(ctx context.Context, opts *PushOptions, args ...string) error {
var err error
if opts.Auth == nil {
opts.Auth, err = config.GetKraftCloudAuthConfig(ctx, opts.Token)
if err != nil {
return fmt.Errorf("could not retrieve credentials: %w", err)
}
}
if opts.Client == nil {
opts.Client = kraftcloud.NewInstancesClient(
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*opts.Auth)),
)
}
workdir, err := os.Getwd()
if err != nil {
return err
}
if opts.Project == nil {
opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile)
if err != nil {
return err
}
}
if err := opts.Project.Validate(ctx); err != nil {
return err
}
// If no services are specified, push all services.
if len(args) == 0 {
for service := range opts.Project.Services {
args = append(args, service)
}
}
var processes []*processtree.ProcessTreeItem
for _, serviceName := range args {
service, ok := opts.Project.Services[serviceName]
if !ok {
return fmt.Errorf("service '%s' not found", serviceName)
}
var pkgName string
if service.Image != "" {
pkgName = service.Image
} else {
pkgName = service.Name
}
user := strings.TrimSuffix(strings.TrimPrefix(opts.Auth.User, "robot$"), ".users.kraftcloud")
if split := strings.Split(pkgName, "/"); len(split) > 1 {
user = split[0]
pkgName = strings.Join(split[1:], "/")
}
if strings.HasPrefix(pkgName, "unikraft.io") {
pkgName = "index." + pkgName
}
if !strings.HasPrefix(pkgName, "index.unikraft.io") {
pkgName = fmt.Sprintf(
"index.unikraft.io/%s/%s",
user,
pkgName,
)
}
packages, err := packmanager.G(ctx).Catalog(ctx,
packmanager.WithRemote(false),
packmanager.WithName(pkgName),
)
if err != nil {
return err
}
if len(packages) == 0 {
log.G(ctx).
WithField("package", pkgName).
Warn("not found")
}
for _, p := range packages {
p := p
processes = append(processes, processtree.NewProcessTreeItem(
fmt.Sprintf("pushing %s", p.String()),
"",
func(ctx context.Context) error {
return p.Push(ctx)
},
))
}
}
model, err := processtree.NewProcessTree(
ctx,
[]processtree.ProcessTreeOption{
processtree.IsParallel(!config.G[config.KraftKit](ctx).NoParallel),
processtree.WithRenderer(log.LoggerTypeFromString(config.G[config.KraftKit](ctx).Log.Type) != log.FANCY),
processtree.WithFailFast(true),
},
processes...,
)
if err != nil {
return err
}
return model.Start()
}
func (opts *PushOptions) Pre(cmd *cobra.Command, args []string) error {
err := utils.PopulateMetroToken(cmd, &opts.Metro, &opts.Token)
if err != nil {
return fmt.Errorf("could not populate metro and token: %w", err)
}
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()
}
return nil
}
func (opts *PushOptions) Run(ctx context.Context, args []string) error {
return Push(ctx, opts, args...)
}