module VagrantPlugins
  module DockerProvisioner
    module Cap
      module Debian
        module DockerInstall
          def self.docker_install(machine, version)
            package = 'lxc-docker'
            package << "-#{version}" if version != :latest

            machine.communicate.tap do |comm|
              comm.sudo("apt-get update -y")
              # TODO: Perform check on the host machine if aufs is installed and using LXC
              if machine.provider_name != :lxc
                comm.sudo("lsmod | grep aufs || modprobe aufs || apt-get install -y linux-image-extra-`uname -r`")
              end
              comm.sudo("apt-get install -y --force-yes -q curl")
              comm.sudo("curl -sSL https://get.docker.com/gpg | apt-key add -")
              comm.sudo("echo deb https://get.docker.com/ubuntu docker main > /etc/apt/sources.list.d/docker.list")
              comm.sudo("apt-get update")
              comm.sudo("echo lxc lxc/directory string /var/lib/lxc | debconf-set-selections")
              comm.sudo("apt-get install -y --force-yes -q xz-utils #{package} -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold'")

              # chmod the directory if it exists
              comm.sudo("chmod 0755 /var/lib/docker")
            end
          end
        end
      end
    end
  end
end

module VagrantPlugins
  module DockerProvisioner
    module Cap
      module Debian
        module DockerInstall
          def self.docker_install(machine, version)
            package = 'lxc-docker'
            package << "-#{version}" if version != :latest

            machine.communicate.tap do |comm|
              comm.sudo("apt-get update -y")
              # TODO: Perform check on the host machine if aufs is installed and using LXC
              if machine.provider_name != :lxc
                # Attempt to install linux-image-extra for this kernel, if it exists
                 package_name = "linux-image-extra-`uname -r`"
                 comm.sudo("lsmod | grep aufs || modprobe aufs || apt-cache show #{package_name} && apt-get install -y #{package_name} || true")
              end
              comm.sudo("apt-get install -y --force-yes -q curl")
              comm.sudo("curl -sSL https://get.docker.com/gpg | apt-key add -")
              comm.sudo("echo deb https://get.docker.com/ubuntu docker main > /etc/apt/sources.list.d/docker.list")
              comm.sudo("apt-get update")
              comm.sudo("echo lxc lxc/directory string /var/lib/lxc | debconf-set-selections")
              comm.sudo("apt-get install -y --force-yes -q xz-utils #{package} -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold'")

              # chmod the directory if it exists
              comm.sudo("chmod 0755 /var/lib/docker")
            end
          end
        end
      end
    end
  end
end

Update docker installer to work on custom kernels
Update docker installer to work on custom kernels

The current docker installer attempt to install the linux-image-extra-`uname -r` DEB package on Debian systems.
This package may not exist, for example on custom kernels (e.g., Linode servers).
If this happens, Vagrant halts the provisioning. However, this package is not really needed in newer Debian releases (such as Ubuntu 14.04).

This small patch checks if the linux-image-extra-`uname -r` package exists, and it will install it if it does. In either case, it will continue provisioning.