zts / chef-cookbook-managed_directory

An LWRP to purge unmanaged files from a directory.
16 stars 17 forks source link

Custom Resources using file resource DSL don't get removed with manage_directory #18

Open rhughes1 opened 5 years ago

rhughes1 commented 5 years ago

Description:

I've built out a custom resource which calls the Chef directory and cookbook_file native resources. It correctly lays the cookbook_file in the directory which was created beforehand. In the recipe I then call managed_directory with the recursive set to true. It seems as though the managed_directory resource doesn't take into account any file or directory resources created inside custom resources

Expected Result

The file and directory shouldn't get deleted as they were created by Chef in the custom resource

Actual Result

The file and directory get deleted when the managed_directory resource is called.

Steps to Reproduce

Create the following

Recipe:

custom_resource 'test.sh' do
  folder_prefix 0
  source 'test/test_file.sh'
  action :create
end

managed_directory '/test/blargh' do
  clean_directories true
  action :clean
end

Custom Resource

action :create do
  directory_path = :: File.join('/test/blargh', new_resource.folder_prefix.to_s) # Basically it's now equal to '/test/blargh/0'
  directory directory_path do
    recursively true
  end

  # Lay down the cookbook file to '/test/blargh/0/test_file.sh'
  cookbook_file ::File.join(directory_path, new_resource.name) do
    source new_resource.source
    owner 'root'
    group 'root'
    mode '0755'
    action :create
  end
end

Kitchen Logs

      * custom_resource [test.sh] action create
           * directory[/test/blargh/0] action create
             - create new directory /test/blargh/0
           * cookbook_file[/test/blargh/0/test_file.sh] action create
             - create new file /test/blargh/0/test_file.sh
             - update content in file/test/blargh/0/test_file.sh from none to 51ed9e
             --- /test/blargh/0/test_file.sh        2018-10-24 14:12:31.651284200 +0000
             +++ /test/blargh/0/test_file.sh20181024-281-ck0gx4       2018-10-24 14:12:31.651284200 +0000
             @@ -1 +1,11 @@
             +#!/bin/bash
             +
             +echo "Test"
             - change mode from '' to '0755'
             - change owner from '' to 'root'
             - change group from '' to 'root'

         * managed_directory[/test/blargh] action clean
           * directory[/test/blargh/0] action delete
             - delete existing directory /test/blargh/0

Platform: CentOS 7.4 Chef Client Version: 14.3.37 Workaround: So it does seem like there's a workaround, but it seems rather inconvenient to do. In the custom resource, change the directory and the cookbook file to this:

run_context.root_run_context.resource_collection << custom_resource_dir = Chef::Resource::Directory.new(directory_path , run_context)
  custom_resource_dir .recursive true
  custom_resource_dir .action :create

 run_context.root_run_context.resource_collection << custome_resource_cookbook_file = Chef::Resource::CookbookFile.new(::File.join(directory_path, new_resource.name), run_context)
  custome_resource_cookbook_file .source new_resource.source
  custome_resource_cookbook_file .cookbook 'test_manage_directory_cookbook'
  custome_resource_cookbook_file .owner 'root'
  custome_resource_cookbook_file .group 'root'
  custome_resource_cookbook_file .mode '0755'
  custome_resource_cookbook_file .action :create

This will add the resources to the RunContext, but there must be a way to not only scan the main RunContext but also scan any custom resources which also use recipe DSL?

lehn-etracker commented 4 years ago

Same problem here! With latest chef-client and motd cookbook. manage_directory is now useless in our environment. Hopefully somebody is able to fix this bug, soon.

zts commented 4 years ago

Apologies to @rhughes1 for never replying to the very well reported issue, and sorry that @lehn-etracker also has this problem.

I've attempted to write tests reproducing the issue, but I have not been successful. My work is in branch https://github.com/zts/chef-cookbook-managed_directory/tree/update_tests , which introduce a simple custom resource which uses the file and directory resources. Surprisingly, manage_directory appears to work.

I'm not sure what I'm doing wrong with the tests, as I'm 100% certain that the problem exists. I figured it would be possible to solve this using the event system, and the change in this PR also looks promising.

I don't currently use this cookbook, but if you can give me a failing test then I'll see if I can figure it out.