func (b *Build) connectToDockerHost() (e error) { if b.client != nil { return nil } if b.DockerHostUser == "" { port := b.DockerPort if port == 0 { port = 4243 } b.client = dockerclient.New(fmt.Sprintf("http://%s:%d", b.DockerHost, port)) return nil } c := gossh.New(b.DockerHost, b.DockerHostUser) c.SetPassword(b.DockerHostPassword) con, err := c.Connection() if err != nil { return err } t := &http.Transport{} t.Dial = con.Dial cl := &http.Client{Transport: t} dc := dockerclient.New("http://" + b.DockerHost + ":4243") dc.Client = cl b.client = dc return nil }
func sshConn(bak *Backup) { fmt.Println("--------------------------------------------------------") client := gossh.New(host, user) defer client.Close() client.SetPassword(password) // client.DebugWriter = MakeLogger("[DEBUG]") client.InfoWriter = MakeLogger("[INFO]") client.ErrorWriter = MakeLogger("[ERROR]") client.Info("正在执行命令: 备份【" + bak.fileName + "】,请耐心等待.") rsp, err := client.Execute(bak.cmd) if rsp.Success() { client.Info("执行命令: 备份【" + bak.fileName + "】成功.") } else { checkError(err) } conn, err := client.Connection() checkError(err) defer conn.Close() sftpConn, err := sftp.NewClient(conn, sftp.MaxPacket(1<<15)) checkError(err) var filePath string if bak.fileName == "zentaopms.tar.gz" { srcSqlPath = "/var/www" filePath = bak.pathName + "/" } else { filePath = MkBakDir(bak.pathName + "/SQL/") } srcFile, err := sftpConn.Open(srcSqlPath + "/" + bak.fileName) defer srcFile.Close() checkError(err) info, _ := srcFile.Stat() dstFile, err := os.Create(filePath + bak.fileName) defer dstFile.Close() checkError(err) client.Info("正在下载备份的文件【" + bak.fileName + "】,请耐心等待.") _, err = io.Copy(dstFile, io.LimitReader(srcFile, info.Size())) checkError(err) client.Info("下载备份文件【" + bak.fileName + "】完成.\n") }
// PerformDeploy performs all of the deployment steps func PerformDeploy(host string, app string, branch string) { config := ReadConfig(fmt.Sprintf("%s.json", app)) client := gossh.New(host, config.User) client.DebugWriter = MakeLogger("DEBUG") client.InfoWriter = MakeLogger("INFO ") client.ErrorWriter = MakeLogger("ERROR") defer client.Close() SetupWorkDirectory(config, client) CheckoutRepo(branch, config, client) RunCommandChain(config.Commands, client) CleanupRelease(config, client) }
func main() { client := gossh.New("10.10.2.222", "root") // my default agent authentication is used. use client.SetPassword("hpiman") // for password authentication client.DebugWriter = MakeLogger("DEBUG") client.InfoWriter = MakeLogger("INFO ") client.ErrorWriter = MakeLogger("ERROR") client.Execute("mysqldump -R -uroot -p123456 hupunac >/root/cody_develop/sql/hupunac.sql") conn, _ := client.Connection() c, err := sftp.NewClient(conn, sftp.MaxPacket(5e9)) c.OpenFile("/root/cody_develop/sql", "hupunac.sql") defer client.Close() }
func main() { client := gossh.New("10.10.3.227", "root") // my default agent authentication is used. use client.SetPassword("123456") // for password authentication client.DebugWriter = MakeLogger("DEBUG") client.InfoWriter = MakeLogger("INFO ") client.ErrorWriter = MakeLogger("ERROR") defer client.Close() conn, _ := client.Connection() defer conn.Close() session, err := conn.NewSession() if err != nil { panic("Failed to create session: " + err.Error()) } defer session.Close() session.Stdout = os.Stdout session.Stderr = os.Stderr session.Stdin = os.Stdin // Set up terminal modes modes := ssh.TerminalModes{ ssh.ECHO: 1, // disable echoing ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud } // Request pseudo terminal if err := session.RequestPty("xterm", 25, 100, modes); err != nil { log.Fatalf("request for pseudo terminal failed: %s", err) } // Start remote shell if err := session.Shell(); err != nil { log.Fatalf("failed to start shell: %s", err) } fmt.Sprint("admin\n") fmt.Sprint("admin2015\n") err = session.Wait() if err != nil { log.Fatal(err) } }
func sshCmd(sqlcmd, srcdir, desdir string, debug bool) { client := gossh.New("10.10.2.222", "root") // my default agent authentication is used. use client.SetPassword("hpiman") // for password authentication if debug { client.DebugWriter = MakeLogger("[DEBUG]") } client.InfoWriter = MakeLogger("[INFO]") client.ErrorWriter = MakeLogger("[ERROR]") defer client.Close() // 获取备份数据库的名字 pc, _, _, _ := runtime.Caller(1) funcname := runtime.FuncForPC(pc).Name() switch funcname { case "main.JavaBackup": backName = "hupunac.sql" case "main.RegisterBackup": backName = "licensemanager.sql" case "main.WebSiteBackup": backName = "hupuwebsite.sql" case "main.BusinessBackup": backName = "hupuerp.sql" case "main.ProcutBackup": backName = "hupu.sql" case "main.BbsBackup": backName = "hupubbs.sql" } rsp, err := client.Execute(sqlcmd) if rsp.Success() { client.InfoWriter("执行命令: [备份" + backName + "] 成功.") } else { checkError(sqlcmd, err) } defer client.Close() conn, err := client.Connection() defer conn.Close() c, err := sftp.NewClient(conn, sftp.MaxPacket(1<<15)) checkError("unable to start sftp subsytem: 10.10.2.222", err) defer c.Close() wr, err := c.Open(srcdir + backName) checkError("unable open the linux file: "+srcdir+backName, err) defer wr.Close() info, _ := wr.Stat() filelocal, err := os.Create(desdir + backName) checkError("unable down the file: "+desdir+backName, err) defer filelocal.Close() client.InfoWriter("正在下载备份文件[" + backName + "]") _, err = io.Copy(filelocal, io.LimitReader(wr, info.Size())) checkError("unable copy the file: "+desdir+backName, err) client.InfoWriter("下载备份文件[" + backName + "]完成\n") }