Esempio n. 1
0
func startNodejs(host client.Anchor, mysqlIP, mysqlPort string) (ip, port string) {
	defer func() {
		if recover() != nil {
			fatalf("connection to host lost")
		}
	}()

	// Start node.js application
	ip = getUbuntuHostPublicIP(host)
	port = "8080"
	job := host.Walk([]string{"nodejs"})
	shell := fmt.Sprintf(
		"sudo /usr/bin/nodejs index.js "+
			"--mysql_host %s --mysql_port %s --api_host %s --api_port %s "+
			"&> /tmp/tutorial-nodejs.log",
		mysqlIP, mysqlPort,
		"0.0.0.0", port,
	)
	proc, err := job.MakeProc(client.Cmd{
		Path:  "/bin/sh",
		Dir:   "/home/ubuntu/nodejs",
		Args:  []string{"-c", shell},
		Scrub: true,
	})
	if err != nil {
		fatalf("nodejs app already running")
	}
	proc.Stdin().Close()
	proc.Stdout().Close()
	proc.Stderr().Close()

	return
}
Esempio n. 2
0
func runShellStdin(host client.Anchor, cmd, stdin string) (string, error) {
	defer func() {
		if recover() != nil {
			fatalf("connection to host lost")
		}
	}()
	job := host.Walk([]string{"shelljob", strconv.Itoa(rand.Int())})
	proc, _ := job.MakeProc(client.Cmd{
		Path:  "/bin/sh",
		Dir:   "/tmp",
		Args:  []string{"-c", cmd},
		Scrub: true,
	})
	go func() {
		io.Copy(proc.Stdin(), bytes.NewBufferString(stdin))
		proc.Stdin().Close() // Must close the standard input of the shell process.
	}()
	proc.Stderr().Close() // Close to indicate discarding standard error
	var buf bytes.Buffer
	io.Copy(&buf, proc.Stdout())
	stat, _ := proc.Wait()
	return buf.String(), stat.Exit
}