2016-10-20 18:58:34 +00:00
|
|
|
package control
|
2016-06-06 22:13:15 +00:00
|
|
|
|
|
|
|
import (
|
2016-10-20 18:58:34 +00:00
|
|
|
"errors"
|
2016-06-06 22:13:15 +00:00
|
|
|
|
|
|
|
"github.com/rancher/os/config"
|
2018-09-16 04:55:26 +00:00
|
|
|
"github.com/rancher/os/pkg/compose"
|
|
|
|
"github.com/rancher/os/pkg/log"
|
2018-10-23 04:13:32 +00:00
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"github.com/docker/libcompose/project/options"
|
2016-06-06 22:13:15 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-10-20 18:58:34 +00:00
|
|
|
func switchConsoleAction(c *cli.Context) error {
|
|
|
|
if len(c.Args()) != 1 {
|
|
|
|
return errors.New("Must specify exactly one existing container")
|
2016-06-06 22:13:15 +00:00
|
|
|
}
|
2016-10-20 18:58:34 +00:00
|
|
|
newConsole := c.Args()[0]
|
2016-06-06 22:13:15 +00:00
|
|
|
|
|
|
|
cfg := config.LoadConfig()
|
|
|
|
|
2016-06-12 19:02:07 +00:00
|
|
|
project, err := compose.GetProject(cfg, true, false)
|
2016-06-06 22:13:15 +00:00
|
|
|
if err != nil {
|
2016-10-20 18:58:34 +00:00
|
|
|
return err
|
2016-06-06 22:13:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:17:07 +00:00
|
|
|
// stop docker and console to avoid zombie process
|
|
|
|
if err = project.Stop(context.Background(), 10, "docker"); err != nil {
|
|
|
|
log.Errorf("Failed to stop Docker: %v", err)
|
|
|
|
}
|
|
|
|
if err = project.Stop(context.Background(), 10, "console"); err != nil {
|
|
|
|
log.Errorf("Failed to stop console: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-06-12 19:02:07 +00:00
|
|
|
if newConsole != "default" {
|
2016-09-15 23:12:15 +00:00
|
|
|
if err = compose.LoadSpecialService(project, cfg, "console", newConsole); err != nil {
|
2016-10-20 18:58:34 +00:00
|
|
|
return err
|
2016-06-12 19:02:07 +00:00
|
|
|
}
|
2016-06-06 22:13:15 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 18:18:26 +00:00
|
|
|
if err = config.Set("rancher.console", newConsole); err != nil {
|
|
|
|
log.Errorf("Failed to update 'rancher.console': %v", err)
|
|
|
|
}
|
|
|
|
|
2016-06-08 02:19:18 +00:00
|
|
|
if err = project.Up(context.Background(), options.Up{
|
|
|
|
Log: true,
|
|
|
|
}, "console"); err != nil {
|
2016-10-20 18:58:34 +00:00
|
|
|
return err
|
2016-06-06 22:13:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:17:07 +00:00
|
|
|
if err = project.Start(context.Background(), "docker"); err != nil {
|
|
|
|
log.Errorf("Failed to start Docker: %v", err)
|
2016-06-06 22:13:15 +00:00
|
|
|
}
|
2016-10-20 18:58:34 +00:00
|
|
|
|
|
|
|
return nil
|
2016-06-06 22:13:15 +00:00
|
|
|
}
|