Puppet command execution through ruby

19 views
Skip to first unread message

ggun

unread,
May 24, 2017, 10:02:58 PM5/24/17
to Puppet Developers, Gaurav Gundal
Hi Experts,

Need your kind help in command to execute in the puppet provider. Below is the code snippet of the puppet provider code.
If I run the code with changes as below the code runs without any issue

 

cmd="yes | #{create_ssm} create --fs '#{@resource[:fs_type]}' -p '#{@resource[:volume_group]}' -n '#{@resource[:logical_volume]}' '#{@resource[:mount_point]}'"

system(cmd)


But when  I run code as below
    system('yes | ssm(*args)')  # I am building args based on input and ssm is the commands as shown in below code snippet
puppet run fails with error as
Error
sh: -c: line 0: syntax error near unexpected token `*args'
sh: -c: line 0: `yes | ssm(*args)'

Please help can I fix above run.


Code Snippets 

  commands :ssm                       => '/usr/bin/ssm',
  
  def create
    puts "Creating FileSystem"
    args = ['create']
    if @resource[:fs_type]
      args.push('--fs', @resource[:fs_type])
    else
      args.push('--fs', 'xfs')
    end
    if @resource[:volume_group]
      args.push('-p', @resource[:volume_group])
    else
      puts "Default volume group will be creted and user needs to manage it"
    end
    if @resource[:logical_volume]
      args.push('-n', @resource[:logical_volume])
    else
      puts "Default logical volume will be creted and user needs to manage it"
    end
    if @resource[:device]
      args << @resource[:device]    
    end
    if @resource[:mount_point]
      FileUtils.mkdir_p(@resource[:mount_point]) unless File.exists?(@resource[:mount_point])
      args << @resource[:mount_point]    
    end
    p args     
 #   p cmd
    system('yes | ssm(*args)')
  rescue Puppet::ExecutionFailure => detail
    raise Puppet::Error, "Could not create filesystem, volume group,and logical group. Due to error:(#{detail.message})" 
 
  end

Shawn Ferry

unread,
May 25, 2017, 12:17:09 PM5/25/17
to puppe...@googlegroups.com, Gaurav Gundal
On May 24, 2017, at 10:02 PM, ggun <gaurav...@gmail.com> wrote:

Hi Experts,

Need your kind help in command to execute in the puppet provider. Below is the code snippet of the puppet provider code.
If I run the code with changes as below the code runs without any issue
 
cmd="yes | #{create_ssm} create --fs '#{@resource[:fs_type]}' -p '#{@resource[:volume_group]}' -n '#{@resource[:logical_volume]}' '#{@resource[:mount_point]}'"
system(cmd)

There are any number of bits of software that call themselves ssm. Assuming this is system storage manager why don’t you pass -f to make it non-interactive?

commands => :ssm => ‘/usr/bin/ssm’
args=%W[-f create --fs @resource[:fs_type] -p @resource[:volume_group] -n @resource[:logical_volume] @resource[:mount_point]]
ssm(*args)


If that isn’t an option you could pass a file containing ‘yes’ 
cmd=%W[ssm create --fs @resource[:fs_type] -p @resource[:volume_group] -n @resource[:logical_volume] @resource[:mount_point]]
Puppet::Util::Execution.execute(cmd, :stdinfile => ‘/path/to/yes-file’)


You really shouldn’t do that. Your invocation is off.  The system call is not using you definition
of ssm above it’s just finding it on the path and you need to expand your args system takes a string.

args=%w(this is s a test)
[5] pry(main)> system("yes | echo #{args.join(' ')}")
this is s a test

  rescue Puppet::ExecutionFailure => detail
    raise Puppet::Error, "Could not create filesystem, volume group,and logical group. Due to error:(#{detail.message})" 
 
  end

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/6b4548f5-df77-45b8-b6de-849c6ba6144d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ggun

unread,
May 25, 2017, 1:19:30 PM5/25/17
to Puppet Developers, gaurav...@glic.com, shawn...@oracle.com
Thank you, I will try the first option. I remember doing that but it did not avoid the warnings. But I will retry.

There is one last help I am looking for. if you have time to look into below issue.


I have just added new parameter in the type and used the parameter command and also mention in the init.pp ssm module call, but the module fails with error 
Error: no parameter named 'command' at /etc/puppetlabs/code/environments/production/modules/xxx/manifests/init.pp:67 on Ssm[RHELDiskManager] at /etc/puppetlabs/code/environments/production/modules/xx/manifests/init.pp:67 on node xxx

This issue is happening for any new parameter that I am trying to add. Is there a limit to the parameter that I can use in the type ? I don't think I am overusing them. But don't know why is it failing

Shawn Ferry

unread,
May 25, 2017, 1:44:15 PM5/25/17
to puppe...@googlegroups.com, gaurav...@glic.com
On May 25, 2017, at 1:19 PM, ggun <gaurav...@gmail.com> wrote:

Thank you, I will try the first option. I remember doing that but it did not avoid the warnings. But I will retry.

There is one last help I am looking for. if you have time to look into below issue.


I have just added new parameter in the type and used the parameter command and also mention in the init.pp ssm module call, but the module fails with error 
Error: no parameter named 'command' at /etc/puppetlabs/code/environments/production/modules/xxx/manifests/init.pp:67 on Ssm[RHELDiskManager] at /etc/puppetlabs/code/environments/production/modules/xx/manifests/init.pp:67 on node xxx

This issue is happening for any new parameter that I am trying to add. Is there a limit to the parameter that I can use in the type ? I don't think I am overusing them. But don't know why is it failing

At a glance I don’t see anything that stands out for why it isn’t working for you



I think in general you shouldn’t call it a parameter command because it may be confusing. I think it should work but maybe ‘operation’ instead. 
You will also need to either add some generic arguments option to be able to form complete commands or create a bunch of optional parameters that apply only to the different operations. 

I don’t think this the correct approach. 


You want to create multiple types e.g. ssm_create ssm_resize maybe  ssm_snapshot ssm_remove since they all have separate arguments and logic. 
Trying to overload all of it into a single type is going to be harder the further you go.



ggun

unread,
May 25, 2017, 2:52:38 PM5/25/17
to Puppet Developers, gaurav...@glic.com, shawn...@oracle.com

Yes, I was planning to have a separate type for other ssm commands. But I got stuck with the current problem as I am not able to add any parameter beside existing.

Richard Clamp

unread,
May 25, 2017, 2:56:48 PM5/25/17
to puppe...@googlegroups.com, gaurav...@glic.com, shawn...@oracle.com
It sounds like you're developing with a master. If so you're going to need to rerun the agent on that node and restart the master so that the type is reloaded and the additional parameters are recognised.

Generally though you're better off developing with `puppet apply` so the type code has a shorter lifespan.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/824cbd6e-b63a-4012-b140-b00ec016d488%40googlegroups.com.

ggun

unread,
May 25, 2017, 3:10:06 PM5/25/17
to Puppet Developers, gaurav...@glic.com, shawn...@oracle.com
I am developing the module on the agent and executing the module as puppet apply -e 'include xxx' -- so I am not using master and but still I am ending up with same issue.
I have restarted the server to make sure there is not caching that is causing this issue.


On Thursday, May 25, 2017 at 2:56:48 PM UTC-4, Richard Clamp wrote:
It sounds like you're developing with a master.  If so you're going to need to rerun the agent on that node and restart the master so that the type is reloaded and the additional parameters are recognised.

Generally though you're better off developing with `puppet apply` so the type code has a shorter lifespan.


> On 25 May 2017, at 19:52, ggun <gaurav...@gmail.com> wrote:
>
> evelop

Richard Clamp

unread,
May 25, 2017, 3:23:18 PM5/25/17
to puppe...@googlegroups.com
This is missing something to explicitly point apply at the type under development, so you could still be picking up a pluginsynced definition.

Try:


puppet apply --modulepath=directory_above_module -e 'include xxx'

Or

export RUBYLIB=path_to_module/lib
puppet apply -e 'include xxx'



> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/358f5565-8ba3-4294-b8ce-e57bb1a31bb4%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages