From 93cd0877dd7d6b76fb9b27c6a9f586afccf0df1e Mon Sep 17 00:00:00 2001 From: Sven Dowideit Date: Sat, 4 Mar 2017 23:15:10 +1000 Subject: [PATCH] Initial spike on the new cmdline Signed-off-by: Sven Dowideit --- cmd/control/cli.go | 111 +++++++++++++++++++++++++-------- cmd/control/service/service.go | 14 ++--- 2 files changed, 91 insertions(+), 34 deletions(-) mode change 100644 => 100755 cmd/control/service/service.go diff --git a/cmd/control/cli.go b/cmd/control/cli.go index ecff8d42..54de0f45 100755 --- a/cmd/control/cli.go +++ b/cmd/control/cli.go @@ -3,13 +3,17 @@ package control import ( "fmt" "os" + "sort" yaml "github.com/cloudfoundry-incubator/candiedyaml" "github.com/codegangsta/cli" + composeApp "github.com/docker/libcompose/cli/app" libcomposeConfig "github.com/docker/libcompose/config" + serviceApp "github.com/rancher/os/cmd/control/service/app" "github.com/rancher/os/cmd/control/service" + "github.com/rancher/os/config" "github.com/rancher/os/log" "github.com/rancher/os/util/network" @@ -31,6 +35,8 @@ func Main() { return nil } + factory := &service.ProjectFactory{} + app.Commands = []cli.Command{ { Name: "fetch", @@ -47,57 +53,86 @@ func Main() { HideHelp: true, Action: listServices, }, { - Name: "install", + Name: "add, install, upgrade", + // TODO: add an --apply or --up ... + // TODO: also support the repo-name prefix ShortName: "", Usage: "install/upgrade service / RancherOS", HideHelp: true, - Action: dummy, + Action: service.Enable, }, { - Name: "remove", + Name: "remove, delete", ShortName: "", Usage: "remove service", HideHelp: true, - Action: dummy, + Action: service.Del, }, { - Name: "logs", - ShortName: "", - Usage: "service logs", - HideHelp: true, - Action: dummy, + Name: "logs, log", + Usage: "View output from containers", + //Before: verifyOneOrMoreServices, + Action: composeApp.WithProject(factory, serviceApp.ProjectLog), + Flags: []cli.Flag{ + cli.IntFlag{ + Name: "lines", + Usage: "number of lines to tail", + Value: 100, + }, + cli.BoolFlag{ + Name: "follow", + Usage: "Follow log output.", + }, + }, }, - // settings + // settings / partial configs { - Name: "get", + Name: "get", + // TODO: also add the merge command functionality ShortName: "", Usage: "get config value(s)", HideHelp: true, - Action: dummy, + Action: configGet, }, { Name: "set", ShortName: "", Usage: "set config value(s)", HideHelp: true, - Action: dummy, + Action: configSet, }, // complete config { - Name: "export", - ShortName: "", - Usage: "export config", - HideHelp: true, - Action: dummy, + Name: "export", + Usage: "export configuration", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "output, o", + Usage: "File to which to save", + }, + cli.BoolFlag{ + Name: "private, p", + Usage: "Include the generated private keys", + }, + cli.BoolFlag{ + Name: "full, f", + Usage: "Export full configuration, including internal and default settings", + }, + }, + Action: export, + }, { + Name: "validate", + Usage: "validate configuration from stdin", + Action: validate, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "input, i", + Usage: "File from which to read", + }, + }, }, { Name: "apply", ShortName: "", Usage: "apply service&config changes", HideHelp: true, Action: dummy, - }, { - Name: "validate", - ShortName: "", - Usage: "validate config / service file", - HideHelp: true, - Action: dummy, }, // old.. { @@ -114,6 +149,7 @@ func Main() { } func dummy(c *cli.Context) error { + fmt.Printf("Not implemented yet - use the `ros old` commands for now\n") return nil } @@ -132,12 +168,33 @@ func listServices(c *cli.Context) error { //get the current cfg, and the make a cfg with all cached services //then iterate through, listing all possible services, and what version is running, vs what version they could run //Can't just merge current cfg and cached services, as we lose service.yml version info + currentConfig := config.LoadConfig() cachedConfigs := GetAllServices() // TODO: sort them! - fmt.Printf("Running\n") - for serviceName, serviceConfig := range currentConfig.Rancher.Services { - fmt.Printf("\t%s: %s\n", serviceName, serviceConfig.Image) + fmt.Printf("Enabled\n") + enabledServices := make([]string, len(currentConfig.Rancher.Services)+len(currentConfig.Rancher.ServicesInclude)) + i := 0 + for k, _ := range currentConfig.Rancher.Services { + enabledServices[i] = k + i++ + } + for k, _ := range currentConfig.Rancher.ServicesInclude { + enabledServices[i] = k + i++ + } + sort.Strings(enabledServices) + for _, serviceName := range enabledServices { + // TODO: add running / stopped, error etc state + // TODO: separate the volumes out too (they don't need the image listed - list the volumes instead) + serviceConfig, _ := currentConfig.Rancher.Services[serviceName] + if serviceConfig != nil { + fmt.Printf("\t%s: %s\n", serviceName, serviceConfig.Image) + + } else { + fmt.Printf("\t%s\n", serviceName) + + } if len(cachedConfigs[serviceName]) > 0 { fmt.Printf("\t\tAlternatives: ") for serviceLongName, _ := range cachedConfigs[serviceName] { diff --git a/cmd/control/service/service.go b/cmd/control/service/service.go old mode 100644 new mode 100755 index d6a4871a..fe805c49 --- a/cmd/control/service/service.go +++ b/cmd/control/service/service.go @@ -15,10 +15,10 @@ import ( "github.com/rancher/os/util/network" ) -type projectFactory struct { +type ProjectFactory struct { } -func (p *projectFactory) Create(c *cli.Context) (project.APIProject, error) { +func (p *ProjectFactory) Create(c *cli.Context) (project.APIProject, error) { cfg := config.LoadConfig() return compose.GetProject(cfg, true, false) } @@ -31,7 +31,7 @@ func beforeApp(c *cli.Context) error { } func Commands() cli.Command { - factory := &projectFactory{} + factory := &ProjectFactory{} app := cli.Command{} app.Name = "service" @@ -63,7 +63,7 @@ func serviceSubCommands() []cli.Command { { Name: "enable", Usage: "turn on an service", - Action: enable, + Action: Enable, }, { Name: "disable", @@ -78,7 +78,7 @@ func serviceSubCommands() []cli.Command { { Name: "delete", Usage: "delete a service", - Action: del, + Action: Del, }, } } @@ -111,7 +111,7 @@ func disable(c *cli.Context) error { return nil } -func del(c *cli.Context) error { +func Del(c *cli.Context) error { changed := false cfg := config.LoadConfig() @@ -135,7 +135,7 @@ func del(c *cli.Context) error { return nil } -func enable(c *cli.Context) error { +func Enable(c *cli.Context) error { cfg := config.LoadConfig() var enabledServices []string