Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Platform neutral way to create a temporary file in Chef Recipe

I have a chef recipe that uses a template to generate and xml document. The location of that document on the file system is not important; the location simply needs to be handed to a command line tool which will read the file and use it to configure an application. The xml file need not persist beyond the execution of the command. Therefore it seems to me that the file should be created in a platform neutral temporary files directory. C:\Temp on windows or /TMP on most nix machines. Does chef provide a means of creating temporary files, detecting the temporary file location, or otherwise accomplishing the objective?

like image 934
Kenneth Baltrinic Avatar asked Dec 17 '14 12:12

Kenneth Baltrinic


People also ask

How do I create a temp file?

To create and use a temporary file The application opens the user-provided source text file by using CreateFile. The application retrieves a temporary file path and file name by using the GetTempPath and GetTempFileName functions, and then uses CreateFile to create the temporary file.

What are Temporary files and how are they created?

A temporary file is any file created by a program that serves a temporary purpose and is created due to various reasons such as temporary backup, when a program is manipulating data larger than the architecture's address space capacity or to break up large chunks of data into more manageable pieces, or simply as a ...

What software creates TMP files?

Some programs that create and use TMP files include Microsoft Word and Apple Safari. NOTE: TMP files are often called "temp files."


1 Answers

Best bet: use Chef::Config['file_cache_path'] which is used by chef to store transient datas. I.e: template "#{Chef::Config['file_cache_path']}/myfile.xml" do

Left (rephrased) after comment as it still worth being there for general purpose:

As per comment, the execute resource will run anyway with this input to ensure the app is configured accordingly, the file has to be present only when the execute resource is run, for other cases what is below is to be kept in mind.

One of the key idea behind chef is idempotency, chef may run 100 times and do things only when they need to be done.

In this case, the template would be generated once, and never change unless there's a parameter change in attributes.

In such a case (the resulting file should be updated), chef will notice it, replace it and act accordingly with any notification defined on it.

like image 176
Tensibai Avatar answered Sep 21 '22 12:09

Tensibai