Ejemplo n.º 1
0
func (riakNode *RiakNode) Run() {
	var err error
	var fetchURI string
	var resp *http.Response

	os.Mkdir("root", 0777)

	riakNode.decompress()

	fetchURI = fmt.Sprintf("%s/static2/riak-2.1.1-bin.tar.gz", riakNode.taskData.URI)
	log.Info("Preparing to fetch riak from: ", fetchURI)
	resp, err = http.Get(fetchURI)
	if err != nil {
		log.Panic("Unable to fetch riak root: ", err)
	}
	err = common.ExtractGZ("root", resp.Body)
	if err != nil {
		log.Panic("Unable to extract riak root: ", err)
	}

	config := riakNode.configureRiak(riakNode.taskData)

	c := cepm.NewCPMd(0, riakNode.metadataManager)
	c.Background()
	riakNode.configureAdvanced(c.GetPort())

	args := []string{"console", "-noinput"}

	kernelDirs, err := filepath.Glob("root/riak/lib/kernel*")
	if err != nil {
		log.Fatal("Could not find kernel directory")
	}

	log.Infof("Found kernel dirs: %v", kernelDirs)

	err = cepm.InstallInto(fmt.Sprint(kernelDirs[0], "/ebin"))
	if err != nil {
		log.Panic(err)
	}
	if err := common.KillEPMD("root/riak"); err != nil {
		log.Fatal("Could not kill EPMd: ", err)
	}
	args = append(args, "-no_epmd")
	os.MkdirAll(fmt.Sprint(kernelDirs[0], "/priv"), 0777)
	ioutil.WriteFile(fmt.Sprint(kernelDirs[0], "/priv/cepmd_port"), []byte(fmt.Sprintf("%d.", c.GetPort())), 0777)

	HealthCheckFun := func() error {
		log.Info("Checking is Riak is started")
		data, err := ioutil.ReadFile("root/riak/log/console.log")
		if err != nil {
			if bytes.Contains(data, []byte("Wait complete for service riak_kv")) {
				log.Info("Riak started, waiting 10 seconds to avoid race conditions (HACK)")
				time.Sleep(10 * time.Second)
				return nil
			}
			return errors.New("Riak KV not yet started")
		}
		return err
	}

	wd, err := os.Getwd()
	if err != nil {
		log.Panic("Could not get wd: ", err)
	}
	chroot := filepath.Join(wd, "root")
	riakNode.pm, err = process_manager.NewProcessManager(func() { return }, "/riak/bin/riak", args, HealthCheckFun, &chroot, riakNode.taskData.UseSuperChroot)

	if err != nil {
		log.Error("Could not start Riak: ", err)

		runStatus := &mesos.TaskStatus{
			TaskId: riakNode.taskInfo.GetTaskId(),
			State:  mesos.TaskState_TASK_FAILED.Enum(),
		}
		_, err = riakNode.executor.Driver.SendStatusUpdate(runStatus)
		if err != nil {
			log.Panic("Got error", err)
		}
		// Shutdown:
		time.Sleep(15 * time.Minute)
		log.Info("Shutting down due to GC, after failing to bring up Riak node")
		riakNode.executor.Driver.Stop()
	} else {
		rexPort := riakNode.taskData.RexPort
		riakNode.startRex(rexPort, c)
		rootNode := riakNode.metadataManager.GetRootNode()

		rootNode.CreateChildIfNotExists("coordinator")
		coordinator, err := rootNode.GetChild("coordinator")
		if err != nil {
			log.Panic(err)
		}
		coordinator.CreateChildIfNotExists("coordinatedNodes")
		coordinatedNodes, err := coordinator.GetChild("coordinatedNodes")
		if err != nil {
			log.Panic(err)
		}

		child, err := coordinatedNodes.MakeChild(riakNode.taskInfo.GetTaskId().GetValue(), true)
		if err != nil {
			log.Panic(err)
		}
		coordinatedData := common.CoordinatedData{
			NodeName:      riakNode.taskData.FullyQualifiedNodeName,
			DisterlPort:   int(config.DisterlPort),
			PBPort:        int(config.PBPort),
			HTTPPort:      int(config.HTTPPort),
			Hostname:      riakNode.executor.slaveInfo.GetHostname(),
			ClusterName:   riakNode.taskData.ClusterName,
			FrameworkName: riakNode.taskData.FrameworkName,
		}
		cdBytes, err := coordinatedData.Serialize()
		if err != nil {
			log.Panic("Could not serialize coordinated data	", err)
		}
		child.SetData(cdBytes)
		// lock.Unlock()
		tsd := common.TaskStatusData{
			RexPort: rexPort,
		}
		tsdBytes, err := tsd.Serialize()

		if err != nil {
			log.Panic("Could not serialize Riak Explorer data", err)
		}
		runStatus := &mesos.TaskStatus{
			TaskId: riakNode.taskInfo.GetTaskId(),
			State:  mesos.TaskState_TASK_RUNNING.Enum(),
			Data:   tsdBytes,
		}
		_, err = riakNode.executor.Driver.SendStatusUpdate(runStatus)
		if err != nil {
			log.Panic("Got error", err)
		}
		riakNode.running = true
		go riakNode.runLoop(child)
	}
}
Ejemplo n.º 2
0
func (riakNode *RiakNode) Run() {
	var err error

	config := riakNode.configureRiak(riakNode.taskData)

	c := cepm.NewCPMd(0, riakNode.metadataManager)
	c.Background()
	riakNode.configureAdvanced(c.GetPort())

	args := []string{"console", "-noinput"}

	kernelDirs, err := filepath.Glob("root/riak/lib/kernel*")
	if err != nil {
		log.Fatal("Could not find kernel directory")
	}

	log.Infof("Found kernel dirs: %v", kernelDirs)

	err = cepm.InstallInto(fmt.Sprint(kernelDirs[0], "/ebin"))
	if err != nil {
		log.Panic(err)
	}
	if err := common.KillEPMD("root/riak"); err != nil {
		log.Fatal("Could not kill EPMd: ", err)
	}
	args = append(args, "-no_epmd")
	os.MkdirAll(fmt.Sprint(kernelDirs[0], "/priv"), 0777)
	ioutil.WriteFile(fmt.Sprint(kernelDirs[0], "/priv/cepmd_port"), []byte(fmt.Sprintf("%d.", c.GetPort())), 0777)

	HealthCheckFun := func() error {
		log.Info("Checking is Riak is started")
		data, err := ioutil.ReadFile("root/riak/log/console.log")
		if err != nil {
			if bytes.Contains(data, []byte("Wait complete for service riak_kv")) {
				log.Info("Riak started, waiting 10 seconds to avoid race conditions (HACK)")
				time.Sleep(10 * time.Second)
				return nil
			}
			return errors.New("Riak KV not yet started")
		}
		return err
	}

	wd, err := os.Getwd()
	if err != nil {
		log.Panic("Could not get wd: ", err)
	}
	chroot := filepath.Join(wd, "root")
	riakNode.pm, err = process_manager.NewProcessManager(func() { return }, "/riak/bin/riak", args, HealthCheckFun, &chroot, riakNode.taskData.UseSuperChroot)

	if err != nil {
		log.Error("Could not start Riak: ", err)

		runStatus := &mesos.TaskStatus{
			TaskId: riakNode.taskInfo.GetTaskId(),
			State:  mesos.TaskState_TASK_FAILED.Enum(),
		}
		_, err = riakNode.executor.Driver.SendStatusUpdate(runStatus)
		if err != nil {
			log.Panic("Got error", err)
		}
		// Shutdown:
		time.Sleep(15 * time.Minute)
		log.Info("Shutting down due to GC, after failing to bring up Riak node")
		riakNode.executor.Driver.Stop()
	} else {
		child := riakNode.getCoordinatedChild()
		riakNode.setCoordinatedData(child, config)

		rexPort := riakNode.taskData.HTTPPort
		tsd := common.TaskStatusData{
			RexPort: rexPort,
		}
		tsdBytes, err := tsd.Serialize()

		if err != nil {
			log.Panic("Could not serialize Riak Explorer data", err)
		}

		runStatus := &mesos.TaskStatus{
			TaskId: riakNode.taskInfo.GetTaskId(),
			State:  mesos.TaskState_TASK_RUNNING.Enum(),
			Data:   tsdBytes,
		}
		_, err = riakNode.executor.Driver.SendStatusUpdate(runStatus)
		if err != nil {
			log.Panic("Got error", err)
		}
		riakNode.running = true
		go riakNode.runLoop(child)
	}
}