Code Monkey home page Code Monkey logo

magetool's Introduction

A Capistrano/SSHKit Implementation with Golang

Download and Import

go get github.com/zhiminwen/magetool
import (
	"github.com/zhiminwen/magetool/shellkit"
	"github.com/zhiminwen/magetool/sshkit"
)

Synopsis

Mage

Use Mage to organize the tasks together

// +build mage
package main
import (
 "os"
 "github.com/zhiminwen/magetool/shellkit"
)
func init() {
 os.Setenv("MAGEFILE_VERBOSE", "true")
}
//Run Cmd
func RunCmd() {
 shellkit.Execute("cmd", "/c", "type test.log & sleep 10")
}
//dir
func Dir() {
 shellkit.Execute("cmd", "/c", "time /t 1>&2 & exit 1")
}

Shellkit

  • Use Shellkit.Execute() to run local command.
  • Use Shellkit.Capture to capture command output.

SSHkit

Init the client

//Use password
client1 := sshkit.NewSSHClient("192.168.5.10", "22", "ubuntu", "password", "")
//Use private key
s2 := sshkit.NewSSHClient("192.168.5.10", "22", "ubuntu", "", "mykeyfile"
// Use a slice of the client

var servers []*sshkit.SSHClient
servers = append(servers, sshkit.NewSSHClient("192.168.5.10", "22", "ubuntu", "password", ""))
servers = append(servers, sshkit.NewSSHClient("192.168.5.10", "22", "ubuntu2", "password", ""))

Execute a command

// Run against a single client
client1.Execute("id; hostname")
//Run against slice of servers
sshkit.Execute(servers, "id; hostname")

Capture a result

result, err := client.Capture("hostname")
sshkit.ExecuteFunc(servers, func(t *sshkit.SSHClient) {
  id, err := t.Capture("id -u")
  if err != nil {
   log.Printf("Failed to get id:%v", err)
   return
  }
  if id == "1000" {
   return
  }
  cmd := fmt.Sprintf("echo %s; hostname", id)
  t.Execute(cmd)
 })

Upload and Download

client1.Upload("example.txt", "/tmp/example.txt")
client1.Download("/etc/hosts", "hosts.txt")
client1.Put(`This is a test content`, "mytest.txt")
list, err := servers[0].Get("/etc/passwd")

Interact with command

client1.Execute(`rm -rf .ssh/known_hosts`)
client1.ExecuteInteractively("scp ubuntu@ubuntu:/tmp/test.txt /tmp/test.txt.dupped", map[string]string{
  `\(yes/no\)`: "yes",
  "password":   "password",
 })

Full example to build a docker image and push to dockerhub

Source Code

magetool's People

Contributors

zhiminwen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

stevenacoffman

magetool's Issues

log.Fatal hides problems

Using log.Fatal instead of returning an error means the caller has to call mage with the verbose flag to see why something is dying. It would be nicer to let the consumer of this library handle those errors as they see fit.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.