0

Well, let's start by saying I'm a chef noob and I am trying to hash this code out.

I am in a full mac shop. I am using Chef to automate system wide changes. As I'm new, I'm rolling it out onto our Mac AV systems.

Basically, there is a folder on a file server that has MAC SCREEN SAVERS directory. I copy the server directory locally to the MAC OS X /User/user_name/Pictures directory.

So, this is what I got in chef:

    local_folder_modified = File.mtime("~/Pictures/SCREEN SAVER NEW MACS")
    server_folder_modified = File.mtime("/Volumes/SERVER/SCREEN\ SAVER\ NEW\ MACS/")


    if server_folder_modified != local_folder_modified
              # file has changed
    then
            require 'fileutils'

                    FileUtils.cd('server_folder_modified') do
                    FileUtils.rm('local_folder_modified/*')
                    FileUtils.cp_r './*', 'local_folder_modified'
            Else
                    end
    end

Anyways, I can't figure how to set the '~' to be the running user of this recipe. So, if Comp_A has user Jim_Beam and Comp_B has user Jack_Daniels, I don't want to set the code to be: ENV[HOME] = /user/jimbeam As it won't work on Jack_Daniels. Right?

I've read that file.expand will work, or ENV, but I am really unsure what will be the best code to say "hey, I want the current user that will need this screen saver - so set the environment as a variable so it works across different nodes".

Anyways, thanks for your help. I hope I am making sense!

E.Z.
  • 139
  • 2
  • 14
  • ::File.expand_path("~/Pictures/SCREEN SAVER NEW MACS") will expand the path for you. As a side note, I cannot see any chef dsl in the example you give. – punkle Apr 08 '14 at 23:58

2 Answers2

1

Yes, use File.expand. It will expand the tilde ~ to be the the home directory of the user running this cookbook. Alternatively, you could do:

"#{ENV['HOME']}/Pictures/SCREEN SAVER NEW MACS"

Like the previous comment, this is not chef DSL or ruby code. What is the source of this code or is it just pseudo-code to ask the question?

Also, chef-client is not frequently run as multiple users in a chef server deployment. It's usually run in a sudo context. So maybe you are referring to a --local-mode or chef-zero application?

jshort
  • 1,006
  • 8
  • 23
  • Awesome- thank you! Yeah, I'm just wrapping my head around Chef now. – E.Z. Apr 14 '14 at 17:33
  • Oops - hit enter too quick. This is pretty much the code that's in a default.rb in a cookbook. I don't know if that answers your question (sorry if it doesn't). BTW - I did NOT know about the chef-client user thing as that is SUPER helpful! – E.Z. Apr 14 '14 at 17:34
0

You may want to use file stat of /dev/console to get the current user. Depending how you are running the chef-client Env[‘Home’] might not give you want you want. Try this:

console_user = Etc.getpwuid(::File.stat("/dev/console").uid).name
home_dir = ::File.join(‘Users’, console_user)

You can see that the chef launchd provider uses this method to determine the console user

Also there is a much simpler way to do what you are trying to accomplish with the remote_file resource. Try this:

console_user = Etc.getpwuid(::File.stat("/dev/console").uid).name
home_dir = ::File.join(‘Users’, console_user)
pics = ::File.join("#{home_dir}/Pictures/")
server_base_url = "https://PLACE_WHERE_STORE/Wallpapers")
[
  ‘Pic1’,
  ‘Pic2’,
].each do |pic|
  remote_file ::File.join(pics, pic) do
    source “#{server_base_url}/#{pic}”
    owner console_user
    group console_user
    mode '0755'
    action :create
  end
end

For added security you should also include checksum

Mike Dodge
  • 31
  • 3