示例#1
0
文件: context.go 项目: ahjdzx/dis
func (c *Context) Clean(rw web.ResponseWriter, req *web.Request) {
	var msg string
	defer c.WriteResponse(msg)

	var stackDir string
	localComps := engine.LocalComponents()
	for _, comp := range localComps {
		cmd := exec.Command(comp.DeployFilepath, "stop")
		output, err := cmd.CombinedOutput()
		if err != nil {
			rw.WriteHeader(http.StatusInternalServerError)
			msg = fmt.Sprintf("Failed to run command: %s ,error: %v", comp.DeployFilepath+" stop", err)
			return
		}
		log.Debug(output)
		if stackDir == "" {
			stackDir = filepath.Dir(comp.Directory)
		} else if stackDir != filepath.Dir(comp.Directory) {
			rw.WriteHeader(http.StatusInternalServerError)
			msg = "stack directory is different."
			return
		}
	}

	if stackDir != "" {
		err := os.RemoveAll(stackDir)
		if err != nil {
			rw.WriteHeader(http.StatusInternalServerError)
			msg = err.Error()
			return
		}
		msg = "clean ok."
	}
}
示例#2
0
文件: service.go 项目: ahjdzx/dis
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
}