-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_images.go
More file actions
89 lines (74 loc) · 2.9 KB
/
generate_images.go
File metadata and controls
89 lines (74 loc) · 2.9 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
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"context"
"errors"
"fmt"
"log"
"path"
"github.com/codesphere-cloud/cs-go/pkg/cs"
"github.com/codesphere-cloud/cs-go/pkg/exporter"
"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/spf13/cobra"
)
type GenerateImagesCmd struct {
cmd *cobra.Command
Opts *GenerateImagesOpts
}
type GenerateImagesOpts struct {
*GenerateOpts
Registry string
ImagePrefix string
}
func (c *GenerateImagesCmd) RunE(_ *cobra.Command, args []string) error {
fs := cs.NewOSFileSystem(".")
exporter := exporter.NewExporterService(fs, c.Opts.Output, "", []string{}, c.Opts.RepoRoot, c.Opts.Force)
if err := c.GenerateImages(fs, exporter); err != nil {
return fmt.Errorf("failed to generate images: %w", err)
}
log.Println("Images created:")
log.Printf("Container images from %s pushed to %s\n", c.Opts.Input, c.Opts.Registry)
log.Println("To generate kubernetes artifacts next, run:")
log.Printf("%s generate kubernetes --reporoot %s -r %s -p %s -i %s -o %s", io.BinName(), c.Opts.RepoRoot, c.Opts.Registry, c.Opts.ImagePrefix, c.Opts.Input, c.Opts.Output)
return nil
}
func AddGenerateImagesCmd(generate *cobra.Command, opts *GenerateOpts) {
images := GenerateImagesCmd{
cmd: &cobra.Command{
Use: "images",
Short: "Builds and pushes container images from the output folder of the `generate docker` command.",
Long: io.Long(`The generated images will be pushed to the specified registry.
As the image name it uses '<registry>/<imagePrefix>-<service-name>:latest'.
For the nginx router it uses '<registry>/<imagePrefix>-cs-router:latest'.
If the imagePrefix is not set, it uses '<registry>/<service-name>:latest'.`),
Example: io.FormatExampleCommands("generate images", []io.Example{
{Cmd: "-r yourRegistry", Desc: "Generate images and push them to yourRegistry"},
{Cmd: "-r yourRegistry -p customImagePrefix", Desc: "Build images and push them to yourRegistry with a custom image prefix"},
}),
},
Opts: &GenerateImagesOpts{
GenerateOpts: opts,
},
}
images.cmd.Flags().StringVarP(&images.Opts.Registry, "registry", "r", "", "Registry to push the resulting images to")
images.cmd.Flags().StringVarP(&images.Opts.ImagePrefix, "imagePrefix", "p", "", "Image prefix to use for the exported images")
generate.AddCommand(images.cmd)
images.cmd.RunE = images.RunE
}
func (c *GenerateImagesCmd) GenerateImages(fs *cs.FileSystem, exp exporter.Exporter) error {
ciInput := path.Join(c.Opts.RepoRoot, c.Opts.Input)
if c.Opts.Registry == "" {
return errors.New("registry is required")
}
_, err := exp.ReadYmlFile(ciInput)
if err != nil {
return fmt.Errorf("failed to read input file %s artifacts: %w", ciInput, err)
}
ctx := context.Background()
err = exp.ExportImages(ctx, c.Opts.Registry, c.Opts.ImagePrefix)
if err != nil {
return fmt.Errorf("failed to export docker artifacts: %w", err)
}
return nil
}