Skip to content

Commit d3a893f

Browse files
Merge branch 'main' into extend-api-key
2 parents 1ffd6f8 + 6f5b5c3 commit d3a893f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1567
-308
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ bin/
2929
oms-cli
3030
oms-service
3131
dist/
32+
oms-workdir/

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This project currently uses a fork of cobra-cli with locally-scoped variables: h
2424
Please use it to add new commands to the OMS CLI like following:
2525

2626
```
27-
cobra-cli add -c false -L -d cli -p install component
27+
cobra-cli add --copyright=false -L -d cli -p install component
2828
```
2929

3030
Run the generated `AddInstallComponent()` function in the parent `cli/cmd/install.go` to add the subcommand.

NOTICE

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@ Module: github.com/codesphere-cloud/cs-go/pkg/io
1414
Version: v0.10.1
1515
License: Apache-2.0
1616
License URL: https://github.com/codesphere-cloud/cs-go/blob/v0.10.1/LICENSE
17-
18-
----------
19-
Module: github.com/codesphere-cloud/oms
20-
Version: Unknown
21-
License: Apache-2.0
22-
License URL: https://github.com/codesphere-cloud/oms/blob/HEAD/LICENSE
23-
17+
2418
----------
2519
Module: github.com/cpuguy83/go-md2man/v2/md2man
2620
Version: v2.0.6

cli/cmd/beta.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Codesphere Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package cmd
5+
6+
import (
7+
"github.com/codesphere-cloud/cs-go/pkg/io"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
type BetaCmd struct {
12+
cmd *cobra.Command
13+
}
14+
15+
func AddBetaCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
16+
beta := BetaCmd{
17+
cmd: &cobra.Command{
18+
Use: "beta",
19+
Short: "Commands for early testing",
20+
Long: io.Long(`OMS CLI commands for early adoption and testing.
21+
Be aware that that usage and behavior may change as the features are developed.`),
22+
},
23+
}
24+
rootCmd.AddCommand(beta.cmd)
25+
26+
AddExtendCmd(beta.cmd, opts)
27+
}

cli/cmd/download.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package cmd
55

66
import (
7-
"fmt"
8-
97
"github.com/codesphere-cloud/cs-go/pkg/io"
108
"github.com/spf13/cobra"
119
)
@@ -15,14 +13,6 @@ type DownloadCmd struct {
1513
cmd *cobra.Command
1614
}
1715

18-
func (c *DownloadCmd) RunE(_ *cobra.Command, args []string) error {
19-
//Command execution goes here
20-
21-
fmt.Printf("running %s", c.cmd.Use)
22-
23-
return nil
24-
}
25-
2616
func AddDownloadCmd(rootCmd *cobra.Command, opts GlobalOptions) {
2717
download := DownloadCmd{
2818
cmd: &cobra.Command{
@@ -33,7 +23,6 @@ func AddDownloadCmd(rootCmd *cobra.Command, opts GlobalOptions) {
3323
},
3424
}
3525
rootCmd.AddCommand(download.cmd)
36-
download.cmd.RunE = download.RunE
3726

3827
AddDownloadPackageCmd(download.cmd, opts)
3928
}

cli/cmd/download_package.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cmd
55

66
import (
77
"fmt"
8+
"log"
89

910
"github.com/codesphere-cloud/cs-go/pkg/io"
1011
"github.com/spf13/cobra"
@@ -17,7 +18,7 @@ import (
1718
type DownloadPackageCmd struct {
1819
cmd *cobra.Command
1920
Opts DownloadPackageOpts
20-
FileWriter util.FileWriter
21+
FileWriter util.FileIO
2122
}
2223

2324
type DownloadPackageOpts struct {
@@ -28,10 +29,10 @@ type DownloadPackageOpts struct {
2829
}
2930

3031
func (c *DownloadPackageCmd) RunE(_ *cobra.Command, args []string) error {
31-
if c.Opts.Hash != "" {
32-
fmt.Printf("Downloading package '%s' with hash '%s'\n", c.Opts.Version, c.Opts.Hash)
32+
if c.Opts.Hash != "" {
33+
log.Printf("Downloading package '%s' with hash '%s'\n", c.Opts.Version, c.Opts.Hash)
3334
} else {
34-
fmt.Printf("Downloading package '%s'\n", c.Opts.Version)
35+
log.Printf("Downloading package '%s'\n", c.Opts.Version)
3536
}
3637

3738
p := portal.NewPortalClient()

cli/cmd/download_package_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ var _ = Describe("ListPackages", func() {
2323
version string
2424
build portal.Build
2525
mockPortal *portal.MockPortal
26-
mockFileWriter *util.MockFileWriter
26+
mockFileWriter *util.MockFileIO
2727
)
2828

2929
BeforeEach(func() {
3030
filename = "installer.tar.gz"
3131
version = "codesphere-1.42.0"
3232
mockPortal = portal.NewMockPortal(GinkgoT())
33-
mockFileWriter = util.NewMockFileWriter(GinkgoT())
33+
mockFileWriter = util.NewMockFileIO(GinkgoT())
3434

3535
c = cmd.DownloadPackageCmd{
3636
Opts: cmd.DownloadPackageOpts{

cli/cmd/extend.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Codesphere Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package cmd
5+
6+
import (
7+
"github.com/codesphere-cloud/cs-go/pkg/io"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// ExtendCmd represents the extend command
12+
type ExtendCmd struct {
13+
cmd *cobra.Command
14+
}
15+
16+
func AddExtendCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
17+
extend := ExtendCmd{
18+
cmd: &cobra.Command{
19+
Use: "extend",
20+
Short: "Extend Codesphere ressources such as base images.",
21+
Long: io.Long(`Extend Codesphere ressources such as base images to customize them for your needs.`),
22+
},
23+
}
24+
rootCmd.AddCommand(extend.cmd)
25+
26+
AddExtendBaseimageCmd(extend.cmd, opts)
27+
}

cli/cmd/extend_baseimage.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)