-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkins_config.py
executable file
·174 lines (160 loc) · 6.24 KB
/
jenkins_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/python3
##Execute this script with -h to see its options, ./jenkins_config.py --help
from os import name
from typing import Text
import xml.etree.ElementTree as ET
import jenkins
import argparse
import sys
## Help menu:
parser=argparse.ArgumentParser()
parser.add_argument('--pass',metavar='JENKINS_PASS',required=True, help='Jenkins Password!')
parser.add_argument('--server',metavar='JENKINS_URL',required=True, help='Jenkins URL(http://jenkins.example.com)')
parser.add_argument('--user',metavar='JENKINS_USER', default='root', help='Jenkins Username(defualt: root)')
parser.add_argument('--list',action='store_true', help='List all jenkins jobs!')
parser.add_argument('--get',metavar='scripts|configs|JOB_NAME',help='Print complete xml config of a job or store xml config or script section of all jobs on disk, configs are store in "./job_configs/" and scripts are store in "./"')
parser.add_argument('--restore',metavar='JOB_NAME|all',help='Restore config of job(s) from "./job_configs/JOB_NAME.xml"')
parser.add_argument('--set',metavar='JOB_NAME|all',help='Set using of GitSCM config instead of script for job(s), --git-url, --git-cred-name and --git-branch are requaired,example: set JOB_NAME')
parser.add_argument('--git-url',metavar='URL',help='Url of git project to set for jenkins files')
parser.add_argument('--git-cred-name',metavar='CRED_NAME',default='git_cred',help='Name of Jenkins credential created before for connection to git')
parser.add_argument('--git-branch',metavar='BRANCH_NAME',default='master',help='Git branch for GitSCM Jenkinsfile')
args=parser.parse_args()
args_vars=vars(parser.parse_args())
if not args.get and not args.set and not args.list:
print("Use an argument: --list|--get|--set")
sys.exit()
if args.set and not args.git_url:
print("To set git repo for script(s) you need to pass --git-url git@git.example.com:example/example.git")
sys.exit()
elif args.set and args.git_url:
git_url=args_vars['git_url']
git_cred_name=args_vars['git_cred_name']
git_branch=args_vars['git_branch']
scm_config_temp ="""
<definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="workflow-cps@2.53">
<scm class="hudson.plugins.git.GitSCM" plugin="git@3.9.0">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>"""+git_url+"""</url>
<credentialsId>"""+git_cred_name+"""</credentialsId>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>"""+git_branch+"""</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<submoduleCfg class="list"/>
<extensions/>
</scm>
<scriptPath>morteza</scriptPath>
<lightweight>true</lightweight>
</definition>"""
## Functions:
username=args_vars['user']
server = jenkins.Jenkins(args_vars['server'],username , password=args_vars['pass'])
try:
jobs = server.get_jobs()
except Exception as e:
print(str(e))
sys.exit()
def get_config(action):
for job in jobs:
job_name= job.get('name')
config = server.get_job_config(job_name)
root = ET.fromstring(config)
xml_str = ET.tostring(root, encoding='unicode', method='xml')
if action == 'configs':
try:
f = open("./job_configs/" + job_name + ".xml", "w")
f.write(xml_str)
f.close()
print('Config for job "' + job_name +'" stored in' + "./job_configs/" + job_name + ".xml")
except:
print("Please create './job_configs' directory")
break
elif action == 'scripts':
counter1 = 0
counter2 = 0
for child in root:
if child.tag == "definition":
while True:
try:
if root[counter1][counter2].tag == 'script':
script = root[counter1][counter2].text
f = open(job_name, "w")
f.write(script)
f.close()
print('script for job "' + job_name +'" stored in' + "./" + job_name)
break
counter2 += 1
except Exception as e:
print('Could not found Script for job "' + job_name +'", error: ' + str(e))
break
break
counter1 += 1
elif action == job_name:
print(xml_str)
break
def list_jobs():
counter = 0
for job in jobs:
counter+=1
print(str(counter)+": "+ job.get('name'))
def set(job_name):
config = server.get_job_config(job_name)
root = ET.fromstring(config)
counter = 0
for child in root:
counter += 1
if child.tag == 'definition':
root.remove(root[counter])
break
test = ET.fromstring(scm_config_temp)
test.find("scriptPath").text = job_name
root.insert(counter,test)
xml_str = ET.tostring(root, encoding='unicode', method='xml')
server.reconfig_job(job_name,xml_str)
print('Job ' + job_name + ' reconfigured!')
print("Git url: "+ giturl)
def set_config(job_name):
if job_name == 'all':
for job in jobs:
try:
job_name= job.get('name')
set(job_name)
except Exception as e:
print('Could not config already configured job "' + job_name +'"!, error: ' + str(e))
else:
set(job_name)
def restore(job_name):
f = open("./job_configs/" + job_name + ".xml", "r")
tree = ET.parse(f)
root = tree.getroot()
xml_str = ET.tostring(root, encoding='unicode', method='xml')
server.reconfig_job(job_name,xml_str)
print('Job ' + job_name + ' Restored!')
f.close()
def restore_config(job_name):
if job_name == 'all':
for job in jobs:
try:
job_name= job.get('name')
restore(job_name)
except Exception as e:
print('Could not restore job "' + job_name +'"!, error: ' + str(e))
else:
restore(job_name)
if args.get:
action = args_vars['get']
get_config(action)
if args.list:
list_jobs()
if args.set:
job_name = args_vars['set']
set_config(job_name)
if args.restore:
job_name = args_vars['restore']
set_config(job_name)