Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Use common flags, correct run behavior with -i support
Browse files Browse the repository at this point in the history
  • Loading branch information
twelho committed May 28, 2019
1 parent 26adcee commit 619e55b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 37 deletions.
6 changes: 3 additions & 3 deletions cmd/ignite/cmd/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewCmdAttach(out io.Writer) *cobra.Command {
Short: "Attach to a running Firecracker VM",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := RunAttach(out, cmd, args, true)
err := RunAttach(out, cmd, args[0], true)
errutils.Check(err)
},
}
Expand All @@ -28,11 +28,11 @@ func NewCmdAttach(out io.Writer) *cobra.Command {

// checkRunning can be used to skip the running check, this is used by CmdRun
// As the in-container ignite takes some time to start up and update the state
func RunAttach(out io.Writer, cmd *cobra.Command, args []string, checkRunning bool) error {
func RunAttach(out io.Writer, cmd *cobra.Command, vmMatch string, checkRunning bool) error {
var md *vmmd.VMMetadata

// Match a single VM using the VMFilter
if matches, err := filter.NewFilterer(vmmd.NewVMFilter(args[0]), metadata.VM.Path(), vmmd.LoadVMMetadata); err == nil {
if matches, err := filter.NewFilterer(vmmd.NewVMFilter(vmMatch), metadata.VM.Path(), vmmd.LoadVMMetadata); err == nil {
if filterable, err := matches.Single(); err == nil {
if md, err = vmmd.ToVMMetadata(filterable); err != nil {
return err
Expand Down
31 changes: 17 additions & 14 deletions cmd/ignite/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,31 @@ import (
"io"
)

type flagData struct {
cpus int64
memory int64
}

// NewCmdCreate creates a new VM from an image
func NewCmdCreate(out io.Writer) *cobra.Command {
fd := &flagData{}
co := &createOptions{}

cmd := &cobra.Command{
Use: "create [image] [kernel] [name]",
Short: "Create a new containerized VM without starting it",
Args: cobra.MinimumNArgs(3),
Run: func(cmd *cobra.Command, args []string) {
err := RunCreate(out, cmd, args, fd)
err := RunCreate(out, cmd, args[0], args[1], args[2], co, false)
errutils.Check(err)
},
}

cmd.Flags().Int64Var(&fd.cpus, "cpus", constants.VM_DEFAULT_CPUS, "VM vCPU count, 1 or even numbers between 1 and 32")
cmd.Flags().Int64Var(&fd.memory, "memory", constants.VM_DEFAULT_MEMORY, "VM RAM in MiB")
cmd.Flags().Int64Var(&co.cpus, "cpus", constants.VM_DEFAULT_CPUS, "VM vCPU count, 1 or even numbers between 1 and 32")
cmd.Flags().Int64Var(&co.memory, "memory", constants.VM_DEFAULT_MEMORY, "VM RAM in MiB")
return cmd
}

func RunCreate(out io.Writer, cmd *cobra.Command, args []string, fd *flagData) error {
func RunCreate(out io.Writer, cmd *cobra.Command, imageMatch, kernelMatch, name string, co *createOptions, start bool) error {
var image *imgmd.ImageMetadata
var kernel *kernmd.KernelMetadata

// Match a single Image using the ImageFilter
if matches, err := filter.NewFilterer(imgmd.NewImageFilter(args[0]), metadata.Image.Path(), imgmd.LoadImageMetadata); err == nil {
if matches, err := filter.NewFilterer(imgmd.NewImageFilter(imageMatch), metadata.Image.Path(), imgmd.LoadImageMetadata); err == nil {
if filterable, err := matches.Single(); err == nil {
if image, err = imgmd.ToImageMetadata(filterable); err != nil {
return err
Expand All @@ -56,7 +51,7 @@ func RunCreate(out io.Writer, cmd *cobra.Command, args []string, fd *flagData) e
}

// Match a single Kernel using the KernelFilter
if matches, err := filter.NewFilterer(kernmd.NewKernelFilter(args[1]), metadata.Kernel.Path(), kernmd.LoadKernelMetadata); err == nil {
if matches, err := filter.NewFilterer(kernmd.NewKernelFilter(kernelMatch), metadata.Kernel.Path(), kernmd.LoadKernelMetadata); err == nil {
if filterable, err := matches.Single(); err == nil {
if kernel, err = kernmd.ToKernelMetadata(filterable); err != nil {
return err
Expand All @@ -74,7 +69,7 @@ func RunCreate(out io.Writer, cmd *cobra.Command, args []string, fd *flagData) e
return err
}

md := vmmd.NewVMMetadata(vmID, args[2], vmmd.NewVMObjectData(image.ID, kernel.ID, fd.cpus, fd.memory))
md := vmmd.NewVMMetadata(vmID, name, vmmd.NewVMObjectData(image.ID, kernel.ID, co.cpus, co.memory))

// Save the metadata
if err := md.Save(); err != nil {
Expand All @@ -87,7 +82,15 @@ func RunCreate(out io.Writer, cmd *cobra.Command, args []string, fd *flagData) e
return err
}

fmt.Println(vmID)
// If start is specified, start teh VM after creation
if start {
if err := RunStart(out, cmd, name); err != nil {
return err
}
} else {
// Print the ID of the created VM
fmt.Println(md.ID)
}

return nil
}
12 changes: 12 additions & 0 deletions cmd/ignite/cmd/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cmd

// This file contains a collection of variables for common flags
var (
interactive bool
all bool
)

type createOptions struct {
cpus int64
memory int64
}
2 changes: 0 additions & 2 deletions cmd/ignite/cmd/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"io"
)

var all bool

// NewCmdPs lists running Firecracker VMs
func NewCmdPs(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Expand Down
26 changes: 14 additions & 12 deletions cmd/ignite/cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
package cmd

import (
"github.com/luxas/ignite/pkg/constants"
"github.com/luxas/ignite/pkg/errutils"
"github.com/spf13/cobra"
"io"
)

// NewCmdRun starts and attaches to a Firecracker VM
// NewCmdRun creates, starts (and attaches to) a Firecracker VM
func NewCmdRun(out io.Writer) *cobra.Command {
co := &createOptions{}

cmd := &cobra.Command{
Use: "run [vm]",
Short: "Start and attach to a Firecracker VM",
Args: cobra.MinimumNArgs(1),
Use: "run [image] [kernel] [name]",
Short: "Create and start a new Firecracker VM",
Args: cobra.MinimumNArgs(3),
Run: func(cmd *cobra.Command, args []string) {
err := RunRun(out, cmd, args)
err := RunRun(out, cmd, args[0], args[1], args[2], co)
errutils.Check(err)
},
}
//cmd.Flags().StringP("output", "o", "", "Output format; available options are 'yaml', 'json' and 'short'")

cmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "Immediately attach to created and started VM")
cmd.Flags().Int64Var(&co.cpus, "cpus", constants.VM_DEFAULT_CPUS, "VM vCPU count, 1 or even numbers between 1 and 32")
cmd.Flags().Int64Var(&co.memory, "memory", constants.VM_DEFAULT_MEMORY, "VM RAM in MiB")
return cmd
}

func RunRun(out io.Writer, cmd *cobra.Command, args []string) error {
if err := RunStart(out, cmd, args); err == nil {
if err = RunAttach(out, cmd, args, false); err != nil {
return err
}
} else {
func RunRun(out io.Writer, cmd *cobra.Command, imageMatch, kernelMatch, name string, co *createOptions) error {
if err := RunCreate(out, cmd, imageMatch, kernelMatch, name, co, true); err != nil {
return err
}

Expand Down
10 changes: 4 additions & 6 deletions cmd/ignite/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ import (
"github.com/spf13/cobra"
)

var interactive bool

// NewCmdStart starts a Firecracker VM
func NewCmdStart(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "start [vm]",
Short: "Start a Firecracker VM",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := RunStart(out, cmd, args)
err := RunStart(out, cmd, args[0])
errutils.Check(err)
},
}
Expand All @@ -33,11 +31,11 @@ func NewCmdStart(out io.Writer) *cobra.Command {
return cmd
}

func RunStart(out io.Writer, cmd *cobra.Command, args []string) error {
func RunStart(out io.Writer, cmd *cobra.Command, vmMatch string) error {
var md *vmmd.VMMetadata

// Match a single VM using the VMFilter
if matches, err := filter.NewFilterer(vmmd.NewVMFilter(args[0]), metadata.VM.Path(), vmmd.LoadVMMetadata); err == nil {
if matches, err := filter.NewFilterer(vmmd.NewVMFilter(vmMatch), metadata.VM.Path(), vmmd.LoadVMMetadata); err == nil {
if filterable, err := matches.Single(); err == nil {
if md, err = vmmd.ToVMMetadata(filterable); err != nil {
return err
Expand Down Expand Up @@ -78,7 +76,7 @@ func RunStart(out io.Writer, cmd *cobra.Command, args []string) error {

// If starting interactively, attach after starting
if interactive {
if err := RunAttach(out, cmd, args, false); err != nil {
if err := RunAttach(out, cmd, vmMatch, false); err != nil {
return err
}
} else {
Expand Down

0 comments on commit 619e55b

Please sign in to comment.