示例#1
0
文件: service.go 项目: pb-it/rexray
func tryToStartDaemon() {
	_, _, thisAbsPath := util.GetThisPathParts()

	fmt.Print("Starting REX-Ray...")

	signal := make(chan byte)
	client := fmt.Sprintf("%s/%s.sock", os.TempDir(), util.RandomString(32))
	log.WithField("client", client).Debug("trying to start service")

	l, lErr := net.Listen("unix", client)
	failOnError(lErr)

	go func() {
		conn, acceptErr := l.Accept()
		if acceptErr != nil {
			fmt.Printf("FAILED!\n  %v\n", acceptErr)
			panic(acceptErr)
		}
		defer conn.Close()
		defer os.Remove(client)

		log.Debug("accepted connection")

		buff := make([]byte, 1)
		conn.Read(buff)

		log.Debug("received data")

		signal <- buff[0]
	}()

	cmdArgs := []string{
		"start",
		fmt.Sprintf("--client=%s", client),
		fmt.Sprintf("--logLevel=%v", r.Config.LogLevel)}

	if r.Config.Host != "" {
		cmdArgs = append(cmdArgs, fmt.Sprintf("--host=%s", r.Config.Host))
	}

	cmd := exec.Command(thisAbsPath, cmdArgs...)
	cmd.Stderr = os.Stderr

	cmdErr := cmd.Start()
	failOnError(cmdErr)

	sigVal := <-signal
	if sigVal != 0 {
		fmt.Println("FAILED!")
		panic(1)
	}

	pid, _ := util.ReadPidFile()
	fmt.Printf("SUCESS!\n\n")
	fmt.Printf("  The REX-Ray daemon is now running at PID %d. To\n", pid)
	fmt.Printf("  shutdown the daemon execute the following command:\n\n")
	fmt.Printf("    sudo %s stop\n\n", thisAbsPath)
}
示例#2
0
func TestNewWithBadConfigFilePath(t *testing.T) {
	if _, err := rexray.NewWithConfigFile(util.RandomString(10)); err == nil {
		t.Fatal("expected error from bad config file path")
	}
}