Created
June 26, 2019 12:24
-
-
Save rplevka/945ddc0fcbbdad6734c6ec43e6edcbfe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import json | |
import os | |
import re | |
import requests | |
from shutil import copyfile | |
from tempfile import mkdtemp | |
update_center_url = 'https://updates.jenkins.io/update-center.json' | |
with open('plugins.txt', 'r') as f: | |
print('fetching the update-center.json') | |
ucjson = requests.get(update_center_url) | |
ucjson.raise_for_status() | |
js = json.loads(re.search('updateCenter.post\((.*)\);', ucjson.text, re.S).group(1)) | |
if js.get('plugins') is None: | |
raise ValueError('The returned json does not contain the \'plugins\' key') | |
tempdir = mkdtemp(prefix='plugins_') | |
def install_plugins(plugin_list): | |
plugin = None | |
for i in plugin_list: | |
if isinstance(i, str): | |
plugin = i.strip() | |
if isinstance(i, dict): | |
plugin = i['name'] | |
try: | |
url = js['plugins'][plugin]['url'] | |
ver = js['plugins'][plugin]['version'] | |
dep = [i for i in js['plugins'][plugin].get('dependencies') if i['optional'] == False] | |
except(KeyError): | |
print('WARNING: {0} not found in UpdateCenter'.format(plugin)) | |
continue | |
if os.path.isfile('/var/jenkins_home/plugins/{0}.hpi'.format(plugin)): | |
print('{0} already installed, skipping'.format(plugin)) | |
continue | |
else: | |
print('Installing: {0}'.format(plugin)) | |
p = requests.get(url) | |
p.raise_for_status() | |
pfn = '{0}/{1}.hpi'.format(tempdir, plugin) | |
with open(pfn, 'wb') as pf: | |
pf.write(p.content) | |
copyfile(pfn, '/var/jenkins_home/plugins/{0}.hpi'.format(plugin)) | |
if len(dep) > 0: | |
print('installing dependencies: {0}'.format(dep)) | |
install_plugins(plugin_list=dep) | |
install_plugins(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment