# -*- coding:utf-8 -*-
import xml.etree.ElementTree as ET
import sys
from jenkinsapi.jenkins import Jenkins
reload(sys)
sys.setdefaultencoding('utf8')
def get_jenkins_server():
JENKINS_URL = "http://127.0.0.1:8080/"
JENKINS_USERNAME = "jenkins"
JENKINS_PASSWORD = "jenkins"
return Jenkins(JENKINS_URL, username=JENKINS_USERNAME, password=JENKINS_PASSWORD, timeout=30)
def get_modified_xml(job_config_xml_tree):
# create builders node
builders_element = ET.Element("builders")
job_config_xml_tree.append(builders_element)
# Move prebuilders to builders
prebuilders_node = job_config_xml_tree.find("prebuilders")
if prebuilders_node is not None and prebuilders_node.getchildren() is not None:
for pre_builder in prebuilders_node.getchildren():
builders_element.append(pre_builder)
job_config_xml_tree.remove(prebuilders_node)
# Create a maven block
maven_node = ET.Element("hudson.tasks.Maven")
builders_element.append(maven_node)
move(job_config_xml_tree.find("goals"), job_config_xml_tree, maven_node, "targets")
maven_name_node = job_config_xml_tree.find("mavenName")
if maven_name_node:
move(maven_name_node, job_config_xml_tree, maven_node)
else:
maven_name_node = ET.Element("mavenName")
maven_name_node.text = "Maven"
maven_node.append(maven_name_node)
move(job_config_xml_tree.find("rootPOM"), job_config_xml_tree, maven_node)
move(job_config_xml_tree.find("mavenOpts"), job_config_xml_tree, maven_node)
move(job_config_xml_tree.find("settings"), job_config_xml_tree, maven_node)
move(job_config_xml_tree.find("globalSettings"), job_config_xml_tree, maven_node)
# items that don't exist in maven step:
remove(job_config_xml_tree, [
'resolveDependencies',
'processPlugins',
'siteArchivingDisabled',
'archivingDisabled',
'mavenValidationLevel',
'disableTriggerDownstreamProjects',
'blockTriggerWhenBuilding',
'fingerprintingDisabled',
'incrementalBuild',
'processPlugins',
'siteArchivingDisabled',
'ignoreUpstremChanges',
'rootModule'
])
# Move postbuilders to builders
postbuilders_node = job_config_xml_tree.find("postbuilders")
if postbuilders_node is not None and postbuilders_node.getchildren() is not None:
for post_builder in postbuilders_node.getchildren():
builders_element.append(post_builder)
job_config_xml_tree.remove(postbuilders_node)
# rename top-level element
# new parent node
new_job_config_xml_tree = ET.Element("project")
for child_node in job_config_xml_tree.getchildren():
new_job_config_xml_tree.append(child_node)
return new_job_config_xml_tree
def move(from_node, from_parent_node, to_node, to_name=None):
if from_node is not None:
if to_name:
tmp_element_node = ET.Element(to_name)
tmp_element_node.text = from_node.text
tmp_element_node.attrib = from_node.attrib
to_node.append(tmp_element_node)
from_parent_node.remove(from_node)
else:
to_node.append(from_node)
from_parent_node.remove(from_node)
def remove(from_node, name_list):
for name in name_list:
tmp_node = from_node.find(name)
if tmp_node is not None:
from_node.remove(tmp_node)
def main():
jenkins_server = get_jenkins_server()
job_list = jenkins_server.get_jobs_list()
for job_name in job_list:
if not job_name.endswith("-DEPRECATED"):
job_obj = jenkins_server.get_job(job_name)
config_text = job_obj.get_config()
config_xml_tree = ET.fromstring(config_text)
if config_xml_tree.tag == "maven2-moduleset":
new_config_xml_tree = get_modified_xml(config_xml_tree)
new_config_xml = ET.tostring(new_config_xml_tree)
jenkins_server.rename_job(job_name, job_name + "-DEPRECATED")
jenkins_server.create_job(job_name, new_config_xml)
print("migrate job from Maven Type to FreeStyle Type: " + job_name)
if __name__ == "__main__":
main()
评论