Skip to content

Commit

Permalink
Add input and output rpm trigger support
Browse files Browse the repository at this point in the history
This adds several rpm-specific flags:

--before-install
--after-install
--before-uninstall
--after-target-uninstall

Fixes #626 (merged by hand)
  • Loading branch information
makushimu authored and jordansissel committed Oct 25, 2014
1 parent f054c12 commit 4e7ffd8
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
42 changes: 41 additions & 1 deletion lib/fpm/package/rpm.rb
Expand Up @@ -135,6 +135,21 @@ class FPM::Package::RPM < FPM::Package
"posttrans script" do |val|
File.expand_path(val) # Get the full path to the script
end # --posttrans

["before-install","after-install","before-uninstall","after-target-uninstall"].each do |trigger_type|
rpm_trigger = []
option "--trigger-#{trigger_type}", "'[OPT]PACKAGE: FILEPATH'", "Adds a rpm trigger script located in FILEPATH, " \
"having 'OPT' options and linking to 'PACKAGE'. PACKAGE can be a comma seperated list of packages. " \
"See: http://rpm.org/api/4.4.2.2/triggers.html" do |trigger|
match = trigger.match(/^(\[.*\]|)(.*): (.*)$/)
@logger.fatal("Trigger '#{trigger_type}' definition can't be parsed ('#{trigger}')") unless match
opt, pkg, file = match.captures
@logger.fatal("File given for --trigger-#{trigger_type} does not exist (#{file})") unless File.exists?(file)
rpm_trigger << [pkg, File.read(file), opt.tr('[]','')]
next rpm_trigger
end
end

private

# Fix path name
Expand Down Expand Up @@ -282,6 +297,21 @@ def converted_from(origin)
setscript.call(:rpm_pretrans)
end # def converted

def rpm_get_trigger_type(flag)
puts "#{flag.to_s(2)}"
if (flag & (1 << 25)) == (1 << 25)
:rpm_trigger_before_install
elsif (flag & (1 << 16)) == (1 << 16)
:rpm_trigger_after_install
elsif (flag & (1 << 17)) == (1 << 17)
:rpm_trigger_before_uninstall
elsif (flag & (1 << 18)) == (1 << 18)
:rpm_trigger_after_target_uninstall
else
@logger.fatal("I don't know about this triggerflag ('#{flag}')")
end
end # def rpm_get_trigger

def input(path)
rpm = ::RPM::File.new(path)

Expand Down Expand Up @@ -313,7 +343,17 @@ def input(path)
# Also taking into account the value of tags[preinprog] etc, something like:
# #!#{tags[:preinprog]}
# #{tags[prein]}
# TODO(sissel): put 'trigger scripts' stuff into attributes

val = tags[:triggerindex].zip(tags[:triggername],tags[:triggerflags],tags[:triggerversion]).group_by{ |x| x[0]}
val = val.collect do |order,data|
new_data = data.collect { |x| [ x[1], rpm.operator(x[2]), x[3]].join(" ").strip}.join(", ")
[order, rpm_get_trigger_type(data[0][2]), new_data]
end
val.each do |order, attr,data|
self.attributes[attr] = [] if self.attributes[attr].nil?
scriptprog = (tags[:triggerscriptprog][order] == '/bin/sh') ? "" : "-p #{tags[:triggerscriptprog][order]}"
self.attributes[attr] << [data,tags[:triggerscripts][order],scriptprog]
end

if !attributes[:no_auto_depends?]
self.dependencies += rpm.requires.collect do |name, operator, version|
Expand Down
42 changes: 42 additions & 0 deletions spec/fpm/package/rpm_spec.rb
Expand Up @@ -155,6 +155,12 @@ def subject.render_template; @rpmspec = template("rpm.erb").result(binding); end
subject.scripts[:rpm_pretrans] = "example rpm_pretrans"


# Test for triggers #626
subject.attributes[:rpm_trigger_before_install] = [["test","#!/bin/sh\necho before_install trigger executed\n"]]
subject.attributes[:rpm_trigger_after_install] = [["test","#!/bin/sh\necho after_install trigger executed\n"]]
subject.attributes[:rpm_trigger_before_uninstall] = [["test","#!/bin/sh\necho before_uninstall trigger executed\n"]]
subject.attributes[:rpm_trigger_after_target_uninstall] = [["test","#!/bin/sh\necho after_target_uninstall trigger executed\n"]]

# Write the rpm out
subject.output(@target)

Expand Down Expand Up @@ -260,6 +266,42 @@ def subject.render_template; @rpmspec = template("rpm.erb").result(binding); end
insist { @rpm.tags[:postin] } == "example after_install"
insist { @rpm.tags[:postinprog] } == "/bin/sh"
end

it "should have the correct 'before_install' trigger script" do
insist { @rpm.tags[:triggername][0] } == "test"
insist { @rpm.tags[:triggerversion][0] } == ""
insist { @rpm.tags[:triggerflags][0] & (1 << 25)} == ( 1 << 25) # See FPM::Package::RPM#rpm_get_trigger_type
insist { @rpm.tags[:triggerindex][0] } == 0
insist { @rpm.tags[:triggerscriptprog][0] } == "/bin/sh"
insist { @rpm.tags[:triggerscripts][0] } == "#!/bin/sh\necho before_install trigger executed"
end

it "should have the correct 'after_install' trigger script" do
insist { @rpm.tags[:triggername][1] } == "test"
insist { @rpm.tags[:triggerversion][1] } == ""
insist { @rpm.tags[:triggerflags][1] & (1 << 16)} == ( 1 << 16) # See FPM::Package::RPM#rpm_get_trigger_type
insist { @rpm.tags[:triggerindex][1] } == 1
insist { @rpm.tags[:triggerscriptprog][1] } == "/bin/sh"
insist { @rpm.tags[:triggerscripts][1] } == "#!/bin/sh\necho after_install trigger executed"
end

it "should have the correct 'before_uninstall' trigger script" do
insist { @rpm.tags[:triggername][2] } == "test"
insist { @rpm.tags[:triggerversion][2] } == ""
insist { @rpm.tags[:triggerflags][2] & (1 << 17)} == ( 1 << 17) # See FPM::Package::RPM#rpm_get_trigger_type
insist { @rpm.tags[:triggerindex][2] } == 2
insist { @rpm.tags[:triggerscriptprog][2] } == "/bin/sh"
insist { @rpm.tags[:triggerscripts][2] } == "#!/bin/sh\necho before_uninstall trigger executed"
end

it "should have the correct 'after_target_uninstall' trigger script" do
insist { @rpm.tags[:triggername][3] } == "test"
insist { @rpm.tags[:triggerversion][3] } == ""
insist { @rpm.tags[:triggerflags][3] & (1 << 18)} == ( 1 << 18) # See FPM::Package::RPM#rpm_get_trigger_type
insist { @rpm.tags[:triggerindex][3] } == 3
insist { @rpm.tags[:triggerscriptprog][3] } == "/bin/sh"
insist { @rpm.tags[:triggerscripts][3] } == "#!/bin/sh\necho after_target_uninstall trigger executed"
end

it "should use md5/gzip by default" do
insist { @rpmtags[:payloadcompressor] } == "gzip"
Expand Down
15 changes: 15 additions & 0 deletions templates/rpm.erb
Expand Up @@ -190,6 +190,21 @@ fi
<% end -%>
<% end -%>
<%# This section adds any triggers, as ordered in the command line -%>
<%
triggermap = {
:before_install => "prein",
:after_install => "in",
:before_uninstall => "un",
:after_target_uninstall => "postun"
}
triggermap.each do |name, rpmtype|
(attributes["rpm_trigger_#{name}".to_sym] or []).each do |trigger_name, trigger_script, trigger_scriptprog| -%>
%trigger<%= rpmtype -%> <%= trigger_scriptprog -%> -- <%= trigger_name %>
<%= trigger_script %>
<% end -%>
<% end -%>

%files
%defattr(<%= attributes[:rpm_defattrfile] %>,<%= attributes[:rpm_user] || "root" %>,<%= attributes[:rpm_group] || "root" %>,<%= attributes[:rpm_defattrdir] %>)
<%# Output config files and then regular files. -%>
Expand Down

0 comments on commit 4e7ffd8

Please sign in to comment.