This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
/
switch_console.go
57 lines (45 loc) · 1.33 KB
/
switch_console.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package control
import (
"errors"
"github.com/rancher/os/config"
"github.com/rancher/os/pkg/compose"
"github.com/rancher/os/pkg/log"
"github.com/codegangsta/cli"
"github.com/docker/libcompose/project/options"
"golang.org/x/net/context"
)
func switchConsoleAction(c *cli.Context) error {
if len(c.Args()) != 1 {
return errors.New("Must specify exactly one existing container")
}
newConsole := c.Args()[0]
cfg := config.LoadConfig()
project, err := compose.GetProject(cfg, true, false)
if err != nil {
return err
}
// 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)
}
if newConsole != "default" {
if err = compose.LoadSpecialService(project, cfg, "console", newConsole); err != nil {
return err
}
}
if err = config.Set("rancher.console", newConsole); err != nil {
log.Errorf("Failed to update 'rancher.console': %v", err)
}
if err = project.Up(context.Background(), options.Up{
Log: true,
}, "console"); err != nil {
return err
}
if err = project.Start(context.Background(), "docker"); err != nil {
log.Errorf("Failed to start Docker: %v", err)
}
return nil
}