| 
									
										
										
										
											2016-06-06 15:13:15 -07:00
										 |  |  | package control | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bufio" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"golang.org/x/net/context" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	log "github.com/Sirupsen/logrus" | 
					
						
							|  |  |  | 	"github.com/codegangsta/cli" | 
					
						
							|  |  |  | 	composeConfig "github.com/docker/libcompose/config" | 
					
						
							|  |  |  | 	"github.com/docker/libcompose/project/options" | 
					
						
							|  |  |  | 	"github.com/rancher/os/compose" | 
					
						
							|  |  |  | 	"github.com/rancher/os/config" | 
					
						
							|  |  |  | 	"github.com/rancher/os/util/network" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func consoleSubcommands() []cli.Command { | 
					
						
							|  |  |  | 	return []cli.Command{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Name:   "switch", | 
					
						
							| 
									
										
										
										
											2016-06-12 22:28:24 -07:00
										 |  |  | 			Usage:  "switch console without a reboot", | 
					
						
							| 
									
										
										
										
											2016-06-06 15:13:15 -07:00
										 |  |  | 			Action: consoleSwitch, | 
					
						
							| 
									
										
										
										
											2016-06-07 22:16:23 -07:00
										 |  |  | 			Flags: []cli.Flag{ | 
					
						
							|  |  |  | 				cli.BoolFlag{ | 
					
						
							|  |  |  | 					Name:  "force, f", | 
					
						
							|  |  |  | 					Usage: "do not prompt for input", | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-06-06 15:13:15 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-06-12 22:28:24 -07:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			Name:   "enable", | 
					
						
							|  |  |  | 			Usage:  "set console to be switched on next reboot", | 
					
						
							|  |  |  | 			Action: consoleEnable, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-06-06 15:13:15 -07:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			Name:   "list", | 
					
						
							|  |  |  | 			Usage:  "list available consoles", | 
					
						
							|  |  |  | 			Action: consoleList, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func consoleSwitch(c *cli.Context) error { | 
					
						
							|  |  |  | 	if len(c.Args()) != 1 { | 
					
						
							| 
									
										
										
										
											2016-06-12 22:28:24 -07:00
										 |  |  | 		log.Fatal("Must specify exactly one console to switch to") | 
					
						
							| 
									
										
										
										
											2016-06-06 15:13:15 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	newConsole := c.Args()[0] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-07 22:16:23 -07:00
										 |  |  | 	if !c.Bool("force") { | 
					
						
							|  |  |  | 		in := bufio.NewReader(os.Stdin) | 
					
						
							| 
									
										
										
										
											2016-06-07 22:17:19 -07:00
										 |  |  | 		fmt.Println("Switching consoles will destroy the current console container and restart Docker.") | 
					
						
							|  |  |  | 		fmt.Println("Note: You will also be logged out.") | 
					
						
							|  |  |  | 		if !yes(in, "Continue") { | 
					
						
							| 
									
										
										
										
											2016-06-07 22:16:23 -07:00
										 |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-06 15:13:15 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cfg := config.LoadConfig() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-12 12:02:07 -07:00
										 |  |  | 	if newConsole != "default" { | 
					
						
							|  |  |  | 		if err := compose.StageServices(cfg, newConsole); err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-06 15:13:15 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	service, err := compose.CreateService(nil, "switch-console", &composeConfig.ServiceConfigV1{ | 
					
						
							|  |  |  | 		LogDriver:  "json-file", | 
					
						
							|  |  |  | 		Privileged: true, | 
					
						
							|  |  |  | 		Net:        "host", | 
					
						
							|  |  |  | 		Pid:        "host", | 
					
						
							| 
									
										
										
										
											2016-06-12 12:02:07 -07:00
										 |  |  | 		Image:      fmt.Sprintf("rancher/os-base:%s", config.VERSION), | 
					
						
							| 
									
										
										
										
											2016-06-06 15:13:15 -07:00
										 |  |  | 		Labels: map[string]string{ | 
					
						
							|  |  |  | 			config.SCOPE: config.SYSTEM, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		Command:     []string{"/usr/bin/switch-console", newConsole}, | 
					
						
							|  |  |  | 		VolumesFrom: []string{"all-volumes"}, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err = service.Delete(context.Background(), options.Delete{}); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-12 12:02:07 -07:00
										 |  |  | 	if err = service.Up(context.Background(), options.Up{}); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return service.Log(context.Background(), true) | 
					
						
							| 
									
										
										
										
											2016-06-06 15:13:15 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-12 22:28:24 -07:00
										 |  |  | func consoleEnable(c *cli.Context) error { | 
					
						
							|  |  |  | 	if len(c.Args()) != 1 { | 
					
						
							|  |  |  | 		log.Fatal("Must specify exactly one console to enable") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	newConsole := c.Args()[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cfg := config.LoadConfig() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if newConsole != "default" { | 
					
						
							|  |  |  | 		if err := compose.StageServices(cfg, newConsole); err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := config.Set("rancher.console", newConsole); err != nil { | 
					
						
							|  |  |  | 		log.Errorf("Failed to update 'rancher.console': %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-06 15:13:15 -07:00
										 |  |  | func consoleList(c *cli.Context) error { | 
					
						
							|  |  |  | 	cfg := config.LoadConfig() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	consoles, err := network.GetConsoles(cfg.Rancher.Repositories.ToArray()) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-12 12:02:07 -07:00
										 |  |  | 	fmt.Println("default") | 
					
						
							| 
									
										
										
										
											2016-06-06 15:13:15 -07:00
										 |  |  | 	for _, console := range consoles { | 
					
						
							|  |  |  | 		fmt.Println(console) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |