Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move/copy files locally with Chef

Tags:

chef-infra

I haven't yet come across a Chef resource that will copy/move files locally. For example, I want to download jetty hightide and unzip it. Once done, copy all the files into a particular folder, like this:

# mv /var/tmp/jetty-hightide-7.4.5.v20110725/* /opt/jetty/ 

BTW, jettyhightide when unzipped, gives you a folder and inside that folder rest of the files are located. Hence unzip jetty-hightide-7.4.5.v20110725.zip -d /opt/jetty/ is useless because it will create a directory /opt/jetty/jetty-hightide-7.4.5.v20110725/* whereas what I really want is /opt/jetty/*. Hence I am looking for a local copy/move resource in Chef.

like image 618
slayedbylucifer Avatar asked Oct 07 '13 09:10

slayedbylucifer


People also ask

How do you copy files to a different location?

Right-click the file or folder you want, and from the menu that displays click Move or Copy. The Move or Copy window opens. Scroll down if necessary to find the destination folder you want. If you need to, click on any folder you see to access its subfolders.

How do I copy and move a file in Linux?

You have to use the cp command. cp is shorthand for copy. The syntax is simple, too. Use cp followed by the file you want to copy and the destination where you want it moved.

How do I copy multiple folders from one directory to another?

Once the files are visible, press Ctrl-A to select all of them, then drag and drop them to the right location. (If you want to copy the files to another folder on the same drive, remember to hold down Ctrl while you drag and drop; see The many ways to copy, move, or delete multiple files for details.)


1 Answers

How to copy single file

First way

I use file statement to copy file (compile-time check)

file "/etc/init.d/someService" do   owner 'root'   group 'root'   mode 0755   content ::File.open("/home/someService").read   action :create end 

here :

  • "/etc/init.d/someService" - target file,
  • "/home/someService" - source file

Also you can wrap ::File.open("/home/someService").read in lazy block

... lazy { ::File.open("/home/someService").read } ... 

Second way

User remote_file statement (run-time check)

remote_file "Copy service file" do    path "/etc/init.d/someService"    source "file:///home/someService"   owner 'root'   group 'root'   mode 0755 end 

Third way

Also you can use shell/batch

For-each directory

Dir[ "/some/directory/resources/**/*" ].each do |curr_path|   file "/some/target/dir/#{Pathname.new(curr_path).basename}" do     owner 'root'     group 'root'     mode 0755     content lazy { IO.read(curr_path, mode: 'rb').read }     action :create   end if File.file?(curr_path)   directory "/some/target/dir/#{File.dirname(curr_path)}" do     path curr_path     owner 'root'     group 'root'     mode 0755     action :create   end if File.directory?(curr_path) end 

This is just idea, because sub-paths in this example is not handled correctly.

like image 195
CAMOBAP Avatar answered Sep 18 '22 15:09

CAMOBAP