Skip to content

Commit

Permalink
Merge pull request #1076 from tmobile/develop
Browse files Browse the repository at this point in the history
Feature: Add support for serverless application templates
  • Loading branch information
devsatishm committed Jun 25, 2019
2 parents 2b73879 + 4c81ddd commit b882c7f
Show file tree
Hide file tree
Showing 285 changed files with 17,027 additions and 3,609 deletions.
129 changes: 70 additions & 59 deletions builds/delete-serverless-service-build-pack/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import groovy.json.JsonSlurperClassic
@Field def apigeeModule
@Field def lambdaEvents
@Field def aclModule
@Field def whiteListModule
@Field def env_deployment_descriptor

@Field def g_base_url = ''
@Field def g_svc_admin_cred_ID = 'SVC_ADMIN'
Expand Down Expand Up @@ -68,14 +70,14 @@ node {
}

accountDetailsPrimary = utilModule.getAccountInfoPrimary();
def jazz_prod_api_id = getApiIDName(accountDetailsPrimary)
g_base_url = "https://${jazz_prod_api_id}.execute-api.${configLoader.AWS.DEFAULTS.REGION}.amazonaws.com/prod"
def coreDomain = "jazz";
def jazz_prod_api_id = awsAPIGatewayModule.getApigatewayInfoCore('PROD', coreDomain, accountDetailsPrimary);
g_base_url = "https://${jazz_prod_api_id}.execute-api.${configLoader.AWS.DEFAULTS.REGION}.amazonaws.com/prod"

auth_token = getAuthToken()

stage("Initialization") {

if (params.domain && params.domain != "") {
if (params.domain && params.domain != "") {
repo_name = params.domain + "_" + params.service_name
}
sh 'rm -rf ' + repo_name
Expand Down Expand Up @@ -112,8 +114,7 @@ node {
service_config.provider = configLoader.JAZZ.DEFAULTS.PROVIDER
}

service_config.credentialId = accountInfo.CREDENTIAL_ID

service_config.credentialId = accountInfo.CREDENTIAL_ID;
lambdaEvents.setServiceConfig(service_config)
environmentMetadataLoader.initialize(service_config, configLoader, scmModule, null, env.BUILD_URL, env.BUILD_ID, g_base_url + "/jazz/environments", auth_token)
def environmentList = environmentMetadataLoader.getEnvironmentLogicalIds()
Expand Down Expand Up @@ -157,9 +158,10 @@ node {
} else {
cloudfrontEnabled = "false"
}
} else if(fileExists('build.slsapp')) {
flowType = "SLSAPP"
} else {
error "Invalid project configuration"

}
}
}
Expand Down Expand Up @@ -252,7 +254,39 @@ node {
}
break

case "WEBSITE":
case "SLSAPP":
stage('Undeploy Service') {
for (_envId in environmentIds) {
current_environment_id = _envId
loadServerlessYml(service_config, _envId)
try {
def branch = environmentMetadataLoader.getEnvironmentBranchName(_envId)
if (branch && branch != 'NA') {
events.setBranch(branch)
sonarModule.setBranch(branch)
branch = 'NA'
}
events.setEnvironment(_envId)
events.sendStartedEvent('DELETE_ENVIRONMENT', "Environment cleanup for " + _envId + " started", null, _envId)
echo 'undeploying function for environment: ' + _envId

cleanupEventSourceMapping(_envId)
unDeployService(_envId)

if (configLoader.CODE_QUALITY.SONAR.ENABLE_SONAR == "true" && configLoader.CODE_QUALITY.SONAR.CLEANUP == "true") {
cleanupCodeQualityReports()
}
archiveAssetDetails(_envId)
events.sendCompletedEvent('DELETE_ENVIRONMENT', "Environment cleanup for " + _envId + " completed", null, _envId)
} catch (ex) {
events.sendFailureEvent('DELETE_ENVIRONMENT', "Environment cleanup for " + _envId + " failed! " + ex.getMessage(), null, _envId)
error "Environment cleanup for " + _envId + " failed! " + ex.getMessage()
}
}
}
break

case "WEBSITE":
stage('Undeploy Website') {
for (_envId in environmentIds) {
current_environment_id = _envId
Expand Down Expand Up @@ -305,15 +339,11 @@ def deletePolicies(serviceId, authToken, aclUrl) {
}
}

def getApiIDName(accountDetails) {
return awsAPIGatewayModule.getApiGatewayCore(accountDetails)
}

def cleanupEventSourceMapping(env) {
withCredentials([
[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: service_config.credentialId, secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']
]) {
def credsId = null
withCredentials([
[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: service_config.credentialId, secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']
]){
def credsId = null
try {
def randomString = utilModule.generateRequestId();
credsId = "jazz-${randomString}";
Expand Down Expand Up @@ -579,7 +609,7 @@ def cleanUpApiGatewayResources(stage, path) {
current_environment = 'prod'
}
echo "Resource search key for ApiGateway : $resource_search_key"
def aws_api_id = getApiId(stage)
def aws_api_id = awsAPIGatewayModule.getApigatewayInfo(stage, service_config['domain'], service_config['service'], accountInfo, service_config)

echo "API Gateway Id is : ${aws_api_id}"

Expand Down Expand Up @@ -634,47 +664,6 @@ def getServiceBucket(stage) {
}
}


/**
* Get the API Id of the gateway specific to an environment. The value will be retrieved from environments table and if not available will try to retrieve it from config
* @param stage the environment
* @return api Id
*/
def getApiId(stage) {
echo "stage: $stage"

def envInfo = environmentMetadataLoader.getEnvironmentInfo()
def envMetadata = [:]
def apiIdMapping = null;

if (envInfo && envInfo["metadata"]) {
envMetadata = envInfo["metadata"]
}

if (envMetadata["AWS_API_ID"] != null) {
return envMetadata["AWS_API_ID"]
}
def accountAPIGateway = utilModule.getApiId(service_config)
if (stage.endsWith('-dev')) {
apiIdMapping = accountAPIGateway["DEV"]
} else if (stage == 'stg') {
apiIdMapping = accountAPIGateway["STG"]
} else if (stage == 'prod') {
apiIdMapping = accountAPIGateway["PROD"]
}

def namespace = service_config['domain']
def service = service_config['service']

if (apiIdMapping["${namespace}_${service}"]) {
return apiIdMapping["${namespace}_${service}"];
} else if (apiIdMapping["${namespace}_*"]) {
return apiIdMapping["${namespace}_*"];
} else {
return apiIdMapping["*"];
}
}

/**
* Clean up the API documentation folder from S3 corresponding to the environment
* @param stage the environment
Expand Down Expand Up @@ -1099,6 +1088,20 @@ def LoadConfiguration() {
return prop
}

/**
** Create Serverless.yml from config
**/

def loadServerlessYml(config, env){
sh "rm -f ./serverless.yml" // remove if exists
environmentMetadataLoader.setEnvironmentLogicalId(env) //set Current Environment
//Get deployment descriptor from environments
env_deployment_descriptor = environmentMetadataLoader.getEnvDeploymentDescriptor()
def deploymentDescriptor = slsBuildRules.prepareServerlessYml(config, env, configLoader, env_deployment_descriptor) // Generating the deployment descriptor
echo "prepareServerlessYml = ${deploymentDescriptor}"
writeYaml(file: './serverless.yml', data: deploymentDescriptor)
}

def loadServerlessConfig() {

def configPackURL = scmModule.getCoreRepoCloneUrl("serverless-config-pack")
Expand Down Expand Up @@ -1241,7 +1244,7 @@ def loadBuildModules(buildModuleUrl){
echo "SCM module loaded successfully."

awsAPIGatewayModule = load "aws-apigateway-module.groovy"
awsAPIGatewayModule.initialize()
awsAPIGatewayModule.initialize()

events = load "events-module.groovy"
echo "Event module loaded successfully."
Expand Down Expand Up @@ -1272,6 +1275,14 @@ def loadBuildModules(buildModuleUrl){

aclModule = load "acl-module.groovy"
echo "ACL module loaded successfully."

whiteListModule = load "whitelist-validator-module.groovy"
whiteListModule.initialize()
echo "whitelist-validator-module module loaded successfully"

slsBuildRules = load "sls-app/sbr.groovy"
slsBuildRules.initialize(steps,whiteListModule)
echo "sls-app/sbr.groovy has been loaded"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def getDistributionConfig(distributionID) {
}catch (ex) {
echo "getDistributionConfig Failed."+ex.getMessage()
echo "getDistributionConfig Failed." + ex
error "getDistributionConfig Failed."+ex.getMessage()
error "getDistributionConfig Failed."+ex.getMessage()

}

Expand Down Expand Up @@ -205,16 +205,16 @@ def loadConfigModule(buildModuleUrl){
]])

configModule = load "config-loader.groovy"
configLoader = configModule.loadConfigData(aws_credential_id, region, instance_prefix)
configLoader = configModule.loadConfigData(aws_credential_id, region, instance_prefix)
echo "config loader loaded successfully."
}
}

def getBuildModuleUrl() {
if (scm_type && scm_type != "bitbucket") {
// right now only bitbucket has this additional tag scm in its git clone path
return "http://${repo_base}/${repo_core}/jazz-build-module.git"
}else {
return "http://${repo_base}/scm/${repo_core}/jazz-build-module.git"
}
if (scm_type && scm_type != "bitbucket") {
// right now only bitbucket has this additional tag scm in its git clone path
return "http://${repo_base}/${repo_core}/jazz-build-module.git"
}else {
return "http://${repo_base}/scm/${repo_core}/jazz-build-module.git"
}
}
29 changes: 18 additions & 11 deletions builds/jazz-build-module/aws-apigateway-module.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,12 @@ def injectLambdaIntegration(method, filePath) {
}
}

def getApiGatewayCore(accountDetailsPrimary){
def gatewayValueInfo
for (item in accountDetailsPrimary.REGIONS) {
if(item.PRIMARY){
gatewayValueInfo = item.API_GATEWAY
break
}
}
return getAPIIdNameMapping(gatewayValueInfo.PROD, 'jazz', '*')
}

def getApigatewayInfo(stage, domain, service, accountDetails, config) {
def gatewayValue
for (item in accountDetails.REGIONS) {
if(item.REGION == config.region){
gatewayValue = item.API_GATEWAY
break
}
}
if(stage && (stage.endsWith('DEV')) || (stage.endsWith('dev'))) {
Expand All @@ -102,6 +92,23 @@ def getApigatewayInfo(stage, domain, service, accountDetails, config) {
}
}

def getApigatewayInfoCore(stage, domain, accountDetails) {
def gatewayValueInfo
for (item in accountDetails.REGIONS) {
if(item.PRIMARY){
gatewayValueInfo = item.API_GATEWAY
break
}
}
if(stage && (stage.endsWith('DEV')) || (stage.endsWith('dev'))) {
return getAPIIdNameMapping(gatewayValueInfo.DEV, domain, '*')
} else if (stage && (stage == 'STG') || (stage == 'stg')) {
return getAPIIdNameMapping(gatewayValueInfo.STG, domain, '*')
} else if (stage && (stage == 'PROD') || (stage == 'prod')) {
return getAPIIdNameMapping(gatewayValueInfo.PROD, domain, '*')
}
}

def getAPIIdNameMapping(apiIdMapping, namespace, service) {
if (!apiIdMapping) {
error "No mapping document provided to lookup API !"
Expand Down
15 changes: 13 additions & 2 deletions builds/jazz-build-module/config-loader.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ echo "the module, 'config-loader', loaded successfully... congratulations..."
*/
def loadConfigData(aws_credential_id, region, instance_prefix) {
def table_name = "${instance_prefix}_JazzConfig"
def credsId
try {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID',
credentialsId: aws_credential_id , secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
UUID uuid = UUID.randomUUID()
def randomString = uuid.toString();
def credsId = "jazz-${randomString}";
credsId = "jazz-${randomString}";
sh "aws configure set profile.${credsId}.region ${region}"
sh "aws configure set profile.${credsId}.aws_access_key_id $AWS_ACCESS_KEY_ID"
sh "aws configure set profile.${credsId}.aws_secret_access_key $AWS_SECRET_ACCESS_KEY"
Expand All @@ -44,8 +45,10 @@ def loadConfigData(aws_credential_id, region, instance_prefix) {
error "No configurations defined in the config catalog."
}
}
}catch(ex) {
} catch(ex) {
error "Failed to fetch the configuration details."
} finally {
resetCredentials(credsId)
}
}

Expand Down Expand Up @@ -92,4 +95,12 @@ def parseJson(jsonString) {
return m
}

def resetCredentials(credsId) {
echo "resetting AWS credentials"
def credPath = System.getenv().HOME + "/.aws/credentials"
def confPath = System.getenv().HOME + "/.aws/config"
sh "sed -i '/${credsId}/,+2d' ${credPath}"
sh "sed -i '/${credsId}/,+1d' ${confPath}"
}

return this
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ def getEnvironmentInfo() {
}
}

def getEnvDeploymentDescriptor() {
def envdeploymentdescriptor =null
if (g_environment_logical_id != null && g_service_config['domain'] != "jazz") {
def getEnvironments = sh(script: "curl -H \"Content-type: application/json\" \
-H \"Jazz-Service-ID: ${g_service_config['service_id']}\" \
-H \"Authorization: $g_login_token \" \
-X GET \"${g_environment_api}?service=${g_service_config['service']}&domain=${g_service_config['domain']}\" ", returnStdout: true).trim()

if( getEnvironments ) {
def environmentsData = parseJson(getEnvironments)
for( env in environmentsData.data.environment) {
if(env['logical_id'] == g_environment_logical_id) {
envdeploymentdescriptor = env['deployment_descriptor']
}
}
}
}
return envdeploymentdescriptor
}


/**
* @param environment_logical_id
*/
Expand Down
5 changes: 4 additions & 1 deletion builds/jazz-build-module/events-module.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ echo "Events module loaded successfully"
'APIGEE_API_PROXY_DEPLOY': 'APIGEE_API_PROXY_DEPLOY',
'APIGEE_API_PROXY_DELETE': 'APIGEE_API_PROXY_DELETE',
'DEPLOY_TO_AWS_APIGATEWAY': 'DEPLOY_TO_AWS_APIGATEWAY',
'DEPLOY_TO_GCP_APIGEE': 'DEPLOY_TO_GCP_APIGEE'
'DEPLOY_TO_GCP_APIGEE': 'DEPLOY_TO_GCP_APIGEE',
'PACKAGE': 'PACKAGE',
'TEMPLATE_VALIDATION': 'TEMPLATE_VALIDATION',
'INSTALL_PLUGINS': 'INSTALL_PLUGINS'
]

/**
Expand Down
Loading

0 comments on commit b882c7f

Please sign in to comment.