pipeline {
    agent any

    stages {
       stage('Publicação') {
          steps {
            script{
               dir("/var/jenkins_home/jobs/Adm/jobs/Adm-Project-Build/builds/$Adm_Build_NUMBER/archive/target")  {
			   
		   if(! fileExists('ventaboletosadm.war')){
	          currentBuild.result = 'FAILURE'
                  error('Artefato não encontrado, favor reconstruir a versão')
	       }

               switch( Empresa ){
                  case "Local RJ - Qualidade":
                     atualizarQa()
                     break
                  case "Local RJ - Dev":
                     atualizarDev()
                     break
                  case "Ouro e Prata - Homolog":
                     atualizarOuroPrata()
                     break
                  case "Garcia - Homolog":
                     atualizarGarcia()
                     break
                case "Andorinha - Homolog":
                     atualizarAndorinha()
                     break
                 case "Planalto - Homolog":
                     atualizarPlanalto()
                     break
                 case "Passaro Marron - Homolog":
                     atualizarPMarron()
                     break
                 case "Ambiente QA 2":
                     atualizarAmbienteQA2()
                     break
                 case "Unica Facil - Homolog":
                     atualizarUnicaFacil()
                     break	 
                 default:
                     echo "Empresa não configurada"
                     break
                }
             }
          }
       }
    }	
 }

    post {
      always {
         script {                 
	    //resolvendo o card
            // step([$class: 'JiraIssueUpdateBuilder', comment: '', jqlSearch: "issue = ${Ticket}", workflowActionName: 'Em Testes'])
            currentBuild.displayName = "${Adm_Build_NAME} em ${Empresa}"
         }
      }
   }
}

//=======================================================================================================================

def atualizarQa(){
   sshPublisher(publishers:
      [sshPublisherDesc(configName: 'Local RJ - QA',
      transfers:
      [sshTransfer(cleanRemote: true,
      excludes: '',
      execCommand: '''ls -la /home/rjconsultores/dist

      echo "Iniciando atualização ADM QUALIDADE"

      /dados/wildfly_qualidade_adm/bin/jboss-cli.sh --user=admin --password=rjmaster09 --connect controller=localhost:10390 --commands="deployment deploy-file --replace /home/rjconsultores/dist/ventaboletosadm.war"

      echo "Reiniciando ADM"

      sudo systemctl restart wildfly_qualidade_adm

      echo "*******************************Finalizando Atualização ADM ${Adm_Build_NUMBER}*******************************"''',
      execTimeout: 120000,
      flatten: false,
      makeEmptyDirs: false,
      noDefaultExcludes: false,
      patternSeparator: '[, ]+',
      remoteDirectory: '/home/rjconsultores/dist',
      remoteDirectorySDF: false,
      removePrefix: '',
      sourceFiles: '**/*.war')],
      usePromotionTimestamp: false,
      useWorkspaceInPromotion: false,
      verbose: false)])
}

//=======================================================================================================================

def atualizarDev(){
   sshPublisher(publishers:
      [sshPublisherDesc(configName: 'Local RJ - DEV',
      transfers:
      [sshTransfer(cleanRemote: true,
      excludes: '',
      execCommand: '''ls -la /tmp/adm

      echo "Iniciando atualização ADM DEV"
	  
	  export JAVA_HOME=/dados/jdk-11.0.5

      /dados/wildfly_adm_dev/bin/jboss-cli.sh --user=admin --password=rjmaster09 --connect controller=localhost:9990 --commands="deployment deploy-file --replace /tmp/adm/ventaboletosadm.war"

      /usr/bin/sleep 15

      echo "Reiniciando ADM"

      sudo systemctl restart wildfly_adm_dev

      echo "*******************************Finalizando Atualização ADM ${Adm_Build_NUMBER}*******************************"''',
      execTimeout: 120000,
      flatten: false,
      makeEmptyDirs: true,
      noDefaultExcludes: false,
      patternSeparator: '[, ]+',
      remoteDirectory: '/tmp/adm',
      remoteDirectorySDF: false,
      removePrefix: '',
      sourceFiles: '**/*.war')],
      usePromotionTimestamp: false,
      useWorkspaceInPromotion: false,
      verbose: false)])
}

//=======================================================================================================================

def atualizarOuroPrata(){
   sshPublisher(publishers:
      [sshPublisherDesc(configName: 'Ouro e Prata - Homolog',
      transfers:
      [sshTransfer(cleanRemote: true,
      excludes: '',
      execCommand: '''ls -la /tmp/adm

      echo "**********Iniciando atualização ADM Ouro e Prata ${BUILD_NUMBER}**********"

      /dados/wildfly_ouroeprata_adm_hml/bin/jboss-cli.sh --user=admin --password=rjmaster09 --connect controller=localhost:9990  --commands="deployment deploy-file --replace /tmp/adm/ventaboletosadm.war"

      echo "Reiniciando ADM"

      sudo systemctl restart wildfly_ouroeprata_adm_hml

      echo "*******************************Finalizando Atualização Ouro e Prata ADM ${Adm_Build_NUMBER}*******************************"''',
      execTimeout: 120000,
      flatten: false,
      makeEmptyDirs: false,
      noDefaultExcludes: false,
      patternSeparator: '[, ]+',
      remoteDirectory: '/tmp/adm',
      remoteDirectorySDF: false,
      removePrefix: '',
      sourceFiles: '**/*.war')],
      usePromotionTimestamp: false,
      useWorkspaceInPromotion: false,
      verbose: false)])
}

//=======================================================================================================================

def atualizarGarcia(){
   sshPublisher(publishers:
      [sshPublisherDesc(configName: 'Garcia - Homolog',
      transfers:
      [sshTransfer(cleanRemote: true,
      excludes: '',
      execCommand: '''ls -la /tmp/adm

      echo "**********Iniciando atualização ADM Garcia - Homolog #${Adm_Build_NUMBER}**********"

      /dados/wildfly_garcia_homolog/bin/jboss-cli.sh --user=admin --password=rjmaster09 --connect controller=localhost:16990  --commands="deployment deploy-file --replace /tmp/adm/ventaboletosadm.war"

      echo "Reiniciando ADM"

      sudo systemctl restart wildfly_garcia_homolog

      echo "*******************************Finalizando Atualização ADM Garcia - Homolog #${Adm_Build_NUMBER}*******************************"''',
      execTimeout: 120000,
      flatten: false,
      makeEmptyDirs: false,
      noDefaultExcludes: false,
      patternSeparator: '[, ]+',
      remoteDirectory: '/tmp/adm',
      remoteDirectorySDF: false,
      removePrefix: '',
      sourceFiles: '**/*.war')],
      usePromotionTimestamp: false,
      useWorkspaceInPromotion: false,
      verbose: false)])
}

//=======================================================================================================================

def atualizarAndorinha(){
   sshPublisher(publishers:
      [sshPublisherDesc(configName: 'Andorinha - Homolog',
      transfers:
      [sshTransfer(cleanRemote: true,
      excludes: '',
      execCommand: '''ls -la /tmp/adm

      echo "**********Iniciando atualização ADM Andorinha - Homolog #${Adm_Build_NUMBER}**********"

      /dados/wildfly_andorinha_homolog/bin/jboss-cli.sh --user=admin --password=rjmaster09 --connect controller=localhost:16590  --commands="deployment deploy-file --replace /tmp/adm/ventaboletosadm.war"

      echo "Reiniciando ADM"

      sudo systemctl restart wildfly_andorinha_homolog

      echo "*******************************Finalizando Atualização ADM Andorinha - Homolog #${Adm_Build_NUMBER}*******************************"''',
      execTimeout: 120000,
      flatten: false,
      makeEmptyDirs: false,
      noDefaultExcludes: false,
      patternSeparator: '[, ]+',
      remoteDirectory: '/tmp/adm',
      remoteDirectorySDF: false,
      removePrefix: '',
      sourceFiles: '**/*.war')],
      usePromotionTimestamp: false,
      useWorkspaceInPromotion: false,
      verbose: false)])
}

//=======================================================================================================================

def atualizarPlanalto(){
   sshPublisher(publishers:
      [sshPublisherDesc(configName: 'Planalto - Homolog',
      transfers:
      [sshTransfer(cleanRemote: true,
      excludes: '',
      execCommand: '''ls -la /tmp/adm

      echo "**********Iniciando atualização ADM Planalto ${Adm_Build_NUMBER}**********"

      export JAVA_HOME=/dados/jdk-11.0.5

      /dados/wildfly_adm_planalto_homolog/bin/jboss-cli.sh --user=admin --password=rjmaster09 --connect controller=localhost:9991  --commands="deployment deploy-file --replace /home/gleimar/ventaboletosadm.war"

      echo "Reiniciando ADM"

      sudo systemctl restart wildfly_adm_planalto_homolog

      echo "*******************************Finalizando Atualização Planalto ADM ${Adm_Build_NUMBER}*******************************"''',
      execTimeout: 120000,
      flatten: false,
      makeEmptyDirs: false,
      noDefaultExcludes: false,
      patternSeparator: '[, ]+',
      remoteDirectory: '/tmp/adm',
      remoteDirectorySDF: false,
      removePrefix: '',
      sourceFiles: '**/*.war')],
      usePromotionTimestamp: false,
      useWorkspaceInPromotion: false,
      verbose: false)])
}

//=======================================================================================================================

def atualizarPMarron(){
   sshPublisher(publishers:
      [sshPublisherDesc(configName: 'Passaro Marron - Homolog',
      transfers:
      [sshTransfer(cleanRemote: true,
      excludes: '',
      execCommand: '''ls -la /tmp/adm

      echo "**********Iniciando atualização ADM Passaro Marron - Homolog #${Adm_Build_NUMBER}******************""
	  
	  export JAVA_HOME=/dados/jdk-11.0.5
  
      /dados/wildfly_pmarron_homolog/bin/jboss-cli.sh --user=admin --password=rjmaster09 --connect controller=localhost:9990 --commands="deployment deploy-file --replace /home/gleimar/ventaboletosadm.war"
	  
	  /bin/sleep 15
      
      echo "Reiniciando ADM"

      systemctl restart wildfly_pmarron_homolog

      echo "*******************************Finalizando Atualização ADM Passaro Marron - Homolog #${Adm_Build_NUMBER}*******************************"''',
      execTimeout: 120000,
      flatten: false,
      makeEmptyDirs: false,
      noDefaultExcludes: false,
      patternSeparator: '[, ]+',
      remoteDirectory: '/tmp/adm',
      remoteDirectorySDF: false,
      removePrefix: '',
      sourceFiles: '**/*.war')],
      usePromotionTimestamp: false,
      useWorkspaceInPromotion: false,
      verbose: false)])
}

//=======================================================================================================================

def atualizarAmbienteQA2(){
   sshPublisher(publishers:
      [sshPublisherDesc(configName: 'Ambiente QA 2',
      transfers:
      [sshTransfer(cleanRemote: true,
      excludes: '',
      execCommand: '''ls -la /tmp/adm
	  
	  
      echo "**********Iniciando atualização ADM Ambiente QA 2 ${BUILD_NUMBER}******************""

      export JAVA_HOME=/dados/jdk-11.0.5

      sudo /dados/wildfly_testefailover/bin/jboss-cli.sh --user=admin --password=rjmaster09 --connect controller=localhost:9990 --commands="deployment deploy-file --replace /tmp/adm/ventaboletosadm.war"
      
	  
      
	  /bin/sleep 15
	  
	  sudo systemctl restart wildfly_testefailover

      echo "*******************************Finalizando AtualizaçãoADM ${Adm_Build_NUMBER}*******************************"''',
      execTimeout: 120000,
      flatten: false,
      makeEmptyDirs: false,
      noDefaultExcludes: false,
      patternSeparator: '[, ]+',
      remoteDirectory: '/tmp/adm',
      remoteDirectorySDF: false,
      removePrefix: '',
      sourceFiles: '**/*.war')],
      usePromotionTimestamp: false,
      useWorkspaceInPromotion: false,
      verbose: false)])
}

//=======================================================================================================================

def atualizarUnicaFacil(){
   sshPublisher(publishers:
      [sshPublisherDesc(configName: 'Unica Facil - Homolog',
      transfers:
      [sshTransfer(cleanRemote: true,
      excludes: '',
      execCommand: '''ls -la /tmp/adm
	  
	  
      echo "**********Iniciando atualização ADM Unica Facil Homolog ${BUILD_NUMBER}******************""

      export JAVA_HOME=/dados/jdk-11.0.5

      /dados/wildfly_unicafacilhomolog_adm/bin/jboss-cli.sh --user=admin --password=rjmaster09 --connect controller=localhost:9992 --commands="deployment deploy-file --replace /tmp/adm/ventaboletosadm.war"
     
	  /bin/sleep 15
	  
	  systemctl restart wildfly_unicafacilhomolog_adm.service

      echo "*******************************Finalizando AtualizaçãoADM ${Adm_Build_NUMBER}*******************************"''',
      execTimeout: 120000,
      flatten: false,
      makeEmptyDirs: false,
      noDefaultExcludes: false,
      patternSeparator: '[, ]+',
      remoteDirectory: '/tmp/adm',
      remoteDirectorySDF: false,
      removePrefix: '',
      sourceFiles: '**/*.war')],
      usePromotionTimestamp: false,
      useWorkspaceInPromotion: false,
      verbose: false)])
}

//=======================================================================================================================