2018-09-21 16:55:31 +02:00
import static com . sap . piper . Prerequisites . checkScript
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-02-20 15:30:34 +01:00
2018-03-19 18:29:41 +01:00
import com.sap.piper.tools.ToolDescriptor
2017-11-24 15:59:34 +01:00
2018-08-15 09:26:38 +02:00
import groovy.transform.Field
2018-11-29 09:54:05 +01:00
@Field String STEP_NAME = getClass ( ) . getName ( )
2018-08-15 09:26:38 +02:00
@Field Set GENERAL_CONFIG_KEYS = [ ]
@Field Set STEP_CONFIG_KEYS = [
'account' ,
'dockerEnvVars' ,
'dockerImage' ,
'dockerOptions' ,
'host' ,
'neoCredentialsId' ,
'neoHome'
]
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS . plus ( [
'applicationName' ,
'archivePath' ,
'deployAccount' , //deprecated, replaced by parameter 'account'
'deployHost' , //deprecated, replaced by parameter 'host'
'deployMode' ,
'propertiesFile' ,
'runtime' ,
'runtimeVersion' ,
'vmSize' ,
'warAction'
] )
2018-02-20 15:30:34 +01:00
2018-08-30 16:33:07 +02:00
void call ( parameters = [ : ] ) {
2018-08-15 09:26:38 +02:00
handlePipelineStepErrors ( stepName: STEP_NAME , stepParameters: parameters ) {
2017-12-11 11:15:51 +01:00
2018-10-31 08:40:12 +01:00
def script = checkScript ( this , parameters ) ? : this
2018-09-21 16:55:31 +02:00
2018-11-08 09:44:11 +01:00
def utils = parameters . utils ? : new Utils ( )
2017-12-11 13:55:07 +01:00
2017-12-11 11:15:51 +01:00
prepareDefaultValues script: script
2018-08-15 09:26:38 +02:00
final Map stepCompatibilityConfiguration = [ : ]
2017-12-11 11:15:51 +01:00
// Backward compatibility: ensure old configuration is taken into account
// The old configuration in not stage / step specific
def defaultDeployHost = script . commonPipelineEnvironment . getConfigProperty ( 'DEPLOY_HOST' )
if ( defaultDeployHost ) {
2018-08-15 09:26:38 +02:00
echo "[WARNING][${STEP_NAME}] A deprecated configuration framework is used for configuring parameter 'DEPLOY_HOST'. This configuration framework will be removed in future versions."
stepCompatibilityConfiguration . put ( 'host' , defaultDeployHost )
2017-12-11 11:15:51 +01:00
}
def defaultDeployAccount = script . commonPipelineEnvironment . getConfigProperty ( 'CI_DEPLOY_ACCOUNT' )
if ( defaultDeployAccount ) {
2018-08-15 09:26:38 +02:00
echo "[WARNING][${STEP_NAME}] A deprecated configuration framework is used for configuring parameter 'DEPLOY_ACCOUNT'. This configuration framekwork will be removed in future versions."
stepCompatibilityConfiguration . put ( 'account' , defaultDeployAccount )
2017-12-11 11:15:51 +01:00
}
2018-01-29 12:50:59 +01:00
if ( parameters . deployHost & & ! parameters . host ) {
2018-08-15 09:26:38 +02:00
echo "[WARNING][${STEP_NAME}] Deprecated parameter 'deployHost' is used. This will not work anymore in future versions. Use parameter 'host' instead."
2018-01-29 12:50:59 +01:00
parameters . put ( 'host' , parameters . deployHost )
2017-12-11 11:15:51 +01:00
}
2017-07-11 15:12:03 +02:00
2018-01-29 12:50:59 +01:00
if ( parameters . deployAccount & & ! parameters . account ) {
2018-08-15 09:26:38 +02:00
echo "[WARNING][${STEP_NAME}] Deprecated parameter 'deployAccount' is used. This will not work anymore in future versions. Use parameter 'account' instead."
2018-01-29 12:50:59 +01:00
parameters . put ( 'account' , parameters . deployAccount )
2017-07-11 15:12:03 +02:00
}
2018-01-30 17:09:17 +01:00
def credId = script . commonPipelineEnvironment . getConfigProperty ( 'neoCredentialsId' )
if ( credId & & ! parameters . neoCredentialsId ) {
2018-08-15 09:26:38 +02:00
echo "[WARNING][${STEP_NAME}] Deprecated parameter 'neoCredentialsId' from old configuration framework is used. This will not work anymore in future versions."
2018-01-30 17:09:17 +01:00
parameters . put ( 'neoCredentialsId' , credId )
}
2018-11-08 10:29:30 +01:00
if ( ! stepCompatibilityConfiguration . 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:
| host: $ { stepCompatibilityConfiguration . get ( 'host' , '<Add host here>' ) }
| account: $ { stepCompatibilityConfiguration . 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-12-11 11:15:51 +01:00
// Backward compatibility end
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 )
. mixinStepConfig ( script . commonPipelineEnvironment , STEP_CONFIG_KEYS )
. mixinStageConfig ( script . commonPipelineEnvironment , parameters . stageName ? : env . STAGE_NAME , STEP_CONFIG_KEYS )
2018-08-17 12:52:01 +02:00
. addIfEmpty ( 'archivePath' , script . commonPipelineEnvironment . getMtarFilePath ( ) )
2018-08-15 09:26:38 +02:00
. mixin ( parameters , PARAMETER_KEYS )
. 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 08:47:34 +01:00
stepParamKey1: 'deployMode' ,
2018-08-30 11:03:34 +02:00
stepParam1: configuration . deployMode = = 'mta' ? 'mta' : 'war' , // ['mta', 'warParams', 'warPropertiesFile']
2019-01-21 08:47:34 +01:00
stepParamKey2: 'warAction' ,
2018-10-30 16:22:42 +01:00
stepParam2: configuration . warAction = = 'rolling-update' ? 'blue-green' : 'standard' , // ['deploy', 'deploy-mta', 'rolling-update']
2019-01-21 08:47:34 +01:00
stepParamKey3: 'scriptMissing' ,
2018-11-08 09:44:11 +01:00
stepParam3: parameters ? . script = = null ,
2019-01-21 08:47:34 +01:00
stepParamKey4: 'legacyConfig' ,
2018-11-08 09:44:11 +01:00
stepParam4: ! stepCompatibilityConfiguration . isEmpty ( ) ,
2018-08-30 11:03:34 +02:00
] , configuration )
2017-12-11 11:15:51 +01:00
def archivePath = configuration . archivePath
if ( archivePath ? . trim ( ) ) {
if ( ! fileExists ( archivePath ) ) {
error "Archive cannot be found with parameter archivePath: '${archivePath}'."
}
} else {
error "Archive path not configured (parameter \"archivePath\")."
}
def deployHost
def deployAccount
2018-02-20 11:13:37 +01:00
def credentialsId = configuration . get ( 'neoCredentialsId' )
2017-12-11 11:15:51 +01:00
def deployMode = configuration . deployMode
def warAction
def propertiesFile
def applicationName
def runtime
def runtimeVersion
def vmSize
2017-12-28 13:10:11 +01:00
2018-04-20 12:23:26 +02:00
def deployModes = [ 'mta' , 'warParams' , 'warPropertiesFile' ]
if ( ! ( deployMode in deployModes ) ) {
throw new Exception ( "[neoDeploy] Invalid deployMode = '${deployMode}'. Valid 'deployMode' values are: ${deployModes}." )
2018-01-16 10:54:17 +01:00
}
2018-04-20 12:23:26 +02:00
if ( deployMode in [ 'warPropertiesFile' , 'warParams' ] ) {
2017-12-11 11:15:51 +01:00
warAction = utils . getMandatoryParameter ( configuration , 'warAction' )
2018-04-20 12:25:15 +02:00
def warActions = [ 'deploy' , 'rolling-update' ]
if ( ! ( warAction in warActions ) ) {
throw new Exception ( "[neoDeploy] Invalid warAction = '${warAction}'. Valid 'warAction' values are: ${warActions}." )
2018-01-16 10:54:17 +01:00
}
2018-04-20 15:00:21 +02:00
} else if ( deployMode = = 'mta' ) {
warAction = 'deploy-mta'
2018-01-16 10:54:17 +01:00
}
2017-12-11 11:15:51 +01:00
2018-01-17 11:17:24 +01:00
if ( deployMode = = 'warPropertiesFile' ) {
2017-12-11 11:15:51 +01:00
propertiesFile = utils . getMandatoryParameter ( configuration , 'propertiesFile' )
2018-01-12 13:02:48 +01:00
if ( ! fileExists ( propertiesFile ) ) {
2018-01-11 15:25:58 +01:00
error "Properties file cannot be found with parameter propertiesFile: '${propertiesFile}'."
}
2017-12-28 13:10:11 +01:00
}
2018-01-17 11:17:24 +01:00
if ( deployMode = = 'warParams' ) {
2017-12-11 11:15:51 +01:00
applicationName = utils . getMandatoryParameter ( configuration , 'applicationName' )
runtime = utils . getMandatoryParameter ( configuration , 'runtime' )
runtimeVersion = utils . getMandatoryParameter ( configuration , 'runtimeVersion' )
2018-04-20 12:24:37 +02:00
def vmSizes = [ 'lite' , 'pro' , 'prem' , 'prem-plus' ]
2017-12-11 11:15:51 +01:00
vmSize = configuration . vmSize
2018-04-20 12:24:37 +02:00
if ( ! ( vmSize in vmSizes ) ) {
throw new Exception ( "[neoDeploy] Invalid vmSize = '${vmSize}'. Valid 'vmSize' values are: ${vmSizes}." )
2018-01-11 15:25:58 +01:00
}
2017-12-28 13:10:11 +01:00
}
2018-04-20 12:23:26 +02:00
if ( deployMode in [ 'mta' , 'warParams' ] ) {
2017-12-11 11:15:51 +01:00
deployHost = utils . getMandatoryParameter ( configuration , 'host' )
deployAccount = utils . getMandatoryParameter ( configuration , 'account' )
2017-12-28 13:10:11 +01:00
}
2018-07-27 14:09:08 +02:00
def neo = new ToolDescriptor ( 'SAP Cloud Platform Console Client' , 'NEO_HOME' , 'neoHome' , '/tools/' , 'neo.sh' , null , 'version' )
def neoExecutable = neo . getToolExecutable ( this , configuration )
def neoDeployScript = "" " # ! /bin/ bash
"${neoExecutable}" $ { warAction } \
2018-04-20 15:42:58 +02:00
- - source "${archivePath}" \
2018-07-27 14:09:08 +02:00
"" "
2018-04-20 15:20:50 +02:00
if ( deployMode in [ 'mta' , 'warParams' ] ) {
2018-07-27 14:09:08 +02:00
neoDeployScript + =
2018-04-20 15:11:54 +02:00
"" " - - host '${deployHost}' \
2017-12-13 10:05:19 +01:00
- - account '${deployAccount}' \
2018-04-20 15:20:50 +02:00
"" "
}
if ( deployMode = = 'mta' ) {
2018-07-27 14:09:08 +02:00
neoDeployScript + = "--synchronous"
2017-12-13 10:05:19 +01:00
}
if ( deployMode = = 'warParams' ) {
2018-07-27 14:09:08 +02:00
neoDeployScript + =
2018-04-20 15:20:50 +02:00
"" " - - application '${applicationName}' \
2017-12-13 10:05:19 +01:00
- - runtime '${runtime}' \
- - runtime - version '${runtimeVersion}' \
- - size '${vmSize}' "" "
}
if ( deployMode = = 'warPropertiesFile' ) {
2018-07-27 14:09:08 +02:00
neoDeployScript + =
2018-04-20 15:11:54 +02:00
"""${propertiesFile}"""
2017-12-13 10:05:19 +01:00
}
2018-01-10 13:22:36 +01:00
withCredentials ( [ usernamePassword (
credentialsId: credentialsId ,
passwordVariable: 'password' ,
usernameVariable: 'username' ) ] ) {
2018-04-20 15:44:39 +02:00
def credentials =
2018-01-10 13:22:36 +01:00
"" " - - user '${username}' \
- - password '${password}' \
"" "
2018-11-09 07:42:33 +01:00
dockerExecute (
script: script ,
dockerImage: configuration . get ( 'dockerImage' ) ,
dockerEnvVars: configuration . get ( 'dockerEnvVars' ) ,
dockerOptions: configuration . get ( 'dockerOptions' )
) {
2018-01-10 13:22:36 +01:00
2018-07-27 14:09:08 +02:00
neo . verify ( this , configuration )
2018-03-19 18:29:41 +01:00
def java = new ToolDescriptor ( 'Java' , 'JAVA_HOME' , '' , '/bin/' , 'java' , '1.8.0' , '-version 2>&1' )
java . verify ( this , configuration )
2018-03-06 16:09:03 +01:00
2018-07-27 14:09:08 +02:00
sh "" " $ { neoDeployScript } \
2018-04-20 15:44:39 +02:00
$ { credentials }
2017-12-28 13:10:11 +01:00
"" "
}
2017-07-11 15:12:03 +02:00
}
}
}