Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This is a Jenkins pipeline script created to orchestrate xAgent performance workloads
- * from the instructions/parameters specified in the canned scenario JSON files.
- *
- * @author Yaroslav Styranivskyy
- * @since 2017-06-22
- */
- import groovy.json.JsonSlurperClassic
- import groovy.json.JsonOutput
- def scenario_config_dir = ""
- if (env.HOME)
- {
- scenario_config_dir = "${env.HOME}/userContent/perf_params"+
- '/agent/jobs/pipe_master_abstract/scenario_configs'
- }
- else if (env.JENKINS_HOME)
- {
- scenario_config_dir = "${env.JENKINS_HOME}/userContent/perf_params"+
- '/agent/jobs/pipe_master_abstract/scenario_configs'
- }
- def config_json = new JsonSlurperClassic().parseText(params.CONFIG)
- def utils = null
- def agentFlows = [:]
- def allFlowsPassed = true
- def allFlowsFailed = true
- node(config_json.execution_node) {
- cleanWs patterns: [[pattern: 'virtualenvs/**', type: 'EXCLUDE']], deleteDirs: true
- stage('Prepare') {
- utils = load "${env.JENKINS_HOME}/userContent/perf_params"+
- '/common/pipeline/utils.groovy'
- echo """
- **********************************
- * JOB CONFIG *
- **********************************
- ${JsonOutput.prettyPrint(params.CONFIG)}
- """
- // Read configuration for the specified scenario.
- def scenario_config = readFile("${scenario_config_dir}/${config_json.workload_scenario}.json")
- def scenario_config_json = new JsonSlurperClassic().parseText(scenario_config)
- echo """
- **********************************
- * CANNED SCENARIO CONFIG *
- **********************************
- ${JsonOutput.prettyPrint(scenario_config)}
- """
- /* Merge "agent" element from canned scenario config with all agents,
- * while ensuring merged items match the agent platform. */
- config_json.hx.agents = config_json.hx.agents.collect {
- def agent = scenario_config_json.hx.agent.collectEntries { k, v ->
- if(! k.contains('install_config')) {
- [(k): (v)]
- }
- else if(k == 'pre_install_config'
- && ! scenario_config_json.hx.agent.containsKey('pre_install_config_'+ it.platform)) {
- [(k): (v)]
- }
- else if(k == 'pre_install_config_'+ it.platform) {
- ['pre_install_config': (v)]
- }
- else if(k == 'post_install_config'
- && ! scenario_config_json.hx.agent.containsKey('post_install_config_'+ it.platform)) {
- [(k): (v)]
- }
- else if(k == 'post_install_config_'+ it.platform) {
- ['post_install_config': (v)]
- }
- else {
- [:]
- }
- }
- it << agent
- }
- scenario_config_json.hx.remove('agent')
- // Merge supplied config and scenario config.
- config_json = utils.mapPlus(scenario_config_json, config_json)
- config_json = new JsonSlurperClassic().parseText(utils.evalExpressions(JsonOutput.toJson(config_json)))
- writeFile(
- file: 'config.json',
- text: JsonOutput.prettyPrint(JsonOutput.toJson(config_json))
- )
- echo """
- **********************************
- * COMBINED CONFIG *
- **********************************
- ${JsonOutput.prettyPrint(JsonOutput.toJson(config_json))}
- """
- /* TODO - hx_reset.py should take parameters in the format of config_json,
- * so this conversion can be avoided. */
- def hx_reset_param = [
- hx_host: config_json.hx.hostname,
- hx_admin_pass: config_json.hx.password
- ]
- writeFile(
- file: "hx_reset_config.json",
- text: JsonOutput.toJson(hx_reset_param)
- )
- // TODO - temporarily transfer JSON to older HX format.
- def hx_config_param = [
- hx_host: config_json.hx.hostname,
- hx_admin_pass: config_json.hx.password,
- hx_api_user: config_json.hx.api_user,
- hx_api_pass: config_json.hx.api_pass,
- hx_config: config_json.hx.config
- ]
- writeFile(
- file: "hx_configure_config.json",
- text: JsonOutput.toJson(hx_config_param)
- )
- // TODO - end.
- currentBuild.displayName = "${params.BUILD_TAG}_${config_json.environment}_#${currentBuild.number}"
- // Checkout projects based on HX release.
- def perf_setup_scm = utils.getProjectSCMInfoByHXRelease('perf_setup', config_json.hx.release,GIT_ORG)
- utils.checkoutAndSetupProject('perf_setup', perf_setup_scm)
- def pyhx_workload_scm = utils.getProjectSCMInfoByHXRelease('pyhx_workload', config_json.hx.release,GIT_ORG)
- utils.checkoutAndSetupProject('pyhx_workload', pyhx_workload_scm)
- // Checkout projects based on xAgent version
- def agent_veresions = config_json.hx.agents.collect { it.version }.unique()
- agent_veresions.each {
- // Checkout projects.
- perf_setup_scm = utils.getProjectSCMInfoByAgentVersion('perf_setup', it, GIT_ORG)
- utils.checkoutAndSetupProject("perf_setup_${it}", perf_setup_scm)
- pyhx_workload_scm = utils.getProjectSCMInfoByAgentVersion('pyhx_workload', it, GIT_ORG)
- utils.checkoutAndSetupProject("pyhx_workload_${it}", pyhx_workload_scm)
- def pwrg_scm = utils.getProjectSCMInfoByAgentVersion('pwrg', it, GIT_ORG)
- utils.checkoutAndSetupProject("pwrg_${it}", pwrg_scm, "pip install -r xAgent/requirements.txt")
- }
- // Setup HX actions if needed.
- if(config_json.workload.containsKey('hx_actions')) {
- def hx_workload = [:] << config_json.workload
- hx_workload['actions'] = hx_workload.remove('hx_actions')
- def hx_workload_config_param = [
- hx: config_json.hx,
- agent: [
- platform: 'hx'
- ],
- workload: hx_workload
- ]
- writeFile(
- file: "hx_actions_config.json",
- text: JsonOutput.toJson(hx_workload_config_param)
- )
- agentFlows['HX Actions'] = {
- stage('HX Actions') {
- utils.runProject('pyhx_workload',
- "agent_workload --config-file ${env.WORKSPACE}/hx_actions_config.json"
- )
- }
- }
- }
- // Setup agent flows.
- config_json.hx.agents.each { agent_conf ->
- def pfm_spec_items = [
- 'scenario_description': 'scenario_description_'+ agent_conf.platform,
- 'actions': 'actions_'+ agent_conf.platform
- ]
- def agent_config_param = [
- hx: config_json.hx,
- agent: agent_conf,
- workload:
- // Ensure workload actions are for the correct platform.
- config_json.workload.collectEntries { k, v ->
- if(pfm_spec_items.containsKey(k) && ! config_json.workload.get(pfm_spec_items.get(k))) {
- [(k): (v)]
- }
- else if(! pfm_spec_items.find{ k.startsWith(it.key) } ) {
- [(k): (v)]
- }
- else if(pfm_spec_items.containsValue(k)) {
- [(pfm_spec_items.find{ it.value == k }.key): (v)]
- }
- else {
- [:]
- }
- }
- ]
- writeFile(
- file: "agent_${agent_conf.hostname}_config.json",
- text: JsonOutput.toJson(agent_config_param)
- )
- agentFlows["${agent_conf.hostname} Flow"] = {
- try {
- stage("Configure Agent") {
- utils.runProject("perf_setup_${agent_conf.version}",
- "agent_configure --config ${env.WORKSPACE}/agent_${agent_conf.hostname}_config.json"
- )
- }
- try {
- stage('Start Monitoring') {
- utils.runProject("perf_setup_${agent_conf.version}",
- "agent_data_collection --config ${env.WORKSPACE}/agent_${agent_conf.hostname}_config.json --action start"
- )
- }
- stage('Execute Workload') {
- utils.runProject("pyhx_workload_${agent_conf.version}",
- "agent_workload --config-file ${env.WORKSPACE}/agent_${agent_conf.hostname}_config.json"
- )
- }
- }
- catch(exc) {
- throw exc
- }
- finally {
- stage('Stop Monitoring') {
- def collection_type = (agent_conf.deployment.type == 'uninstall' ||
- agent_conf.deployment.type == 'none') ? 'basic' : 'xagent'
- utils.runProject("perf_setup_${agent_conf.version}", """
- agent_data_collection --config ${env.WORKSPACE}/agent_${agent_conf.hostname}_config.json \
- --action stop --collection_type ${collection_type}
- """
- )
- dir("perf_setup_${agent_conf.version}") {
- archive (includes: "performance_${agent_conf.hostname}.zip")
- }
- }
- }
- stage('Generate Report') {
- dir("perf_setup_${agent_conf.version}") {
- def zipRootEntry = sh(
- script: "zipinfo -1 performance_${agent_conf.hostname}.zip | head -1",
- returnStdout: true
- ).trim()
- if(zipRootEntry.equalsIgnoreCase('performance/')) {
- unzip(
- zipFile: "performance_${agent_conf.hostname}.zip",
- dir: "performance_${agent_conf.hostname}"
- )
- sh "rm -rf performance_${agent_conf.hostname}.zip"
- dir("performance_${agent_conf.hostname}") {
- sh "mv ${zipRootEntry} performance_${agent_conf.hostname}"
- sh "zip -r ../performance_${agent_conf.hostname}.zip ."
- }
- sh "rm -rf performance_${agent_conf.hostname}"
- }
- }
- def reportGenCmd = """cd xAgent
- python generator_xagent.py \\
- --perf ${env.WORKSPACE}/perf_setup_${agent_conf.version}/performance_${agent_conf.hostname}.zip \\
- --output output_${agent_conf.hostname}"""
- if(config_json.containsKey("report") && config_json.report.containsKey("dashboard")
- && config_json.report.dashboard.type == 'couchdb') {
- reportGenCmd += """ \\
- --couchdb_ip ${config_json.report.dashboard.couchdb_ip} \\
- --couchdb_db ${config_json.report.dashboard.couchdb_db}"""
- }
- utils.runProject("pwrg_${agent_conf.version}", reportGenCmd)
- dir("pwrg_${agent_conf.version}") {
- archive (includes: "output_${agent_conf.hostname}/**")
- }
- }
- allFlowsFailed = false
- }
- catch(ex) {
- allFlowsPassed = false
- throw ex
- }
- }
- }
- }
- if(config_json.hx.deployment.type == "reset") {
- stage('Reset HX') {
- utils.runProject('perf_setup',
- "hx_reset --config_file ${env.WORKSPACE}/hx_reset_config.json --regen_pki"
- )
- sleep 60
- }
- }
- //Configure HX appliance if required.
- if(config_json.hx.config.type != 'none') {
- stage('Configure HX') {
- utils.runProject('perf_setup',
- "hx_configure --config_file ${env.WORKSPACE}/hx_configure_config.json"
- )
- def set_addr = "/bin/bash ${env.JENKINS_HOME}/userContent/policy_scripts/set_default_addr " + config_json.hx.hostname
- def sout = new StringBuilder(), serr = new StringBuilder()
- def proc = set_addr.execute()
- proc.consumeProcessOutput(sout, serr)
- proc.waitForOrKill(20000)
- def rc = proc.exitValue()
- println "out> $sout err> $serr rc> $rc"
- if (rc != 0)
- {
- return(rc)
- }
- }
- }
- stage('Deploy Agents') {
- def agent_deploy = build(job: 'agent_deploy', parameters: [
- [$class: 'StringParameterValue', name: 'BUILD_TAG', value: params.BUILD_TAG],
- [$class: 'StringParameterValue', name: 'HX_RELEASE', value: config_json.hx.release],
- [$class: 'StringParameterValue', name: 'CONFIG', value: JsonOutput.toJson(config_json)],
- new org.jvnet.jenkins.plugins.nodelabelparameter.NodeParameterValue(
- 'EXECUTION_NODE', 'description', config_json.get('execution_node', 'master')
- )
- ], wait: true, propagate: false)
- def deployOut = agent_deploy.getRawBuild().getLogReader().getText()
- echo deployOut
- def numFailed = deployOut.findAll(".*failed=1.*").size() + deployOut.findAll(".*unreachable=1.*").size()
- if(numFailed > 0 && numFailed < config_json.hx.agents.size()) {
- currentBuild.result = 'UNSTABLE'
- }
- else if(agent_deploy.result == 'FAILURE') {
- throw new Exception('Deployment failed for all agents!!!')
- }
- }
- parallel(agentFlows)
- if(!allFlowsPassed && !allFlowsFailed) {
- currentBuild.result = 'UNSTABLE'
- }
- else if(allFlowsFailed) {
- currentBuild.result = 'FAILURE'
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement