コード例 #1
0
ファイル: CommandImpl.java プロジェクト: ni350305/autodeploy
  /**
   * 创建命令文件 [root@localhost ~]# cat test.sh <br>
   * #!/bin/sh ips=( "192.168.1.2" "192.168.1.3" "192.168.1.4"); <br>
   * users=("root" "billy" "tom" "lilei"); <br>
   * ports=("1234" "9527" "3210" "2345"); <br>
   * authtypes=(0 1 1 0); <br>
   * ipsLength=${#ips[@]} <br>
   * for((i=0;i<ipsLength;i++)) <br>
   * do<br>
   * if [ ${authtypes[i]} -eq 0 ]; then <br>
   * echo "${users[i]}:${ports[i]}:${ips[i]} 密码认证" <br>
   * else<br>
   * echo ${users[i]}:${ports[i]}:${ips[i]} 密钥认证 <br>
   * fi <br>
   * done
   *
   * @param deployFileDTO
   * @return 命令
   * @throws Exception
   */
  @Override
  public String createCommandFile(DeployTaskDTO deployTaskDTO) throws Exception {

    StringBuilder commandSb = new StringBuilder();
    commandSb.append("#!/bin/sh\n");
    // 添加ip user port的数组
    this.appendIpsUsersPortsAuthtypesToCommandBuilder(commandSb, deployTaskDTO.getServers());
    commandSb.append("ipsLength=${#ips[@]}\n");
    commandSb.append("for((i=0;i<ipsLength;i++))\n");
    commandSb.append("do\n");
    if (deployTaskDTO.getIsConcurrence() == 1) {
      // 指定采用多线程
      commandSb.append("if [ 0 -eq 0 ];then\n");
      // 线程控制
      commandSb.append("if [ $(($i%").append(this.threads).append(")) -eq 0 ]; then\n");
      commandSb.append("wait\n");
      commandSb.append("fi\n");
    }
    // 部署命令和部署任务的添加
    this.apppendTasks(commandSb, deployTaskDTO);
    if (deployTaskDTO.getIsConcurrence() == 1) {
      commandSb.append("fi&\n");
    }
    commandSb.append("done\n");
    commandSb.append("wait\n");
    String folder =
        fileDeployRoot + deployTaskDTO.getProjectId() + "/deploytask" + deployTaskDTO.getId() + "/";
    File folderFile = new File(folder);
    if (folderFile.exists()) {
      FileUtils.deleteQuietly(folderFile);
    }
    String filePath = folder + DateUtils.getCurrentDateTime().getTime() + ".sh";
    FileUtils.write(new File(filePath), commandSb.toString(), "UTF-8");
    return filePath;
  }
コード例 #2
0
ファイル: CommandImpl.java プロジェクト: ni350305/autodeploy
  /**
   * 部署命令和部署任务的添加
   *
   * @param commandSb
   * @param deployTaskDTO
   * @throws IOException
   */
  private void apppendTasks(StringBuilder commandSb, DeployTaskDTO deployTaskDTO)
      throws IOException {

    StringBuilder passwdTaskSb = new StringBuilder(); // 密码方式,任务命令
    StringBuilder keyFileSb = new StringBuilder(); // 密钥方式,任务命令
    for (DeployTaskDetailDTO detailDTO : deployTaskDTO.getDeployTaskDetails()) {

      switch (detailDTO.getType()) {
        case DeployType.TYPE_COMMAND:
          // 命令类型!
          // 命令
          String command = detailDTO.getCommand();
          // 先进入文件夹,再执行命令
          String commandFolder = "";
          String commandEndStr = "";
          String tempFirstCommand = command.split(" ")[0];
          if (tempFirstCommand.indexOf("/") != -1) {
            commandFolder = tempFirstCommand.substring(0, tempFirstCommand.lastIndexOf("/"));
            commandEndStr = command.substring(tempFirstCommand.lastIndexOf("/"));
          }
          // -------------------------------密钥方式
          keyFileSb.append("#").append(detailDTO.getName()).append("\n");
          keyFileSb
              .append("echo \"执行命令:")
              .append(detailDTO.getName())
              .append(" ${ips[i]}")
              .append("\"\n");
          keyFileSb
              .append("ssh -p ${ports[i]} ${users[i]}@${ips[i]} -i ")
              .append(serverRoot)
              .append("${users[i]}${ips[i]} ")
              .append("\"")
              .append("source /etc/profile;");
          if (tempFirstCommand.indexOf("/") != -1) {
            keyFileSb
                .append("chmod +x ")
                .append(command.split(" ")[0])
                .append(";")
                .append("cd ")
                .append(commandFolder)
                .append(";");
            keyFileSb.append("." + commandEndStr);
          } else {
            keyFileSb.append(command);
          }
          keyFileSb.append("\" \n");
          // ------------------------------密码方式
          passwdTaskSb.append("#").append(detailDTO.getName()).append("\n");
          passwdTaskSb
              .append("echo \"执行命令:")
              .append(detailDTO.getName())
              .append(" ${ips[i]}")
              .append("\"\n");
          passwdTaskSb
              .append("sshpass -f ")
              .append(serverRoot)
              .append("${users[i]}${ips[i]} ")
              .append(" ssh -p ${ports[i]} ${users[i]}@${ips[i]} ")
              .append("\"")
              .append("source /etc/profile;");
          if (tempFirstCommand.indexOf("/") != -1) {
            passwdTaskSb
                .append("chmod +x ")
                .append(command.split(" ")[0])
                .append(";")
                .append("cd ")
                .append(commandFolder)
                .append(";")
                .append("." + commandEndStr);
          } else {
            passwdTaskSb.append(command);
          }
          passwdTaskSb.append("\" \n");
          continue;
        case DeployType.TYPE_FILE:
          // 文件类型!
          // 先压缩文件
          synchronized (detailDTO.getId()) {
            this.tarFile(deployTaskDTO.getProjectId(), detailDTO.getId());
          }
          // ------------------------密钥方式---------------------
          if (detailDTO.getIsEmptyFolder() == 1) {
            // 部署前是否移除,当前的远程文件夹
            keyFileSb.append("#先删除文件夹\n");
            keyFileSb.append("echo \"先删除文件夹 ${ips[i]}\"\n");
            keyFileSb
                .append("ssh -p ${ports[i]} ${users[i]}@${ips[i]} -i ")
                .append(serverRoot)
                .append("${users[i]}${ips[i]} ")
                .append("\"rm -rf ")
                .append(detailDTO.getDeployPath())
                .append("\"\n");
          }
          keyFileSb
              .append("ssh -p ${ports[i]} ${users[i]}@${ips[i]} -i ")
              .append(serverRoot)
              .append("${users[i]}${ips[i]} ")
              .append("\"mkdir -p ")
              .append(detailDTO.getDeployPath())
              .append("\"\n");
          keyFileSb.append("#部署文件到远程服务器\n");
          keyFileSb
              .append("echo \"部署文件: ")
              .append(detailDTO.getFileName())
              .append(" 到远程服务器 ${ips[i]}\" \n");
          keyFileSb
              .append("scp -P ${ports[i]} -i ")
              .append(serverRoot)
              .append("${users[i]}${ips[i]} ")
              .append(this.fileDeployRoot)
              .append(deployTaskDTO.getProjectId())
              .append("/deployfile")
              .append(detailDTO.getId())
              .append("/")
              .append("file")
              .append(String.valueOf(detailDTO.getId()))
              .append(this.FILE_END_TAGS)
              .append(" ${users[i]}@${ips[i]}:")
              .append(detailDTO.getDeployPath())
              .append("\n");
          keyFileSb.append("#解压文件\n");
          keyFileSb
              .append("echo \"解压文件 ")
              .append(detailDTO.getFileName())
              .append(" ${ips[i]}\" \n");
          keyFileSb
              .append("ssh -p ${ports[i]} ${users[i]}@${ips[i]} -i ")
              .append(serverRoot)
              .append("${users[i]}${ips[i]} ")
              .append(" \"cd ")
              .append(detailDTO.getDeployPath())
              .append("; ")
              .append("source /etc/profile;");
          // if (detailDTO.getFileName().endsWith(".war")) {
          // keyFileSb.append("jar -xvf
          // ").append("file").append(String.valueOf(detailDTO.getId())).append(this.FILE_END_TAGS);
          // } else {
          // if (detailDTO.getFileName().endsWith(".zip")) {
          // keyFileSb.append("unzip
          // ").append("file").append(String.valueOf(detailDTO.getId())).append(this.FILE_END_TAGS);
          // } else {
          // if (detailDTO.getFileName().endsWith(".gz") ||
          // detailDTO.getFileName().endsWith(".rar")) {
          keyFileSb
              .append("tar -xvf ")
              .append("file")
              .append(String.valueOf(detailDTO.getId()))
              .append(this.FILE_END_TAGS)
              .append(";");
          // }
          // }
          // }
          keyFileSb
              .append("rm -rf ")
              .append("file")
              .append(String.valueOf(detailDTO.getId()))
              .append(this.FILE_END_TAGS);
          keyFileSb.append("\"\n");
          // ------------------------密码方式---------------------
          if (detailDTO.getIsEmptyFolder() == 1) {
            // 部署前是否移除,当前的远程文件夹
            passwdTaskSb.append("#先删除文件夹\n");
            passwdTaskSb.append("echo \"先删除文件夹 ${ips[i]}\"\n");
            passwdTaskSb
                .append("sshpass -f ")
                .append(serverRoot)
                .append("${users[i]}${ips[i]} ")
                .append(" ssh -p ${ports[i]} ${users[i]}@${ips[i]} ")
                .append("\"")
                .append("rm -rf ")
                .append(detailDTO.getDeployPath())
                .append("\"\n");
          }
          passwdTaskSb
              .append("sshpass -f ")
              .append(serverRoot)
              .append("${users[i]}${ips[i]} ")
              .append(" ssh -p ${ports[i]} ${users[i]}@${ips[i]} ")
              .append("\"mkdir -p ")
              .append(detailDTO.getDeployPath())
              .append("\"\n");
          passwdTaskSb.append("#部署文件到远程服务器\n");
          passwdTaskSb
              .append("echo \"部署文件: ")
              .append(detailDTO.getFileName())
              .append(" 到远程服务器 ${ips[i]}\" \n");
          passwdTaskSb
              .append("sshpass -f ")
              .append(serverRoot)
              .append("${users[i]}${ips[i]} ")
              .append(" scp -P ${ports[i]} ")
              .append(this.fileDeployRoot)
              .append(deployTaskDTO.getProjectId())
              .append("/deployfile")
              .append(detailDTO.getId())
              .append("/")
              .append("file")
              .append(String.valueOf(detailDTO.getId()))
              .append(this.FILE_END_TAGS)
              .append(" ${users[i]}@${ips[i]}:")
              .append(detailDTO.getDeployPath())
              .append("\n");
          passwdTaskSb.append("#解压文件\n");
          passwdTaskSb
              .append("echo \"解压文件 ")
              .append(detailDTO.getFileName())
              .append(" ${ips[i]}\" \n");
          passwdTaskSb
              .append("sshpass -f ")
              .append(serverRoot)
              .append("${users[i]}${ips[i]} ")
              .append(" ssh -p ${ports[i]} ${users[i]}@${ips[i]} ")
              .append(" \"cd ")
              .append(detailDTO.getDeployPath())
              .append("; ")
              .append("source /etc/profile;");
          // if (detailDTO.getFileName().endsWith(".war")) {
          // passwdTaskSb.append("jar -xvf
          // ").append("file").append(String.valueOf(detailDTO.getId()))
          // .append(this.FILE_END_TAGS);
          // } else {
          // if (detailDTO.getFileName().endsWith(".zip")) {
          // passwdTaskSb.append("unzip ").append("file").append(String.valueOf(detailDTO.getId()))
          // .append(this.FILE_END_TAGS);
          // } else {
          // if (detailDTO.getFileName().endsWith(".gz") ||
          // detailDTO.getFileName().endsWith(".rar")) {
          passwdTaskSb
              .append("tar -xvf ")
              .append("file")
              .append(String.valueOf(detailDTO.getId()))
              .append(this.FILE_END_TAGS)
              .append(";");
          // }
          // }
          // }
          passwdTaskSb
              .append("rm -rf ")
              .append("file")
              .append(String.valueOf(detailDTO.getId()))
              .append(this.FILE_END_TAGS);
          passwdTaskSb.append("\"\n");
          continue;
      }
    }
    // 命令类型
    commandSb.append("if [ ${authtypes[i]} -eq 1 ]; then\n");
    commandSb.append(keyFileSb);
    commandSb.append("else\n");
    commandSb.append(passwdTaskSb);
    commandSb.append("fi\n");
  }