Class: Chef::Provider::Link

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

Instance Attribute Summary

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

#action_nothing, build_from_file, #cookbook_name, #initialize, #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

This class inherits a constructor from Chef::Provider

Dynamic Method Handling

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

Instance Method Details

#action_createObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/chef/provider/link.rb', line 117

def action_create
  if @current_resource.to != ::File.expand_path(@new_resource.to, @new_resource.target_file)
    Chef::Log.info("Creating a #{@new_resource.link_type} link from #{@new_resource.to} -> #{@new_resource.target_file} for #{@new_resource}")
    if @new_resource.link_type == :symbolic
      unless (::File.symlink?(@new_resource.target_file) && ::File.readlink(@new_resource.target_file) == @new_resource.to)
        if ::File.symlink?(@new_resource.target_file) || ::File.exist?(@new_resource.target_file)
          ::File.unlink(@new_resource.target_file)
        end
        ::File.symlink(@new_resource.to,@new_resource.target_file)
    end
    elsif @new_resource.link_type == :hard
      ::File.link(@new_resource.to, @new_resource.target_file)
    end
    @new_resource.updated_by_last_action(true)
  end
  if @new_resource.link_type == :symbolic
    set_owner unless @new_resource.owner.nil?
    set_group unless @new_resource.group.nil?
  end
end

#action_deleteObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/chef/provider/link.rb', line 138

def action_delete
  if @new_resource.link_type == :symbolic 
    if ::File.symlink?(@new_resource.target_file)
      Chef::Log.info("Deleting #{@new_resource} at #{@new_resource.target_file}")
      ::File.delete(@new_resource.target_file)
      @new_resource.updated_by_last_action(true)
    elsif ::File.exists?(@new_resource.target_file)
      raise Chef::Exceptions::Link, "Cannot delete #{@new_resource} at #{@new_resource.target_file}! Not a symbolic link."
    end
  elsif @new_resource.link_type == :hard 
    if ::File.exists?(@new_resource.target_file)
       if ::File.exists?(@new_resource.to) && ::File.stat(@current_resource.target_file).ino == ::File.stat(@new_resource.to).ino
         Chef::Log.info("Deleting #{@new_resource} at #{@new_resource.target_file}")
         ::File.delete(@new_resource.target_file)
         @new_resource.updated_by_last_action(true)
       else
         raise Chef::Exceptions::Link, "Cannot delete #{@new_resource} at #{@new_resource.target_file}! Not a hard link."
       end
    end
  end
end

#compare_groupObject

Compares the group of a symlink. Returns true if they are the same, false if they are not.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef/provider/link.rb', line 95

def compare_group
  return false if @new_resource.group.nil?
  
  @set_group_id = case @new_resource.group
                  when /^\d+$/, Integer
                    @new_resource.group.to_i
                  else
                    Etc.getgrnam(@new_resource.group).gid
                  end
  
  @set_group_id == @current_resource.group
end

#compare_ownerObject

Compare the ownership of a symlink. Returns true if they are the same, false if they are not.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef/provider/link.rb', line 70

def compare_owner
  return false if @new_resource.owner.nil?
  
  @set_user_id = case @new_resource.owner
                 when /^\d+$/, Integer
                   @new_resource.owner.to_i
                 else
                   # This raises an ArgumentError if you can't find the user         
                   Etc.getpwnam(@new_resource.owner).uid
                 end
  
  @set_user_id == @current_resource.owner
end

#load_current_resourceObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chef/provider/link.rb', line 40

def load_current_resource
  @current_resource = Chef::Resource::Link.new(@new_resource.name)
  @current_resource.target_file(@new_resource.target_file)
  @current_resource.link_type(@new_resource.link_type)
  if @new_resource.link_type == :symbolic          
    if ::File.exists?(@current_resource.target_file) && ::File.symlink?(@current_resource.target_file)
      @current_resource.to(
        ::File.expand_path(::File.readlink(@current_resource.target_file))
      )
      cstats = ::File.lstat(@current_resource.target_file)
      @current_resource.owner(cstats.uid)
      @current_resource.group(cstats.gid)
    else
      @current_resource.to("")
    end
  elsif @new_resource.link_type == :hard
    if ::File.exists?(@current_resource.target_file) && ::File.exists?(@new_resource.to)
      if ::File.stat(@current_resource.target_file).ino == ::File.stat(@new_resource.to).ino
        @current_resource.to(@new_resource.to)
      else
        @current_resource.to("")
      end
    else
      @current_resource.to("")
    end
  end
  @current_resource
end

#set_groupObject



108
109
110
111
112
113
114
115
# File 'lib/chef/provider/link.rb', line 108

def set_group
  unless compare_group
    Chef::Log.info("Setting group to #{@set_group_id} for #{@new_resource}")
    @set_group_id = negative_complement(@set_group_id)
    ::File.lchown(nil, @set_group_id, @new_resource.target_file)
    @new_resource.updated_by_last_action(true)
  end
end

#set_ownerObject

Set the ownership on the symlink, assuming it is not set correctly already.



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

def set_owner
  unless compare_owner
    Chef::Log.info("Setting owner to #{@set_user_id} for #{@new_resource}")
    @set_user_id = negative_complement(@set_user_id)
    ::File.lchown(@set_user_id, nil, @new_resource.target_file)
    @new_resource.updated_by_last_action(true)
  end
end