Exemplo n.º 1
0
func isRunning() bool {
	c, err := client.NewUnixConn(Viper)
	if c != nil {
		c.Close()
	}
	if err == nil {
		return true
	}
	return false
}
Exemplo n.º 2
0
)

// runCmd represents the run command
var runCmd = &cobra.Command{
	Use:   "run",
	Short: "Run the given command in the lateral server",
	Long:  `A longer description that spans multiple lines and likely contains examples`,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			panic(fmt.Errorf("No command specified"))
		}
		fds, err := platform.GetFds()
		if err != nil {
			panic(fmt.Errorf("Failed to determine filedescriptors to send: %v", err))
		}
		c, err := client.NewUnixConn(Viper)
		if err != nil {
			panic(fmt.Errorf("Error connecting to server: %v", err))
		}
		defer c.Close()
		wd, err := os.Getwd()
		if err != nil {
			panic(fmt.Errorf("Error determining working directory"))
		}
		exe, err := exec.LookPath(args[0])
		if err != nil {
			panic(fmt.Errorf("Failed to find executable %v", args[0]))
		}
		req := &server.Request{
			Type:   server.REQUEST_RUN,
			HasFds: true,
Exemplo n.º 3
0
// Integration testing client+server with an actual unix socket.
func TestRunConnection(t *testing.T) {
	v := makeTestViper()
	l, err := server.NewUnixListener(v)
	if err != nil {
		t.Fatal(err)
	}
	defer l.Close()
	runFinished := make(chan struct{})
	go func() {
		server.Run(v, l)
		close(runFinished)
	}()
	c, err := client.NewUnixConn(v)
	if err != nil {
		t.Fatal(err)
	}
	defer c.Close()
	req := &server.Request{
		Type: server.REQUEST_GETPID,
	}
	err = client.SendRequest(c, req)
	if err != nil {
		t.Fatal(err)
	}
	resp, err := client.ReceiveResponse(c)
	if err != nil {
		t.Fatal("got error", err)
	} else if resp.Type == server.RESPONSE_ERR {
		t.Fatal("got error", resp.Message)
	}
	if resp.Getpid.Pid != os.Getpid() {
		t.Error("Pid didn't match")
	}

	req.Fds = []int{0, 1, 2}
	req.HasFds = true
	req.Type = server.REQUEST_RUN
	req.Run = &server.RequestRun{
		Exe:  "/bin/echo",
		Args: []string{"/bin/echo", "foo"},
		Env:  os.Environ(),
	}

	err = client.SendRequest(c, req)
	if err != nil {
		t.Fatal(err)
	}
	resp, err = client.ReceiveResponse(c)
	if err != nil {
		t.Fatal("got error", err)
	} else if resp.Type == server.RESPONSE_ERR {
		t.Fatal("got error", resp.Message)
	}

	sdr := &server.Request{
		Type: server.REQUEST_SHUTDOWN,
	}
	err = client.SendRequest(c, sdr)
	if err != nil {
		t.Fatal(err)
	}
	resp, err = client.ReceiveResponse(c)
	if err != nil {
		t.Fatal("got error", err)
	} else if resp.Type == server.RESPONSE_ERR {
		t.Fatal("got error", resp.Message)
	}
	// This channel gets closed when Run() returns.
	<-runFinished
}