123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- package control
- import (
- "fmt"
- "io/ioutil"
- "sort"
- "strings"
- "golang.org/x/net/context"
- log "github.com/Sirupsen/logrus"
- "github.com/codegangsta/cli"
- "github.com/docker/libcompose/project/options"
- "github.com/rancher/os/compose"
- "github.com/rancher/os/config"
- "github.com/rancher/os/util/network"
- )
- const (
- dockerDone = "/run/docker-done"
- )
- func engineSubcommands() []cli.Command {
- return []cli.Command{
- {
- Name: "switch",
- Usage: "switch Docker engine without a reboot",
- Action: engineSwitch,
- Flags: []cli.Flag{
- cli.BoolFlag{
- Name: "force, f",
- Usage: "do not prompt for input",
- },
- cli.BoolFlag{
- Name: "no-pull",
- Usage: "don't pull console image",
- },
- },
- },
- {
- Name: "enable",
- Usage: "set Docker engine to be switched on next reboot",
- Action: engineEnable,
- },
- {
- Name: "list",
- Usage: "list available Docker engines",
- Action: engineList,
- },
- }
- }
- func engineSwitch(c *cli.Context) error {
- if len(c.Args()) != 1 {
- log.Fatal("Must specify exactly one Docker engine to switch to")
- }
- newEngine := c.Args()[0]
- cfg := config.LoadConfig()
- project, err := compose.GetProject(cfg, true, false)
- if err != nil {
- log.Fatal(err)
- }
- if err = project.Stop(context.Background(), 10, "docker"); err != nil {
- log.Fatal(err)
- }
- if err = compose.LoadSpecialService(project, cfg, "docker", newEngine); err != nil {
- log.Fatal(err)
- }
- if err = project.Up(context.Background(), options.Up{}, "docker"); err != nil {
- log.Fatal(err)
- }
- if err := config.Set("rancher.docker.engine", newEngine); err != nil {
- log.Errorf("Failed to update rancher.docker.engine: %v", err)
- }
- return nil
- }
- func engineEnable(c *cli.Context) error {
- if len(c.Args()) != 1 {
- log.Fatal("Must specify exactly one Docker engine to enable")
- }
- newEngine := c.Args()[0]
- cfg := config.LoadConfig()
- if err := compose.StageServices(cfg, newEngine); err != nil {
- return err
- }
- if err := config.Set("rancher.docker.engine", newEngine); err != nil {
- log.Errorf("Failed to update 'rancher.docker.engine': %v", err)
- }
- return nil
- }
- 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)
- currentEngine := currentEngine()
- for _, engine := range engines {
- if engine == currentEngine {
- fmt.Printf("current %s\n", engine)
- } else if engine == cfg.Rancher.Docker.Engine {
- fmt.Printf("enabled %s\n", engine)
- } else {
- fmt.Printf("disabled %s\n", engine)
- }
- }
- return nil
- }
- func currentEngine() (engine string) {
- engineBytes, err := ioutil.ReadFile(dockerDone)
- if err == nil {
- engine = strings.TrimSpace(string(engineBytes))
- } else {
- log.Warnf("Failed to detect current Docker engine: %v", err)
- }
- return
- }
|