123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- package control
- import (
- "fmt"
- "log"
- "strings"
- "github.com/codegangsta/cli"
- "github.com/rancherio/os/config"
- "github.com/rancherio/os/docker"
- "github.com/rancherio/os/util"
- )
- func serviceSubCommands() []cli.Command {
- return []cli.Command{
- {
- Name: "enable",
- Usage: "turn on an service",
- Action: enable,
- },
- {
- Name: "disable",
- Usage: "turn off an service",
- Action: disable,
- },
- {
- Name: "list",
- Usage: "list services and state",
- Action: list,
- },
- {
- Name: "delete",
- Usage: "delete a service",
- Action: del,
- },
- }
- }
- func disable(c *cli.Context) {
- changed := false
- cfg, err := config.LoadConfig()
- if err != nil {
- log.Fatal(err)
- }
- for _, service := range c.Args() {
- if _, ok := cfg.ServicesInclude[service]; !ok {
- continue
- }
- cfg.ServicesInclude[service] = false
- changed = true
- }
- if changed {
- if err = cfg.Set("services_include", cfg.ServicesInclude); err != nil {
- log.Fatal(err)
- }
- }
- }
- func del(c *cli.Context) {
- changed := false
- cfg, err := config.LoadConfig()
- if err != nil {
- log.Fatal(err)
- }
- for _, service := range c.Args() {
- if _, ok := cfg.ServicesInclude[service]; !ok {
- continue
- }
- delete(cfg.ServicesInclude, service)
- changed = true
- }
- if changed {
- if err = cfg.Set("services_include", cfg.ServicesInclude); err != nil {
- log.Fatal(err)
- }
- }
- }
- func enable(c *cli.Context) {
- changed := false
- cfg, err := config.LoadConfig()
- if err != nil {
- log.Fatal(err)
- }
- for _, service := range c.Args() {
- if val, ok := cfg.ServicesInclude[service]; !ok || !val {
- if strings.HasPrefix(service, "/") && !strings.HasPrefix(service, "/var/lib/rancher/conf") {
- log.Fatalf("ERROR: Service should be in path /var/lib/rancher/conf")
- }
- if _, err := docker.LoadServiceResource(service, true, cfg); err != nil {
- log.Fatalf("could not load service %s", service)
- }
- cfg.ServicesInclude[service] = true
- changed = true
- }
- }
- if changed {
- if err = cfg.Set("services_include", cfg.ServicesInclude); err != nil {
- log.Fatal(err)
- }
- }
- }
- func list(c *cli.Context) {
- cfg, err := config.LoadConfig()
- if err != nil {
- log.Fatal(err)
- }
- 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 {
- if enabled {
- fmt.Printf("enabled %s\n", service)
- } else {
- fmt.Printf("disabled %s\n", service)
- }
- }
- }
|