|
| 1 | +// Copyright (c) Codesphere Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "log" |
| 10 | + "path" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
| 13 | + |
| 14 | + "github.com/codesphere-cloud/cs-go/pkg/io" |
| 15 | + "github.com/codesphere-cloud/oms/internal/env" |
| 16 | + "github.com/codesphere-cloud/oms/internal/installer" |
| 17 | + "github.com/codesphere-cloud/oms/internal/system" |
| 18 | + "github.com/codesphere-cloud/oms/internal/tmpl" |
| 19 | +) |
| 20 | + |
| 21 | +// ExtendBaseimageCmd represents the baseimage command |
| 22 | +type ExtendBaseimageCmd struct { |
| 23 | + cmd *cobra.Command |
| 24 | + Opts *ExtendBaseimageOpts |
| 25 | + Env env.Env |
| 26 | +} |
| 27 | + |
| 28 | +type ExtendBaseimageOpts struct { |
| 29 | + *GlobalOptions |
| 30 | + Package string |
| 31 | + Dockerfile string |
| 32 | + Force bool |
| 33 | +} |
| 34 | + |
| 35 | +const baseimagePath = "./codesphere/images" |
| 36 | +const defaultBaseimage = "workspace-agent-24.04.tar" |
| 37 | + |
| 38 | +func (c *ExtendBaseimageCmd) RunE(_ *cobra.Command, args []string) error { |
| 39 | + if c.Opts.Package == "" { |
| 40 | + return errors.New("required option package not set") |
| 41 | + } |
| 42 | + |
| 43 | + workdir := c.Env.GetOmsWorkdir() |
| 44 | + p := installer.NewPackage(workdir, c.Opts.Package) |
| 45 | + |
| 46 | + err := c.ExtendBaseimage(p, args) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("failed to extend baseimage: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +func AddExtendBaseimageCmd(extend *cobra.Command, opts *GlobalOptions) { |
| 55 | + baseimage := ExtendBaseimageCmd{ |
| 56 | + cmd: &cobra.Command{ |
| 57 | + Use: "baseimage", |
| 58 | + Short: "Extend Codesphere's workspace base image for customization", |
| 59 | + Long: io.Long(`Loads the baseimage from Codesphere package and generates a Dockerfile based on it. |
| 60 | + This enables you to extend Codesphere's base image with specific dependencies. |
| 61 | +
|
| 62 | + To use the custom base image, you need to push the resulting image to your container registry and |
| 63 | + reference it in your install-config for the Codesphere installation process to pick it up and include it in Codesphere`), |
| 64 | + }, |
| 65 | + Opts: &ExtendBaseimageOpts{GlobalOptions: opts}, |
| 66 | + Env: env.NewEnv(), |
| 67 | + } |
| 68 | + baseimage.cmd.Flags().StringVarP(&baseimage.Opts.Package, "package", "p", "", "Package file (e.g. codesphere-v1.2.3-installer.tar.gz) to load base image from") |
| 69 | + baseimage.cmd.Flags().StringVarP(&baseimage.Opts.Dockerfile, "dockerfile", "d", "Dockerfile", "Output Dockerfile to generate for extending the base image") |
| 70 | + baseimage.cmd.Flags().BoolVarP(&baseimage.Opts.Force, "force", "f", false, "Enforce package extraction") |
| 71 | + extend.AddCommand(baseimage.cmd) |
| 72 | + baseimage.cmd.RunE = baseimage.RunE |
| 73 | +} |
| 74 | + |
| 75 | +func (c *ExtendBaseimageCmd) ExtendBaseimage(p *installer.Package, args []string) error { |
| 76 | + baseImageTarPath := path.Join(baseimagePath, defaultBaseimage) |
| 77 | + err := p.ExtractDependency(baseImageTarPath, c.Opts.Force) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("failed to extract package to workdir: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + extractedBaseImagePath := p.GetDependencyPath(baseImageTarPath) |
| 83 | + d := system.NewDockerEngine() |
| 84 | + |
| 85 | + imagenames, err := d.GetImageNames(p.FileIO, extractedBaseImagePath) |
| 86 | + if err != nil || len(imagenames) == 0 { |
| 87 | + return fmt.Errorf("failed to read image tags: %w", err) |
| 88 | + } |
| 89 | + log.Println(imagenames) |
| 90 | + |
| 91 | + err = tmpl.GenerateDockerfile(p.FileIO, c.Opts.Dockerfile, imagenames[0]) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("failed to generate dockerfile: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + log.Printf("Loading container image from package into local docker daemon: %s", extractedBaseImagePath) |
| 97 | + err = d.LoadLocalContainerImage(extractedBaseImagePath) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("failed to load baseimage file %s: %w", baseImageTarPath, err) |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
0 commit comments