-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop command for stopping VMs (#236)
- Loading branch information
1 parent
1f3e427
commit 3e1dbfd
Showing
9 changed files
with
211 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/outofforest/ioc/v2" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/outofforest/osman" | ||
"github.com/outofforest/osman/config" | ||
"github.com/outofforest/osman/infra/format" | ||
) | ||
|
||
// NewStopCommand creates new stop command | ||
func NewStopCommand(cmdF *CmdFactory) *cobra.Command { | ||
var storageF *config.StorageFactory | ||
var filterF *config.FilterFactory | ||
var formatF *config.FormatFactory | ||
stopF := &config.StopFactory{} | ||
|
||
cmd := &cobra.Command{ | ||
Short: "Stops VMs", | ||
Use: "stop [flags] [name][:tag]", | ||
RunE: cmdF.Cmd(func(c *ioc.Container) { | ||
c.Singleton(storageF.Config) | ||
c.Singleton(filterF.Config) | ||
c.Singleton(formatF.Config) | ||
c.Singleton(stopF.Config) | ||
}, func(c *ioc.Container, formatter format.Formatter) error { | ||
var results []osman.Result | ||
var err error | ||
c.Call(osman.Stop, &results, &err) | ||
if err != nil { | ||
return err | ||
} | ||
err = nil | ||
for _, r := range results { | ||
if r.Result != nil { | ||
err = errors.New("some stops failed") | ||
break | ||
} | ||
} | ||
fmt.Println(formatter.Format(results)) | ||
return nil | ||
}), | ||
} | ||
storageF = cmdF.AddStorageFlags(cmd) | ||
filterF = cmdF.AddFilterFlags(cmd, []string{config.BuildTypeVM}) | ||
formatF = cmdF.AddFormatFlags(cmd) | ||
cmd.Flags().StringVar(&stopF.LibvirtAddr, "libvirt-addr", "unix:///var/run/libvirt/libvirt-sock", "Address libvirt listens on") | ||
cmd.Flags().BoolVar(&stopF.All, "all", false, "It is required to set this flag to stop builds if no filters are provided") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package config | ||
|
||
// StopFactory collects data for stop config | ||
type StopFactory struct { | ||
// If no filter is provided it is required to set this flag to stop builds | ||
All bool | ||
|
||
// LibvirtAddr is the address libvirt listens on | ||
LibvirtAddr string | ||
} | ||
|
||
// Config returns new stop config | ||
func (f *StopFactory) Config() Stop { | ||
config := Stop{ | ||
All: f.All, | ||
LibvirtAddr: f.LibvirtAddr, | ||
} | ||
return config | ||
} | ||
|
||
// Stop stores configuration for stop command | ||
type Stop struct { | ||
// If no filter is provided it is required to set this flag to stop builds | ||
All bool | ||
|
||
// LibvirtAddr is the address libvirt listens on | ||
LibvirtAddr string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters