-
Notifications
You must be signed in to change notification settings - Fork 64
/
build.go
145 lines (113 loc) · 3.14 KB
/
build.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
// 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 build
import (
"context"
"fmt"
"os"
"strings"
"github.com/compose-spec/compose-go/v2/types"
"github.com/spf13/cobra"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/compose"
"kraftkit.sh/internal/cli/kraft/build"
"kraftkit.sh/internal/cli/kraft/pkg"
"kraftkit.sh/log"
"kraftkit.sh/packmanager"
)
type BuildOptions struct {
composefile string
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&BuildOptions{}, cobra.Command{
Short: "Build or rebuild services",
Use: "build",
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "compose",
},
})
if err != nil {
panic(err)
}
return cmd
}
func (opts *BuildOptions) 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 *BuildOptions) 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
}
services, err := project.GetServices(args...)
if err != nil {
return err
}
for _, service := range services {
if service.Build == nil {
continue
}
if err := buildService(ctx, service); err != nil {
return err
}
if service.Image != "" {
if err := pkgService(ctx, service); err != nil {
return err
}
}
}
return nil
}
func platArchFromService(service types.ServiceConfig) (string, string, error) {
// The service platform should be in the form <platform>/<arch>
parts := strings.SplitN(service.Platform, "/", 2)
if len(parts) != 2 {
return "", "", fmt.Errorf("invalid platform: %s for service %s", service.Platform, service.Name)
}
return parts[0], parts[1], nil
}
func buildService(ctx context.Context, service types.ServiceConfig) error {
if service.Build == nil {
return fmt.Errorf("service %s has no build context", service.Name)
}
plat, arch, err := platArchFromService(service)
if err != nil {
return err
}
log.G(ctx).Infof("Building service %s...", service.Name)
buildOptions := build.BuildOptions{Platform: plat, Architecture: arch}
return buildOptions.Run(ctx, []string{service.Build.Context})
}
func pkgService(ctx context.Context, service types.ServiceConfig) error {
plat, arch, err := platArchFromService(service)
if err != nil {
return err
}
log.G(ctx).Infof("packaging service %s...", service.Name)
pkgOptions := pkg.PkgOptions{
Architecture: arch,
Name: service.Image,
Format: "oci",
Platform: plat,
Strategy: packmanager.StrategyOverwrite,
}
return pkgOptions.Run(ctx, []string{service.Build.Context})
}