2018-10-30 17:49:53 +02:00
|
|
|
import static com.sap.piper.Prerequisites.checkScript
|
2024-10-30 12:59:47 +02:00
|
|
|
import static com.sap.piper.BashUtils.quoteAndEscape as q
|
2018-10-30 17:49:53 +02:00
|
|
|
|
2019-04-01 09:47:16 +02:00
|
|
|
import com.sap.piper.GenerateDocumentation
|
2018-10-12 16:06:41 +02:00
|
|
|
import com.sap.piper.ConfigurationHelper
|
2018-10-15 14:18:47 +02:00
|
|
|
import com.sap.piper.Utils
|
2018-10-12 16:06:41 +02:00
|
|
|
import groovy.transform.Field
|
|
|
|
|
2018-11-29 10:54:05 +02:00
|
|
|
@Field String STEP_NAME = getClass().getName()
|
2018-10-25 13:52:56 +02:00
|
|
|
|
|
|
|
@Field Set GENERAL_CONFIG_KEYS = STEP_CONFIG_KEYS
|
|
|
|
|
2018-10-12 16:06:41 +02:00
|
|
|
@Field Set STEP_CONFIG_KEYS = [
|
2019-04-01 09:47:16 +02:00
|
|
|
/** Optionally with `healthEndpoint` the health function is called if endpoint is not the standard url.*/
|
2018-10-15 14:18:47 +02:00
|
|
|
'healthEndpoint',
|
2019-04-01 09:47:16 +02:00
|
|
|
/**
|
|
|
|
* Health check function is called providing full qualified `testServerUrl` to the health check.
|
2019-04-02 21:01:09 +02:00
|
|
|
*
|
2019-04-01 09:47:16 +02:00
|
|
|
*/
|
2018-10-12 16:06:41 +02:00
|
|
|
'testServerUrl'
|
|
|
|
]
|
2018-10-25 13:52:56 +02:00
|
|
|
|
2018-10-12 16:06:41 +02:00
|
|
|
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
|
|
|
|
|
2019-04-01 09:47:16 +02:00
|
|
|
/**
|
|
|
|
* Calls the health endpoint url of the application.
|
|
|
|
*
|
|
|
|
* The intention of the check is to verify that a suitable health endpoint is available. Such a health endpoint is required for operation purposes.
|
|
|
|
*
|
|
|
|
* This check is used as a real-life test for your productive health endpoints.
|
|
|
|
*
|
|
|
|
* !!! note "Check Depth"
|
|
|
|
* Typically, tools performing simple health checks are not too smart. Therefore it is important to choose an endpoint for checking wisely.
|
|
|
|
*
|
|
|
|
* This check therefore only checks if the application/service url returns `HTTP 200`.
|
|
|
|
*
|
|
|
|
* This is in line with health check capabilities of platforms which are used for example in load balancing scenarios. Here you can find an [example for Amazon AWS](http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-healthchecks.html).
|
|
|
|
*/
|
|
|
|
@GenerateDocumentation
|
2018-10-12 16:06:41 +02:00
|
|
|
void call(Map parameters = [:]) {
|
|
|
|
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
|
2018-10-31 09:40:12 +02:00
|
|
|
def script = checkScript(this, parameters) ?: this
|
2020-08-26 15:32:58 +02:00
|
|
|
String stageName = parameters.stageName ?: env.STAGE_NAME
|
|
|
|
|
2018-10-12 16:06:41 +02:00
|
|
|
// load default & individual configuration
|
2018-10-18 11:02:09 +02:00
|
|
|
Map config = ConfigurationHelper.newInstance(this)
|
2020-08-26 15:32:58 +02:00
|
|
|
.loadStepDefaults([:], stageName)
|
2018-10-25 13:52:56 +02:00
|
|
|
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
|
2018-10-12 16:06:41 +02:00
|
|
|
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
|
2020-08-26 15:32:58 +02:00
|
|
|
.mixinStageConfig(script.commonPipelineEnvironment, stageName, STEP_CONFIG_KEYS)
|
2018-10-12 16:06:41 +02:00
|
|
|
.mixin(parameters, PARAMETER_KEYS)
|
|
|
|
.withMandatoryProperty('testServerUrl')
|
|
|
|
.use()
|
|
|
|
|
|
|
|
def checkUrl = config.testServerUrl
|
|
|
|
if(config.healthEndpoint){
|
|
|
|
if(!checkUrl.endsWith('/'))
|
|
|
|
checkUrl += '/'
|
|
|
|
checkUrl += config.healthEndpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
def statusCode = curl(checkUrl)
|
|
|
|
if (statusCode != '200') {
|
|
|
|
error "Health check failed: ${statusCode}"
|
|
|
|
} else {
|
|
|
|
echo "Health check for ${checkUrl} successful"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def curl(url){
|
|
|
|
return sh(
|
|
|
|
returnStdout: true,
|
2024-10-30 12:59:47 +02:00
|
|
|
script: "curl -so /dev/null -w '%{response_code}' ${q(url)}"
|
2018-10-12 16:06:41 +02:00
|
|
|
).trim()
|
|
|
|
}
|