Module: Puppet::Jenkins::Plugins

Defined in:
lib/puppet/jenkins/plugins.rb

Class Method Summary collapse

Class Method Details

.availableHash

Returns a Hash containing a mapping of a plugin name to its manifest data.

Returns:

  • (Hash)

    a Hash containing a mapping of a plugin name to its manifest data



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/puppet/jenkins/plugins.rb', line 42

def self.available
  return {} unless exists?
  plugins = {}
  Dir.entries(Puppet::Jenkins.plugins_dir).each do |plugin|
    # Skip useless directories
    next if plugin == '..'
    next if plugin == '.'

    plugin_dir = File.join(Puppet::Jenkins.plugins_dir, plugin)
    # Without an unpacked plugin directory, we can't find a version
    next unless File.directory?(plugin_dir)

    manifest = File.join(plugin_dir, 'META-INF', 'MANIFEST.MF')
    begin
      manifest = manifest_data(File.read(manifest))
      plugins[plugin] = manifest if manifest
    rescue StandardError
      # Nothing really to do about it, failing means no version which will
      # result in a new plugin if needed
    end
  end
  plugins
end

.exists?Boolean

Determine whether or not the jenkins plugin directory exists

Returns:

  • (Boolean)

    T



69
70
71
72
73
74
# File 'lib/puppet/jenkins/plugins.rb', line 69

def self.exists?
  home = Puppet::Jenkins.home_dir
  return false if home.nil?
  return false unless File.directory? Puppet::Jenkins.plugins_dir
  true
end

.manifest_data(manifest_str) ⇒ Hash, NilClass

Return structured data for the given plugin manifest string

Returns:

  • (Hash)

    A hash containing symbolized manifest keys and their string values

  • (NilClass)

    A nil if manifest_str nil or an empty string



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
# File 'lib/puppet/jenkins/plugins.rb', line 11

def self.manifest_data(manifest_str)
  return {} if manifest_str.nil? || manifest_str.empty?

  data = {}
  manifest_str.split("\n").each do |line|
    next if line.empty?
    # Parse out "Plugin-Version: 1.2" for example
    parts = line.split(': ')

    # If the line starts with a space or we can't get at least two parts
    # (key and value), that means it's really just a word-wrap from the
    # previous line, and not a key, skip!
    next if parts.size < 2
    next if parts.first[0] == ' '

    key = parts.first.downcase.tr('-', '_').chomp
    # Skip garbage keys
    next if key.nil? || key.empty?

    # Re-join any colon delimited strings in the value back together,
    # e.g.: "http://wiki.jenkins-ci.org/display/JENKINS/Ant+Plugin"
    value = parts[1..-1].join(':').chomp

    data[key.to_sym] = value
  end

  data
end

.plugins_from_updatecenter(filename) ⇒ Hash

Parse the update-center.json file which Jenkins uses to maintain it’s internal dependency graph for plugins

This document is technically JSONP formatted so we must munge the file a bit to load the JSON bits properly

Returns:

  • (Hash)

    Parsed version of the update center JSON



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
# File 'lib/puppet/jenkins/plugins.rb', line 83

def self.plugins_from_updatecenter(filename)
  parser = nil
  begin
    # Using Kernel#require to make it easier to test this from RSpec
    ::Kernel.require 'json'
    parser = proc { |s| JSON.parse(s) }
  rescue LoadError
    # swallow the exception and embed okjson, see:
    # <https://github.com/jenkinsci/puppet-jenkins/issues/166>
    # <https://github.com/jenkinsci/puppet-jenkins/issues/176>
    ::Kernel.require 'puppet/jenkins/okjson'
    parser = proc { |s| OkJson.decode(s) }
  end

  File.open(filename, 'r') do |fd|
    buffer = fd.read
    return {} if buffer.nil? || buffer.empty?
    buffer = buffer.split("\n")
    # Trim off the first and last lines, which are the JSONP gunk
    buffer = buffer[1...-1]

    data = parser.call(buffer.join("\n"))
    return data['plugins'] || {}
  end
  {}
end