Skip to content

Commit

Permalink
Merge pull request #81 from cuisongliu/bugfix/69
Browse files Browse the repository at this point in the history
Bugfix/69
  • Loading branch information
fanux authored Aug 9, 2019
2 parents 70f6c26 + f631b6a commit 7f0d095
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func init() {

cleanCmd.Flags().StringVar(&install.User, "user", "root", "servers user name for ssh")
cleanCmd.Flags().StringVar(&install.Passwd, "passwd", "passwd", "password for ssh")
cleanCmd.Flags().StringVar(&install.PrivateKeyFile, "pk", "/root/.ssh/id_rsa", "private key for ssh")

cleanCmd.Flags().StringSliceVar(&masters, "master", []string{}, "kubernetes masters")
cleanCmd.Flags().StringSliceVar(&nodes, "node", []string{}, "kubernetes nodes")
Expand Down
2 changes: 2 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func init() {
// Here you will define your flags and configuration settings.
initCmd.Flags().StringVar(&install.User, "user", "root", "servers user name for ssh")
initCmd.Flags().StringVar(&install.Passwd, "passwd", "passwd", "password for ssh")
initCmd.Flags().StringVar(&install.PrivateKeyFile, "pk", "/root/.ssh/id_rsa", "private key for ssh")

initCmd.Flags().StringVar(&install.KubeadmFile, "kubeadm-config", "", "kubeadm-config.yaml template file")

initCmd.Flags().StringVar(&vip, "vip", "10.103.97.2", "virtual ip")
Expand Down
2 changes: 2 additions & 0 deletions cmd/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func init() {
rootCmd.AddCommand(joinCmd)
joinCmd.Flags().StringVar(&install.User, "user", "root", "servers user name for ssh")
joinCmd.Flags().StringVar(&install.Passwd, "passwd", "passwd", "password for ssh")
joinCmd.Flags().StringVar(&install.PrivateKeyFile, "pk", "/root/.ssh/id_rsa", "private key for ssh")

joinCmd.Flags().StringVar(&vip, "vip", "10.103.97.2", "virtual ip")
joinCmd.Flags().StringSliceVar(&masters, "master", []string{}, "kubernetes masters")
joinCmd.Flags().StringSliceVar(&nodes, "node", []string{}, "kubernetes nodes")
Expand Down
44 changes: 33 additions & 11 deletions install/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
Expand All @@ -20,10 +21,11 @@ import (

//username
var (
User string
Passwd string
KubeadmFile string
Version string
User string
Passwd string
PrivateKeyFile string
KubeadmFile string
Version string
)

const oneMBByte = 1024 * 1024
Expand All @@ -36,7 +38,7 @@ func AddrReformat(host string) string {
}

func ReturnCmd(host, cmd string) string {
session, _ := Connect(User, Passwd, host)
session, _ := Connect(User, Passwd, PrivateKeyFile, host)
defer session.Close()
b, _ := session.CombinedOutput(cmd)
return string(b)
Expand Down Expand Up @@ -79,7 +81,7 @@ func WatchFileSize(host, filename string, size int) {
//Cmd is
func Cmd(host string, cmd string) []byte {
logger.Info(host, " ", cmd)
session, err := Connect(User, Passwd, host)
session, err := Connect(User, Passwd, PrivateKeyFile, host)
if err != nil {
logger.Error(" Error create ssh session failed", err)
panic(1)
Expand Down Expand Up @@ -118,7 +120,7 @@ func RemoteFilExist(host, remoteFilePath string) bool {

//Copy is
func Copy(host, localFilePath, remoteFilePath string) {
sftpClient, err := SftpConnect(User, Passwd, host)
sftpClient, err := SftpConnect(User, Passwd, PrivateKeyFile, host)
if err != nil {
logger.Error("scpCopy:", err)
panic(1)
Expand Down Expand Up @@ -149,10 +151,30 @@ func Copy(host, localFilePath, remoteFilePath string) {
logger.Alert("transfer total size is:", totalMB, "MB")
}
}
func readFile(name string) string {
content, err := ioutil.ReadFile(name)
if err != nil {
logger.Error(err)
return ""
}

return string(content)
}
func sshAuthMethod(passwd, pkFile string) ssh.AuthMethod {
var am ssh.AuthMethod
if pkFile != "" {
pkData := readFile(pkFile)
pk, _ := ssh.ParsePrivateKey([]byte(pkData))
am = ssh.PublicKeys(pk)
} else {
am = ssh.Password(passwd)
}
return am
}

//Connect is
func Connect(user, passwd, host string) (*ssh.Session, error) {
auth := []ssh.AuthMethod{ssh.Password(passwd)}
func Connect(user, passwd, pkFile, host string) (*ssh.Session, error) {
auth := []ssh.AuthMethod{sshAuthMethod(passwd, pkFile)}
config := ssh.Config{
Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"},
}
Expand Down Expand Up @@ -192,7 +214,7 @@ func Connect(user, passwd, host string) (*ssh.Session, error) {
}

//SftpConnect is
func SftpConnect(user, password, host string) (*sftp.Client, error) {
func SftpConnect(user, passwd, pkFile, host string) (*sftp.Client, error) {
var (
auth []ssh.AuthMethod
addr string
Expand All @@ -203,7 +225,7 @@ func SftpConnect(user, password, host string) (*sftp.Client, error) {
)
// get auth method
auth = make([]ssh.AuthMethod, 0)
auth = append(auth, ssh.Password(password))
auth = append(auth, sshAuthMethod(passwd, pkFile))

clientConfig = &ssh.ClientConfig{
User: user,
Expand Down

0 comments on commit 7f0d095

Please sign in to comment.