Ejemplo n.º 1
0
Archivo: engine.go Proyecto: ahjdzx/dis
func requestHeartbeat(heartbeatURL string, hbtRequest *model.HeartbeatRequest) (hbtResponse *model.HeartbeatResponse, err error) {
	b, err := json.Marshal(hbtRequest)
	if err != nil {
		log.Errorln(err)
		return
	}
	resp, err := http.Post(heartbeatURL, "application/json;charset=utf-8", bytes.NewBuffer(b))
	if err != nil {
		log.Errorln(err)
		return
	}
	if resp.StatusCode != http.StatusOK {
		log.Warnf("StatusCode: %d", resp.StatusCode)
		return
	}

	defer resp.Body.Close()
	result, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Errorln(err)
		return
	}
	hbtResponse = &model.HeartbeatResponse{}
	err = json.Unmarshal(result, hbtResponse)
	if err != nil {
		log.Errorln(err)
		return
	}
	return
}
Ejemplo n.º 2
0
Archivo: runner.go Proyecto: ahjdzx/dis
func Run() {
	Watcher.Add(&DockerContainers{})
	for {
		select {
		case data := <-Watcher.DataCh:
			{
				view = data

				log.Debugf("name: %s, data: %v", data.Dependency.Name(), data.Data)
				switch data.Dependency.Name() {
				case "docker_containers":
					{
						containers := data.Data.([]*Container)
						localComps := LocalComponents()
						log.Infoln("localComps = ", localComps)
						for _, comp := range localComps {
							switch comp.Name {
							case "dis-collectd":
								{
									err := rewriteConfigForCollectd(comp, containers)
									if err != nil {
										log.Errorln(err)
										continue
									}
									out, err := utils.ExecCommand(false, comp.DeployFilepath, "reload")
									if err != nil {
										log.Errorf("Execute command '%s reload' error: %s", comp.DeployFilepath, err)
										continue
									}
									log.Debugln(out)
								}
							case "logstash-forwarder":
								{
									err := rewriteConfigForLogstashForwarder(comp, containers)
									if err != nil {
										log.Errorln(err)
										continue
									}
									out, err := utils.ExecCommand(false, comp.DeployFilepath, "restart")
									if err != nil {
										log.Errorf("Execute command '%s restart' error: %s", comp.DeployFilepath, err.Error())
										continue
									}
									log.Debugln(out)
								}
							}
						}
					}
				}
			}
		case err := <-Watcher.ErrCh:
			log.Errorln(err)
		}
	}
}
Ejemplo n.º 3
0
Archivo: engine.go Proyecto: ahjdzx/dis
func LocalComponents() (remoteComponents map[string]*model.Component) {
	subDirs, err := utils.SubDirs(stackDir)
	if err != nil {
		log.Errorln(err)
		return
	}

	remoteComponents = make(map[string]*model.Component)
	for _, subDir := range subDirs {
		log.Infoln("subDir: ", subDir)

		// If .version file not found in current directory, so we considered that not a remote component existed.
		versionFile := path.Join(stackDir, subDir, ".version")
		if !utils.IsExist(versionFile) {
			log.Errorf("Can't found .version file: %s", versionFile)
			continue
		}

		versionBytes, err := utils.ReadFile(versionFile)
		if err != nil {
			log.Errorf("Failed to read %s/.version : %v", subDir, err)
			continue
		}

		version := strings.TrimSpace(string(versionBytes))
		cmd := exec.Command("./deploy", "status")
		cmd.Dir = path.Join(stackDir, subDir, version)
		statusOutput, err := cmd.CombinedOutput()
		if err != nil {
			log.Errorf("Failed to run command: %s ,error: %v", "./deploy status", err)
			continue
		}
		status := strings.TrimSpace(string(statusOutput))
		log.Infof("local componet: %s, stauts: %s", subDir, status)
		if strings.Contains(status, "stoped") {
			os.Remove(versionFile)
			continue
		}

		remoteComp := &model.Component{
			Name:      subDir,
			Version:   version,
			Status:    status,
			Timestamp: time.Now(),
		}
		remoteComp.InitAttrs(stackDir)
		remoteComponents[remoteComp.Name] = remoteComp
	}
	return
}
Ejemplo n.º 4
0
Archivo: engine.go Proyecto: ahjdzx/dis
// Update or create
func heartbeat() {
	log.Debugln("heartbeat......")
	log.Debugln("stackDir is ", stackDir)

	localComps := LocalComponents()

	hbtRequest, err := buildHeartbeatRequest()
	if err != nil {
		log.Errorln(err)
		return
	}
	hbtRequest.RemoteComponents = localComps

	log.Infoln("request: ", hbtRequest)
	// http request to server heartbeat url.
	if config.AppConfig().Server == "" {
		log.Errorln("configuration server is blank.")
		return
	}
	heartbeatURL := fmt.Sprintf("http://%s/heartbeat", config.AppConfig().Server)
	hbtResponse, err := requestHeartbeat(heartbeatURL, hbtRequest)
	if err != nil {
		log.Errorln(err)
		return
	}
	log.Infoln("response: ", hbtResponse)

	if hbtResponse != nil && hbtResponse.Components != nil {
		log.Infoln("response comps : ", hbtResponse.Components)
		for _, newComp := range hbtResponse.Components {
			newComp.InitAttrs(stackDir)
			log.Debugf("newComp: %s", newComp)

			// 1. Create version directory.   stack_dir/comp_name/version_number/
			if utils.IsExist(newComp.VersionDir) {
				_, err := utils.ExecCommand(false, "mkdir", "-p", newComp.VersionDir)
				if err != nil {
					log.Errorln(err)
					continue
				}
			}

			// 2. Download new version component.
			downURL := fmt.Sprintf("http://%s/%s", config.AppConfig().Server, newComp.TarballFilename)
			err = downloadFromURL(newComp.TarballFilepath, downURL)
			if err != nil {
				log.Errorln(err)
				continue
			}
			// 3. Uncompress the new component file.
			output, err := utils.ExecCommand(false, "tar", "-zxf", newComp.TarballFilepath, "-C", newComp.VersionDir)
			if err != nil {
				log.Errorln(err)
				continue
			}
			log.Debugf("Untar file: %s , output: %s", newComp.TarballFilepath, output)

			if newComp.Cmd == "start" {
				if view != nil {
					containers := view.Data.([]*Container)
					switch newComp.Name {
					case "dis-collectd":
						{
							err := rewriteConfigForCollectd(newComp, containers)
							if err != nil {
								log.Errorln("rewriteConfigForCollectd error: ", err)
								continue
							}
						}
					case "logstash-forwarder":
						{
							err := rewriteConfigForLogstashForwarder(newComp, containers)
							if err != nil {
								log.Errorln("rewriteConfigForLogstashForwarder error: ", err)
								continue
							}
						}
					}
				}

				log.Debugln("new component DeployFilepath = ", newComp.DeployFilepath)
				output, err = utils.ExecCommand(false, newComp.DeployFilepath, "start")
				if err != nil {
					log.Errorln(err)
					continue
				}
				log.Debugf("Execute command: %s %s, output: %s\n", newComp.DeployFilepath, newComp.Cmd, output)

				// Componet directory was existed, then shutdown old verison component.
				if utils.IsExist(path.Join(newComp.Directory, ".version")) {
					oldVersion_b, err := utils.ReadFile(path.Join(newComp.Directory, ".version"))
					if err != nil {
						log.Errorln(err)
						continue
					}
					oldDeployFilePath := path.Join(newComp.Directory, strings.TrimSpace(string(oldVersion_b)), "deploy")
					_, err = utils.ExecCommand(false, oldDeployFilePath, "stop")
					if err != nil {
						log.Errorln(err)
						continue
					}
				}

				// Write the verion number to .version in the parent directory.
				log.Debugln("write version number file,  ", newComp.Directory)
				err = utils.WriteFile(path.Join(newComp.Directory, ".version"), []byte(newComp.Version), 0644)
				if err != nil {
					log.Errorln(err)
					continue
				}

				// Register this component to consul.
				err = common.ConsulRegister(newComp.Name, newComp.DeployFilepath)
				if err != nil {
					log.Errorln(err)
					continue
				}
			} else {
				log.Debugln("new component DeployFilepath = ", newComp.DeployFilepath)
				output, err = utils.ExecCommand(false, newComp.DeployFilepath, newComp.Cmd)
				if err != nil {
					log.Errorln(err)
					continue
				}
				log.Debugln("Execute command: %s %s, output: %s\n", newComp.DeployFilepath, newComp.Cmd, output)
			}
		}
	}
}