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

Inherits:
Chef::Provider::Package show all
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, #node

Instance Method Summary collapse

Methods inherited from Chef::Provider::Package

#action_install, #action_purge, #action_remove, #action_upgrade, #expand_options, #get_preseed_file, #initialize, #preseed_package, #purge_package, #should_remove_package, #upgrade_package

Methods included from Mixin::Command

handle_command_failures, not_if, only_if, output_of_command, popen4, run_command, run_command_with_systems_locale

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #initialize

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #filename_to_qualified_string

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Constructor Details

This class inherits a constructor from Chef::Provider::Package

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chef/provider/package/freebsd.rb', line 29

def current_installed_version
  command = "pkg_info -E \"#{package_name}*\""
  status = popen4(command) do |pid, stdin, stdout, stderr|
    stdout.each do |line|
      case line
      when /^#{package_name}-(.+)/
        return $1
      end
    end
  end
  unless status.exitstatus == 0 || status.exitstatus == 1
    raise Chef::Exceptions::Package, "#{command} failed - #{status.inspect}!"
  end
  nil
end

#install_package(name, version) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/chef/provider/package/freebsd.rb', line 109

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


96
97
98
# File 'lib/chef/provider/package/freebsd.rb', line 96

def latest_link_name
  ports_makefile_variable_value("LATEST_LINK")
end

#load_current_resourceObject



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

def load_current_resource
  @current_resource = Chef::Resource::Package.new(@new_resource.name)
  @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



101
102
103
104
105
106
107
# File 'lib/chef/provider/package/freebsd.rb', line 101

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

#port_pathObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chef/provider/package/freebsd.rb', line 45

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
    popen4("whereis -s #{@new_resource.package_name}") do |pid, stdin, stdout, stderr|
      stdout.each do |line|
        case line
        when /^#{@new_resource.package_name}:\s+(.+)$/
          return $1
        end
      end
    end
    raise Chef::Exception::Package, "Could not find port with the name #{@new_resource.package_name}"
  end          
end

#ports_candidate_versionObject



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

def ports_candidate_version
  ports_makefile_variable_value("PORTVERSION")
end

#ports_makefile_variable_value(variable) ⇒ Object



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

def ports_makefile_variable_value(variable)
  command = "cd #{port_path}; make -V #{variable}"
  status = popen4(command) do |pid, stdin, stdout, stderr|
    return stdout.readline.strip
  end
  unless status.exitstatus == 0 || status.exitstatus == 1
    raise Chef::Exceptions::Package, "#{command} failed - #{status.inspect}!"
  end
  nil
end

#remove_package(name, version) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/chef/provider/package/freebsd.rb', line 138

def remove_package(name, version)
  # a version is mandatory
  if version
    run_command_with_systems_locale(
      :command => "pkg_delete #{package_name}-#{version}"
    )
  else
    run_command_with_systems_locale(
      :command => "pkg_delete #{package_name}-#{@current_resource.version}"
    )
  end
end