Skip to content

Commit

Permalink
16173 - Plugin packager should get dependency versions from ddl
Browse files Browse the repository at this point in the history
In the past the plugin packager only allowed mcollective version
dependencies to be set with cli arguments.

The plugin packager now uses mcollective version dependencies
specified in the plugin's ddl. Plugin version and mcollective
type (ie pe-mcollective) can now be specified on the cli.

Example: mco plugin package --mcname="pe-mcollective" --mcversion="2"

The mcserver, mcclient and mccommon flags have been removed from
the plugin application.
  • Loading branch information
ploubser authored and ripienaar committed Sep 4, 2012
1 parent 5f33c73 commit 49ad89c
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 61 deletions.
4 changes: 3 additions & 1 deletion lib/mcollective/pluginpackager.rb
Expand Up @@ -16,13 +16,15 @@ def self.[](klass)
# Fetch and return metadata from plugin DDL
def self.get_metadata(path, type)
ddl = DDL.new("package", type.to_sym, false)

begin
ddl_file = File.read(Dir.glob(File.join(path, type, "*.ddl")).first)
rescue Exception
raise "failed to load ddl file in plugin directory : #{File.join(path, type)}"
end
ddl.instance_eval ddl_file
ddl.meta

return ddl.meta, ddl.requirements[:mcollective]
end

# Checks if a directory is present and not empty
Expand Down
23 changes: 12 additions & 11 deletions lib/mcollective/pluginpackager/agent_definition.rb
Expand Up @@ -3,22 +3,23 @@ module PluginPackager
# MCollective Agent Plugin package
class AgentDefinition
attr_accessor :path, :packagedata, :metadata, :target_path, :vendor, :iteration, :preinstall
attr_accessor :plugintype, :dependencies, :postinstall, :mcserver, :mcclient, :mccommon
attr_accessor :plugintype, :dependencies, :postinstall, :mcname, :mcversion

def initialize(path, name, vendor, preinstall, postinstall, iteration, dependencies, mcodependency, plugintype)
def initialize(path, name, vendor, preinstall, postinstall, iteration, dependencies, mcdependency, plugintype)
@plugintype = plugintype
@path = path
@packagedata = {}
@iteration = iteration || 1
@preinstall = preinstall
@postinstall = postinstall
@vendor = vendor || "Puppet Labs"
@mcserver = mcodependency[:server] || "mcollective"
@mcclient = mcodependency[:client] || "mcollective-client"
@mccommon = mcodependency[:common] || "mcollective-common"
@dependencies = dependencies || []
@target_path = File.expand_path(@path)
@metadata = PluginPackager.get_metadata(@path, "agent")
@metadata, mcversion = PluginPackager.get_metadata(@path, "agent")
@mcname = mcdependency[:mcname] || "mcollective"
@mcversion = mcdependency[:mcversion] || mcversion
@dependencies << {:name => "#{@mcname}-common", :version => @mcversion}

@metadata[:name] = (name || @metadata[:name]).downcase.gsub(" ", "-")
identify_packages
end
Expand All @@ -36,7 +37,7 @@ def identify_packages
# Obtain Agent package files and dependencies.
def agent
agent = {:files => [],
:dependencies => @dependencies.clone << @mcserver,
:dependencies => @dependencies.clone,
:description => "Agent plugin for #{@metadata[:name]}"}

agentdir = File.join(@path, "agent")
Expand All @@ -47,14 +48,14 @@ def agent
else
return nil
end
agent[:dependencies] << ["mcollective-#{@metadata[:name]}-common", @metadata[:version]]
agent[:dependencies] << {:name => "#{@mcname}-#{@metadata[:name]}-common", :version => @metadata[:version]}
agent
end

# Obtain client package files and dependencies.
def client
client = {:files => [],
:dependencies => @dependencies.clone << @mcclient,
:dependencies => @dependencies.clone,
:description => "Client plugin for #{@metadata[:name]}"}

clientdir = File.join(@path, "application")
Expand All @@ -64,14 +65,14 @@ def client
client[:files] += Dir.glob(File.join(clientdir, "*")) if PluginPackager.check_dir_present clientdir
client[:files] += Dir.glob(File.join(bindir,"*")) if PluginPackager.check_dir_present bindir
client[:files] += Dir.glob(File.join(aggregatedir, "*")) if PluginPackager.check_dir_present aggregatedir
client[:dependencies] << ["mcollective-#{@metadata[:name]}-common", @metadata[:version]]
client[:dependencies] << {:name => "#{@mcname}-#{@metadata[:name]}-common", :version => @metadata[:version]}
client[:files].empty? ? nil : client
end

# Obtain common package files and dependencies.
def common
common = {:files =>[],
:dependencies => @dependencies.clone << @mccommon,
:dependencies => @dependencies.clone,
:description => "Common libraries for #{@metadata[:name]}"}

datadir = File.join(@path, "data", "**")
Expand Down
20 changes: 11 additions & 9 deletions lib/mcollective/pluginpackager/standard_definition.rb
Expand Up @@ -2,10 +2,9 @@ module MCollective
module PluginPackager
class StandardDefinition
attr_accessor :path, :packagedata, :metadata, :target_path, :vendor, :iteration
attr_accessor :plugintype, :preinstall, :postinstall, :dependencies, :mcserver
attr_accessor :mccommon
attr_accessor :plugintype, :preinstall, :postinstall, :dependencies, :mcname, :mcversion

def initialize(path, name, vendor, preinstall, postinstall, iteration, dependencies, mcodependency, plugintype)
def initialize(path, name, vendor, preinstall, postinstall, iteration, dependencies, mcdependency, plugintype)
@plugintype = plugintype
@path = path
@packagedata = {}
Expand All @@ -14,10 +13,12 @@ def initialize(path, name, vendor, preinstall, postinstall, iteration, dependenc
@postinstall = postinstall
@vendor = vendor || "Puppet Labs"
@dependencies = dependencies || []
@mcserver = mcodependency[:server] || "mcollective"
@mccommon = mcodependency[:common] || "mcollective-common"
@target_path = File.expand_path(@path)
@metadata = PluginPackager.get_metadata(@path, @plugintype)
@metadata, mcversion = PluginPackager.get_metadata(@path, @plugintype)

@mcname = mcdependency[:mcname] || "mcollective"
@mcversion = mcdependency[:mcversion] || mcversion
@dependencies << {:name => "#{mcname}-common", :version => @mcversion}
@metadata[:name] = (name || @metadata[:name]).downcase.gsub(" ", "-")
identify_packages
end
Expand All @@ -33,7 +34,7 @@ def identify_packages
# Obtain standard plugin files and dependencies
def plugin
plugindata = {:files => [],
:dependencies => @dependencies.clone << @mcserver,
:dependencies => @dependencies.clone,
:description => "#{@name} #{@plugintype} plugin for the Marionette Collective."}

plugindir = File.join(@path, @plugintype.to_s)
Expand All @@ -43,14 +44,15 @@ def plugin
return nil
end

plugindata[:dependencies] <<["mcollective-#{@metadata[:name]}-common", @metadata[:version]] if @packagedata[:common]
plugindata[:dependencies] << {:name => "#{@mcname}-#{@metadata[:name]}-common",
:version => @metadata[:version]} if @packagedata[:common]
plugindata
end

# Obtain list of common files
def common
common = {:files => [],
:dependencies => @dependencies.clone << @mccommon,
:dependencies => @dependencies.clone,
:description => "Common libraries for #{@name} connector plugin"}

commondir = File.join(@path, "util")
Expand Down
24 changes: 9 additions & 15 deletions plugins/mcollective/application/plugin.rb
Expand Up @@ -47,21 +47,16 @@ class Application::Plugin<Application
:arguments => ["--pluginpath PATH"],
:type => String

option :mccommon,
:description => "Set the mcollective common package that the plugin depends on",
:arguments => ["--mc-common-pkg PACKAGE"],
option :mcname,
:description => "MCollective type (mcollective, pe-mcollective) that the packages depend on",
:arguments => ["--mcname NAME"],
:type => String

option :mcserver,
:description => "Set the mcollective server package that the plugin depends on",
:arguments => ["--mc-server-pkg PACKAGE"],
option :mcversion,
:description => "Version of MCollective that the packages depend on",
:arguments => "--mcversion MCVERSION",
:type => String

option :mcclient,
:description => "Set the mcollective client package that the plugin depends on",
:arguments => ["--mc-client-pkg PACKAGE"],
:type =>String

option :dependency,
:description => "Adds a dependency to the plugin",
:arguments => ["--dependency DEPENDENCIES"],
Expand Down Expand Up @@ -273,14 +268,13 @@ def prepare_plugin
PluginPackager.load_packagers
plugin_class = PluginPackager[configuration[:plugintype]]
configuration[:dependency] = configuration[:dependency][0].split(" ") if configuration[:dependency] && configuration[:dependency].size == 1
mcodependency = {:server => configuration[:mcserver],
:client => configuration[:mcclient],
:common => configuration[:mccommon]}
configuration[:dependency].map!{|dep| {:name => dep, :version => nil}} if configuration[:dependency]
mcdependency = {:mcname => configuration[:mcname], :mcversion => configuration[:mcversion]}

plugin_class.new(configuration[:target], configuration[:pluginname],
configuration[:vendor], configuration[:preinstall],
configuration[:postinstall], configuration[:iteration],
configuration[:dependency], mcodependency , plugintype)
configuration[:dependency], mcdependency , plugintype)
end

def directory_for_type(type)
Expand Down
4 changes: 2 additions & 2 deletions plugins/mcollective/pluginpackager/debpackage_packager.rb
Expand Up @@ -25,8 +25,8 @@ def create_packages
@tmpdir = Dir.mktmpdir("mcollective_packager")
@current_package_type = type
@current_package_data = data
@current_package_shortname = "mcollective-#{@plugin.metadata[:name]}-#{@current_package_type}"
@current_package_fullname = "mcollective-#{@plugin.metadata[:name]}-#{@current_package_type}" +
@current_package_shortname = "#{@plugin.mcname}-#{@plugin.metadata[:name]}-#{@current_package_type}"
@current_package_fullname = "#{@plugin.mcname}-#{@plugin.metadata[:name]}-#{@current_package_type}" +
"_#{@plugin.metadata[:version]}-#{@plugin.iteration}"

@build_dir = File.join(@tmpdir, "#{@current_package_shortname}_#{@plugin.metadata[:version]}")
Expand Down
4 changes: 2 additions & 2 deletions plugins/mcollective/pluginpackager/rpmpackage_packager.rb
Expand Up @@ -15,7 +15,7 @@ def initialize(plugin, pluginpath = nil, signature = nil, verbose = false)
else
raise RuntimeError, "creating rpms require 'rpmbuild' or 'rpmbuild-md5' to be installed"
end

@plugin = plugin
@verbose = verbose
@libdir = pluginpath || "/usr/libexec/mcollective/mcollective/"
Expand All @@ -29,7 +29,7 @@ def create_packages
begin
@current_package_type = type
@current_package_data = data
@current_package_name = "mcollective-#{@plugin.metadata[:name]}-#{@current_package_type}"
@current_package_name = "#{@plugin.mcname}-#{@plugin.metadata[:name]}-#{@current_package_type}"
@tmpdir = Dir.mktmpdir("mcollective_packager")
prepare_tmpdirs data
create_package type, data
Expand Down
Expand Up @@ -7,6 +7,6 @@ Standards-Version: 3.8.0

Package: <%= @current_package_shortname %>
Architecture: all
<% @current_package_data[:dependencies].map!{|dep| dep.is_a?(Array) ? "#{dep[0]} (= #{dep[1]}-#{@plugin.iteration})" : dep} -%>
<% @current_package_data[:dependencies].map!{|dep| (dep[:version]) ? "#{dep[:name]}(>= #{dep[:version]})" : dep[:name]}-%>
Depends: <%= @current_package_data[:dependencies].join(", ")%>
Description: <%= @plugin.metadata[:description] %>
Expand Up @@ -11,7 +11,7 @@ BuildArch: noarch
Group: System Tools
Source0: <%= "#{@current_package_name}-#{@plugin.metadata[:version]}.tgz" %>
<% @current_package_data[:dependencies].each do |dep|-%>
Requires: <%= dep.is_a?(Array) ? "%s = %s" % [dep[0], dep[1]] : dep%>
Requires: <%= dep[:name] -%> <%= ">= #{dep[:version]}" if dep[:version]%>
<% end -%>

%description
Expand Down
21 changes: 11 additions & 10 deletions spec/unit/pluginpackager/agent_definition_spec.rb
Expand Up @@ -18,16 +18,15 @@ module PluginPackager

it "should set dependencies if present" do
AgentDefinition.any_instance.expects(:common)
agent = AgentDefinition.new(".", "test-package", nil, nil, nil, nil, ["foo"], {}, "agent")
agent.dependencies.should == ["foo"]
agent = AgentDefinition.new(".", "test-package", nil, nil, nil, nil, [:name => "foo", :version => nil], {}, "agent")
agent.dependencies.should == [{:name => "foo", :version => nil}, {:name => "mcollective-common", :version => nil}]
end

it "should set mc server, client and common dependencies" do
it "should set mc name and version" do
AgentDefinition.any_instance.expects(:common)
agent = AgentDefinition.new(".", "test-package", nil, nil, nil, nil, [], {:server => "pe-mcollective"}, "agent")
agent.mcserver.should == "pe-mcollective"
agent.mcclient.should == "mcollective-client"
agent.mccommon.should == "mcollective-common"
agent = AgentDefinition.new(".", "test-package", nil, nil, nil, nil, [], {:mcname =>"pe-mcollective-common", :mcversion =>"1.2"}, "agent")
agent.mcname.should == "pe-mcollective-common"
agent.mcversion.should == "1.2"
end
end

Expand Down Expand Up @@ -62,7 +61,7 @@ module PluginPackager
File.stubs(:join).with(".", "agent").returns("tmpdir/agent")
File.stubs(:join).with("tmpdir/agent", "*.ddl").returns("tmpdir/agent/*.ddl")
File.stubs(:join).with("tmpdir/agent", "*").returns("tmpdir/agent/*")
Dir.stubs(:glob).with("tmpdir/agent/*.ddl").returns([])
Dir.stubs(:glob).with("tmpdir/agent/*.ddl").returns([])
Dir.stubs(:glob).with("tmpdir/agent/*").returns(["implementation.rb"])

agent = AgentDefinition.new(".", nil, nil, nil, nil, nil, [], {}, "agent")
Expand All @@ -78,7 +77,8 @@ module PluginPackager
Dir.stubs(:glob).returns([])

agent = AgentDefinition.new(".", nil, nil, nil, nil, nil, [], {}, "agent")
agent.packagedata[:agent][:dependencies].should == ["mcollective", ["mcollective-foo-common", 1]]
agent.packagedata[:agent][:dependencies].should == [{:name => "mcollective-common", :version => nil},
{:name => "mcollective-foo-common", :version =>1}]
end
end

Expand Down Expand Up @@ -157,7 +157,8 @@ module PluginPackager
Dir.expects(:glob).with("aggregatedir/*").returns(["aggregate.rb"])

client = AgentDefinition.new(".", nil, nil, nil, nil, nil, [], {}, "agent")
client.packagedata[:client][:dependencies].should == ["mcollective-client", ["mcollective-foo-common", 1]]
client.packagedata[:client][:dependencies].should == [{:name => "mcollective-common", :version => nil},
{:name => "mcollective-foo-common", :version => 1}]
end
end
end
Expand Down
16 changes: 9 additions & 7 deletions spec/unit/pluginpackager/standard_definition_spec.rb
Expand Up @@ -16,14 +16,15 @@ module PluginPackager
end

it "should set dependencies if present" do
plugin = StandardDefinition.new(".", "test plugin", nil, nil, nil, nil, ["foo"], {}, "testplugin")
plugin.dependencies.should == ["foo"]
plugin = StandardDefinition.new(".", "test plugin", nil, nil, nil, nil, [{:name => "foo", :version => nil}], {}, "testplugin")
plugin.dependencies.should == [{:name => "foo", :version => nil},
{:name => "mcollective-common", :version => nil}]
end

it "should set mc server, client and common dependencies" do
plugin = StandardDefinition.new(".", "test plugin", nil, nil, nil, nil, [], {:server => "pe-mcollective"}, "testplugin")
plugin.mcserver.should == "pe-mcollective"
plugin.mccommon.should == "mcollective-common"
it "should set mc name and version dependencies" do
plugin = StandardDefinition.new(".", "test plugin", nil, nil, nil, nil, [], {:mcname => "pe-mcollective", :mcversion => "1"}, "testplugin")
plugin.mcname.should == "pe-mcollective"
plugin.mcversion.should == "1"
end
end

Expand Down Expand Up @@ -61,7 +62,8 @@ module PluginPackager
Dir.expects(:glob).with("./testplugin/*").returns(["file.rb"])
plugin = StandardDefinition.new(".", nil, nil, nil, nil, nil, [], {}, "testplugin")
plugin.packagedata["testplugin"][:files].should == ["file.rb"]
plugin.packagedata["testplugin"][:dependencies].should == ["mcollective", ["mcollective-foo-common", 1]]
plugin.packagedata["testplugin"][:dependencies].should == [{:name => "mcollective-common", :version => nil},
{:name => "mcollective-foo-common", :version => 1}]
end
end

Expand Down
7 changes: 5 additions & 2 deletions spec/unit/pluginpackager_spec.rb
Expand Up @@ -42,9 +42,12 @@ module MCollective
Dir.stubs(:glob).returns(["foo.ddl"])
File.expects(:read).with("foo.ddl").returns("foo_ddl")
ddl.expects(:instance_eval).with("foo_ddl")
ddl.expects(:meta)
ddl.expects(:meta).returns("metadata")
ddl.expects(:requirements).returns({:mcollective => 1})

PluginPackager.get_metadata("/tmp", "foo")
meta, requirements = PluginPackager.get_metadata("/tmp", "foo")
meta.should == "metadata"
requirements.should == 1
end
end

Expand Down
Expand Up @@ -19,6 +19,7 @@ module PluginPackager
before :each do
PluginPackager.stubs(:build_tool?).with("debuild").returns(true)
@plugin = mock()
@plugin.stubs(:mcname).returns("mcollective")
end

after :all do
Expand Down
Expand Up @@ -21,6 +21,7 @@ module PluginPackager
@plugin = mock()
@plugin.stubs(:iteration).returns("1")
@plugin.stubs(:metadata).returns({:name => "test", :version => "1"})
@plugin.stubs(:mcname).returns("mcollective")
end

after :all do
Expand Down

0 comments on commit 49ad89c

Please sign in to comment.