Nico
Nico
Creator of this small website
Sep 5, 2012 1 min read

Chef & FreeBSD : use pkgng

thumbnail for this post

Baptiste Daroussin did an incredible job on FreeBSD with the new packages system, named PkgNG. It brings modern workflow, options and shiny features that were needed for a long time. Say goodbye to painfully long upgrades.

However, Chef is not yet able to use this packaging system as it does not have a PkgNG provider, or not had. This is a hacky way to do so but here is a way to use PkgNG with chef, making it the default provider for your packages.

require 'chef/mixin/shell_out'
require 'chef/platform'
require 'chef'

include Chef::Mixin::ShellOut

class Chef
  class Provider
    class Package
      class Pkgng < Chef::Provider::Package

        def initialize(*args)
          super
          @current_resource = Chef::Resource::Package.new(@new_resource.name)
          @current_resource.package_name(@new_resource.package_name)
          @current_resource
        end

        def check_package_state
          pkg_info = shell_out!("pkg info #{package_name}", :env => nil, :returns => [0,70])

          version = nil
          t = pkg_info.stdout.match(/(.*)-(\d+\.\d+\.\d+(\.\d+.*\s)?)\s+(.*)/)
          unless t.nil?
            version = t[2].strip
          end

          return version
        end

        def load_current_resource
           @current_resource.package_name(@new_resource.package_name)
           @current_resource.version(self.check_package_state())
           @candidate_version = ""
        end

        def package_name
          @new_resource.package_name
        end

        def install_package(name, version = "")
          shell_out!("pkg install -y #{package_name}", :env => nil).status
          Chef::Log.debug("PKGNG : #{@new_resource} installed from: #{@new_resource.source}")
        end

        def remove_package(name, version = nil)
            shell_out!("pkg delete -y #{package_name}", :env => nil).status
        end
      end
    end
  end
end
Chef::Platform.set({
  :platform => :freebsd,
  :resource => :package,
  :provider => Chef::Provider::Package::Pkgng
})

Read more : poudriere & pkgng