Have two resources and append one to another in the Chef remote_file - ruby

I would like to copy http://seapower/spring.txt and http://seapower/has_sprung.txt and append second one to the first one in a new file named src_filepath.txt:
remote_file 'src_filepath.txt' do
source 'http://seapower/spring.txt', 'http://seapower/has_sprung.txt'
checksum node['nginx']['foo123']['checksum']
owner 'root'
group 'root'
mode '0755'
end
It doesn't work and just copy the first file to src_filepath.txt

Something like this is probably a good place to start and then tweak however you like:
cache1 = "#{Chef::Config[:file_cache_path]}/content1"
cache2 = "#{Chef::Config[:file_cache_path]}/content2"
# this will not redownload if cache1 exists and has not been updated
remote_file cache1 do
source "http://source.url/content1"
end
# this will not redownload if cache1 exists and has not been updated
remote_file cache2 do
source "http://source.url/content2"
end
# this will not update the file if the contents has not changed
file "/my/combined/file" do
content lazy { IO.read(cache1) + IO.read(cache2) }
end

This is not something Chef supports directly. You could use multiple remote_file resources and either a ruby_block or execute plus cat to implement the concat.

remote_file does not support concatenation, so you would not be able to implement this using that resource directly, however you could piece together the desired result using the file resource and Net::HTTP like so:
file_path = '/path/to/your_whole_file'
unless File.exist?(file_path) &&
Digest::SHA256.hexdigest(File.read(file_path)) == 'your_file_checksum'
file file_path do
content(
Net::HTTP.get(URI('http://source.url/content1')) +
Net::HTTP.get(URI('http://source.url/content2'))
)
owner 'root'
group 'root'
mode '0755'
end
end
The reason for the Digest::SHA256 call at the beginning is to prevent Chef from trying to download both files during every Chef run. Note that you may have to require the net/http and digest gems at the top of your recipe for this to work.
Also, because it's against best practices to put Ruby code directly into your recipes, you may want to wrap the above code in a simple custom resource.

Related

How to do an "unless" conditional when changing permissions in a ruby_block in chef?

In chef I Have a ruby_block where I am changing permissions and ownership of a directory. How can I do a check where the permissions are only changed if they have not already been changed by the " FileUtils.chown" statement? I need to do this within the ruby_block if possible because i am ganna have other code in the ruby block. What would my "unless" statement be? Here is my code:
ruby_block 'exe' do
block do
FileUtils.chmod 0755, '/make/news'
FileUtils.chown('root', 'root', '/make/news')
end
end
The correct way to do this is to use Chef's file resource:
file '/make/news' do
mode 0755
owner 'root'
group 'root'
end
You're going down the road of trying to re-write the file resource which is not a good idea.
Using the Chef Resource's not_if Guard
Chef resources share a number of common functions. The ruby_block resource supports the not_if property as a conditional guard. The general format is:
ruby_block 'custom chmod' do
block do
#
end
not_if { true }
end
So, you could program your logic this way, but it will eventually bite you badly. Chef often works better if you use a file or directory resource declaratively using a separate block to manage permissions, and then (if necessary) chain it with a notification from some other block that needs a given permission set. For example:
directory '/make/news' do
mode '0755'
owner 'root'
group 'root'
action :nothing
end
ruby_block 'do something with news' do
block do
#
end
only_if { true }
notifies :create, 'directory[/make/news]', :before
end
That said, the goal of configuration management is to continuously converge, so I'd strongly question whether creating this interdependency between resource blocks is truly necessary in the first place. If possible, just converge your directory permissions every time to enforce them. While this may create a sequencing dependency within your recipe, a more declarative approach often simplifies cookbook and recipe debugging in the long run. Your individual mileage may vary.

Keeping files updated with a Chef recipe

The challenge prompt is above, and my latest attempt is below. The directories and files are created as expected, and the read-out after executing chef-apply multipleCopies.rb tells me the files are linked, but when I update any one of the files, the others do not follow suit. Any ideas? Here is my code:
for x in 1..3
directory "multipleCopy#{x}" do
mode '0755'
action :create
end
end
file "multipleCopy1/secret.txt" do
mode '0755'
action :create
end
for x in 2..3
link "multipleCopy#{x}/secret.txt" do
to "multipleCopy1/secret.txt"
link_type :hard
subscribes :reload, "multipleCopy1/secret.txt", :immediately
end
end
Note: For less headache, I am testing the recipe locally before uploading to the ubuntu server referenced in the prompt, which is why my file paths are different and why I have not yet included the ownership properties.
So a file hard link doesn't seem to be what the question is going for (though I would say your solution is maybe better since this is really not what Chef is for, more on that later). Instead they seem to want you to have three actually different files, but sync the contents.
So first the easy parts, creating the directories and the empty initial files. It's rare to see those for loops used in Ruby code, though it is syntactically valid:
3.times do |n|
directory "/var/save/multipleCopy#{n+1}" do
owner "ubuntu"
group "root"
mode "755"
end
file "/var/save/multipleCopy#{n+1}/secret.txt" do
owner "root
group "root"
mode "755"
end
end
But that doesn't implement the hard part of sync'ing the files. For that we need to first analyze the mtimes on the files and use the most recent as the file content to set.
latest_file = 3.times.sort_by { |n| ::File.mtime("/var/save/multipleCopy#{n+1}/secret.txt") rescue 0 }
latest_content = ::File.read("/var/save/multipleCopy#{latest_file+1}/secret.txt") rescue nil
and then in the file resource:
file "/var/save/multipleCopy#{n+1}/secret.txt" do
owner "root
group "root"
mode "755"
content latest_content
end
As for this not being a good use of Chef: Chef is about writing code which asserts the desired state of the machine. In the case of files like this, rather than doing this kind of funky stuff to check if a file has been edited, you would just say that Chef owns the file content for all three and if you want to update it, you do it via your cookbook (and then usually use a template or cookbook_file resource).

copy all files from ftp folder using Chef

remote_file block copies only one specific file.
Is there any possibility in Chef to copy all files from specific folder on ftp?
my current code is quite weird as for me:
require 'net/ftp'
ftp = Net::FTP::new("server")
ftp.login("user", "password")
ftp.chdir("/folder")
fileList = ftp.nlst('*.jar')
fileList.each do |file|
remote_file "C:\\Temp\\" + file do
source "ftp://user:password#server/folder/" + file
action :create_if_missing
end
end
ftp.close
If your solution works, why not wrap it in an LWRP? They are quite easy to create, and would tuck away the implementation in its own file. This is what I would do.
See: http://docs.opscode.com/chef/lwrps_custom.html
And for a real-life - easy to understand - example, see:
https://github.com/opscode-cookbooks/ssh_known_hosts/blob/master/providers/entry.rb
https://github.com/opscode-cookbooks/ssh_known_hosts/blob/master/resources/entry.rb

Removing boilerplate attributes from resources

In chef, each resource is defined like this:
directory "/home/akihiro/folder" do
owner "akihiro"
group "akihiro"
mode 0755
end
If this is the only task under akihiro's home directory, that's fine.
Unfortunately, I have to create directories, copy files, and apply templates, all under the same home directory as the owner. Therefore owner "akihiro"; group "akihiro" must be set on every resource, which is very redundant.
If the resource could be written like this,
directory "/home/akihiro/folder" do
as_akihiro
mode 0755
end
where as_akihiro is defined somewhere outside the resource, the recipe would get much clearer.
Is it possible to remove the boilerplate attributes by defining a new method?
You have a few options here.
Rubyish
You can create a Ruby module that defines this method:
module Impersonator
def as(person, perms = '0755')
send(:owner, person)
send(:group, person)
send(:mode, perms)
end
end
And then include this module in the resource:
Chef::Resource.send(:include, Impersonator)
And then use it:
directory '/foo/bar' do
as 'akihiro' # or as 'akihiro', '0644'
end
Chefish
If I understand your use case correct, the preferred way to handle this is with an LWRP (or HWRP). You indicated this process occurs multiple times and wraps core Chef resources. This is a great use case for an LWRP. Essentially you wrap and parameterize all of these resources into a single "wrapper".
# providers/default.rb
action :run do
user new_resource.username do
# ...
end
directory "/home/#{new_resource.username}" do
owner new_resource.username
group new_resource.group
mode new_resource.mode
end
# Other resources, using the `new_resource` object
end
And then in a Chef recipe, you would use this resource (assuming it is named "company_user"):
company_user 'akihiro'
That is possible. What works for me is to open the class that implements the "directory" resource, Chef::Resource::Directory and add a method as_akihiro. To do so, add a library to *your_cookbook*/libraries/as_user_helper.rb
class Chef::Resource::Directory
def as_akihiro()
owner "akihiro"
group "akihiro"
end
and you're done.

Chef 10: How to use remote_file or similar to get a file from a Windows share?

I'm trying to use remote_file to cache a local copy of a large package on a Windows share. How is this done?
I can't get it to work with a drive-letter-based path, a UNC-based path, or a file: URL.
Don't have windows to test, but something like this should work:
require 'fileutils'
remote_path = '...'
local_path = '...'
ruby "cache-#{remote_path}" do
block { FileUtils.copy_file(remote_path, local_path) }
not_if { File.exists?(local_path) }
end
I worked out a trick that I think is pretty neat. I created the following definition (and put it in definitions/default.rb):
define :file_from_network, :action => :create do
myPath = (params[:path] || params[:name])
mySource = params[:source]
if File.exist?(mySource)
file myPath do
action params[:action]
content File.open(mySource) {|io| io.read}
end
else
Chef::Log.error("File #{mySource} not found!")
end
end
Definitions do not work quite the same way resources do, but this was easy to implement and does what I need it to do. The in-memory read makes it impractical for huge files, of course, but it allows Chef to check that the content is different before triggering an action on the file resource.

Resources