anish anil
anish anil

Reputation: 2641

Is there a sequence of steps followed in chef recipes

In my recipe I have the following added:

directory '/opt/test/scripts' do
    owner 'root'
    group 'root'
    mode '0755'
    action :create
    recursive true
    not_if 'grep /opt/test'
end

DownLoad file and change permissions:

uri=URI.parse('https://repo.test2.io/BwIaTHE0ohsUts74LYZMzrt/pair.sh')
response = Net::HTTP.get_response(uri)
File.write('/opt/test/scripts/pair.sh', response.body)
File = File.new( "/opt/test/scripts/pair.sh", "w" ).chmod( 0755 )

It looks strange, since it is trying to download the file and not create a directory where the file needs to be downloaded tooo.

It tries to download the file to the /opt directory and since the directory is not available it simply fails the execution process...

ERROR OBSERVED

Recipe Compile Error in /var/chef/cache/cookbooks/ill/recipes/default.rb

Errno::ENOENT

No such file or directory @ rb_sysopen - /opt/test/scripts/pair.sh

Is there a control mechanism that CHEF implements or is there a way to make CHEF understand the directory creation should happen first and then download??

Thank you

Upvotes: 0

Views: 351

Answers (1)

coderanger
coderanger

Reputation: 54249

Chef uses a two-pass execution model detailed in https://coderanger.net/two-pass/ but basically first the Ruby code that is the recipe is run, which "compile" down to resources, and then the resources are evaluated. So putting a mix of resource DSL and plain Ruby in there means they do not execute in the order you expect (because the directory resource was only queued up, not actually run). That said, what you want here is to use a remote_file resource instead of your manual code, though in the more general case you can use a ruby_block resource to delay running some code until converge time.

Upvotes: 1

Related Questions