Class: Chef::Provider::Package::Freebsd

Inherits:
Chef::Provider::Package show all
Includes:
Mixin::ShellOut
Defined in:
lib/chef/provider/package/freebsd.rb

Instance Attribute Summary

Attributes inherited from Chef::Provider::Package

#candidate_version

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#shell_out, #shell_out!

Methods inherited from Chef::Provider::Package

#action_install, #action_purge, #action_remove, #action_upgrade, #expand_options, #get_preseed_file, #preseed_package, #preseed_resource, #purge_package, #removing_package?, #upgrade_package

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #not_if, #only_if, #output_of_command, #run_command, #run_command_with_systems_locale

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #cookbook_name, #node, #resource_collection

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #search, #value_for_platform

Constructor Details

#initialize(*args) ⇒ Freebsd

Returns a new instance of Freebsd.



30
31
32
33
# File 'lib/chef/provider/package/freebsd.rb', line 30

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Method Details

#current_installed_versionObject



35
36
37
38
# File 'lib/chef/provider/package/freebsd.rb', line 35

def current_installed_version
  pkg_info = shell_out!("pkg_info -E \"#{package_name}*\"", :env => nil, :returns => [0,1])
  pkg_info.stdout[/^#{package_name}-(.+)/, 1]
end

#install_package(name, version) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chef/provider/package/freebsd.rb', line 93

def install_package(name, version)
  unless @current_resource.version
    case @new_resource.source
    when /^ports$/
      shell_out!("make -DBATCH install", :cwd => port_path, :env => nil).status
    when /^http/, /^ftp/
      shell_out!("pkg_add -r #{package_name}", :env => { "PACKAGESITE" => @new_resource.source, 'LC_ALL' => nil }).status
      Chef::Log.info("Installed package #{package_name} from: #{@new_resource.source}")
    when /^\//
      shell_out!("pkg_add #{@new_resource.name}", :env => { "PKG_PATH" => @new_resource.source , 'LC_ALL'=>nil}).status
      Chef::Log.info("Installed package #{@new_resource.name} from: #{@new_resource.source}")
    else
      shell_out!("pkg_add -r #{latest_link_name}", :env => nil).status
      Chef::Log.info("Installed package #{package_name}")
    end
  end
end


80
81
82
# File 'lib/chef/provider/package/freebsd.rb', line 80

def latest_link_name
  ports_makefile_variable_value("LATEST_LINK")
end

#load_current_resourceObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/provider/package/freebsd.rb', line 68

def load_current_resource
  @current_resource.package_name(@new_resource.package_name)

  @current_resource.version(current_installed_version)
  Chef::Log.debug("Current version is #{@current_resource.version}") if @current_resource.version

  @candidate_version = ports_candidate_version
  Chef::Log.debug("Ports candidate version is #{@candidate_version}") if @candidate_version

  @current_resource
end

#package_nameObject

The name of the package (without the version number) as understood by pkg_add and pkg_info



85
86
87
88
89
90
91
# File 'lib/chef/provider/package/freebsd.rb', line 85

def package_name
  if ports_makefile_variable_value("PKGNAME") =~ /^(.+)-[^-]+$/
    $1
  else
    raise Chef::Exceptions::Package, "Unexpected form for PKGNAME variable in #{port_path}/Makefile"
  end
end

#port_pathObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chef/provider/package/freebsd.rb', line 40

def port_path
  case @new_resource.package_name
  # When the package name starts with a '/' treat it as the full path to the ports directory
  when /^\//
    @new_resource.package_name
  # Otherwise if the package name contains a '/' not at the start (like 'www/wordpress') treat as a relative
  # path from /usr/ports
  when /\//
    "/usr/ports/#{@new_resource.package_name}"
  # Otherwise look up the path to the ports directory using 'whereis'
  else
    whereis = shell_out!("whereis -s #{@new_resource.package_name}", :env => nil)
    unless path = whereis.stdout[/^#{@new_resource.package_name}:\s+(.+)$/, 1]
      raise Chef::Exceptions::Package, "Could not find port with the name #{@new_resource.package_name}"
    end
    path
  end
end

#ports_candidate_versionObject



64
65
66
# File 'lib/chef/provider/package/freebsd.rb', line 64

def ports_candidate_version
  ports_makefile_variable_value("PORTVERSION")
end

#ports_makefile_variable_value(variable) ⇒ Object



59
60
61
62
# File 'lib/chef/provider/package/freebsd.rb', line 59

def ports_makefile_variable_value(variable)
  make_v = shell_out!("make -V #{variable}", :cwd => port_path, :env => nil, :returns => [0,1])
  make_v.stdout.strip.split($\).first # $\ is the line separator, i.e., newline
end

#remove_package(name, version) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/chef/provider/package/freebsd.rb', line 111

def remove_package(name, version)
  # a version is mandatory
  if version
    shell_out!("pkg_delete #{package_name}-#{version}", :env => nil).status
  else
    shell_out!("pkg_delete #{package_name}-#{@current_resource.version}", :env => nil).status
  end
end