1
0
mirror of https://github.com/rancher/os.git synced 2025-07-13 06:34:04 +00:00

Keep the sshd config file when using the previous console version

This commit is contained in:
niusmallnan 2019-04-30 11:03:17 +08:00 committed by niusmallnan
parent 19cf9b28bc
commit 4546fac3c2

View File

@ -31,6 +31,8 @@ const (
rancherHome = "/home/rancher" rancherHome = "/home/rancher"
startScript = "/opt/rancher/bin/start.sh" startScript = "/opt/rancher/bin/start.sh"
runLockDir = "/run/lock" runLockDir = "/run/lock"
sshdFile = "/etc/ssh/sshd_config"
sshdTplFile = "/etc/ssh/sshd_config.tpl"
) )
type symlink struct { type symlink struct {
@ -322,26 +324,33 @@ func writeRespawn(user string, sshd, recovery bool) error {
} }
func modifySshdConfig(cfg *config.CloudConfig) error { func modifySshdConfig(cfg *config.CloudConfig) error {
os.Remove("/etc/ssh/sshd_config") _, err := os.Stat(sshdTplFile)
sshdTpl, err := template.ParseFiles("/etc/ssh/sshd_config.tpl") if err == nil {
if err != nil { os.Remove(sshdFile)
return err sshdTpl, err := template.ParseFiles(sshdTplFile)
} if err != nil {
f, err := os.OpenFile("/etc/ssh/sshd_config", os.O_WRONLY|os.O_CREATE, 0644) return err
if err != nil { }
return err f, err := os.OpenFile(sshdFile, os.O_WRONLY|os.O_CREATE, 0644)
} if err != nil {
defer f.Close() return err
}
defer f.Close()
config := map[string]string{} config := map[string]string{}
if cfg.Rancher.SSH.Port > 0 && cfg.Rancher.SSH.Port < 65355 { if cfg.Rancher.SSH.Port > 0 && cfg.Rancher.SSH.Port < 65355 {
config["Port"] = strconv.Itoa(cfg.Rancher.SSH.Port) config["Port"] = strconv.Itoa(cfg.Rancher.SSH.Port)
} }
if cfg.Rancher.SSH.ListenAddress != "" { if cfg.Rancher.SSH.ListenAddress != "" {
config["ListenAddress"] = cfg.Rancher.SSH.ListenAddress config["ListenAddress"] = cfg.Rancher.SSH.ListenAddress
}
return sshdTpl.Execute(f, config)
} else if os.IsNotExist(err) {
return nil
} }
return sshdTpl.Execute(f, config) return err
} }
func setupSSH(cfg *config.CloudConfig) error { func setupSSH(cfg *config.CloudConfig) error {