Class: Chef::Provider::Link

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

Instance Attribute Summary

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #node

Instance Method Summary collapse

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, #convert_to_snake_case, #filename_to_qualified_string

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



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

def action_create
  if @current_resource.to != @new_resource.to
    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
      run_command(
        :command => "ln -nfs #{@new_resource.to} #{@new_resource.target_file}"
      )
    elsif @new_resource.link_type == :hard
      ::File.link(@new_resource.to, @new_resource.target_file)
    end
    @new_resource.updated = 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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/chef/provider/link.rb', line 134

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 = 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 = 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.



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

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.



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

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



39
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
# File 'lib/chef/provider/link.rb', line 39

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



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

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 = true
  end
end

#set_ownerObject

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



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

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 = true
  end
end