Use template or cookbook_file resources to put something in a user's home directory?


#1

Is there any way to use the template and cookbook_file resources to put files in or under a user’s home directory without hardcoding the directory as “/home/username”?

Is there some way to get the chef-client to interpret ~username like the shell does?


Stephen Corbesero, Performance Engineer
Synchronoss - Mobile Innovation for a Connected World
stephen.corbesero@synchronoss.com | www.synchronoss.com


#2

On Tue, Jan 7, 2014 at 11:08 AM, Stephen Corbesero
Stephen.Corbesero@synchronoss.com wrote:

Is there any way to use the template and cookbook_file resources to put
files in or under a user’s home directory without hardcoding the directory
as “/home/username”?

Is there some way to get the chef-client to interpret ~username like the
shell does?

I think File.expand_path should do it?

irb(main):002:0> File.expand_path(’~juliandunn’)
=> “/Users/juliandunn”

  • Julian


[ Julian C. Dunn jdunn@aquezada.com * Sorry, I’m ]
[ WWW: http://www.aquezada.com/staff/julian * only Web 1.0 ]
[ gopher://sdf.org/1/users/keymaker/ * compliant! ]
[ PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9 ]


#3

Dir.home will give you the current users home directory.

Dir.home(user) will give you the home directory of the user specified

On 1/7/14 8:24 AM, “Julian C. Dunn” jdunn@aquezada.com wrote:

On Tue, Jan 7, 2014 at 11:08 AM, Stephen Corbesero
Stephen.Corbesero@synchronoss.com wrote:

Is there any way to use the template and cookbook_file resources to put
files in or under a user¹s home directory without hardcoding the
directory
as ³/home/username²?

Is there some way to get the chef-client to interpret ~username like the
shell does?

I think File.expand_path should do it?

irb(main):002:0> File.expand_path(’~juliandunn’)
=> “/Users/juliandunn”

  • Julian


[ Julian C. Dunn jdunn@aquezada.com * Sorry, I’m ]
[ WWW: http://www.aquezada.com/staff/julian * only Web 1.0 ]
[ gopher://sdf.org/1/users/keymaker/ * compliant! ]
[ PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9 ]


#4

Can I put that in the name of a resource?

For example,

cookbook_file “#{Dir.home(someuser)}/xyzzy” do

End


Stephen Corbesero, Performance Engineer

-----Original Message-----
From: Moser, Kevin [mailto:Kevin.Moser@nordstrom.com]
Sent: Tuesday, January 07, 2014 11:27 AM
To: chef@lists.opscode.com
Subject: [chef] Re: Re: Use template or cookbook_file resources to put something in a user’s home directory?

Dir.home will give you the current users home directory.

Dir.home(user) will give you the home directory of the user specified

On 1/7/14 8:24 AM, “Julian C. Dunn” jdunn@aquezada.com wrote:

On Tue, Jan 7, 2014 at 11:08 AM, Stephen Corbesero
Stephen.Corbesero@synchronoss.com wrote:

Is there any way to use the template and cookbook_file resources to
put files in or under a user¹s home directory without hardcoding the
directory as ³/home/username²?

Is there some way to get the chef-client to interpret ~username like
the shell does?

I think File.expand_path should do it?

irb(main):002:0> File.expand_path(’~juliandunn’) => “/Users/juliandunn”

  • Julian


[ Julian C. Dunn jdunn@aquezada.com * Sorry, I’m ]
[ WWW: http://www.aquezada.com/staff/julian * only Web 1.0 ]
[ gopher://sdf.org/1/users/keymaker/ * compliant! ]
[ PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9 ]


#5

Yup, you should be able to, assuming at the time of compile that the user
exists. As this code would get executed at compile time.

On 1/7/14 8:32 AM, “Stephen Corbesero” Stephen.Corbesero@synchronoss.com
wrote:

Can I put that in the name of a resource?

For example,

cookbook_file “#{Dir.home(someuser)}/xyzzy” do

End


Stephen Corbesero, Performance Engineer

-----Original Message-----
From: Moser, Kevin [mailto:Kevin.Moser@nordstrom.com]
Sent: Tuesday, January 07, 2014 11:27 AM
To: chef@lists.opscode.com
Subject: [chef] Re: Re: Use template or cookbook_file resources to put
something in a user’s home directory?

Dir.home will give you the current users home directory.

Dir.home(user) will give you the home directory of the user specified

On 1/7/14 8:24 AM, “Julian C. Dunn” jdunn@aquezada.com wrote:

On Tue, Jan 7, 2014 at 11:08 AM, Stephen Corbesero
Stephen.Corbesero@synchronoss.com wrote:

Is there any way to use the template and cookbook_file resources to
put files in or under a user¹s home directory without hardcoding the
directory as ³/home/username²?

Is there some way to get the chef-client to interpret ~username like
the shell does?

I think File.expand_path should do it?

irb(main):002:0> File.expand_path(’~juliandunn’) => “/Users/juliandunn”

  • Julian


[ Julian C. Dunn jdunn@aquezada.com * Sorry, I’m ]
[ WWW: http://www.aquezada.com/staff/julian * only Web 1.0 ]
[ gopher://sdf.org/1/users/keymaker/ * compliant! ]
[ PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9 ]


#6

It worked, but I wonder if compile time is too soon.

If this is part of the first run of a chef-client, that user would not exist at the beginning of the run.

Do all the recipes get compiled before any of them start executing?


Stephen Corbesero, Performance Engineer

-----Original Message-----
From: Moser, Kevin [mailto:Kevin.Moser@nordstrom.com]
Sent: Tuesday, January 07, 2014 11:35 AM
To: chef@lists.opscode.com
Subject: [chef] Re: RE: Re: Re: Use template or cookbook_file resources to put something in a user’s home directory?

Yup, you should be able to, assuming at the time of compile that the user exists. As this code would get executed at compile time.

On 1/7/14 8:32 AM, “Stephen Corbesero” Stephen.Corbesero@synchronoss.com
wrote:

Can I put that in the name of a resource?

For example,

cookbook_file “#{Dir.home(someuser)}/xyzzy” do

End


Stephen Corbesero, Performance Engineer

-----Original Message-----
From: Moser, Kevin [mailto:Kevin.Moser@nordstrom.com]
Sent: Tuesday, January 07, 2014 11:27 AM
To: chef@lists.opscode.com
Subject: [chef] Re: Re: Use template or cookbook_file resources to put
something in a user’s home directory?

Dir.home will give you the current users home directory.

Dir.home(user) will give you the home directory of the user specified

On 1/7/14 8:24 AM, “Julian C. Dunn” jdunn@aquezada.com wrote:

On Tue, Jan 7, 2014 at 11:08 AM, Stephen Corbesero
Stephen.Corbesero@synchronoss.com wrote:

Is there any way to use the template and cookbook_file resources to
put files in or under a user¹s home directory without hardcoding the
directory as ³/home/username²?

Is there some way to get the chef-client to interpret ~username like
the shell does?

I think File.expand_path should do it?

irb(main):002:0> File.expand_path(’~juliandunn’) => “/Users/juliandunn”

  • Julian


[ Julian C. Dunn jdunn@aquezada.com * Sorry, I’m ]
[ WWW: http://www.aquezada.com/staff/julian * only Web 1.0 ]
[ gopher://sdf.org/1/users/keymaker/ * compliant! ]
[ PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9 ]


#7

When you are creating that user, are you setting the home directory on the
"user" resource? If so, is that coming out of an attribute? You would
probably just want to reference that attribute then since on first run you
know that would be there.

If not, then yeah, you would have to do some sort of notified (by the user
resource) ruby_block to update/create your cookbook_file resource on the
fly.

The resource collection is fully populated (that means all recipes in the
run list get compiled) before any convergence happens.

Another thing you could do is on your user resource force it to run at
compile so that you know it runs before the rest of your compile. This
just means that if your compile fails after it then the user already got
created so if that is problematic you wouldn’t want to do that.

user “blah” do
end.run_action(:create)

On 1/7/14 9:52 AM, “Stephen Corbesero” Stephen.Corbesero@synchronoss.com
wrote:

It worked, but I wonder if compile time is too soon.

If this is part of the first run of a chef-client, that user would not
exist at the beginning of the run.

Do all the recipes get compiled before any of them start executing?


Stephen Corbesero, Performance Engineer

-----Original Message-----
From: Moser, Kevin [mailto:Kevin.Moser@nordstrom.com]
Sent: Tuesday, January 07, 2014 11:35 AM
To: chef@lists.opscode.com
Subject: [chef] Re: RE: Re: Re: Use template or cookbook_file resources
to put something in a user’s home directory?

Yup, you should be able to, assuming at the time of compile that the user
exists. As this code would get executed at compile time.

On 1/7/14 8:32 AM, “Stephen Corbesero” Stephen.Corbesero@synchronoss.com
wrote:

Can I put that in the name of a resource?

For example,

cookbook_file “#{Dir.home(someuser)}/xyzzy” do

End


Stephen Corbesero, Performance Engineer

-----Original Message-----
From: Moser, Kevin [mailto:Kevin.Moser@nordstrom.com]
Sent: Tuesday, January 07, 2014 11:27 AM
To: chef@lists.opscode.com
Subject: [chef] Re: Re: Use template or cookbook_file resources to put
something in a user’s home directory?

Dir.home will give you the current users home directory.

Dir.home(user) will give you the home directory of the user specified

On 1/7/14 8:24 AM, “Julian C. Dunn” jdunn@aquezada.com wrote:

On Tue, Jan 7, 2014 at 11:08 AM, Stephen Corbesero
Stephen.Corbesero@synchronoss.com wrote:

Is there any way to use the template and cookbook_file resources to
put files in or under a user¹s home directory without hardcoding the
directory as ³/home/username²?

Is there some way to get the chef-client to interpret ~username like
the shell does?

I think File.expand_path should do it?

irb(main):002:0> File.expand_path(’~juliandunn’) => “/Users/juliandunn”

  • Julian


[ Julian C. Dunn jdunn@aquezada.com * Sorry, I’m ]
[ WWW: http://www.aquezada.com/staff/julian * only Web 1.0 ]
[ gopher://sdf.org/1/users/keymaker/ * compliant! ]
[ PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9 ]