예제 #1
0
파일: context.go 프로젝트: ahjdzx/dis
func (c *Context) Destory(rw web.ResponseWriter, req *web.Request) {
	resp := &Response{}
	defer c.WriteResponse(resp)

	body, err := ioutil.ReadAll(req.Body)
	if err != nil {
		rw.WriteHeader(http.StatusBadRequest)
		resp.Message = err.Error()
		return
	}

	sshConfig := &sshlib.SSHConfig{}
	err = json.Unmarshal(body, sshConfig)
	if err != nil {
		rw.WriteHeader(http.StatusBadRequest)
		resp.Message = err.Error()
		return
	}
	log.Info("Destory...... ", sshConfig.Address)

	client, err := sshlib.NewSSHClient(sshConfig)
	if err != nil {
		rw.WriteHeader(http.StatusBadRequest)
		resp.Message = err.Error()
		return
	}
	defer client.Close()

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

		out, err := sshlib.RunCommand(client, comp.DeployFilepath+" stop")
		if err != nil {
			log.Error(err)
			resp.FailedFiles = append(resp.FailedFiles, comp.TarballFilename)
			continue
		}
		log.Info("command stop output: ", out)

		out, err = sshlib.RunCommand(client, "rm -rf "+comp.Directory)
		if err != nil {
			log.Error(err)
			resp.FailedFiles = append(resp.FailedFiles, comp.TarballFilename)
			continue
		}
		log.Info("command 'rm -rf ' output: ", out)
		resp.SucceedFiles = append(resp.SucceedFiles, comp.TarballFilename)
	}
	resp.Message = "Clean up 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
}