Example #1
0
func StartLocalServer(ctx *macaron.Context, log *logs.BeeLogger) (int, []byte) {
	var stdout, stderr bytes.Buffer
	header := ctx.Req.Header
	ip := header.Get("ip")
	port := header.Get("port")
	masterip := header.Get("masterip")
	masterport := header.Get("masterport")
	chunknum := header.Get("chunknum")
	groupid := header.Get("groupid")
	datadir := header.Get("datadir")
	errlogdir := header.Get("errlogdir")
	masterlistenmode := header.Get("masterlistenmode")
	// check if chunkserver binary exsist,if not ,create it
	_, err := os.Stat("./oss/chunkserver/spy_server")
	if err != nil && os.IsNotExist(err) {
		return http.StatusInternalServerError, []byte("Cannot find chunkserver excution file")
	}
	// check if errlog folder exsist , if not ,create it
	_, err = os.Stat(errlogdir)
	if err != nil || os.IsNotExist(err) {
		os.MkdirAll(errlogdir, 0777)
	}
	// check if data folder exsist , if not ,create it
	_, err = os.Stat(datadir)
	if err != nil || os.IsNotExist(err) {
		os.MkdirAll(datadir, 0777)
	}
	// execute chunkserver start script in a new goroutine, if failed, send https request to notify master node
	go func() {
		cmd := exec.Command("./oss/chunkserver/install.sh", "-i", ip, "-p", port, "-m", masterip, "-n", masterport, "-c", chunknum, "-g", groupid, "-d", datadir, "-e", errlogdir)
		cmd.Stdout = &stdout
		cmd.Stderr = &stderr
		err = cmd.Run()
		if err != nil {
			log.Alert("[OSS]Start local ChunkServer error, error INFO: %v", err)
			log.Alert("[OSS]Start local ChunkServer error, STDOUT: %s", stdout.Bytes())
			log.Alert("[OSS]Start local ChunkServer error, STDERR: %s", stderr.Bytes())
			header2send := make(map[string][]string, 0)
			header2send["nodeip"] = []string{ip}
			header2send["nodeport"] = []string{port}
			header2send["groupid"] = []string{groupid}
			header2send["stderr"] = []string{string(stderr.Bytes())}
			header2send["stdout"] = []string{string(stdout.Bytes())}
			switch masterlistenmode {
			case "http":
				result, statusCode, err := util.Call("PUT", "http://"+masterip+":80", "/oss/chunkserver/info", nil, header2send)
				if err != nil {
					log.Error("[OSS] Sent chunkserver info back to chunkmaster error: %v", err)
				}
				if statusCode != http.StatusOK {
					log.Error("[OSS] Sent chunkserver info back to chunkmaster failed STATUS CODE:%v ,result:%s", statusCode, result)
				}
			default:
				result, statusCode, err := util.CallHttps("PUT", "https://"+masterip+":443", "/oss/chunkserver/info", nil, header2send)
				if err != nil {
					log.Error("[OSS] Sent chunkserver info back to chunkmaster error: %v", err)
				}
				if statusCode != http.StatusOK {
					log.Error("[OSS] Sent chunkserver info back to chunkmaster failed STATUS CODE:%v ,result:%s", statusCode, result)
				}
			}
		}
	}()
	return http.StatusOK, []byte("")
}