Skip to content

Instantly share code, notes, and snippets.

@martiell
Created November 19, 2018 17:07
Show Gist options
  • Save martiell/dc32085f8b0bcd214e317be14dc771ab to your computer and use it in GitHub Desktop.
Save martiell/dc32085f8b0bcd214e317be14dc771ab to your computer and use it in GitHub Desktop.
Find transitive dependencies of a set of Jenkins plugins using update centre metadata
#!/usr/bin/env python3
# Finds the transitive closure of dependencies for a set of jenkins plugins.
# The jenkins puppet module does not resolve transitive dependencies, so all
# the transitive dependencies need to be listed in the yaml of plugins to
# install.
#
# This script reads JSON from the update centre for the relevant version of
# Jenkins. For Jenkins 2.60, run the following before this script:
# curl -sLO http://updates.jenkins-ci.org/2.60/update-center.actual.json
#
# The output of this script can be copied and pasted into the yaml (but may
# need the indentation adjusted).
from json import load
from yaml import dump
desired = [
'build-monitor-plugin',
'description-setter',
'git',
'htmlpublisher',
'job-dsl',
'ldap',
'matrix-auth',
'promoted-builds',
'slack',
'sonar',
'testng-plugin',
'workflow-aggregator',
]
def collect(plugins, plugin, dependencies=[]):
current = plugins.get(plugin)
if current is None:
print('Unknown ', plugin)
declared = current.get('dependencies')
deps = [d.get('name') for d in declared if not d.get('optional')]
dependencies.append(plugin)
for p in deps:
if p not in dependencies:
collect(plugins, p, dependencies)
def latest_versions(available, required):
return {plugin: available.get(plugin).get('version')
for plugin in required}
def to_yaml(versions):
yaml = {plugin: {"version": version}
for plugin, version in versions.items()}
return dump(yaml, default_flow_style=None)
def main():
with open('update-center.actual.json', 'r') as json:
updates = load(json)
plugins = updates.get('plugins')
collected = []
for plugin in desired:
collect(plugins, plugin, collected)
latest = latest_versions(plugins, collected)
print(to_yaml(latest))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment