-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild_images.go
More file actions
105 lines (84 loc) · 2.92 KB
/
build_images.go
File metadata and controls
105 lines (84 loc) · 2.92 KB
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
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"context"
"fmt"
"log"
"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/codesphere-cloud/oms/internal/env"
"github.com/codesphere-cloud/oms/internal/installer"
"github.com/codesphere-cloud/oms/internal/system"
"github.com/codesphere-cloud/oms/internal/util"
"github.com/codesphere-cloud/oms/internal/version"
"github.com/spf13/cobra"
)
// BuildImagesCmd represents the build images command
type BuildImagesCmd struct {
cmd *cobra.Command
Opts *BuildImagesOpts
Env env.Env
}
type BuildImagesOpts struct {
*GlobalOptions
Config string
}
func (c *BuildImagesCmd) RunE(_ *cobra.Command, args []string) error {
cm := installer.NewConfig()
im := system.NewImage(context.Background())
err := c.BuildAndPushImages(cm, im)
if err != nil {
return fmt.Errorf("failed to build and push images: %w", err)
}
return nil
}
func AddBuildImagesCmd(build *cobra.Command, opts *GlobalOptions) {
buildImages := BuildImagesCmd{
cmd: &cobra.Command{
Use: "images",
Short: "Build and push container images",
Long: io.Long(`Build and push container images based on the configuration file.
Extracts necessary images configuration to get the bomRef and processes them.`),
},
Opts: &BuildImagesOpts{GlobalOptions: opts},
Env: env.NewEnv(),
}
buildImages.cmd.Flags().StringVarP(&buildImages.Opts.Config, "config", "c", "", "Path to the configuration YAML file")
util.MarkFlagRequired(buildImages.cmd, "config")
build.AddCommand(buildImages.cmd)
buildImages.cmd.RunE = buildImages.RunE
}
func (c *BuildImagesCmd) BuildAndPushImages(cm installer.ConfigManager, im system.ImageManager) error {
config, err := cm.ParseConfigYaml(c.Opts.Config)
if err != nil {
return fmt.Errorf("failed to parse config: %w", err)
}
if len(config.Codesphere.DeployConfig.Images) == 0 {
return fmt.Errorf("no images defined in the config")
}
if len(config.Registry.Server) == 0 {
return fmt.Errorf("registry server not defined in the config")
}
v := &version.Build{}
codesphereVersion := v.Version()
for imageName, imageConfig := range config.Codesphere.DeployConfig.Images {
for flavorName, flavorConfig := range imageConfig.Flavors {
log.Printf("Processing image '%s' with flavor '%s'", imageName, flavorName)
if flavorConfig.Image.Dockerfile == "" {
log.Printf("Skipping flavor '%s', no dockerfile defined", flavorName)
continue
}
targetImage := fmt.Sprintf("%s/%s-%s:%s", config.Registry.Server, imageName, flavorName, codesphereVersion)
err := im.BuildImage(flavorConfig.Image.Dockerfile, targetImage, ".")
if err != nil {
return fmt.Errorf("failed to build image %s: %w", targetImage, err)
}
err = im.PushImage(targetImage)
if err != nil {
return fmt.Errorf("failed to push image %s: %w", targetImage, err)
}
log.Printf("Successfully built and pushed image: %s", targetImage)
}
}
return nil
}