-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
executable file
·199 lines (158 loc) · 7.4 KB
/
setup.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/python -u
import os
import shutil
import jenkins
from argparse import ArgumentParser
from ConfigParser import SafeConfigParser
from distutils.version import LooseVersion
class JenkinsUtility:
def __init__(self, user, passwd, host="http://localhost:8080"):
self.server = jenkins.Jenkins(host, username=user, password=passwd)
self.info = self.server.get_info()
self.url = self.info["primaryView"]["url"]
def run_cmd(self, cmd):
print(cmd)
os.system(cmd)
def download_cli_jar(self):
# http://[server]/jnlpJars/jenkins-cli.jar
self.run_cmd("wget {}jnlpJars/jenkins-cli.jar".format(self.url))
def run_jenkins_cli(self, cmd):
self.download_cli_jar()
self.run_cmd("java -jar jenkins-cli.jar -s {} {}".format(self.url, cmd))
def restart_jenkins(self):
print("Restarting Jenkins.")
self.run_jenkins_cli("safe-restart")
def path_exists(self, path):
if not os.path.exists(path):
os.makedirs(path)
def install_plugin(self, name, version):
plugin = '"{}@{}"'.format(name, version)
url = "{}/pluginManager/installNecessaryPlugins".format(self.url)
request = self.server.Request(url,
data="<jenkins><install plugin={} /></jenkins>".format(plugin),
headers={"Content-Type": "text/xml"})
print("Install {}.".format(plugin))
res = self.server.jenkins_open(request)
def verify_plugins(self, confFile="jenkins.cfg"):
plugins = {}
print "Installed Plugins:"
for p in self.server.get_plugins_info():
print "\t{}: {}".format(p["shortName"], p["version"])
plugins[p["shortName"]] = p["version"]
conf = SafeConfigParser()
conf.read(confFile)
for p in conf.items("Plugins"):
installed = plugins.get(p[0])
# Verify plugin version
if(installed is not None
and LooseVersion(installed) >= LooseVersion(p[1])):
continue
self.install_plugin(*p)
def copy_job(self, jobName):
jobConf = self.server.get_job_config(jobName) # XML to write out
path = "jobs/{}".format(jobName)
self.path_exists(path)
with open("{}/config.xml".format(path), "w") as f:
f.write(jobConf)
print("\tCreated {}/config.xml".format(path))
def copy_view(self, viewName):
viewConf = self.server.get_view_config(viewName) # XML to write out
# No centralized "views" location
self.path_exists("views")
with open("views/{}.xml".format(viewName), "w") as f:
f.write(viewConf)
print("\tCreated views/{}.xml".format(viewName))
def copy_scriptler(self):
""" If security is configured this method requires that the
Anonymouse group has Overall:Read permissions.
"""
if os.path.exists("scriptler"):
shutil.rmtree("scriptler")
cmd = "git clone {}scriptler.git".format(self.url)
self.run_cmd(cmd)
def copy_jenkins(self, jenkinsPath):
print("Copy Jenkins config.xml...")
host = self.url.split("//")[1].split(":")[0]
self.run_cmd("scp {}:{}/config.xml .".format(host, jenkinsPath))
print("Copy Jobs:")
for job in self.info.get("jobs"):
self.copy_job(job.get("name"))
print("Copy Views:")
for view in [v for v in self.info.get("views")
if v.get("name") != "All"]:
self.copy_view(view["name"])
self.copy_scriptler()
def verify_job(self, job, config_xml):
if self.server.job_exists(job):
self.server.reconfig_job(job, config_xml)
else:
self.server.create_job(job, config_xml)
def verify_view(self, view, config_xml):
if self.server.view_exists(view):
self.server.reconfig_view(view, config_xml)
else:
self.server.create_view(view, config_xml)
def update_jobs(self):
print("Updating Jobs:")
for job in os.listdir("jobs"):
with open(os.path.join("jobs", job, "config.xml"), "r") as f:
config_xml = f.read()
print("\tUpdate/create {} job.".format(job))
self.verify_job(job, config_xml)
def update_views(self):
print("Updating Views:")
for view in os.listdir("views"):
with open(os.path.join("views", view), "r") as f:
config_xml = f.read()
view = view.rstrip(".xml")
print("\tUpdate/create {} view.".format(view))
self.verify_view(view, config_xml)
def update_jenkins(self, confFile, jenkinsPath):
try:
version = self.server.get_version()
print("Jenkins ver: {}".format(version))
except jenkins.BadHTTPException as e:
print("Unable to get the Jenkins version.\n\t{}".format(e))
self.verify_plugins(confFile)
self.update_jobs()
self.update_views()
host = self.url.split("//")[1].split(":")[0]
print("Update Scriptler...")
self.run_cmd("scp scriptler/* {}:scriptler".format(host))
self.run_cmd("ssh {} 'sudo mv scriptler/* {}/scriptler/scripts'".format(
host, jenkinsPath))
self.restart_jenkins()
if __name__ == "__main__":
description = ["Utility to backup and update Jenkins.",
"Recommend running 'ssh-copy-id user@[jenkinshost]' before executing",
"this script to eliminate password prompts",
"(for scriptler and jenkins/config.xml).",
"The jenkins/config.xml file can be copied, but not updated",
"because there are no API options for restoring its content."]
parser = ArgumentParser(description=" ".join(description))
parser.add_argument("username", action="store",
help="Jenkins username for authentication.")
parser.add_argument("password", action="store",
help="Password or API Token for authentication.")
parser.add_argument("--host", action="store",
default="http://localhost:8080",
help="Jenkins host, default is 'http://localhost:8080'.")
parser.add_argument("-c", "--copy", action="store_true", default=False,
help="Back up the existing local Jenkins instance.")
parser.add_argument("-u", "--update", action="store_true", default=False,
help="Update the local Jenkins instance from pwd.")
parser.add_argument("-p", "--jenkinsPath", action="store",
default="/var/lib/jenkins",
help="Path to the Jenkins installation, default /var/lib/jenkins.")
parser.add_argument("--config", action="store", default="jenkins.cfg",
help="Location of the config file, default is 'jenkins.cfg'.")
args = parser.parse_args()
ju = JenkinsUtility(args.username, args.password, args.host)
if(args.copy and args.jenkinsPath):
print("Copy existing Jenkins instance at {}.".format(args.host))
ju.copy_jenkins(args.jenkinsPath)
elif(args.update and args.jenkinsPath):
print("Update existing Jenkins instance at {}.".format(args.host))
ju.update_jenkins(args.config, args.jenkinsPath)
else:
parser.print_help()