我编写了以下python脚本来获取每个Jenkins作业使用的Jenkins插件列表。
#!/usr/bin/python
import os
mypath = '/work/jenkins/jobs' for root, subFolders,
files in os.walk(mypath): if 'config.xml' in files:
with open(os.path.join(root, 'config.xml'), 'r') as fin:
for lines in fin:
if 'plugin=' in lines:
before_keyword,keyword,after_keyword = lines.partition('plugin=')
print root,after_keyword
它为.eg生成以下输出:
/work/jenkins/jobs/job1 "subversion@2.2">
/work/jenkins/jobs/job1 "ant@1.2">
/work/jenkins/jobs/job1 "ant@1.2">
/work/jenkins/jobs/job2 "maven-plugin@2.1">
/work/jenkins/jobs/job2 "subversion@2.2">
更新:我注意到我可以从/work/jenkins/config.xml获取一些系统插件,但列表不完整。
感谢任何帮助。 谢谢
答案 0 :(得分:4)
以下是满足要求1,2,3,4和5的代码; 我无法完成要求2,因为我不知道该怎么做我想我做了正确的要求2,不是吗?:
#!/usr/bin/env python2
# coding: utf-8
# Veni Sancte Spiritus
# This code is under public domain.
import os
import argparse
import logging
import re
import openpyxl
logging.basicConfig(
format="[%(levelname)s]: %(message)s", level=logging.INFO)
try:
import openpyxl
except ImportError:
logging.error("Install openpyxl; pip install openpyxl")
exit(1)
parser = argparse.ArgumentParser(description="get plugins used by Jenkins job")
parser.add_argument("--excel", metavar="dir/output1.xslx",
help="Output to excel file")
parser.add_argument("--jenkinshome", metavar="/var/lib/jenkins",
help="Jenkins home location (default: {})".format(
os.getcwd()), default=os.getcwd())
arguments = parser.parse_args()
alreadyadded = []
pluginsalreadyadded = {}
if arguments.excel:
workbook = openpyxl.Workbook(optimized_write=True)
worksheet1 = workbook.create_sheet(0, "Output")
worksheet1.append(["Job name", "Jenkins-CI plugin"])
worksheet2 = workbook.create_sheet(1, "Output grouping jobs by plugin")
worksheet2.append(["Jobs", "Jenkins-CI plugin"])
worksheet3 = workbook.create_sheet(2, "Plugins installed on Jenkins")
worksheet3.append(["Plugin name", "Plugin version"])
for root, subFolders, files in os.walk(os.path.join(
arguments.jenkinshome, "jobs")):
if 'config.xml' in files:
with open(os.path.join(root, 'config.xml'), 'r') as fin:
for lines in fin:
if 'plugin=' in lines:
before_keyword, keyword, after_keyword = lines.partition(
'plugin=')
jobname = os.path.basename(root)
plugin = re.sub(r'"(.+)@([0-9.]+)"/?>\n?',
r'\1 ver. \2', after_keyword)
if [jobname, plugin] not in alreadyadded:
logging.info("{}: {}".format(jobname, plugin))
if arguments.excel:
worksheet1.append([jobname, plugin])
if plugin not in pluginsalreadyadded:
pluginsalreadyadded[plugin] = []
pluginsalreadyadded[plugin].append(jobname)
alreadyadded.append([jobname, plugin])
for root, subFolder, files in os.walk(os.path.join(
arguments.jenkinshome, "plugins")):
if "MANIFEST.MF" in files:
with open(os.path.join(root, "MANIFEST.MF")) as fon:
name = None
version = None
for line in fon:
if "Extension-Name: " in line:
beforekyw, kyw, name = line.partition("Extension-Name: ")
# clean the string
name = re.search(r'(\w+)', name).group(0)
elif "Plugin-Version: " in line:
beforekyw, kyw, version = line.partition(
"Plugin-Version: ")
version = re.sub(r'\n', '', version)
# clean the string
version = re.search(r'(.+)', version).group(0)
logging.info("{} ver. {}".format(name, version))
if arguments.excel:
worksheet3.append([name, version])
if arguments.excel:
filedest = os.path.expanduser(arguments.excel)
filedest = os.path.abspath(filedest)
path = os.path.dirname(filedest)
if not os.path.isdir(path):
os.makedirs(path)
# write output for worksheet2
for plugin, jobs in pluginsalreadyadded.iteritems():
worksheet2.append([" | ".join(jobs), plugin])
# save the file
workbook.save(filedest)
这里是输出:
<(jorge@abril)---(mar may 13 14:30:25)>
[recetario][59] $ ./jenkins-plugins.py --jenkinshome /var/lib/jenkins --excel hoja.xslx
[INFO]: Robert S. - Music metadata from Amazon (Windows build): mercurial ver. 1.50
[INFO]: Robert S. - Music metadata from Amazon (Windows build): jclouds-jenkins ver. 2.5.1
[INFO]: Robert S. - Music metadata from Amazon (Windows build): gcm-notification ver. 1.0
[INFO]: Robert S. - Music metadata from Amazon (Windows build): instant-messaging ver. 1.28
[INFO]: Robert S. - playcomscrap: mercurial ver. 1.50
[INFO]: Robert S. - playcomscrap: shiningpanda ver. 0.20
[INFO]: Robert S. - playcomscrap: gcm-notification ver. 1.0
[INFO]: Robert S. - playcomscrap: instant-messaging ver. 1.28
[INFO]: Robert S. - 419742: mercurial ver. 1.49
[INFO]: Robert S. - Scrap Game Product Information (Windows build): mercurial ver. 1.50
[INFO]: playcomscrap - construye instalable: mercurial ver. 1.50
[INFO]: playcomscrap - construye instalable: gcm-notification ver. 1.0
[INFO]: playcomscrap - construye instalable: instant-messaging ver. 1.28
[INFO]: Robert S. - Scrap Game Product Information (Unit tests): mercurial ver. 1.50
[INFO]: Robert S. - Scrap Game Product Information (Unit tests): shiningpanda ver. 0.20
[INFO]: Danny L. - FLICK 2: mercurial ver. 1.50
[INFO]: jabber ver. 1.25
[INFO]: emotional ver. 1.1
[INFO]: cvs ver. 2.11
[INFO]: mailer ver. 1.8
[INFO]: jclouds ver. 2.5.1
[INFO]: matrix ver. 1.2
[INFO]: python ver. 1.2
[INFO]: compact ver. 1.10
[INFO]: pam ver. 1.1
[INFO]: ldap ver. 1.9
[INFO]: xvfb ver. 1.0.10
[INFO]: mailcommander ver. 1.0.0
[INFO]: ant ver. 1.2
[INFO]: ssh ver. 1.6.1
[INFO]: ssh ver. 1.6
[INFO]: credentials ver. 1.10
[INFO]: jira ver. 1.39
[INFO]: gcm ver. 1.0
[INFO]: libvirt ver. 1.8.4
[INFO]: scm ver. 0.2
[INFO]: subversion ver. 2.3
[INFO]: external ver. 1.2
[INFO]: antisamy ver. 1.1
[INFO]: shiningpanda ver. 0.20
[INFO]: publish ver. 1.11
[INFO]: windows ver. 1.0
[INFO]: maven ver. 2.3
[INFO]: instant ver. 1.28
[INFO]: mercurial ver. 1.50
[INFO]: xvnc ver. 1.14
[INFO]: javadoc ver. 1.1
[INFO]: translation ver. 1.11
[INFO]: matrix ver. 1.2
[INFO]: xunit ver. 1.88
[INFO]: postbuild ver. 1.8
[INFO]: jquery ver. 1.7.2-1
<(jorge@abril)---(mar may 13 14:30:27)>
[recetario][60] $
这就是excel文件的样子:
是的,第二个输出看起来很难看,因为我无法根据需要制作多行的单元格值,因为在打开文件时,最后一个作业名称是单元格的值,因此,我用|
分隔每个职位名称。
使用--help
命令......好吧,你应该知道那个命令做了什么:)。 您需要安装openpyxl
,因为此模块需要将输出写入Excel文件。