Tim Mooney
2013-Jun-28 21:03 UTC
[Puppet Users] passing an environment variable to a command in a provider
All- We have some custom types & providers related to mysql (mysql_user, mysql_grant, mysql_db) written by an admin that''s no longer here. The provider just uses the mysql command to run various commands, e.g: Puppet::Type.type(:mysql_user).provide(:mysql) do desc "Provider for a mysql user" optional_commands :mysql => ''mysql'' mk_resource_methods def create debug "mysql_user create" @property_hash[:ensure] = :present mysql(''mysql'',''-e'',"create user ''%s'' identified by ''%s'';" % [@resource[:name].sub("@","''@''"),@resource[:password]]) end def flush debug "in flush" mysql(''mysql'',''-e'',''flush privileges;'') @property_hash.clear end # other stuff elided end For this particular provider/type to work, though, it requires that you actually have root''s environment, because it relies on reading some config from /root/.my.cnf. That means that on most of our hosts, doing sudo puppet agent --test works fine, but on hosts where we use our mysql module with the custom types and provider, we can''t do that. We instead have to sudo su - puppet agent --test to make certain we''ve picked up root''s environment, specifically HOME. What I would like to do is augment the provider so that the mysql command is always invoked with the environment augmented with HOME=/root or (even better) HOME=roots_home_from_facter. I''m not certain how to pass an environment variable to an external command that''s invoked as part of a puppet provider, though, and the searches I''ve done so far haven''t turned up anything helpful. Can anyone that''s familiar with writing types and providers shed some light on what I should be doing to augment this? I know this is as much ruby ignorance as puppet ignorance, but I have to believe that there are people here that can point me in the right direction. Thanks, Tim -- Tim Mooney Tim.Mooney@ndsu.edu Enterprise Computing & Infrastructure 701-231-1076 (Voice) Room 242-J6, IACC Building 701-231-8541 (Fax) North Dakota State University, Fargo, ND 58105-5164 -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Nan Liu
2013-Jun-28 22:26 UTC
Re: [Puppet Users] passing an environment variable to a command in a provider
On Fri, Jun 28, 2013 at 2:03 PM, Tim Mooney <Tim.Mooney@ndsu.edu> wrote:> We have some custom types & providers related to mysql (mysql_user, > mysql_grant, mysql_db) written by an admin that''s no longer here. The > provider just uses the mysql command to run various commands, e.g: > > Puppet::Type.type(:mysql_user)**.provide(:mysql) do > desc "Provider for a mysql user" > > optional_commands :mysql => ''mysql'' > > mk_resource_methods > > def create > debug "mysql_user create" > @property_hash[:ensure] = :present > mysql(''mysql'',''-e'',"create user ''%s'' identified by ''%s'';" % > [@resource[:name].sub("@","''@''**"),@resource[:password]]) > end > > def flush > debug "in flush" > mysql(''mysql'',''-e'',''flush privileges;'') > @property_hash.clear > end > > # other stuff elided > end > > For this particular provider/type to work, though, it requires that > you actually have root''s environment, because it relies on reading some > config from /root/.my.cnf. > > That means that on most of our hosts, doing > > sudo puppet agent --test > > works fine, but on hosts where we use our mysql module with the custom > types and provider, we can''t do that. We instead have to > > sudo su - > puppet agent --test > > to make certain we''ve picked up root''s environment, specifically HOME. > > What I would like to do is augment the provider so that the mysql command > is always invoked with the environment augmented with HOME=/root or > (even better) HOME=roots_home_from_facter. > > I''m not certain how to pass an environment variable to an external command > that''s invoked as part of a puppet provider, though, and the searches I''ve > done so far haven''t turned up anything helpful. > > Can anyone that''s familiar with writing types and providers shed some > light on what I should be doing to augment this? I know this is as much > ruby ignorance as puppet ignorance, but I have to believe that there are > people here that can point me in the right direction. >In Puppet 3, home environment can be passed something like: has_command(:brew, ''brew'') do environment({ ''HOME'' => ENV[''HOME''] }) end HTH, Nan -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Wil Cooley
2013-Jun-30 03:37 UTC
Re: [Puppet Users] passing an environment variable to a command in a provider
On Jun 28, 2013 2:06 PM, "Tim Mooney" <Tim.Mooney@ndsu.edu> wrote:> > works fine, but on hosts where we use our mysql module with the custom > types and provider, we can''t do that. We instead have to > > sudo su - > puppet agent --testAn alternative and trivia in addition to what Nan said: You can use sudo -H to set $HOME, or sudo -i to get a full login environment like ''su -'' gives. There are also sudoers config params that can make at least the former default, and probably the latter too. And just for kicks, you can get a shell with: sudo -s Additionally, you can explicitly pass the path of the `my.cnf`, rather than relying on $HOME, which is what I''d do. Then you could make it a resource parameter and gain some flexibility. Sorry I''m a little short on details; I''m on my phone and don''t have the man pages handy. Wil -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Tim Mooney
2013-Jul-01 21:09 UTC
Re: [Puppet Users] passing an environment variable to a command in a provider
In regard to: Re: [Puppet Users] passing an environment variable to a...:> On Fri, Jun 28, 2013 at 2:03 PM, Tim Mooney <Tim.Mooney@ndsu.edu> wrote: > >> We have some custom types & providers related to mysql (mysql_user, >> mysql_grant, mysql_db) written by an admin that''s no longer here. The >> provider just uses the mysql command to run various commands, e.g: >> >> Puppet::Type.type(:mysql_user)**.provide(:mysql) do >> desc "Provider for a mysql user" >> >> optional_commands :mysql => ''mysql'' >> >> mk_resource_methods >> >> def create >> debug "mysql_user create" >> @property_hash[:ensure] = :present >> mysql(''mysql'',''-e'',"create user ''%s'' identified by ''%s'';" % >> [@resource[:name].sub("@","''@''**"),@resource[:password]]) >> end >> >> def flush >> debug "in flush" >> mysql(''mysql'',''-e'',''flush privileges;'') >> @property_hash.clear >> end >> >> # other stuff elided >> end >> >> For this particular provider/type to work, though, it requires that >> you actually have root''s environment, because it relies on reading some >> config from /root/.my.cnf. >> >> That means that on most of our hosts, doing >> >> sudo puppet agent --test >> >> works fine, but on hosts where we use our mysql module with the custom >> types and provider, we can''t do that. We instead have to >> >> sudo su - >> puppet agent --test >> >> to make certain we''ve picked up root''s environment, specifically HOME. >> >> What I would like to do is augment the provider so that the mysql command >> is always invoked with the environment augmented with HOME=/root or >> (even better) HOME=roots_home_from_facter. >> > In Puppet 3, home environment can be passed something like: > > has_command(:brew, ''brew'') do > environment({ ''HOME'' => ENV[''HOME''] }) > endThanks Nan. Even though I know better, I forgot to supply one bit of info -- we''re still running puppet 2.7.x and I''m not certain when we''re going to move to 3.2.x. Is there a puppet 2.7.x equivalent? Tim -- Tim Mooney Tim.Mooney@ndsu.edu Enterprise Computing & Infrastructure 701-231-1076 (Voice) Room 242-J6, IACC Building 701-231-8541 (Fax) North Dakota State University, Fargo, ND 58105-5164 -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Tim Mooney
2013-Jul-01 22:45 UTC
Re: [Puppet Users] passing an environment variable to a command in a provider
In regard to: Re: [Puppet Users] passing an environment variable to a...:> On Jun 28, 2013 2:06 PM, "Tim Mooney" <Tim.Mooney@ndsu.edu> wrote: >> >> works fine, but on hosts where we use our mysql module with the custom >> types and provider, we can''t do that. We instead have to >> >> sudo su - >> puppet agent --test > > An alternative and trivia in addition to what Nan said: > > You can use > > sudo -H > > to set $HOME, or > > sudo -i > > to get a full login environment like ''su -'' gives. There are also sudoers > config params that can make at least the former default, and probably the > latter too.Thanks Wil. I knew about each of these, but I would rather fix the type/provider to work even with normal sudo usage, rather than requiring people to remember to use one of these. Yes, they could use aliases or shell functions, but I still think it''s a better solution to make type/provider work for any case.> Additionally, you can explicitly pass the path of the `my.cnf`, rather than > relying on $HOME,I''ve thought about this too and I agree that it''s even better. To do it right, though, I would want to make it use the root_home fact. That''s one more thing I don''t know how to do yet, though. It looks like http://docs.puppetlabs.com/guides/provider_development.html has a section on using facts in providers, so I''ll do some experimenting and see if I can''t muddle through. Tim -- Tim Mooney Tim.Mooney@ndsu.edu Enterprise Computing & Infrastructure 701-231-1076 (Voice) Room 242-J6, IACC Building 701-231-8541 (Fax) North Dakota State University, Fargo, ND 58105-5164 -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.