示例#1
0
文件: main.go 项目: ahjdzx/dis
func initClientSet() (*engine.ClientSet, error) {
	clientSet := engine.NewClientSet()

	err := common.CreateConsulAPIClient(config.AppConfig().Consul.ConfigJsonFile, config.AppConfig().Consul.Port)
	if err != nil {
		return nil, err
	}
	clientSet.Add(common.ConsulAPIClient)

	err = common.CreateDockerAPIClient(config.AppConfig().DockerEndpoint)
	if err != nil {
		return nil, err
	}
	err = clientSet.Add(common.DockerAPIClient)
	return clientSet, err
}
示例#2
0
文件: api.go 项目: ahjdzx/dis
func Start() {
	if !config.AppConfig().Http.Enabled {
		return
	}
	addr := config.AppConfig().Http.Listen
	if addr == "" {
		return
	}
	server := &http.Server{
		Addr:    addr,
		Handler: initRouter(),
	}

	log.Infoln("http listening on ", addr)
	log.Fatalln(server.ListenAndServe())
}
示例#3
0
文件: engine.go 项目: ahjdzx/dis
func Boot() {
	dur := randomNanoSec()
	for {
		time.Sleep(dur)
		heartbeat()
		dur = time.Duration(int64(config.AppConfig().Heartbeat)) * time.Second
	}
}
示例#4
0
文件: runner.go 项目: ahjdzx/dis
func rewriteConfigForCollectd(comp *model.Component, containers []*Container) error {
	log.Infoln("rewrite configuration file ", comp.Name)
	// 1. Load configuration file.
	c := &cfg_collected.Config{}
	_, err := toml.DecodeFile(comp.ConfigFilepath, c)
	if err != nil {
		return err
	}

	// 2. Change the configuration content.
	inputConfig := &cfg_collected.InputConfig{
		Enabled:  true,
		Interval: DefaultInterval,
		SubInput: make([]*cfg_collected.InputConfig, len(containers)),
	}
	for i, ctn := range containers {
		serviceType := strings.Split(path.Base(ctn.Image), ":")[0]
		var interval int
		itv, ok := config.AppConfig().Intervals[serviceType]
		if !ok {

			log.Warnf("service %s hasn't config interval, using default %d", serviceType, DefaultInterval)
			interval = DefaultInterval
		} else {
			interval = itv.Value
		}
		subConfig := &cfg_collected.InputConfig{
			Enabled:  true,
			Interval: interval,
			Tags:     fmt.Sprintf("cid:%s", ctn.ID),
		}
		inputConfig.SubInput[i] = subConfig
	}
	c.Input["container"] = inputConfig

	// 3. Rewrite the configuration file.
	buf := bytes.NewBuffer(nil)
	encoder := toml.NewEncoder(buf)
	err = encoder.Encode(c)
	if err != nil {
		return err
	}
	encoder.Indent = "  "
	log.Debugln("configuration file path is ", comp.ConfigFilepath)
	err = utils.WriteFile(comp.ConfigFilepath, buf.Bytes(), 0644)
	return err
}
示例#5
0
文件: engine.go 项目: 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)
			}
		}
	}
}
示例#6
0
文件: engine.go 项目: ahjdzx/dis
func randomNanoSec() time.Duration {
	ns := int64(config.AppConfig().Heartbeat) * 1e9
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	return time.Duration(r.Int63n(ns)) * time.Nanosecond
}