Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ bin/
oms-cli
oms-service
dist/
oms-workdir/
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This project currently uses a fork of cobra-cli with locally-scoped variables: h
Please use it to add new commands to the OMS CLI like following:

```
cobra-cli add -c false -L -d cli -p install component
cobra-cli add --copyright=false -L -d cli -p install component
```

Run the generated `AddInstallComponent()` function in the parent `cli/cmd/install.go` to add the subcommand.
Expand Down
8 changes: 1 addition & 7 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ Module: github.com/codesphere-cloud/cs-go/pkg/io
Version: v0.10.1
License: Apache-2.0
License URL: https://github.com/codesphere-cloud/cs-go/blob/v0.10.1/LICENSE

----------
Module: github.com/codesphere-cloud/oms
Version: Unknown
License: Apache-2.0
License URL: https://github.com/codesphere-cloud/oms/blob/HEAD/LICENSE


----------
Module: github.com/cpuguy83/go-md2man/v2/md2man
Version: v2.0.6
Expand Down
27 changes: 27 additions & 0 deletions cli/cmd/beta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/spf13/cobra"
)

type BetaCmd struct {
cmd *cobra.Command
}

func AddBetaCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
beta := BetaCmd{
cmd: &cobra.Command{
Use: "beta",
Short: "Commands for early testing",
Long: io.Long(`OMS CLI commands for early adoption and testing.
Be aware that that usage and behavior may change as the features are developed.`),
},
}
rootCmd.AddCommand(beta.cmd)

AddExtendCmd(beta.cmd, opts)
}
11 changes: 0 additions & 11 deletions cli/cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package cmd

import (
"fmt"

"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/spf13/cobra"
)
Expand All @@ -15,14 +13,6 @@ type DownloadCmd struct {
cmd *cobra.Command
}

func (c *DownloadCmd) RunE(_ *cobra.Command, args []string) error {
//Command execution goes here

fmt.Printf("running %s", c.cmd.Use)

return nil
}

func AddDownloadCmd(rootCmd *cobra.Command, opts GlobalOptions) {
download := DownloadCmd{
cmd: &cobra.Command{
Expand All @@ -33,7 +23,6 @@ func AddDownloadCmd(rootCmd *cobra.Command, opts GlobalOptions) {
},
}
rootCmd.AddCommand(download.cmd)
download.cmd.RunE = download.RunE

AddDownloadPackageCmd(download.cmd, opts)
}
9 changes: 5 additions & 4 deletions cli/cmd/download_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"fmt"
"log"

"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/spf13/cobra"
Expand All @@ -17,7 +18,7 @@ import (
type DownloadPackageCmd struct {
cmd *cobra.Command
Opts DownloadPackageOpts
FileWriter util.FileWriter
FileWriter util.FileIO
}

type DownloadPackageOpts struct {
Expand All @@ -28,10 +29,10 @@ type DownloadPackageOpts struct {
}

func (c *DownloadPackageCmd) RunE(_ *cobra.Command, args []string) error {
if c.Opts.Hash != "" {
fmt.Printf("Downloading package '%s' with hash '%s'\n", c.Opts.Version, c.Opts.Hash)
if c.Opts.Hash != "" {
log.Printf("Downloading package '%s' with hash '%s'\n", c.Opts.Version, c.Opts.Hash)
} else {
fmt.Printf("Downloading package '%s'\n", c.Opts.Version)
log.Printf("Downloading package '%s'\n", c.Opts.Version)
}

p := portal.NewPortalClient()
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/download_package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ var _ = Describe("ListPackages", func() {
version string
build portal.Build
mockPortal *portal.MockPortal
mockFileWriter *util.MockFileWriter
mockFileWriter *util.MockFileIO
)

BeforeEach(func() {
filename = "installer.tar.gz"
version = "codesphere-1.42.0"
mockPortal = portal.NewMockPortal(GinkgoT())
mockFileWriter = util.NewMockFileWriter(GinkgoT())
mockFileWriter = util.NewMockFileIO(GinkgoT())

c = cmd.DownloadPackageCmd{
Opts: cmd.DownloadPackageOpts{
Expand Down
27 changes: 27 additions & 0 deletions cli/cmd/extend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/spf13/cobra"
)

// ExtendCmd represents the extend command
type ExtendCmd struct {
cmd *cobra.Command
}

func AddExtendCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
extend := ExtendCmd{
cmd: &cobra.Command{
Use: "extend",
Short: "Extend Codesphere ressources such as base images.",
Long: io.Long(`Extend Codesphere ressources such as base images to customize them for your needs.`),
},
}
rootCmd.AddCommand(extend.cmd)

AddExtendBaseimageCmd(extend.cmd, opts)
}
103 changes: 103 additions & 0 deletions cli/cmd/extend_baseimage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"errors"
"fmt"
"log"
"path"

"github.com/spf13/cobra"

"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/tmpl"
)

// ExtendBaseimageCmd represents the baseimage command
type ExtendBaseimageCmd struct {
cmd *cobra.Command
Opts *ExtendBaseimageOpts
Env env.Env
}

type ExtendBaseimageOpts struct {
*GlobalOptions
Package string
Dockerfile string
Force bool
}

const baseimagePath = "./codesphere/images"
const defaultBaseimage = "workspace-agent-24.04.tar"

func (c *ExtendBaseimageCmd) RunE(_ *cobra.Command, args []string) error {
if c.Opts.Package == "" {
return errors.New("required option package not set")
}

workdir := c.Env.GetOmsWorkdir()
p := installer.NewPackage(workdir, c.Opts.Package)

err := c.ExtendBaseimage(p, args)
if err != nil {
return fmt.Errorf("failed to extend baseimage: %w", err)
}

return nil
}

func AddExtendBaseimageCmd(extend *cobra.Command, opts *GlobalOptions) {
baseimage := ExtendBaseimageCmd{
cmd: &cobra.Command{
Use: "baseimage",
Short: "Extend Codesphere's workspace base image for customization",
Long: io.Long(`Loads the baseimage from Codesphere package and generates a Dockerfile based on it.
This enables you to extend Codesphere's base image with specific dependencies.

To use the custom base image, you need to push the resulting image to your container registry and
reference it in your install-config for the Codesphere installation process to pick it up and include it in Codesphere`),
},
Opts: &ExtendBaseimageOpts{GlobalOptions: opts},
Env: env.NewEnv(),
}
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")
baseimage.cmd.Flags().StringVarP(&baseimage.Opts.Dockerfile, "dockerfile", "d", "Dockerfile", "Output Dockerfile to generate for extending the base image")
baseimage.cmd.Flags().BoolVarP(&baseimage.Opts.Force, "force", "f", false, "Enforce package extraction")
extend.AddCommand(baseimage.cmd)
baseimage.cmd.RunE = baseimage.RunE
}

func (c *ExtendBaseimageCmd) ExtendBaseimage(p *installer.Package, args []string) error {
baseImageTarPath := path.Join(baseimagePath, defaultBaseimage)
err := p.ExtractDependency(baseImageTarPath, c.Opts.Force)
if err != nil {
return fmt.Errorf("failed to extract package to workdir: %w", err)
}

extractedBaseImagePath := p.GetDependencyPath(baseImageTarPath)
d := system.NewDockerEngine()

imagenames, err := d.GetImageNames(p.FileIO, extractedBaseImagePath)
if err != nil || len(imagenames) == 0 {
return fmt.Errorf("failed to read image tags: %w", err)
}
log.Println(imagenames)

err = tmpl.GenerateDockerfile(p.FileIO, c.Opts.Dockerfile, imagenames[0])
if err != nil {
return fmt.Errorf("failed to generate dockerfile: %w", err)
}

log.Printf("Loading container image from package into local docker daemon: %s", extractedBaseImagePath)
err = d.LoadLocalContainerImage(extractedBaseImagePath)
if err != nil {
return fmt.Errorf("failed to load baseimage file %s: %w", baseImageTarPath, err)
}

return nil
}
Loading