1
0
mirror of https://github.com/rancher/os.git synced 2025-07-10 21:33:04 +00:00

Initial spike on the new cmdline

Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
Sven Dowideit 2017-03-04 23:15:10 +10:00
parent 8d941162d8
commit 93cd0877dd
2 changed files with 91 additions and 34 deletions

View File

@ -3,13 +3,17 @@ package control
import ( import (
"fmt" "fmt"
"os" "os"
"sort"
yaml "github.com/cloudfoundry-incubator/candiedyaml" yaml "github.com/cloudfoundry-incubator/candiedyaml"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
composeApp "github.com/docker/libcompose/cli/app"
libcomposeConfig "github.com/docker/libcompose/config" 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/cmd/control/service"
"github.com/rancher/os/config" "github.com/rancher/os/config"
"github.com/rancher/os/log" "github.com/rancher/os/log"
"github.com/rancher/os/util/network" "github.com/rancher/os/util/network"
@ -31,6 +35,8 @@ func Main() {
return nil return nil
} }
factory := &service.ProjectFactory{}
app.Commands = []cli.Command{ app.Commands = []cli.Command{
{ {
Name: "fetch", Name: "fetch",
@ -47,57 +53,86 @@ func Main() {
HideHelp: true, HideHelp: true,
Action: listServices, Action: listServices,
}, { }, {
Name: "install", Name: "add, install, upgrade",
// TODO: add an --apply or --up ...
// TODO: also support the repo-name prefix
ShortName: "", ShortName: "",
Usage: "install/upgrade service / RancherOS", Usage: "install/upgrade service / RancherOS",
HideHelp: true, HideHelp: true,
Action: dummy, Action: service.Enable,
}, { }, {
Name: "remove", Name: "remove, delete",
ShortName: "", ShortName: "",
Usage: "remove service", Usage: "remove service",
HideHelp: true, HideHelp: true,
Action: dummy, Action: service.Del,
}, { }, {
Name: "logs", Name: "logs, log",
ShortName: "", Usage: "View output from containers",
Usage: "service logs", //Before: verifyOneOrMoreServices,
HideHelp: true, Action: composeApp.WithProject(factory, serviceApp.ProjectLog),
Action: dummy, 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: "", ShortName: "",
Usage: "get config value(s)", Usage: "get config value(s)",
HideHelp: true, HideHelp: true,
Action: dummy, Action: configGet,
}, { }, {
Name: "set", Name: "set",
ShortName: "", ShortName: "",
Usage: "set config value(s)", Usage: "set config value(s)",
HideHelp: true, HideHelp: true,
Action: dummy, Action: configSet,
}, },
// complete config // complete config
{ {
Name: "export", Name: "export",
ShortName: "", Usage: "export configuration",
Usage: "export config", Flags: []cli.Flag{
HideHelp: true, cli.StringFlag{
Action: dummy, 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", Name: "apply",
ShortName: "", ShortName: "",
Usage: "apply service&config changes", Usage: "apply service&config changes",
HideHelp: true, HideHelp: true,
Action: dummy, Action: dummy,
}, {
Name: "validate",
ShortName: "",
Usage: "validate config / service file",
HideHelp: true,
Action: dummy,
}, },
// old.. // old..
{ {
@ -114,6 +149,7 @@ func Main() {
} }
func dummy(c *cli.Context) error { func dummy(c *cli.Context) error {
fmt.Printf("Not implemented yet - use the `ros old` commands for now\n")
return nil 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 //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 //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 //Can't just merge current cfg and cached services, as we lose service.yml version info
currentConfig := config.LoadConfig() currentConfig := config.LoadConfig()
cachedConfigs := GetAllServices() cachedConfigs := GetAllServices()
// TODO: sort them! // TODO: sort them!
fmt.Printf("Running\n") fmt.Printf("Enabled\n")
for serviceName, serviceConfig := range currentConfig.Rancher.Services { enabledServices := make([]string, len(currentConfig.Rancher.Services)+len(currentConfig.Rancher.ServicesInclude))
fmt.Printf("\t%s: %s\n", serviceName, serviceConfig.Image) 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 { if len(cachedConfigs[serviceName]) > 0 {
fmt.Printf("\t\tAlternatives: ") fmt.Printf("\t\tAlternatives: ")
for serviceLongName, _ := range cachedConfigs[serviceName] { for serviceLongName, _ := range cachedConfigs[serviceName] {

14
cmd/control/service/service.go Normal file → Executable file
View File

@ -15,10 +15,10 @@ import (
"github.com/rancher/os/util/network" "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() cfg := config.LoadConfig()
return compose.GetProject(cfg, true, false) return compose.GetProject(cfg, true, false)
} }
@ -31,7 +31,7 @@ func beforeApp(c *cli.Context) error {
} }
func Commands() cli.Command { func Commands() cli.Command {
factory := &projectFactory{} factory := &ProjectFactory{}
app := cli.Command{} app := cli.Command{}
app.Name = "service" app.Name = "service"
@ -63,7 +63,7 @@ func serviceSubCommands() []cli.Command {
{ {
Name: "enable", Name: "enable",
Usage: "turn on an service", Usage: "turn on an service",
Action: enable, Action: Enable,
}, },
{ {
Name: "disable", Name: "disable",
@ -78,7 +78,7 @@ func serviceSubCommands() []cli.Command {
{ {
Name: "delete", Name: "delete",
Usage: "delete a service", Usage: "delete a service",
Action: del, Action: Del,
}, },
} }
} }
@ -111,7 +111,7 @@ func disable(c *cli.Context) error {
return nil return nil
} }
func del(c *cli.Context) error { func Del(c *cli.Context) error {
changed := false changed := false
cfg := config.LoadConfig() cfg := config.LoadConfig()
@ -135,7 +135,7 @@ func del(c *cli.Context) error {
return nil return nil
} }
func enable(c *cli.Context) error { func Enable(c *cli.Context) error {
cfg := config.LoadConfig() cfg := config.LoadConfig()
var enabledServices []string var enabledServices []string