Exemplo n.º 1
0
Arquivo: runner.go Projeto: 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)
		}
	}
}
Exemplo n.º 2
0
Arquivo: engine.go Projeto: 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
}
Exemplo n.º 3
0
Arquivo: engine.go Projeto: ahjdzx/dis
func init() {
	var err error
	CurrentDir, err = utils.GetCurrentDir()
	if err != nil {
		log.Errorf("Get current dir error: %s", err.Error())
		return
	}
	stackDir = path.Dir(path.Dir(CurrentDir))
}
Exemplo n.º 4
0
func transfer(sshConfig *sshlib.SSHConfig) (succeedFiles, failedFiles []string, err error) {
	client, err := sshlib.NewSSHClient(sshConfig)
	if err != nil {
		return
	}
	defer client.Close()

	scp := sshlib.NewScp(client)

	localTarballDir := config.AppConfig().LocalTarballDir
	for _, comp := range config.AppConfig().Components {
		comp.InitAttrs(comp.TarballDir)

		_, err := sshlib.RunCommand(client, "ls "+comp.VersionDir)
		if err != nil { // fileDir not exists.
			// Create a new directory to save tarball file.
			err = sshlib.Mkdir(client, comp.VersionDir)
			if err != nil {
				log.Errorf("Mkdir %s error: %s", comp.VersionDir, err.Error())
				failedFiles = append(failedFiles, comp.TarballFilename)
				continue
			}
		}

		localTarbalPath := path.Join(localTarballDir, comp.TarballFilename)
		remoteFilePath := comp.TarballFilepath

		_, err = sshlib.RunCommand(client, "ls "+remoteFilePath)
		if err != nil { // remoteFilePath not exists.
			err = scp.PushFile(localTarbalPath, remoteFilePath)
			if err != nil {
				log.Errorf("Push file: %s --> %s, error: %s", localTarbalPath, remoteFilePath, err.Error())
				failedFiles = append(failedFiles, comp.TarballFilename)
				continue
			}
			log.Debug("push file ", localTarbalPath, " successfully.")
			succeedFiles = append(succeedFiles, comp.TarballFilename)

			// Unpack the tarball or zip file.
			_, err = sshlib.RunCommand(client, "tar -zxf "+remoteFilePath+" -C "+comp.VersionDir)
			if err != nil {
				log.Errorf("Unpack file %s error: %s", remoteFilePath, err.Error())
				continue
			}

			// if component name is AGENT_NAME, then use ssh execute the cmd option.
			if comp.Name == model.AGENT_NAME {
				var output string
				switch comp.Cmd {
				case "start":
					startCmd := fmt.Sprintf("cd %s && ./deploy start", comp.VersionDir)
					output, err = sshlib.RunCommand(client, startCmd)
					if err != nil {
						log.Errorf("Command: %s, Error: %s", comp.Cmd, err.Error())
						continue
					}
					if output != "" {
						log.Debugf("Command: %s, Output: %s", comp.Cmd, string(output))
					}
				case "status": // TODO
					statusCmd := fmt.Sprintf("cd %s && ./deploy status", comp.VersionDir)
					output, err = sshlib.RunCommand(client, statusCmd)
					if err != nil {
						log.Errorf("Command: %s, Error: %s", comp.Cmd, err.Error())
						continue
					}
					if output != "" {
						log.Debugf("Command: %s, Output: %s", comp.Cmd, string(output))
					}
				}

			}
		}

	}

	return
}