Skip to content Enterprise Search or jump to… Pull requests Issues Explore @503328418 You don’t have an email address. We recommend adding at least one email. An email address lets you receive notifications via email, reset your password, and more. PD-CORE / JenkinsLibrary Private Code Issues Pull requests 1 Wiki Security Insights JenkinsLibrary/vars/ec2_check.groovy @204047628 204047628 Update ec2_check.groovy Latest commit f6a39a0 on 2 Nov 2022 History 1 contributor 184 lines (163 sloc) 7.48 KB def call(body) { def config = [ENV:'dev'] body.resolveStrategy = Closure.DELEGATE_FIRST body.delegate = config body() def role_arn = '' def AWS_REGION = '' def EMR_INSTANCE_NAME_PATTERN = 'EMR' def sign = '' Map AWS_ACCOUNT = ['dev': '011821064023', 'ingestion':'497021971364', 'pre-prod': '436012210803', 'prod-as': '277651853236', 'prod-ds': '867396380247', 'management': '030929358026'] pipeline { agent { docker { image 'registry.gear.ge.com/pd-mnd/aws-cli:1.0.4' label 'dind' } } environment { ROLENAME = 'terraform-admin' // for AWS west accounts of 011821064023, 277651853236, 277651853236, 867396380247 ROLENAME_PREPROD = 'usw02-stg-mnd-iam-terraform-admin-role' //for AWS west account 436012210803 ROLENAME_INGESTION = 'bosh-provisioner' //for AWS east account 497021971364 SESSION_NAME = "Jenkins-EC2-checkes_${config.ENV}" arn = "arn:aws:iam::${AWS_ACCOUNT[config.ENV]}" ec2_running_txt = "ec2_running_${config.ENV}.txt" ec2_without_auto_scalling_txt = "ec2_without_auto_scalling_${config.ENV}.txt" ec2_with_emr_cluster_master_and_no_auto_scalling_txt = "ec2_with_emr_cluster_master_and_no_auto_scalling_${config.ENV}.txt" instanceID_auto_scaling_txt = "instanceID_auto_scaling_${config.ENV}.txt" ec2_auto_scaling_only_txt = "ec2_auto_scaling_only_${config.ENV}.txt" ec2_emr_cluster_master_txt = "ec2_emr_cluster_master_${config.ENV}.txt" ec2_txt_folder = "ec2-check/${config.ENV}" DEFAULT_RECIPIENTS = 'pdf.t3.support@ge.com,jianmin.zhang@ge.com' DIFF_RESULT = "diff_result_${config.ENV}.txt" EMAIL_SUBJECT = "EC2 in ${config.ENV} ENV not match the inventory baseline in github" } options { timeout(time: 10, unit: 'MINUTES') //skipDefaultCheckout(true) //buildDiscarder(logRotator(daysToKeepStr: '30', numToKeepStr: '30')) } stages{ stage('Assume Role'){ steps{ script{ if (config.ENV == 'ingestion') { AWS_REGION = 'us-east-1' } else { AWS_REGION = 'us-west-2' } if (config.ENV == 'pre-prod') { role_arn = "${arn}:role/${ROLENAME_PREPROD}" EMR_INSTANCE_NAME_PATTERN = '-emr-' } else if (config.ENV == 'ingestion') { role_arn = "${arn}:role/${ROLENAME_INGESTION}" } else { role_arn = "${arn}:role/${ROLENAME}" } echo "role_arn = ${role_arn}" withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'AWS_POWER_TF_DEV', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) { sh("aws sts assume-role --output json --duration-seconds 2000 --role-arn '${role_arn}' --region ${AWS_REGION} --role-session-name '${SESSION_NAME}' > ~/temp_creds") } } } } stage('List Instances'){ steps{ script{ sh """ set +x export AWS_ACCESS_KEY_ID=`cat ~/temp_creds | jq -r '.Credentials.AccessKeyId'` export AWS_SECRET_ACCESS_KEY=`cat ~/temp_creds | jq -r '.Credentials.SecretAccessKey'` export AWS_SESSION_TOKEN=`cat ~/temp_creds | jq -r '.Credentials.SessionToken'` export AWS_DEFAULT_REGION=$AWS_REGION set -x bash ec2-check/ec2_check.sh $AWS_REGION """ ec2_running_count = sh (script: "wc -l ./$ec2_running_txt | awk '{print \$1}'", returnStdout: true).trim() echo "Total running instances: $ec2_running_count" // get rid of ec2 that is in auto scaling group if (fileExists("$instanceID_auto_scaling_txt")){ readFile("$ec2_running_txt").split('\n').each { line, count -> def found = false readFile("$instanceID_auto_scaling_txt").split('\n').each { id, count_id -> if (line =~ id){ sh "echo '$line' >> $ec2_auto_scaling_only_txt" found = true } } if (!found){ sh "echo '$line' >> $ec2_without_auto_scalling_txt" } } } // get rid of ec2 that is in emr instance group if (fileExists("$ec2_without_auto_scalling_txt")){ sh "cat $ec2_without_auto_scalling_txt | grep -v $EMR_INSTANCE_NAME_PATTERN > $ec2_with_emr_cluster_master_and_no_auto_scalling_txt" } else { sh "cat $ec2_running_txt | grep -v $EMR_INSTANCE_NAME_PATTERN > $ec2_with_emr_cluster_master_and_no_auto_scalling_txt" } // add emr master only instances if (fileExists("$ec2_emr_cluster_master_txt")){ sh "cat $ec2_emr_cluster_master_txt >> $ec2_with_emr_cluster_master_and_no_auto_scalling_txt" } echo "============================================" echo "Running EC2 instances that are in auto scaling group:" sh "cat $ec2_auto_scaling_only_txt" echo "============================================" echo "Running EC2 instances that are MASTER node in EMR cluster:" sh "cat $ec2_emr_cluster_master_txt" echo "============================================" echo "Running EC2 instances excluding those in auto scaling group and core and task nodes in EMR instance group:" sh "cat $ec2_with_emr_cluster_master_and_no_auto_scalling_txt" sh "ls -l ${ec2_txt_folder}/$ec2_with_emr_cluster_master_and_no_auto_scalling_txt" sh "diff -b -i $ec2_with_emr_cluster_master_and_no_auto_scalling_txt ${ec2_txt_folder}/$ec2_with_emr_cluster_master_and_no_auto_scalling_txt > ${DIFF_RESULT} ||:" def output_list = readFile("${DIFF_RESULT}") if (output_list.size() != 0) { sh "cat ${DIFF_RESULT}" sign = '!!' emailNotification() } currentBuild.displayName = '#' + env.BUILD_NUMBER + ':' + 'ec2-running' + '=' + ec2_running_count + sign //display total number of running ec2 } } } } // stages post { failure { script{ email_failure() } } } } //pipeline } def emailNotification() { emailext subject: "${EMAIL_SUBJECT}", body: "${env.BUILD_URL} shows ${EMAIL_SUBJECT}", mimeType: 'text/html', attachLog: false, attachmentsPattern: "${DIFF_RESULT}", recipientProviders: [ [$class: 'CulpritsRecipientProvider'], [$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider'] ], to: 'pdf.t3.support@ge.com,jianmin.zhang@ge.com' } def email_failure() { emailext subject: '$DEFAULT_SUBJECT', body: '$DEFAULT_CONTENT', mimeType: 'text/html', attachLog: false, recipientProviders: [ [$class: 'CulpritsRecipientProvider'], [$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider'] ], to: 'jianmin.zhang@ge.com' } GE Global Research GE Global Research GE Global Research © 2023 GitHub, Inc. Help Support API Training Blog About GitHub Enterprise Server 3.6.5
The function add takes two arguments a and b and returns the sum of a and b.