package cluster

import (
	"bytes"
	"fmt"
	"log"
	"mfw/util"
	"net"

	"golang.org/x/crypto/ssh"
)

func ssHConnect(user, password, host string, port int) (*ssh.Session, error) {
	util.Catch()
	var (
		auth         []ssh.AuthMethod
		addr         string
		clientConfig *ssh.ClientConfig
		client       *ssh.Client
		session      *ssh.Session
		err          error
	)
	// get auth method
	auth = make([]ssh.AuthMethod, 0)
	auth = append(auth, ssh.Password(password))
	hostKeyCallbk := func(hostname string, remote net.Addr, key ssh.PublicKey) error {
		return nil
	}
	clientConfig = &ssh.ClientConfig{
		User: user,
		Auth: auth,
		// Timeout:             30 * time.Second,
		HostKeyCallback: hostKeyCallbk,
	}
	// connet to ssh
	addr = fmt.Sprintf("%s:%d", host, port)

	if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
		return nil, err
	}
	// create session
	if session, err = client.NewSession(); err != nil {
		return nil, err
	}
	return session, nil
}

/*
#!/bin/bash
kill -9 $(pidof extract_v3)
rm -rf extract_v3*
mkdir extract_v3
cd /opt/extract_v3
wget http://10.171.112.160:9090/res/extract_v3.tgz
tar -xzvf extract_v3.tgz
chmod 777 extract_v3
nohup ./extract_v3 >/opt/extract_v3/nohup 2>&1 &
exit
*/

func RunSsh(ip string) bool {
	util.Catch()
	var b bool
	var stdOut, stdErr bytes.Buffer
	session, err := ssHConnect("root", Password, ip, 22)
	if err != nil {
		log.Println(err)
		return b
	}
	defer session.Close()
	session.Stdout = &stdOut
	session.Stderr = &stdErr

	var sshstr = `
#!/bin/bash
kill -9 $(pidof udp2019)
yum install -y docker docker-compose unzip
systemctl start docker
docker pull registry-vpc.cn-beijing.aliyuncs.com/file2text/github_ocr:tleyden5iwx_open-ocr-preprocessor
docker tag 00a689ddd4f8 tleyden5iwx/open-ocr-preprocessor:latest
docker rmi registry-vpc.cn-beijing.aliyuncs.com/file2text/github_ocr:tleyden5iwx_open-ocr-preprocessor
docker pull registry-vpc.cn-beijing.aliyuncs.com/file2text/github_ocr:rabbitmq
docker tag 5335b737c380 rabbitmq:3.6.5-management
docker rmi registry-vpc.cn-beijing.aliyuncs.com/file2text/github_ocr:rabbitmq
docker pull registry-vpc.cn-beijing.aliyuncs.com/file2text/github_ocr:tleyden5iwx_open-ocr-2
docker tag c99e1ea480 tleyden5iwx/open-ocr-2:latest
docker rmi registry-vpc.cn-beijing.aliyuncs.com/file2text/github_ocr:tleyden5iwx_open-ocr-2
cd /home/
wget http://10.171.112.160:10100/res/auto.zip
unzip auto.zip
wget http://10.171.112.160:10100/res/open-ocr.tar
tar -xvf open-ocr.tar 
chmod 777 /home/f2text/file2text
chmod 777 /home/udp/udp2019

cd /home/open-ocr/docker-compose/
export OPEN_OCR_INSTANCE=open-ocr-2
export RABBITMQ_HOST=` + ip + `
docker-compose up -d
docker pull registry-vpc.cn-beijing.aliyuncs.com/file2text/gocv:v2-binbash2
docker run --name f2text -itd -v /home/:/home/ --net host registry-vpc.cn-beijing.aliyuncs.com/file2text/gocv:v2-binbash2 /bin/bash
docker exec  f2text /etc/profile.d/auto.sh

cd /home/udp/
sed -i "s/127.0.0.1/` + ip + `/g" /home/udp/config.json
nohup ./udp2019 >/home/udp/log.out 2>&1 &

`
	log.Println("连接到ip:", ip,"成功")
	if err := session.Run(sshstr); err == nil {
		b = true
		log.Println(ip, "ssh over ok")
	} else {
		log.Println(err)
	}
	return b
}
func SshPgrep(ip, cmd string) (bool, string ){
	var bStdout bytes.Buffer
	session, err := ssHConnect("root", Password, ip, 22)
	defer session.Close()
	var b bool
	if err != nil {
		log.Println(err)
		return b,cmd+" ssh err"+err.Error()
	}
	session.Stdout = &bStdout
	var tmpstr string
	if err := session.Run(cmd); err == nil {
		b = true
		if len(bStdout.String()) == 0 {
			b = false
		}
		log.Println(ip, cmd, "pid:", bStdout.String())
		tmpstr = fmt.Sprintf(" %s  %s  %s  %s",ip, cmd, "pid:", bStdout.String())
	} else {
		log.Println(ip, cmd, "err", err)
		tmpstr = fmt.Sprintf(" %s  %s  %s  %s",ip, cmd, "err:", err.Error())
	}
	return b,tmpstr
}
