jenkins-pipeline实践 - tempest

properties([
    buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '60', numToKeepStr: '')),
    parameters([
        string(defaultValue: '', description: 'auth url', name: 'auth_url'),
        password(defaultValue: '', description: 'admin password', name: 'password'),
        string(defaultValue: 'identity image volume network compute', description: 'services', name: 'services'),
        string(defaultValue: '', description: 'testcase label.eg: smoke', name: 'label'),
    ]),
])
 
 
node('tempest') { timestamps{
    println(params)
    auth_url = params.get('auth_url')
    project_domain_name = 'Default'
    user_domain_name = 'Default'
    project_name = 'admin'
    username = 'admin'
    password = params.get('password')
 
    services = params.get('services').split()
    label = params.get('label')
 
    emailto = [
    ].join(',')
    date_s = sh(returnStdout: true, script: "date +%s").trim()
   
    tempest_version = '10'
    venv = "tempest-${tempest_version}"
    host = "${auth_url.split('://')[1].split('/')[0].split(':')[0]}"
    workspace = "${venv}-${host}"
   
    resultmap = [:]
   
    try {
        stage('prepare') {
            sh """
                yum install epel-release -y
                yum install python-pip -y
                yum install python-devel -y
                yum install gcc -y
                yum install jq -y
                pip install tox
            """
            dir(env.WORKSPACE) {
                withEnv(["venv=${venv}", "tempest_version=${tempest_version}"]) { sh '''
                    virtualenv --no-site-packages ${venv}
                    source ${venv}/bin/activate
   
                    pip install tempest==${tempest_version}  # Todo: 1 install from git; 2 plugins
                    pip install python-openstackclient
                    pip install junitxml
   
                    python_lib_dir=`python -c \'from distutils.sysconfig import get_python_lib; print(get_python_lib())\'`
                    cp -fu $python_lib_dir/tempest/lib/common/rest_client.py{,.bak}
                    cp -fu $python_lib_dir/tempest/cmd/cleanup_service.py{,.bak}
                    sed -i \'/def validate_response(.*):$/ a\\        return\' $python_lib_dir/tempest/lib/common/rest_client.py
                    sed  -i \'/def _get_network_id/,/return/s/if (net\\[\'\\\'\'\\([a-z_]*\\)\'\\\'\'\\] == \\([a-z_]*\\) and net\\[\'\\\'\'name\'\\\'\'\\] == net_name):/if (net.get(\'\\\'\'\\1\'\\\'\') == \\2 and net\\[\'\\\'\'name\'\\\'\'\\] == net_name):/\' $python_lib_dir/tempest/cmd/cleanup_service.py
                '''}
            }
        }
   
        withEnv(["PATH=${env.WORKSPACE}/${venv}/bin:${env.PATH}"]) {
            stage('init') { dir(env.WORKSPACE) {
                sh """
                rm -rf ${workspace}/{*,.[!.]*}
                mkdir -p ${workspace} && cd ${workspace} && tempest init
                mkdir -p output/${date_s}
                """
            }}
   
            stage('config') {
                def osc = "openstack --os-auth-url ${auth_url} --os-identity-api-version 3 --os-project-domain-name ${project_domain_name} --os-user-domain-name ${user_domain_name} --os-project-name ${project_name} --os-username ${username} --os-password ${password}"
 
                def public_network_name = 'provider'
                def public_network_id = sh(returnStdout: true, script: "${osc} network list -f json | jq '.[] | select(.Name==\"${public_network_name}\") | .ID' | head -n 1 | tr -d '\"'").trim()
 
                def fixed_network_name = 'tempest_tenant_net'
                def fixed_network_id = sh(returnStdout: true, script: "${osc} network list -f json | jq '.[] | select(.Name==\"${fixed_network_name}\") | .ID' | head -n 1 | tr -d '\"'").trim()
                if(! fixed_network_id) {
                    sh "${osc} network create --share --enable ${fixed_network_name}"
                    fixed_network_id = sh(returnStdout: true, script:"${osc} subnet create --network ${fixed_network_name} --dns-nameserver 8.8.4.4 --gateway 172.16.1.1 --subnet-range 172.16.1.0/24 ${fixed_network_name}_subnet -f value -c id").trim()
                }
 
                def public_router_name = 'tempest_router'
                def public_router_id = sh(returnStdout: true, script: "${osc} router list -f json | jq '.[] | select(.Name==\"${public_router_name}\") | .ID' | head -n 1 | tr -d '\"'").trim()
                if(! public_router_id) {
                    public_router_id = sh(returnStdout: true, script: "${osc} router create ${public_router_name} -f value -c id").trim()
                    sh "${osc} router set --external-gateway provider ${public_router_name}"
                }

                def image_id = sh(returnStdout: true, script: "${osc} image list -f json | jq '.[] | select(.Name==\"cirros\") | .ID' | head -n 1 | tr -d '\"'").trim()
                if(! image_id) {
                    image_id = sh(returnStdout: true, script: "${osc} image create cirros --file /opt/tempest/cirros-*.img --disk-format qcow2 --container-format bare --public -f value -c id").trim()
                }
   
                sh """
                cat < entry.value == false}) {
                currentBuild.result = 'FAILURE'
                echo currentBuild.result
            }

            if(fileExists("${date_s}/result.html")){
                emailbody = "\${FILE, path=\"${date_s}/result.html\"}"
            }else{
                emailbody = '${SCRIPT, template="groovy-html.template"}'
            }

            emailext to: emailto, replyTo: '$DEFAULT_REPLYTO', subject: "\$DEFAULT_SUBJECT - ${host}-${label?:'full'}", body: emailbody, mimeType: 'text/html', attachmentsPattern: "${date_s}/*.html", attachLog: false
        }
   
        withEnv(["PATH=${env.WORKSPACE}/${venv}/bin:${env.PATH}"]) { stage('cleanup') {
            dir("${workspace}") {
                if(sh(returnStatus: true, script: "tempest cleanup")){echo 'cleanup fail'}
            }
            // cleanWs()
        }}
    }
    echo "END job."
}}

你可能感兴趣的:(jenkins-pipeline实践 - tempest)