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

Commit

Permalink
CLI validation for services, console, and engines
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwget committed Dec 5, 2016
1 parent f1f41df commit cc3c786
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 21 deletions.
30 changes: 22 additions & 8 deletions cmd/control/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"github.com/codegangsta/cli"
composeConfig "github.com/docker/libcompose/config"
"github.com/docker/libcompose/project/options"
"github.com/rancher/os/cmd/control/service"
"github.com/rancher/os/compose"
"github.com/rancher/os/config"
"github.com/rancher/os/log"
"github.com/rancher/os/util"
"github.com/rancher/os/util/network"
)

Expand Down Expand Up @@ -54,6 +56,7 @@ func consoleSwitch(c *cli.Context) error {
newConsole := c.Args()[0]

cfg := config.LoadConfig()
validateConsole(newConsole, cfg)
if newConsole == currentConsole() {
log.Warnf("Console is already set to %s", newConsole)
}
Expand Down Expand Up @@ -106,6 +109,7 @@ func consoleEnable(c *cli.Context) error {
newConsole := c.Args()[0]

cfg := config.LoadConfig()
validateConsole(newConsole, cfg)

if newConsole != "default" {
if err := compose.StageServices(cfg, newConsole); err != nil {
Expand All @@ -122,14 +126,7 @@ func consoleEnable(c *cli.Context) error {

func consoleList(c *cli.Context) error {
cfg := config.LoadConfig()

consoles, err := network.GetConsoles(cfg.Rancher.Repositories.ToArray())
if err != nil {
return err
}
consoles = append(consoles, "default")
sort.Strings(consoles)

consoles := availableConsoles(cfg)
currentConsole := currentConsole()

for _, console := range consoles {
Expand All @@ -145,6 +142,23 @@ func consoleList(c *cli.Context) error {
return nil
}

func validateConsole(console string, cfg *config.CloudConfig) {
consoles := availableConsoles(cfg)
if !service.IsLocalOrURL(console) && !util.Contains(consoles, console) {
log.Fatalf("%s is not a valid console", console)
}
}

func availableConsoles(cfg *config.CloudConfig) []string {
consoles, err := network.GetConsoles(cfg.Rancher.Repositories.ToArray())
if err != nil {
log.Fatal(err)
}
consoles = append(consoles, "default")
sort.Strings(consoles)
return consoles
}

func currentConsole() (console string) {
consoleBytes, err := ioutil.ReadFile("/run/console-done")
if err == nil {
Expand Down
28 changes: 21 additions & 7 deletions cmd/control/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (

"github.com/codegangsta/cli"
"github.com/docker/libcompose/project/options"
"github.com/rancher/os/cmd/control/service"
"github.com/rancher/os/compose"
"github.com/rancher/os/config"
"github.com/rancher/os/log"
"github.com/rancher/os/util"
"github.com/rancher/os/util/network"
)

Expand Down Expand Up @@ -53,6 +55,7 @@ func engineSwitch(c *cli.Context) error {
newEngine := c.Args()[0]

cfg := config.LoadConfig()
validateEngine(newEngine, cfg)

project, err := compose.GetProject(cfg, true, false)
if err != nil {
Expand Down Expand Up @@ -85,6 +88,7 @@ func engineEnable(c *cli.Context) error {
newEngine := c.Args()[0]

cfg := config.LoadConfig()
validateEngine(newEngine, cfg)

if err := compose.StageServices(cfg, newEngine); err != nil {
return err
Expand All @@ -99,13 +103,7 @@ func engineEnable(c *cli.Context) error {

func engineList(c *cli.Context) error {
cfg := config.LoadConfig()

engines, err := network.GetEngines(cfg.Rancher.Repositories.ToArray())
if err != nil {
return err
}
sort.Strings(engines)

engines := availableEngines(cfg)
currentEngine := currentEngine()

for _, engine := range engines {
Expand All @@ -121,6 +119,22 @@ func engineList(c *cli.Context) error {
return nil
}

func validateEngine(engine string, cfg *config.CloudConfig) {
engines := availableEngines(cfg)
if !service.IsLocalOrURL(engine) && !util.Contains(engines, engine) {
log.Fatalf("%s is not a valid engine", engine)
}
}

func availableEngines(cfg *config.CloudConfig) []string {
engines, err := network.GetEngines(cfg.Rancher.Repositories.ToArray())
if err != nil {
log.Fatal(err)
}
sort.Strings(engines)
return engines
}

func currentEngine() (engine string) {
engineBytes, err := ioutil.ReadFile(dockerDone)
if err == nil {
Expand Down
38 changes: 33 additions & 5 deletions cmd/control/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/rancher/os/compose"
"github.com/rancher/os/config"
"github.com/rancher/os/log"
"github.com/rancher/os/util"
"github.com/rancher/os/util/network"
)

Expand Down Expand Up @@ -91,6 +92,8 @@ func disable(c *cli.Context) error {
cfg := config.LoadConfig()

for _, service := range c.Args() {
validateService(service, cfg)

if _, ok := cfg.Rancher.ServicesInclude[service]; !ok {
continue
}
Expand All @@ -113,9 +116,12 @@ func del(c *cli.Context) error {
cfg := config.LoadConfig()

for _, service := range c.Args() {
validateService(service, cfg)

if _, ok := cfg.Rancher.ServicesInclude[service]; !ok {
continue
}

delete(cfg.Rancher.ServicesInclude, service)
changed = true
}
Expand All @@ -135,8 +141,10 @@ func enable(c *cli.Context) error {
var enabledServices []string

for _, service := range c.Args() {
validateService(service, cfg)

if val, ok := cfg.Rancher.ServicesInclude[service]; !ok || !val {
if strings.HasPrefix(service, "/") && !strings.HasPrefix(service, "/var/lib/rancher/conf") {
if isLocal(service) && !strings.HasPrefix(service, "/var/lib/rancher/conf") {
log.Fatalf("ERROR: Service should be in path /var/lib/rancher/conf")
}

Expand Down Expand Up @@ -166,10 +174,7 @@ func list(c *cli.Context) error {
clone[service] = enabled
}

services, err := network.GetServices(cfg.Rancher.Repositories.ToArray())
if err != nil {
log.Fatalf("Failed to get services: %v", err)
}
services := availableService(cfg)

for _, service := range services {
if enabled, ok := clone[service]; ok {
Expand All @@ -194,3 +199,26 @@ func list(c *cli.Context) error {

return nil
}

func isLocal(service string) bool {
return strings.HasPrefix(service, "/")
}

func IsLocalOrURL(service string) bool {
return isLocal(service) || strings.HasPrefix(service, "http:/") || strings.HasPrefix(service, "http:/")
}

func validateService(service string, cfg *config.CloudConfig) {
services := availableService(cfg)
if !IsLocalOrURL(service) && !util.Contains(services, service) {
log.Fatalf("%s is not a valid service", service)
}
}

func availableService(cfg *config.CloudConfig) []string {
services, err := network.GetServices(cfg.Rancher.Repositories.ToArray())
if err != nil {
log.Fatalf("Failed to get services: %v", err)
}
return services
}
4 changes: 3 additions & 1 deletion tests/consoles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ func (s *QemuSuite) TestConsoleCommand(c *C) {

s.CheckCall(c, `
sudo ros console list | grep default | grep current
sudo ros console list | grep debian | grep disabled`)
sudo ros console list | grep debian | grep disabled
(sudo ros console switch invalid 2>&1 || true) | grep "invalid is not a valid console"
(sudo ros console enable invalid 2>&1 || true) | grep "invalid is not a valid console"`)

s.MakeCall("sudo ros console switch -f debian")
c.Assert(s.WaitForSSH(), IsNil)
Expand Down
3 changes: 3 additions & 0 deletions tests/custom_docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ set -ex
docker version | grep 1.10.3
sudo ros engine list | grep 1.10.3 | grep current
(sudo ros engine switch invalid 2>&1 || true) | grep "invalid is not a valid engine"
(sudo ros engine enable invalid 2>&1 || true) | grep "invalid is not a valid engine"
docker run -d --restart=always nginx
docker ps | grep nginx`)

Expand Down

0 comments on commit cc3c786

Please sign in to comment.