Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 80 additions & 7 deletions resources/macos/uninstaller.tool.erb
Original file line number Diff line number Diff line change
@@ -1,24 +1,97 @@
#!/bin/bash

# Uninstaller for <%= @name %>, generated by vanagon.
#
# Removal is driven by the installer receipt rather than by a fixed list of
# directories, so directories shared with other packages (for example
# /opt/puppetlabs/bin, which is used by both openvox-agent and openbolt) are
# only removed when nothing else owns them.

export LC_ALL=C

pkg_id="<%= @identifier %>.<%= @name %>"

# Top-level directories that belong to the operating system. They are never
# removed, even when the receipt lists them and they end up empty.
protected_dirs=" Applications Library System Users bin etc opt private private/etc private/var sbin usr usr/local var "

is_protected() {
case "${protected_dirs}" in
*" ${1} "*) return 0 ;;
*) return 1 ;;
esac
}

<%- if has_services? -%>
<%- get_services.each do |service| -%>
echo "Unloading service <%= service.name %>..."
/bin/launchctl unload <%= service.service_file %>
echo "OK"
echo "Removing service file <%= service.service_file %>..."
/bin/rm <%= service.service_file %>
/bin/rm -f <%= service.service_file %>
echo "OK"
<%- end -%>
<%- end -%>

echo "Removing identifier: <%= @identifier-%>.<%= @name -%>..."
if /usr/sbin/pkgutil --files <%= @identifier-%>.<%= @name -%> >/dev/null 2>&1 ; then
/usr/sbin/pkgutil --forget <%= @identifier-%>.<%= @name -%>

pkg_info=$(/usr/sbin/pkgutil --pkg-info "${pkg_id}" 2>/dev/null)
if [ -z "${pkg_info}" ]; then
echo "No installer receipt found for ${pkg_id}; nothing to remove." >&2
exit 1
fi

volume=$(printf '%s\n' "${pkg_info}" | /usr/bin/sed -n 's/^volume: //p')
volume="${volume:-/}"
location=$(printf '%s\n' "${pkg_info}" | /usr/bin/sed -n 's/^location: //p')
root="${volume%/}/${location#/}"
root="${root%/}"

workdir=$(/usr/bin/mktemp -d -t "${pkg_id}-uninstall")
trap '/bin/rm -rf "${workdir}"' EXIT

/usr/sbin/pkgutil --volume "${volume}" --files "${pkg_id}" | /usr/bin/sort > "${workdir}/entries"
/usr/sbin/pkgutil --volume "${volume}" --only-dirs --files "${pkg_id}" | /usr/bin/sort > "${workdir}/directories"

echo "Removing files installed by ${pkg_id}..."
/usr/bin/comm -23 "${workdir}/entries" "${workdir}/directories" |
while IFS= read -r entry; do
/bin/rm -f "${root}/${entry}"
done
echo "OK"

echo "Removing Files"
/bin/rm -Rf <%= get_directories.map {|d| d.path}.join(" ").to_s -%>
# Directories declared by the project are removed along with anything written
# to them after installation (logs, caches, configuration), unless another
# installed package also claims them.
remove_unshared_directory() {
entry="${1#/}"
dir="${root}/${entry}"
[ -d "${dir}" ] || return 0
if is_protected "${entry}"; then
echo "Keeping ${dir}: it belongs to the operating system"
return 0
fi
owners=$(/usr/sbin/pkgutil --volume "${volume}" --file-info "${dir}" 2>/dev/null |
/usr/bin/sed -n 's/^pkgid: //p' | /usr/bin/grep -vx "${pkg_id}")
if [ -n "${owners}" ]; then
echo "Keeping ${dir}: it is also used by $(printf '%s' "${owners}" | /usr/bin/tr '\n' ' ')"
return 0
fi
/bin/rm -Rf "${dir}"
}

echo "Removing directories owned by ${pkg_id}..."
<%- get_directories.map(&:path).each do |dir| -%>
remove_unshared_directory '<%= dir %>'
<%- end -%>
echo "OK"

echo "Removing empty directories..."
/usr/bin/sort -r "${workdir}/directories" |
while IFS= read -r entry; do
is_protected "${entry}" && continue
/bin/rmdir "${root}/${entry}" 2>/dev/null
done
echo "OK"

echo "Removing identifier: ${pkg_id}..."
/usr/sbin/pkgutil --volume "${volume}" --forget "${pkg_id}"
echo "OK"
52 changes: 51 additions & 1 deletion spec/lib/vanagon/platform/macos_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'tempfile'
require 'vanagon/platform'
require 'vanagon/project'
require 'vanagon/common'

describe "Vanagon::Platform::MacOS" do
let(:block) {
Expand All @@ -17,5 +20,52 @@
expect(plat._platform.send(:mktemp)).to eq("mktemp -d -t 'tmp'")
end
end
end

describe "uninstaller" do
let(:configdir) { '/a/b/c' }
let(:template) { File.expand_path('../../../../resources/macos/uninstaller.tool.erb', __dir__) }
let(:project_block) {
%Q[ project 'test-fixture' do |proj|
proj.identifier 'org.example'
proj.directory '/opt/puppetlabs/test-fixture'
proj.directory '/opt/puppetlabs/bin'
end
]
}
let(:uninstaller) do
proj = Vanagon::Project::DSL.new('test-fixture', configdir, plat._platform, [])
proj.instance_eval(project_block)
project = proj._project
project.erb_string(template, project.instance_eval { binding })
end

it "is valid bash" do
Tempfile.create('uninstaller') do |file|
file.write(uninstaller)
file.flush
expect(system('/bin/bash', '-n', file.path)).to be(true)
end
end

it "identifies the package by its receipt" do
expect(uninstaller).to include('pkg_id="org.example.test-fixture"')
end

it "removes the files recorded in the package receipt" do
expect(uninstaller).to include('--files "${pkg_id}"')
end

it "forgets the package receipt" do
expect(uninstaller).to include('--forget "${pkg_id}"')
end

it "removes declared directories only when no other package claims them" do
expect(uninstaller).to include("remove_unshared_directory '/opt/puppetlabs/test-fixture'")
expect(uninstaller).to include("remove_unshared_directory '/opt/puppetlabs/bin'")
end

it "does not unconditionally remove declared directories" do
expect(uninstaller).not_to match(%r{^/bin/rm -Rf /opt/puppetlabs})
end
end
end