Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apt: add support for ppa. #202

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 39 additions & 10 deletions lib/sprinkle/installers/apt.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module Sprinkle
module Installers
# The Apt package installer uses the +apt-get+ command to install
# packages. The apt installer has only one option which can be
# packages. The apt installer has one option which can be
# modified which is the +dependencies_only+ option. When this is
# set to true, the installer uses +build-dep+ instead of +install+
# to only build the dependencies.
#
# to only build the dependencies. If you want to install from Ubuntu
# PPA, set the `ppa` option.
#
# == Example Usage
#
# First, a simple installation of the magic_beans package:
Expand All @@ -19,23 +20,31 @@ module Installers
#
# package :magic_beans_depends do
# apt 'magic_beans_package' do
# dependencies_only true
# dependencies_only true
# end
# end
#
# Third, get magic_beans from ppa:user/ppa-name:
#
# package :magic_beans do
# apt 'magic_bean' do
# ppa 'user/ppa-name'
# end
# end
#
# As you can see, setting options is as simple as creating a
# block and calling the option as a method with the value as
# block and calling the option as a method with the value as
# its parameter.
class Apt < PackageInstaller
def initialize(parent, *packages, &block) #:nodoc:
super parent, *packages, &block
@options.reverse_merge!(:dependencies_only => false)
@options.reverse_merge!(:dependencies_only => false, :ppa => nil)
end
attributes :dependencies_only

attributes :dependencies_only, :ppa

auto_api

verify_api do
def has_apt(package)
@commands << "dpkg --status #{package} | grep \"ok installed\""
Expand All @@ -44,12 +53,32 @@ def has_apt(package)

protected

def ppa_repository_exist?(ppa) #:nodoc:
ppa_user, ppa_name = ppa.split('/')
sources_glob = ['/etc/apt/sources.list', '/etc/apt/sources.list.d/*.list']
ppa_pattern = /^deb http:\/\/ppa.launchpad.net\/#{ppa}/
Dir.glob(sources_glob).any? do |f|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can this possibly work? Ruby is running locally, not remotely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed fixes.

open(f).any? do |line|
line =~ ppa_pattern
end
end
end

def install_commands #:nodoc:
command = @options[:dependencies_only] ? 'build-dep' : 'install'
noninteractive = "#{sudo_cmd}env DEBCONF_TERSE='yes' DEBIAN_PRIORITY='critical' DEBIAN_FRONTEND=noninteractive"
"#{noninteractive} apt-get --force-yes -qyu #{command} #{@packages.join(' ')}"
if @options[:ppa]
ppa = @options[:ppa]
unless ppa_repository_exist?(ppa)
add_ppa = "#{sudo_cmd}add-apt-repository ppa:#{ppa}"
apt_update = "#{noninteractive} apt-get update"
ppa_cmd = "#{add_ppa} && #{apt_update} &&"
end
end
"#{ppa_cmd} #{noninteractive} apt-get --force-yes -qyu #{command} #{@packages.join(' ')}"
end


end
end
end
24 changes: 18 additions & 6 deletions spec/sprinkle/installers/apt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
before do
@package = create_pkg "name", :use_sudo => false
end

def create_pkg(name="name", opts={})
@package = Sprinkle::Package::Package.new(name) {}
@package.use_sudo opts[:use_sudo]
Expand Down Expand Up @@ -34,7 +34,7 @@ def create_apt(*debs, &block)
end

end


describe 'during installation' do

Expand All @@ -45,14 +45,14 @@ def create_apt(*debs, &block)
end
@install_commands = @installer.send :install_commands
end

it 'should use sudo if package specifies' do
@package = create_pkg "name", :use_sudo => true
@installer = create_apt 'ruby'
@install_commands = @installer.send :install_commands
@install_commands.should =~ /sudo env/
end

it 'should use sudo if installer specifies' do
@package = create_pkg "name", :use_sudo => false
@installer = create_apt 'ruby', :sudo => true
Expand All @@ -78,7 +78,7 @@ def create_apt(*debs, &block)
end

end

describe 'during dependencies only installation' do

before do
Expand All @@ -89,7 +89,19 @@ def create_apt(*debs, &block)
it 'should invoke the apt installer with build-dep command for all specified packages' do
@install_commands.should =~ /apt-get --force-yes -qyu build-dep ruby/
end

end

describe 'during installation from a Ubuntu PPA' do

before do
# We use the (outdated) ppa name in launchpad help:
# https://help.launchpad.net/Packaging/PPA/InstallingSoftware
@installer = create_apt('gwibber') { ppa 'gwibber-daily/ppa'}
@install_commands = @installer.send :install_commands
end

it 'should add the PPA repository before invoking apt-get' do
@install_commands.should =~ /add-apt-repository ppa:/
end
end