Skip to content

Commit

Permalink
Move qemu to own package (closes #571)
Browse files Browse the repository at this point in the history
* Move qemu components into separated file

* Isolate config package

* Move console colors into constants package
  • Loading branch information
fabioDMFerreira committed Feb 16, 2021
1 parent c53d019 commit 9ac8ef4
Show file tree
Hide file tree
Showing 80 changed files with 507 additions and 425 deletions.
4 changes: 2 additions & 2 deletions cmd/cmd_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/config"
"github.com/spf13/cobra"
)

Expand All @@ -31,7 +31,7 @@ func buildCommandHandler(cmd *cobra.Command, args []string) {
globalFlags := NewGlobalCommandFlags(flags)
buildImageFlags := NewBuildImageCommandFlags(flags)

c := lepton.NewConfig()
c := config.NewConfig()

c.Program = args[0]
checkProgramExists(c.Program)
Expand Down
8 changes: 4 additions & 4 deletions cmd/cmd_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"time"

"github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/config"
api "github.com/nanovms/ops/lepton"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -51,7 +51,7 @@ func imageCreateCommand() *cobra.Command {

func imageCreateCommandHandler(cmd *cobra.Command, args []string) {
flags := cmd.Flags()
c := lepton.NewConfig()
c := config.NewConfig()

configFlags := NewConfigCommandFlags(flags)
globalFlags := NewGlobalCommandFlags(flags)
Expand Down Expand Up @@ -117,7 +117,7 @@ func imageListCommandHandler(cmd *cobra.Command, args []string) {
globalFlags := NewGlobalCommandFlags(flags)
providerFlags := NewProviderCommandFlags(flags)

c := lepton.NewConfig()
c := config.NewConfig()

mergeContainer := NewMergeConfigContainer(configFlags, globalFlags, providerFlags)
err := mergeContainer.Merge(c)
Expand Down Expand Up @@ -163,7 +163,7 @@ func imageDeleteCommandHandler(cmd *cobra.Command, args []string) {
globalFlags := NewGlobalCommandFlags(flags)
providerFlags := NewProviderCommandFlags(flags)

c := lepton.NewConfig()
c := config.NewConfig()

mergeContainer := NewMergeConfigContainer(configFlags, globalFlags, providerFlags)
err := mergeContainer.Merge(c)
Expand Down
6 changes: 3 additions & 3 deletions cmd/cmd_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
"time"

"github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/config"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -308,14 +308,14 @@ func instanceLogsCommandHandler(cmd *cobra.Command, args []string) {
}
}

func getInstanceCommandDefaultConfig(cmd *cobra.Command) (c *lepton.Config, err error) {
func getInstanceCommandDefaultConfig(cmd *cobra.Command) (c *config.Config, err error) {
flags := cmd.Flags()

configFlags := NewConfigCommandFlags(flags)
globalFlags := NewGlobalCommandFlags(flags)
providerFlags := NewProviderCommandFlags(flags)

c = lepton.NewConfig()
c = config.NewConfig()

mergeContainer := NewMergeConfigContainer(configFlags, globalFlags, providerFlags)
err = mergeContainer.Merge(c)
Expand Down
4 changes: 2 additions & 2 deletions cmd/cmd_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"

"github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/config"
api "github.com/nanovms/ops/lepton"
)

Expand Down Expand Up @@ -218,7 +218,7 @@ func loadCommandHandler(cmd *cobra.Command, args []string) {

pkgFlags.Package = args[0]

c := lepton.NewConfig()
c := config.NewConfig()

mergeContainer := NewMergeConfigContainer(configFlags, buildImageFlags, globalFlags, startImageFlags, pkgFlags)
err := mergeContainer.Merge(c)
Expand Down
3 changes: 2 additions & 1 deletion cmd/cmd_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

api "github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/qemu"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -43,7 +44,7 @@ func (p *Profile) setProfile() {
p.OpsVersion = api.Version
p.NanosVersion = api.LocalReleaseVersion

qv, err := api.QemuVersion()
qv, err := qemu.Version()
if err != nil {
fmt.Println(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"path"

"github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/config"
api "github.com/nanovms/ops/lepton"
"github.com/spf13/cobra"
)
Expand All @@ -27,7 +27,7 @@ func RunCommand() *cobra.Command {

func runCommandHandler(cmd *cobra.Command, args []string) {

c := lepton.NewConfig()
c := config.NewConfig()

program := args[0]
c.Program = program
Expand Down
6 changes: 3 additions & 3 deletions cmd/cmd_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"strconv"

"github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/config"
api "github.com/nanovms/ops/lepton"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -191,15 +191,15 @@ func volumeDetachCommandHandler(cmd *cobra.Command, args []string) {
}
}

func getVolumeCommandDefaultConfig(cmd *cobra.Command) (c *lepton.Config, err error) {
func getVolumeCommandDefaultConfig(cmd *cobra.Command) (c *config.Config, err error) {
flags := cmd.Flags()

configFlags := NewConfigCommandFlags(flags)
globalFlags := NewGlobalCommandFlags(flags)
nightlyFlags := NewNightlyCommandFlags(flags)
providerFlags := NewProviderCommandFlags(flags)

c = lepton.NewConfig()
c = config.NewConfig()

mergeContainer := NewMergeConfigContainer(configFlags, globalFlags, nightlyFlags, providerFlags)
err = mergeContainer.Merge(c)
Expand Down
6 changes: 3 additions & 3 deletions cmd/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import (
"os"

"github.com/go-errors/errors"
api "github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/constants"
"github.com/spf13/cobra"
)

func exitWithError(errs string) {
fmt.Println(fmt.Sprintf(api.ErrorColor, errs))
fmt.Println(fmt.Sprintf(constants.ErrorColor, errs))
os.Exit(1)
}

func exitForCmd(cmd *cobra.Command, errs string) {
fmt.Println(fmt.Sprintf(api.ErrorColor, errs))
fmt.Println(fmt.Sprintf(constants.ErrorColor, errs))
cmd.Help()
os.Exit(1)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/flags.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cmd

import (
"github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/config"
)

// MergeConfigFlags are flags structures able to override ops configuration attributes
type MergeConfigFlags interface {
MergeToConfig(config *lepton.Config) error
MergeToConfig(config *config.Config) error
}

// MergeConfigContainer is responsible for merge a list of flags attributes to ops configuration
Expand All @@ -21,7 +21,7 @@ func NewMergeConfigContainer(flags ...MergeConfigFlags) *MergeConfigContainer {
}

// Merge uses a list of flags to override configuration properties.
func (m *MergeConfigContainer) Merge(config *lepton.Config) error {
func (m *MergeConfigContainer) Merge(config *config.Config) error {

for _, f := range m.flags {
err := f.MergeToConfig(config)
Expand Down
19 changes: 16 additions & 3 deletions cmd/flags_build_image.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package cmd

import (
"fmt"
"path"
"path/filepath"
"strings"

"github.com/nanovms/ops/config"
"github.com/nanovms/ops/lepton"
api "github.com/nanovms/ops/lepton"
"github.com/spf13/pflag"
)
Expand All @@ -17,7 +22,7 @@ type BuildImageCommandFlags struct {
}

// MergeToConfig overrides configuration passed by argument with command flags values
func (flags *BuildImageCommandFlags) MergeToConfig(c *api.Config) (err error) {
func (flags *BuildImageCommandFlags) MergeToConfig(c *config.Config) (err error) {
if len(flags.CmdEnvs) > 0 {
if len(c.Env) == 0 {
c.Env = make(map[string]string)
Expand Down Expand Up @@ -50,8 +55,16 @@ func (flags *BuildImageCommandFlags) MergeToConfig(c *api.Config) (err error) {
}

if c.RunConfig.Imagename != "" {
c.SetImage()
c.RunConfig.Imagename = c.RunConfig.Imagename + ".img"
imageName := c.RunConfig.Imagename
if imageName == "" {
imageName = lepton.GenerateImageName(c.Program)
c.CloudConfig.ImageName = fmt.Sprintf("%v-image", filepath.Base(c.Program))
} else {
c.CloudConfig.ImageName = imageName
images := path.Join(lepton.GetOpsHome(), "images")
imageName = path.Join(images, filepath.Base(imageName)+".img")
}
c.RunConfig.Imagename = imageName
}

if c.Args != nil {
Expand Down
15 changes: 8 additions & 7 deletions cmd/flags_build_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/nanovms/ops/cmd"
"github.com/nanovms/ops/config"
"github.com/nanovms/ops/lepton"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -47,17 +48,17 @@ func TestBuildImageFlagsMergeToConfig(t *testing.T) {
opsPath := lepton.GetOpsHome() + "/" + lepton.LocalReleaseVersion
imagesPath := lepton.GetOpsHome() + "/images"

config := &lepton.Config{}
expected := &lepton.Config{
c := &config.Config{}
expected := &config.Config{
Boot: opsPath + "/boot.img",
Kernel: opsPath + "/kernel.img",
Mounts: map[string]string{
volumeName: "/files",
},
CloudConfig: lepton.ProviderConfig{
CloudConfig: config.ProviderConfig{
ImageName: "test-image",
},
RunConfig: lepton.RunConfig{
RunConfig: config.RunConfig{
Imagename: imagesPath + "/test-image.img",
},
Args: []string{"a b c d"},
Expand All @@ -67,11 +68,11 @@ func TestBuildImageFlagsMergeToConfig(t *testing.T) {
NameServer: "8.8.8.8",
}

buildImageFlags.MergeToConfig(config)
buildImageFlags.MergeToConfig(c)

config.RunConfig.Mounts = nil
c.RunConfig.Mounts = nil

assert.Equal(t, expected, config)
assert.Equal(t, expected, c)
}

func newBuildImageFlagSet() (flagSet *pflag.FlagSet) {
Expand Down
21 changes: 10 additions & 11 deletions cmd/flags_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"os/user"
"strings"

"github.com/nanovms/ops/lepton"
api "github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/config"
"github.com/spf13/pflag"
)

Expand All @@ -19,35 +18,35 @@ type ConfigCommandFlags struct {
}

// MergeToConfig reads a json configuration file
func (flags *ConfigCommandFlags) MergeToConfig(config *lepton.Config) (err error) {
func (flags *ConfigCommandFlags) MergeToConfig(c *config.Config) (err error) {
if flags.Config != "" {
var data []byte

*config = *lepton.NewConfig()
*c = *config.NewConfig()
data, err = ioutil.ReadFile(flags.Config)
if err != nil {
err = fmt.Errorf("error reading config: %v", err)
return
}

err = json.Unmarshal(data, &config)
err = json.Unmarshal(data, &c)
if err != nil {
err = fmt.Errorf("error config: %v", err)
return
}
return
}

config = unWarpDefaultConfig()
c = unWarpDefaultConfig()
return

}

// unWarpConfig parses lepton config file from file
func unWarpConfig(file string) *api.Config {
var c api.Config
func unWarpConfig(file string) *config.Config {
var c config.Config
if file != "" {
c = *api.NewConfig()
c = *config.NewConfig()
data, err := ioutil.ReadFile(file)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading config: %v\n", err)
Expand All @@ -65,8 +64,8 @@ func unWarpConfig(file string) *api.Config {
}

// unWarpDefaultConfig gets default config file from env
func unWarpDefaultConfig() *api.Config {
c := *api.NewConfig()
func unWarpDefaultConfig() *config.Config {
c := *config.NewConfig()
conf := os.Getenv("OPS_DEFAULT_CONFIG")
if conf != "" {
data, err := ioutil.ReadFile(conf)
Expand Down
10 changes: 5 additions & 5 deletions cmd/flags_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/nanovms/ops/cmd"
"github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/config"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)
Expand All @@ -23,12 +23,12 @@ func TestConfigFlags(t *testing.T) {

func TestConfigFlagsMergeToConfig(t *testing.T) {
configFileName := "test-" + String(5) + ".json"
expected := &lepton.Config{
CloudConfig: lepton.ProviderConfig{
expected := &config.Config{
CloudConfig: config.ProviderConfig{
ProjectID: "projectid",
BucketName: "thebucketname",
},
RunConfig: lepton.RunConfig{
RunConfig: config.RunConfig{
Memory: "2G",
},
}
Expand All @@ -40,7 +40,7 @@ func TestConfigFlagsMergeToConfig(t *testing.T) {
flagSet.Set("config", configFileName)
configFlags := cmd.NewConfigCommandFlags(flagSet)

actual := &lepton.Config{}
actual := &config.Config{}

err := configFlags.MergeToConfig(actual)

Expand Down
Loading

0 comments on commit 9ac8ef4

Please sign in to comment.