1
0
mirror of https://github.com/rancher/os.git synced 2025-08-31 06:11:12 +00:00
Files
os/cmd/control/service.go

113 lines
2.0 KiB
Go
Raw Normal View History

2015-02-22 20:56:25 -07:00
package control
import (
"fmt"
"log"
"github.com/codegangsta/cli"
"github.com/rancherio/os/config"
2015-04-15 22:57:59 -07:00
"github.com/rancherio/os/util"
2015-02-22 20:56:25 -07:00
)
2015-04-06 07:28:17 -07:00
func serviceSubCommands() []cli.Command {
2015-02-22 20:56:25 -07:00
return []cli.Command{
{
Name: "enable",
2015-04-06 07:28:17 -07:00
Usage: "turn on an service",
2015-02-22 20:56:25 -07:00
Action: enable,
},
{
Name: "disable",
2015-04-06 07:28:17 -07:00
Usage: "turn off an service",
2015-02-22 20:56:25 -07:00
Action: disable,
},
{
Name: "list",
2015-04-06 07:28:17 -07:00
Usage: "list services and state",
2015-02-22 20:56:25 -07:00
Action: list,
},
}
}
func disable(c *cli.Context) {
changed := false
cfg, err := config.LoadConfig()
if err != nil {
log.Fatal(err)
}
2015-04-06 07:28:17 -07:00
for _, service := range c.Args() {
2015-04-15 22:57:59 -07:00
if _, ok := cfg.ServicesInclude[service]; !ok {
2015-04-15 23:16:23 -07:00
continue
2015-02-22 20:56:25 -07:00
}
2015-04-15 22:57:59 -07:00
cfg.ServicesInclude[service] = false
2015-04-15 23:16:23 -07:00
changed = true
2015-02-22 20:56:25 -07:00
}
if changed {
2015-04-15 22:57:59 -07:00
if err = cfg.Set("services_include", cfg.ServicesInclude); err != nil {
2015-02-22 20:56:25 -07:00
log.Fatal(err)
}
}
}
func enable(c *cli.Context) {
changed := false
cfg, err := config.LoadConfig()
if err != nil {
log.Fatal(err)
}
2015-04-06 07:28:17 -07:00
for _, service := range c.Args() {
2015-04-15 22:57:59 -07:00
if val, ok := cfg.ServicesInclude[service]; !ok || !val {
cfg.ServicesInclude[service] = true
2015-02-22 20:56:25 -07:00
changed = true
}
}
if changed {
2015-04-15 22:57:59 -07:00
if err = cfg.Set("services_include", cfg.ServicesInclude); err != nil {
2015-02-22 20:56:25 -07:00
log.Fatal(err)
}
}
}
func list(c *cli.Context) {
cfg, err := config.LoadConfig()
if err != nil {
log.Fatal(err)
}
2015-04-15 22:57:59 -07:00
clone := make(map[string]bool)
for service, enabled := range cfg.ServicesInclude {
clone[service] = enabled
}
services, err := util.GetServices(cfg.Repositories.ToArray())
if err != nil {
log.Fatalf("Failed to get services: %v", err)
}
for _, service := range services {
if enabled, ok := clone[service]; ok {
delete(clone, service)
if enabled {
fmt.Printf("enabled %s\n", service)
} else {
fmt.Printf("disabled %s\n", service)
}
} else {
fmt.Printf("disabled %s\n", service)
}
}
for service, enabled := range clone {
2015-04-15 23:16:23 -07:00
if enabled {
2015-04-09 23:19:50 -07:00
fmt.Printf("enabled %s\n", service)
2015-02-22 20:56:25 -07:00
} else {
2015-04-09 23:19:50 -07:00
fmt.Printf("disabled %s\n", service)
2015-02-22 20:56:25 -07:00
}
}
}