gopkg.in/dedis/onet.v2@v2.0.0-20181115163211-c8f3724038a7/simul/platform/cliutils.go (about)

     1  package platform
     2  
     3  // Used for shell-commands
     4  
     5  import (
     6  	"bufio"
     7  	"bytes"
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"runtime"
    12  
    13  	"gopkg.in/dedis/onet.v2/log"
    14  )
    15  
    16  // Scp copies the given files to the remote host
    17  func Scp(username, host, file, dest string) error {
    18  	addr := host + ":" + dest
    19  	if username != "" {
    20  		addr = username + "@" + addr
    21  	}
    22  	cmd := exec.Command("scp", "-r", file, addr)
    23  	cmd.Stdout = os.Stdout
    24  	cmd.Stderr = os.Stderr
    25  	return cmd.Run()
    26  }
    27  
    28  // Rsync copies files or directories to the remote host. If the DebugVisible
    29  // is > 1, the rsync-operation is displayed on screen.
    30  func Rsync(username, host, file, dest string) error {
    31  	addr := host + ":" + dest
    32  	if username != "" {
    33  		addr = username + "@" + addr
    34  	}
    35  	cmd := exec.Command("rsync", "-Pauz", "-e", "ssh -T -o Compression=no -x", file, addr)
    36  	cmd.Stderr = os.Stderr
    37  	if log.DebugVisible() > 1 {
    38  		cmd.Stdout = os.Stdout
    39  	}
    40  	return cmd.Run()
    41  }
    42  
    43  // SSHRun runs a command on the remote host
    44  func SSHRun(username, host, command string) ([]byte, error) {
    45  	addr := host
    46  	if username != "" {
    47  		addr = username + "@" + addr
    48  	}
    49  
    50  	cmd := exec.Command("ssh", "-o", "StrictHostKeyChecking=no", addr,
    51  		"eval '"+command+"'")
    52  	return cmd.Output()
    53  }
    54  
    55  // SSHRunStdout runs a command on the remote host but redirects stdout and
    56  // stderr of the Ssh-command to the os.Stderr and os.Stdout
    57  func SSHRunStdout(username, host, command string) error {
    58  	addr := host
    59  	if username != "" {
    60  		addr = username + "@" + addr
    61  	}
    62  
    63  	log.Lvl4("Going to ssh to", addr, command)
    64  	cmd := exec.Command("ssh", "-o", "StrictHostKeyChecking=no", addr,
    65  		"eval '"+command+"'")
    66  	cmd.Stderr = os.Stderr
    67  	cmd.Stdout = os.Stdout
    68  	return cmd.Run()
    69  }
    70  
    71  // Build builds the the golang packages in `path` and stores the result in `out`. Besides specifying the environment
    72  // variables GOOS and GOARCH you can pass any additional argument using the buildArgs
    73  // argument. The command which will be executed is of the following form:
    74  // $ go build -v buildArgs... -o out path
    75  func Build(path, out, goarch, goos string, buildArgs ...string) (string, error) {
    76  	// When cross-compiling:
    77  	// Run "go install" for the stdlib, to speed up future builds.
    78  	// The first time we run this it builds and installs. Afterwards,
    79  	// this finishes quickly and the later "go build" is faster.
    80  	if goarch != runtime.GOARCH || goos != runtime.GOOS {
    81  		cmd := exec.Command("go", []string{"env", "GOROOT"}...)
    82  		gosrcB, err := cmd.Output()
    83  		if err == nil {
    84  			gosrcB := bytes.TrimRight(gosrcB, "\n\r")
    85  			gosrc := filepath.Join(string(gosrcB), "src")
    86  			cmd = exec.Command("go", []string{"install", "./..."}...)
    87  			log.Lvl4("Installing cross-compilation stdlib in", gosrc)
    88  			cmd.Env = append([]string{"GOOS=" + goos, "GOARCH=" + goarch}, os.Environ()...)
    89  			cmd.Dir = gosrc
    90  			log.Lvl4("Command:", cmd.Args, "in directory", gosrc)
    91  			// Ignore errors from here; perhaps we didn't have rights to write.
    92  			cmd.Run()
    93  		}
    94  	}
    95  
    96  	var cmd *exec.Cmd
    97  	var b bytes.Buffer
    98  	buildBuffer := bufio.NewWriter(&b)
    99  	wd, _ := os.Getwd()
   100  	log.Lvl4("In directory", wd)
   101  	var args []string
   102  	args = append(args, "build", "-v")
   103  	args = append(args, buildArgs...)
   104  	args = append(args, "-o", out, path)
   105  	cmd = exec.Command("go", args...)
   106  	log.Lvl4("Building", cmd.Args, "in", path)
   107  	cmd.Stdout = buildBuffer
   108  	cmd.Stderr = buildBuffer
   109  	cmd.Env = append([]string{"GOOS=" + goos, "GOARCH=" + goarch}, os.Environ()...)
   110  	wd, err := os.Getwd()
   111  	log.Lvl4(wd)
   112  	log.Lvl4("Command:", cmd.Args)
   113  	err = cmd.Run()
   114  	log.Lvl4(b.String())
   115  	return b.String(), err
   116  }
   117  
   118  // KillGo kills all go-instances
   119  func KillGo() {
   120  	cmd := exec.Command("killall", "go")
   121  	if err := cmd.Run(); err != nil {
   122  		log.Lvl3("Couldn't kill all go instances:", err)
   123  	}
   124  }