亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

python 獲取Jenkins job數(shù)據(jù)

系統(tǒng) 2646 0
1.獲取數(shù)據(jù)

python version 2.7
假設(shè)我們要獲取的Jenkins job名字為test_flow,該job觸發(fā)了另外兩個Jenkins job
test1 和test2 job.
我們要獲取test_flow job的運行時間,狀態(tài),number,jobname.

我們把功能性函數(shù)寫在一個文件中 Build class類中,方便主函數(shù)調(diào)用

python-jenkins模塊
build.py
            
              
                import
              
               jenkins

              
                import
              
               ssl

              
                import
              
               re

              
                import
              
               datetime

ssl
              
                .
              
              _create_default_https_context 
              
                =
              
               ssl
              
                .
              
              _create_unverified_context

              
                #build 類主要獲取Jenkins job參數(shù)
              
              
                class
              
              
                Build
              
              
                :
              
              
                def
              
              
                __init__
              
              
                (
              
              self
              
                ,
              
              jobname
              
                ,
              
               url
              
                ,
              
               number
              
                )
              
              
                :
              
              
        self
              
                .
              
              jobname 
              
                =
              
               jobname   
        self
              
                .
              
              number 
              
                =
              
               number
        self
              
                .
              
              jenkins_url 
              
                =
              
               url
        self
              
                .
              
              jenkins_server 
              
                =
              
               jenkins
              
                .
              
              Jenkins
              
                (
              
              self
              
                .
              
              jenkins_url
              
                )
              
              
        self
              
                .
              
              build_console 
              
                =
              
              
                ''
              
              
                def
              
              
                _getConsoleFromJenkins
              
              
                (
              
              self
              
                )
              
              
                :
              
              
        self
              
                .
              
              build_console 
              
                =
              
               self
              
                .
              
              jenkins_server
              
                .
              
              get_build_console_output
              
                (
              
              self
              
                .
              
              jobname
              
                ,
              
               self
              
                .
              
              number
              
                )
              
              
                return
              
               self
              
                .
              
              build_console
    
              
                def
              
              
                get_next_job_build_number
              
              
                (
              
              self
              
                ,
              
               name
              
                )
              
              
                :
              
              
        number 
              
                =
              
              
                0
              
              
        status 
              
                =
              
              
                "Running"
              
              
                if
              
               self
              
                .
              
              build_console 
              
                ==
              
              
                ''
              
              
                :
              
              
            self
              
                .
              
              _getConsoleFromJenkins
              
                (
              
              
                )
              
              
        pattern 
              
                =
              
               re
              
                .
              
              
                compile
              
              
                (
              
              r
              
                'Starting building: '
              
              
                +
              
              
                str
              
              
                (
              
              name
              
                )
              
              
                +
              
              
                ' #(.*)'
              
              
                )
              
              
        match 
              
                =
              
               re
              
                .
              
              search
              
                (
              
              pattern
              
                ,
              
               self
              
                .
              
              build_console
              
                ,
              
              
                0
              
              
                )
              
              
                if
              
               match
              
                :
              
              
            numberstr 
              
                =
              
               match
              
                .
              
              group
              
                (
              
              
                1
              
              
                )
              
              
            number 
              
                =
              
              
                int
              
              
                (
              
              numberstr
              
                )
              
              
            status 
              
                =
              
              
                "Completed"
              
              
                return
              
              
                {
              
              
                "job"
              
              
                :
              
               name
              
                ,
              
              
                "number"
              
              
                :
              
               number
              
                ,
              
              
                "status"
              
              
                :
              
               status
              
                }
              
              
                def
              
              
                _setAttrFromJenkins
              
              
                (
              
              self
              
                )
              
              
                :
              
              
                try
              
              
                :
              
              
                build_info 
              
                =
              
               self
              
                .
              
              jenkins_server
              
                .
              
              get_build_info
              
                (
              
              self
              
                .
              
              jobname
              
                ,
              
               self
              
                .
              
              number
              
                )
              
              
                except
              
              
                :
              
              
                return
              
              
                False
              
              
            self
              
                .
              
              href 
              
                =
              
               build_info
              
                [
              
              
                'url'
              
              
                ]
              
              
            self
              
                .
              
              duration 
              
                =
              
               build_info
              
                [
              
              
                'duration'
              
              
                ]
              
              
            self
              
                .
              
              build_time 
              
                =
              
               build_info
              
                [
              
              
                'timestamp'
              
              
                ]
              
              
                if
              
               build_info
              
                [
              
              
                'building'
              
              
                ]
              
              
                :
              
              
                self
              
                .
              
              status 
              
                =
              
              
                "Running"
              
              
                else
              
              
                :
              
              
                self
              
                .
              
              status 
              
                =
              
               build_info
              
                [
              
              
                'result'
              
              
                ]
              
              
                return
              
              
                True
              
              
                #獲取jenkins job的各種信息然后組裝到字典attr中
              
              
                def
              
              
                attr_to_dict
              
              
                (
              
              self
              
                )
              
              
                :
              
              
        attr 
              
                =
              
              
                dict
              
              
                (
              
              
                )
              
              
                if
              
               self
              
                .
              
              _setAttrFromJenkins
              
                (
              
              
                )
              
              
                ==
              
              
                False
              
              
                :
              
              
                return
              
              
                None
              
              
        attr
              
                [
              
              
                'job'
              
              
                ]
              
              
                =
              
               self
              
                .
              
              jobname
        attr
              
                [
              
              
                'number'
              
              
                ]
              
              
                =
              
               self
              
                .
              
              number
        attr
              
                [
              
              
                'href'
              
              
                ]
              
              
                =
              
               self
              
                .
              
              href
        attr
              
                [
              
              
                'duration'
              
              
                ]
              
              
                =
              
              
                int
              
              
                (
              
              self
              
                .
              
              duration 
              
                /
              
              
                1000
              
              
                )
              
              
        attr
              
                [
              
              
                'status'
              
              
                ]
              
              
                =
              
               self
              
                .
              
              status
        attr
              
                [
              
              
                'timestamp'
              
              
                ]
              
              
                =
              
               self
              
                .
              
              build_time
        
              
                # cet time zone
              
              
        attr
              
                [
              
              
                'build_time'
              
              
                ]
              
              
                =
              
               datetime
              
                .
              
              datetime
              
                .
              
              utcfromtimestamp
              
                (
              
              
                float
              
              
                (
              
              self
              
                .
              
              build_time
              
                )
              
              
                /
              
              
                1000
              
              
                +
              
              
                3600
              
              
                )
              
              
                .
              
              strftime
              
                (
              
              
                '%Y-%m-%d %H:%M:%S'
              
              
                )
              
              
                if
              
               self
              
                .
              
              status 
              
                !=
              
              
                "Running"
              
              
                :
              
              
            attr
              
                [
              
              
                'finish_time'
              
              
                ]
              
              
                =
              
               datetime
              
                .
              
              datetime
              
                .
              
              utcfromtimestamp
              
                (
              
              
                float
              
              
                (
              
              self
              
                .
              
              build_time
              
                )
              
              
                /
              
              
                1000
              
              
                +
              
              
                3600
              
              
                +
              
              
                float
              
              
                (
              
              self
              
                .
              
              duration
              
                )
              
              
                /
              
              
                1000
              
              
                )
              
              
                .
              
              strftime
              
                (
              
              
                '%Y-%m-%d %H:%M:%S'
              
              
                )
              
              
                return
              
               attr

              
                class
              
              
                Job
              
              
                :
              
              
                def
              
              
                __init__
              
              
                (
              
              self
              
                ,
              
              jobname
              
                ,
              
              url
              
                )
              
              
                :
              
              
        self
              
                .
              
              jenkins_job 
              
                =
              
               jobname
        self
              
                .
              
              jenkins_url 
              
                =
              
               url
        self
              
                .
              
              jenkins_server 
              
                =
              
               jenkins
              
                .
              
              Jenkins
              
                (
              
              self
              
                .
              
              jenkins_url
              
                )
              
              
                #獲取最后完成的number
              
              
                def
              
              
                get_last_completed
              
              
                (
              
              self
              
                )
              
              
                :
              
              
        job_info 
              
                =
              
               self
              
                .
              
              jenkins_server
              
                .
              
              get_job_info
              
                (
              
              self
              
                .
              
              jenkins_job
              
                )
              
              
                if
              
               job_info 
              
                is
              
              
                None
              
              
                :
              
              
                print
              
              
                (
              
              
                "yes it is none \n"
              
              
                )
              
              
                return
              
              
                0
              
              
                return
              
              
                int
              
              
                (
              
              job_info
              
                [
              
              
                'lastCompletedBuild'
              
              
                ]
              
              
                [
              
              
                'number'
              
              
                ]
              
              
                )
              
              
                #獲取jenkins job的已經(jīng)運行的number隊列
              
              
                def
              
              
                get_build_list
              
              
                (
              
              self
              
                )
              
              
                :
              
              
        build 
              
                =
              
               self
              
                .
              
              jenkins_server
              
                .
              
              get_job_info
              
                (
              
              self
              
                .
              
              jenkins_job
              
                )
              
              
                if
              
               build 
              
                is
              
              
                None
              
              
                :
              
              
                print
              
              
                (
              
              
                "yes it is none \n"
              
              
                )
              
              
                return
              
               build
              
                [
              
              
                'builds'
              
              
                ]
              
            
          
2.es數(shù)據(jù)庫的創(chuàng)建

獲取的數(shù)據(jù)需要存儲到es數(shù)據(jù)庫中,這里用到的是Elasticsearch6.01版本

es.py
            
              
                from
              
               elasticsearch 
              
                import
              
               Elasticsearch



              
                class
              
              
                Elastic
              
              
                :
              
              
                def
              
              
                __init__
              
              
                (
              
              self
              
                ,
              
               host
              
                ,
              
               index
              
                )
              
              
                :
              
              
        self
              
                .
              
              index 
              
                =
              
               index
        self
              
                .
              
              es 
              
                =
              
               Elasticsearch
              
                (
              
              host
              
                )
              
              
                def
              
              
                exists
              
              
                (
              
              self
              
                )
              
              
                :
              
              
                return
              
               self
              
                .
              
              es
              
                .
              
              indices
              
                .
              
              exists
              
                (
              
              self
              
                .
              
              index
              
                )
              
              
                def
              
              
                create_index
              
              
                (
              
              self
              
                )
              
              
                :
              
              
        build_mapping 
              
                =
              
              
                {
              
              
                'properties'
              
              
                :
              
              
                {
              
              
                'timestamp'
              
              
                :
              
              
                {
              
              
                'type'
              
              
                :
              
              
                'date'
              
              
                }
              
              
                ,
              
              
                }
              
              
                }
              
              
        create_index_body 
              
                =
              
              
                {
              
              
                'setting'
              
              
                :
              
              
                {
              
              
                'number_of_shards'
              
              
                :
              
              
                1
              
              
                ,
              
              
                'number_of_replicas'
              
              
                :
              
              
                0
              
              
                }
              
              
                ,
              
              
                'mappings'
              
              
                :
              
              
                {
              
              
                '_doc'
              
              
                :
              
              
                {
              
              
                'properties'
              
              
                :
              
              
                {
              
              
                'job'
              
              
                :
              
              
                {
              
              
                'type'
              
              
                :
              
              
                'keyword'
              
              
                }
              
              
                ,
              
              
                'timestamp'
              
              
                :
              
              
                {
              
              
                'type'
              
              
                :
              
              
                'date'
              
              
                }
              
              
                ,
              
              
                'status'
              
              
                :
              
              
                {
              
              
                'type'
              
              
                :
              
              
                'keyword'
              
              
                }
              
              
                ,
              
              
                'item'
              
              
                :
              
              
                {
              
              
                'type'
              
              
                :
              
              
                'object'
              
              
                ,
              
              
                'properties'
              
              
                :
              
              
                {
              
              
                'test1'
              
              
                :
              
              build_mapping
              
                ,
              
              
                }
              
              
                }
              
              
                }
              
              
                }
              
              
                }
              
              
                }
              
              
                try
              
              
                :
              
              
           self
              
                .
              
              es
              
                .
              
              indices
              
                .
              
              create
              
                (
              
              index
              
                =
              
              self
              
                .
              
              index
              
                ,
              
              body
              
                =
              
              create_index_body
              
                )
              
              
                except
              
               TransportError 
              
                as
              
               e
              
                :
              
              
                if
              
               e
              
                .
              
              error 
              
                ==
              
              
                'index_already_exists_exception'
              
              
                :
              
              
                pass
              
              
                else
              
              
                :
              
              
                raise
              
              
                def
              
              
                save
              
              
                (
              
              self
              
                ,
              
               body
              
                )
              
              
                :
              
              
                try
              
              
                :
              
              
            self
              
                .
              
              es
              
                .
              
              index
              
                (
              
              self
              
                .
              
              index
              
                ,
              
               doc_type
              
                =
              
              
                '_doc'
              
              
                ,
              
               body
              
                =
              
              body 
              
                )
              
              
                except
              
              
                :
              
              
                print
              
              
                (
              
              
                "save error !!!"
              
              
                )
              
              
                def
              
              
                update
              
              
                (
              
              self
              
                ,
              
               _id
              
                ,
              
               body
              
                )
              
              
                :
              
              
                try
              
              
                :
              
              
            self
              
                .
              
              es
              
                .
              
              index
              
                (
              
              self
              
                .
              
              index
              
                ,
              
               doc_type
              
                =
              
              
                '_doc'
              
              
                ,
              
              
                id
              
              
                =
              
              _id
              
                ,
              
               body
              
                =
              
              body 
              
                )
              
              
                except
              
              
                :
              
              
                print
              
              
                (
              
              
                "update error !!!"
              
              
                )
              
            
          

3.主函數(shù)

getdata.py
            
              
                #!/usr/local/bin/python
              
              
                import
              
               re

              
                import
              
               sys

              
                import
              
               argparse

              
                from
              
               elasticsearch 
              
                import
              
               Elasticsearch

              
                from
              
               module
              
                .
              
              es 
              
                import
              
              
                *
              
              
                from
              
               module
              
                .
              
              build 
              
                import
              
              
                *
              
              

job_name 
              
                =
              
               test_flow
jenkins_url 
              
                =
              
               http
              
                :
              
              
                //
              
              jenkins
              
                .
              
              test
              
                .
              
              com
              
                /
              
              jenkins
job 
              
                =
              
               Job
              
                (
              
              job_name
              
                ,
              
               jenkins_url
              
                )
              
              
doc 
              
                =
              
              
                dict
              
              
                (
              
              
                )
              
              
testjob 
              
                =
              
               Build
              
                (
              
              job_name
              
                ,
              
               jenkins_url
              
                ,
              
               number
              
                )
              
              
                #建立對象
              
              
doc 
              
                =
              
               testjob
              
                .
              
              attr_to_dict
              
                (
              
              
                )
              
              
                #獲取job參數(shù)存到字典中
              
              
es
              
                .
              
              save
              
                (
              
              doc
              
                )
              
              
                #把數(shù)據(jù)存儲到es數(shù)據(jù)庫中
              
            
          

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 夜夜艹日日艹 | 精品日产1区2区 | 在线看一区二区 | 黄色免费看视频 | 国产高清一区二区三区免费视频 | 亚洲国产网 | 99久久九九| 一二三区免费视频 | 免费人成在线观看网站品爱网 | 中国精品久久精品三级 | 在线观看免费精品国产 | 欧美另类69xxxxx 视频 | www.九九| 五月婷婷激情 | 欧美黄色网页 | 欧美一区二区手机在线观看视频 | 久久99热这里只有精品国产 | 亚洲欧美中文字幕高清在线一 | 日韩欧美~中文字幕 | www.成人在线视频 | 九九九九热精品免费视频 | 国产一区二区三区在线观看免费 | 色在线播放 | 麻豆国产精品视频 | 久久天天躁狠狠躁夜夜 | 四虎精品影院4hutv四虎 | 日本a∨在线观看 | 日韩毛片免费 | 国产精品热久久 | 久久精品国产99久久72 | 久久在线播放 | 亚洲欧美日韩高清专区一区 | 黄色www| sese久久| 女人l8毛片a一级毛片免费 | 欧美成人午夜免费完成 | 色综久久天天综合绕视看 | 日本aaaaa级毛片 | 日本黄色录像 | 色偷偷精品视频在线播放 | 国产福利视频在线观看 |