1
0
mirror of https://github.com/rancher/os.git synced 2025-09-01 14:48:55 +00:00

fix current console detection

Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
Sven Dowideit
2017-07-14 18:05:57 +10:00
parent d9d3c2b0d8
commit 6fcc1e3967
5 changed files with 44 additions and 24 deletions

View File

@@ -6,7 +6,6 @@ import (
"os/exec" "os/exec"
"runtime" "runtime"
"strings" "strings"
"syscall"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/rancher/os/config" "github.com/rancher/os/config"
@@ -58,11 +57,12 @@ func autologinAction(c *cli.Context) error {
banner = strings.Replace(banner, "\\l", tty, -1) banner = strings.Replace(banner, "\\l", tty, -1)
banner = strings.Replace(banner, "\\\\", "\\", -1) banner = strings.Replace(banner, "\\\\", "\\", -1)
banner = banner + "\n" banner = banner + "\n"
banner = banner + "Autologin " + CurrentConsole() + "\n"
fmt.Printf(banner) fmt.Printf(banner)
loginBin := "" loginBin := ""
args := []string{} args := []string{}
if cfg.Rancher.Console == "Acentos" || cfg.Rancher.Console == "Afedora" { if CurrentConsole() == "centos" || CurrentConsole() == "fedora" {
// For some reason, centos and fedora ttyS0 and tty1 don't work with `login -f rancher` // For some reason, centos and fedora ttyS0 and tty1 don't work with `login -f rancher`
// until I make time to read their source, lets just give us a way to get work done // until I make time to read their source, lets just give us a way to get work done
loginBin = "bash" loginBin = "bash"
@@ -78,18 +78,18 @@ func autologinAction(c *cli.Context) error {
fmt.Printf("error finding %s in path: %s", cmd.Args[0], err) fmt.Printf("error finding %s in path: %s", cmd.Args[0], err)
return err return err
} }
os.Setenv("PS1", `[`+cfg.Rancher.Console+`: \l \u@\h \W]\$`) os.Setenv("PS1", `[`+CurrentConsole()+`: \l \u@\h \W]\$`)
return syscall.Exec(loginBinPath, args, os.Environ()) // Causes all sorts of issues
//return syscall.Exec(loginBinPath, args, os.Environ())
cmd = exec.Command(loginBinPath, args...)
cmd.Env = os.Environ()
// cmd = exec.Command(loginBinPath, args...) cmd.Stderr = os.Stderr
// cmd.Env = os.Environ() cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
// cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil {
// cmd.Stdout = os.Stdout log.Errorf("\nError starting %s: %s", cmd.Args[0], err)
// cmd.Stdin = os.Stdin }
// if err := cmd.Run(); err != nil { return nil
// log.Errorf("\nError starting %s: %s", cmd.Args[0], err)
// }
// return nil
} }

View File

@@ -2,18 +2,19 @@ package control
import ( import (
"fmt" "fmt"
"io/ioutil"
"sort" "sort"
"strings" "strings"
"golang.org/x/net/context" "golang.org/x/net/context"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/docker/docker/reference"
composeConfig "github.com/docker/libcompose/config" composeConfig "github.com/docker/libcompose/config"
"github.com/docker/libcompose/project/options" "github.com/docker/libcompose/project/options"
"github.com/rancher/os/cmd/control/service" "github.com/rancher/os/cmd/control/service"
"github.com/rancher/os/compose" "github.com/rancher/os/compose"
"github.com/rancher/os/config" "github.com/rancher/os/config"
"github.com/rancher/os/docker"
"github.com/rancher/os/log" "github.com/rancher/os/log"
"github.com/rancher/os/util" "github.com/rancher/os/util"
"github.com/rancher/os/util/network" "github.com/rancher/os/util/network"
@@ -57,7 +58,7 @@ func consoleSwitch(c *cli.Context) error {
cfg := config.LoadConfig() cfg := config.LoadConfig()
validateConsole(newConsole, cfg) validateConsole(newConsole, cfg)
if newConsole == currentConsole() { if newConsole == CurrentConsole() {
log.Warnf("Console is already set to %s", newConsole) log.Warnf("Console is already set to %s", newConsole)
} }
@@ -127,10 +128,10 @@ func consoleEnable(c *cli.Context) error {
func consoleList(c *cli.Context) error { func consoleList(c *cli.Context) error {
cfg := config.LoadConfig() cfg := config.LoadConfig()
consoles := availableConsoles(cfg) consoles := availableConsoles(cfg)
currentConsole := currentConsole() CurrentConsole := CurrentConsole()
for _, console := range consoles { for _, console := range consoles {
if console == currentConsole { if console == CurrentConsole {
fmt.Printf("current %s\n", console) fmt.Printf("current %s\n", console)
} else if console == cfg.Rancher.Console { } else if console == cfg.Rancher.Console {
fmt.Printf("enabled %s\n", console) fmt.Printf("enabled %s\n", console)
@@ -159,12 +160,28 @@ func availableConsoles(cfg *config.CloudConfig) []string {
return consoles return consoles
} }
func currentConsole() (console string) { // CurrentConsole gets the name of the console that's running
consoleBytes, err := ioutil.ReadFile("/run/console-done") func CurrentConsole() (console string) {
if err == nil { // TODO: replace this docker container look up with a libcompose service lookup?
console = strings.TrimSpace(string(consoleBytes))
} else { // sudo system-docker inspect --format "{{.Config.Image}}" console
client, err := docker.NewSystemClient()
if err != nil {
log.Warnf("Failed to detect current console: %v", err) log.Warnf("Failed to detect current console: %v", err)
} return
}
info, err := client.ContainerInspect(context.Background(), "console")
if err != nil {
log.Warnf("Failed to detect current console: %v", err)
return
}
// parse image name, then remove os- prefix and the console suffix
image, err := reference.ParseNamed(info.Config.Image)
if err != nil {
log.Warnf("Failed to detect current console(%s): %v", info.Config.Image, err)
return
}
console = strings.TrimPrefix(strings.TrimSuffix(image.Name(), "console"), "rancher/os-")
return return
} }

View File

@@ -126,7 +126,7 @@ func consoleInitFunc() error {
log.Error(err) log.Error(err)
} }
if err := ioutil.WriteFile(consoleDone, []byte(cfg.Rancher.Console), 0644); err != nil { if err := ioutil.WriteFile(consoleDone, []byte(CurrentConsole()), 0644); err != nil {
log.Error(err) log.Error(err)
} }

View File

@@ -22,6 +22,7 @@ const (
) )
func dockerInitAction(c *cli.Context) error { func dockerInitAction(c *cli.Context) error {
// TODO: this should be replaced by a "Console ready event watcher"
for { for {
if _, err := os.Stat(consoleDone); err == nil { if _, err := os.Stat(consoleDone); err == nil {
break break

View File

@@ -10,6 +10,8 @@ import (
) )
var ( var (
// TODO: move this into the os-config file so it can be customised.
recoveryDockerService = composeConfig.ServiceConfigV1{ recoveryDockerService = composeConfig.ServiceConfigV1{
Image: config.OsBase, Image: config.OsBase,
Command: yaml.Command{ Command: yaml.Command{