2018-08-15 09:26:38 +02:00
import com.sap.piper.ConfigurationHelper
2017-07-11 15:12:03 +02:00
import com.sap.piper.Utils
2018-03-19 19:29:41 +02:00
import com.sap.piper.tools.ToolDescriptor
2019-01-28 12:32:24 +02:00
import com.sap.piper.tools.neo.DeployMode
import com.sap.piper.tools.neo.NeoCommandHelper
import com.sap.piper.tools.neo.WarAction
2018-08-15 09:26:38 +02:00
import groovy.transform.Field
2019-01-28 12:32:24 +02:00
import static com . sap . piper . Prerequisites . checkScript
2018-11-29 10:54:05 +02:00
@Field String STEP_NAME = getClass ( ) . getName ( )
2019-01-28 12:32:24 +02:00
@Field Set GENERAL_CONFIG_KEYS = [
'neo'
]
@Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS . plus ( [
2018-08-15 09:26:38 +02:00
'dockerEnvVars' ,
'dockerImage' ,
'dockerOptions' ,
'neoHome'
2019-01-28 12:32:24 +02:00
] )
2018-08-15 09:26:38 +02:00
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS . plus ( [
2019-01-28 12:32:24 +02:00
'source' ,
2018-08-15 09:26:38 +02:00
'deployMode' ,
'warAction'
] )
2018-02-20 16:30:34 +02:00
2019-01-28 12:32:24 +02:00
@Field Map CONFIG_KEY_COMPATIBILITY = [
neo : [
host : 'host' ,
account : 'account' ,
application : 'applicationName' ,
credentialsId : 'neoCredentialsId' ,
propertiesFile: 'propertiesFile' ,
runtime : 'runtime' ,
runtimeVersion: 'runtimeVersion' ,
size : 'vmSize'
] ,
source: 'archivePath'
]
2018-08-30 16:33:07 +02:00
void call ( parameters = [ : ] ) {
2019-01-28 12:32:24 +02:00
handlePipelineStepErrors ( stepName: STEP_NAME , stepParameters: parameters ) {
2017-12-11 12:15:51 +02:00
2018-10-31 09:40:12 +02:00
def script = checkScript ( this , parameters ) ? : this
2018-09-21 16:55:31 +02:00
2018-11-08 10:44:11 +02:00
def utils = parameters . utils ? : new Utils ( )
2017-12-11 14:55:07 +02:00
2017-12-11 12:15:51 +02:00
prepareDefaultValues script: script
2019-01-28 12:32:24 +02:00
Map stepCompatibilityConfiguration = handleCompatibility ( script , parameters )
2018-08-30 16:33:07 +02:00
2018-08-15 09:26:38 +02:00
// load default & individual configuration
2018-10-17 11:05:20 +02:00
Map configuration = ConfigurationHelper . newInstance ( this )
2018-09-07 10:08:16 +02:00
. loadStepDefaults ( )
2018-08-15 09:26:38 +02:00
. mixinGeneralConfig ( script . commonPipelineEnvironment , GENERAL_CONFIG_KEYS )
. mixin ( stepCompatibilityConfiguration )
2019-01-28 12:32:24 +02:00
. mixinStepConfig ( script . commonPipelineEnvironment , STEP_CONFIG_KEYS , CONFIG_KEY_COMPATIBILITY )
. mixinStageConfig ( script . commonPipelineEnvironment , parameters . stageName ? : env . STAGE_NAME , STEP_CONFIG_KEYS , CONFIG_KEY_COMPATIBILITY )
. addIfEmpty ( 'source' , script . commonPipelineEnvironment . getMtarFilePath ( ) )
. mixin ( parameters , PARAMETER_KEYS , CONFIG_KEY_COMPATIBILITY )
. withMandatoryProperty ( 'neo' )
. withMandatoryProperty ( 'source' )
. withPropertyInValues ( 'deployMode' , DeployMode . stringValues ( ) )
2018-08-15 09:26:38 +02:00
. use ( )
2018-08-30 16:33:07 +02:00
2018-08-30 11:03:34 +02:00
utils . pushToSWA ( [
2018-08-30 16:33:07 +02:00
step: STEP_NAME ,
2019-01-21 09:47:34 +02:00
stepParamKey1: 'deployMode' ,
2018-08-30 11:03:34 +02:00
stepParam1: configuration . deployMode = = 'mta' ? 'mta' : 'war' , // ['mta', 'warParams', 'warPropertiesFile']
2019-01-21 09:47:34 +02:00
stepParamKey2: 'warAction' ,
2018-10-30 17:22:42 +02:00
stepParam2: configuration . warAction = = 'rolling-update' ? 'blue-green' : 'standard' , // ['deploy', 'deploy-mta', 'rolling-update']
2019-01-21 09:47:34 +02:00
stepParamKey3: 'scriptMissing' ,
2018-11-08 10:44:11 +02:00
stepParam3: parameters ? . script = = null ,
2019-01-21 09:47:34 +02:00
stepParamKey4: 'legacyConfig' ,
2019-01-28 12:32:24 +02:00
stepParam4: ! stepCompatibilityConfiguration . isEmpty ( ) ,
2018-08-30 11:03:34 +02:00
] , configuration )
2017-12-11 12:15:51 +02:00
2019-01-28 12:32:24 +02:00
ToolDescriptor neo = new ToolDescriptor ( 'SAP Cloud Platform Console Client' , 'NEO_HOME' , 'neoHome' , '/tools/' , 'neo.sh' , null , 'version' )
ToolDescriptor java = new ToolDescriptor ( 'Java' , 'JAVA_HOME' , '' , '/bin/' , 'java' , '1.8.0' , '-version 2>&1' )
if ( configuration . neo . credentialsId ) {
withCredentials ( [ usernamePassword (
credentialsId: configuration . neo . credentialsId ,
passwordVariable: 'NEO_PASSWORD' ,
usernameVariable: 'NEO_USERNAME' ) ] ) {
assertPasswordRules ( NEO_PASSWORD )
dockerExecute (
script: script ,
dockerImage: configuration . dockerImage ,
dockerEnvVars: configuration . dockerEnvVars ,
dockerOptions: configuration . dockerOptions
) {
neo . verify ( this , configuration )
java . verify ( this , configuration )
String neoExecutable = neo . getToolExecutable ( script , configuration )
DeployMode deployMode = DeployMode . fromString ( configuration . deployMode )
NeoCommandHelper neoCommandHelper = new NeoCommandHelper (
this ,
deployMode ,
configuration . neo ,
neoExecutable ,
NEO_USERNAME ,
NEO_PASSWORD ,
configuration . source
)
lock ( "$STEP_NAME :${neoCommandHelper.resourceLock()}" ) {
deploy ( script , utils , configuration , neoCommandHelper , configuration . dockerImage , deployMode )
}
}
2017-12-11 12:15:51 +02:00
}
} else {
2019-01-28 12:32:24 +02:00
error ( "[neoDeploy] No credentials defined for the deployment. Please specify the value for credentialsId for neo." )
2017-12-11 12:15:51 +02:00
}
2019-01-28 12:32:24 +02:00
}
}
2017-12-11 12:15:51 +02:00
2019-01-28 12:32:24 +02:00
private deploy ( script , utils , Map configuration , NeoCommandHelper neoCommandHelper , dockerImage , DeployMode deployMode ) {
2018-01-16 11:54:17 +02:00
2019-01-28 12:32:24 +02:00
try {
sh "mkdir -p logs/neo"
withEnv ( [ "neo_logging_location=${pwd()}/logs/neo" ] ) {
if ( deployMode . isWarDeployment ( ) ) {
ConfigurationHelper . newInstance ( this , configuration ) . withPropertyInValues ( 'warAction' , WarAction . stringValues ( ) )
WarAction warAction = WarAction . fromString ( configuration . warAction )
2017-12-11 12:15:51 +02:00
2019-01-28 12:32:24 +02:00
if ( warAction = = WarAction . ROLLING_UPDATE ) {
if ( ! isAppRunning ( neoCommandHelper ) ) {
warAction = WarAction . DEPLOY
echo "Rolling update not possible because application is not running. Falling back to standard deployment."
}
}
echo "Link to the application dashboard: ${neoCommandHelper.cloudCockpitLink()}"
2017-12-28 14:10:11 +02:00
2019-01-28 12:32:24 +02:00
if ( warAction = = WarAction . ROLLING_UPDATE ) {
sh neoCommandHelper . rollingUpdateCommand ( )
} else {
sh neoCommandHelper . deployCommand ( )
sh neoCommandHelper . restartCommand ( )
}
} else if ( deployMode = = DeployMode . MTA ) {
sh neoCommandHelper . deployMta ( )
2018-01-11 16:25:58 +02:00
}
2017-12-28 14:10:11 +02:00
}
2019-01-28 12:32:24 +02:00
}
catch ( Exception ex ) {
if ( dockerImage ) {
echo "Error while deploying to SAP Cloud Platform. Here are the neo.sh logs:"
sh "cat logs/neo/*"
2017-12-28 14:10:11 +02:00
}
2019-01-28 12:32:24 +02:00
throw ex
}
}
2017-12-28 14:10:11 +02:00
2019-01-28 12:32:24 +02:00
private boolean isAppRunning ( NeoCommandHelper commandHelper ) {
def status = sh script: "${commandHelper.statusCommand()} || true" , returnStdout: true
return status . contains ( 'Status: STARTED' )
}
2018-04-20 15:20:50 +02:00
2019-01-28 12:32:24 +02:00
private handleCompatibility ( script , parameters ) {
final Map neoCompatibilityConfiguration = [ : ]
2017-12-13 11:05:19 +02:00
2019-01-28 12:32:24 +02:00
// Backward compatibility: ensure old configuration is taken into account
// The old configuration in not stage / step specific
2017-12-13 11:05:19 +02:00
2019-01-28 12:32:24 +02:00
def defaultDeployHost = script . commonPipelineEnvironment . getConfigProperty ( 'DEPLOY_HOST' )
if ( defaultDeployHost ) {
echo "[WARNING][${STEP_NAME}] A deprecated configuration framework is used for configuring parameter 'DEPLOY_HOST'. This configuration framework will be removed in future versions."
neoCompatibilityConfiguration . put ( 'host' , defaultDeployHost )
}
2017-12-13 11:05:19 +02:00
2019-01-28 12:32:24 +02:00
def defaultDeployAccount = script . commonPipelineEnvironment . getConfigProperty ( 'CI_DEPLOY_ACCOUNT' )
if ( defaultDeployAccount ) {
echo "[WARNING][${STEP_NAME}] A deprecated configuration framework is used for configuring parameter 'DEPLOY_ACCOUNT'. This configuration framekwork will be removed in future versions."
neoCompatibilityConfiguration . put ( 'account' , defaultDeployAccount )
}
if ( parameters . deployHost & & ! parameters . host ) {
echo "[WARNING][${STEP_NAME}] Deprecated parameter 'deployHost' is used. This will not work anymore in future versions. Use parameter 'host' instead."
parameters . put ( 'host' , parameters . deployHost )
}
if ( parameters . deployAccount & & ! parameters . account ) {
echo "[WARNING][${STEP_NAME}] Deprecated parameter 'deployAccount' is used. This will not work anymore in future versions. Use parameter 'account' instead."
parameters . put ( 'account' , parameters . deployAccount )
}
def credId = script . commonPipelineEnvironment . getConfigProperty ( 'neoCredentialsId' )
if ( credId & & ! parameters . neoCredentialsId ) {
echo "[WARNING][${STEP_NAME}] Deprecated parameter 'neoCredentialsId' from old configuration framework is used. This will not work anymore in future versions."
parameters . put ( 'neoCredentialsId' , credId )
}
if ( ! neoCompatibilityConfiguration . isEmpty ( ) ) {
echo "[WARNING][$STEP_NAME] You are using a deprecated configuration framework. This will be removed in " +
'futureVersions.\nAdd snippet below to \'./pipeline/config.yml\' and remove ' +
'file \'.pipeline/configuration.properties\'.\n' +
"" " | steps:
| neoDeploy:
| neo:
| host: $ { neoCompatibilityConfiguration . get ( 'host' , '<Add host here>' ) }
| account: $ { neoCompatibilityConfiguration . get ( 'account' , '<Add account here>' ) }
"" " . stripMargin ( )
if ( Boolean . getBoolean ( 'com.sap.piper.featureFlag.buildUnstableWhenOldConfigFrameworkIsUsedByNeoDeploy' ) ) {
script . currentBuild . setResult ( 'UNSTABLE' )
echo "[WARNING][$STEP_NAME] Build has been set to unstable since old config framework is used."
2017-07-11 15:12:03 +02:00
}
2019-01-28 12:32:24 +02:00
return [ neo: neoCompatibilityConfiguration ]
}
return [ : ]
}
private assertPasswordRules ( String password ) {
if ( password . startsWith ( "@" ) ) {
error ( "Your password for the deployment to SAP Cloud Platform contains characters which are not " +
"supported by the neo tools. " +
"For example it is not allowed that the password starts with @. " +
"Please consult the documentation for the neo command line tool for more information: " +
"https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/8900b22376f84c609ee9baf5bf67130a.html" )
2017-07-11 15:12:03 +02:00
}
}