Start Up is a simple deployment tool that performs given set of commands on multiple hosts in parallel.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

137 lines
2.4 KiB

package stup
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"os/user"
"github.com/pkg/errors"
)
type LocalhostClient struct {
cmd *exec.Cmd
user string
pass string
sudo bool
stdin io.WriteCloser
stdout io.Reader
stderr io.Reader
running bool
env string //export FOO="bar"; export BAR="baz";
}
func (c *LocalhostClient) Connect(_ string) error {
u, err := user.Current()
if err != nil {
return err
}
if c.user == "" {
c.user = u.Username
}
return nil
}
func (c *LocalhostClient) Run(task *Task) error {
var err error
if c.running {
return fmt.Errorf("Command already running")
}
cmd := exec.Command("bash", "-c", c.env+task.Run)
c.cmd = cmd
c.stdout, err = cmd.StdoutPipe()
if err != nil {
return err
}
c.stderr, err = cmd.StderrPipe()
if err != nil {
return err
}
c.stdin, err = cmd.StdinPipe()
if err != nil {
return err
}
var rsOut bytes.Buffer
endChan := make(chan struct{})
if c.sudo {
go sudoFunc(c.pass, c.stdin, &rsOut, endChan)
}
if err := c.cmd.Start(); err != nil {
fmt.Fprintf(os.Stderr, "%v", errors.Wrap(err, "some went wrong when trying execute remote command"))
return ErrTask{task, err.Error()}
}
if err := c.Wait(); err != nil {
return err
}
c.running = true
return nil
}
func (c *LocalhostClient) Wait() error {
if !c.running {
return fmt.Errorf("trying to wait on stopped command")
}
err := c.cmd.Wait()
c.running = false
return err
}
func (c *LocalhostClient) Close() error {
return nil
}
func (c *LocalhostClient) Stdin() io.WriteCloser {
return c.stdin
}
func (c *LocalhostClient) Stderr() io.Reader {
return c.stderr
}
func (c *LocalhostClient) Stdout() io.Reader {
return c.stdout
}
func (c *LocalhostClient) Prefix() (string, int) {
host := c.user + "@localhost" + " | "
return ResetColor + host, len(host)
}
func (c *LocalhostClient) Write(p []byte) (n int, err error) {
return c.stdin.Write(p)
}
func (c *LocalhostClient) WriteClose() error {
return c.stdin.Close()
}
func (c *LocalhostClient) Signal(sig os.Signal) error {
return c.cmd.Process.Signal(sig)
}
func ResolveLocalPath(cwd, path, env string) (string, error) {
// Check if file exists first. Use bash to resolve $ENV_VARs.
cmd := exec.Command("bash", "-c", env+"echo -n "+path)
cmd.Dir = cwd
resolvedFilename, err := cmd.Output()
if err != nil {
return "", errors.Wrap(err, "resolving path failed")
}
return string(resolvedFilename), nil
}