Stop multiple services


#1

I have multiple services that I need to stop , my code is as below ,

ruby_block “stopsvc” do
block do
services = File.readlines("/root/svc")
services.each do |svc|
require
Chef::Provider::Service::Init::Redhat
service “svc” do
action [ :stop, :disable]
end
end
end
end

“can’t convert Class into String” is the error I am seeing . Please point what
I need ,


#2

instead of service “svc” it should be service svc , without quote.

why you are wrapping the service resources inside a ruby_block? You can
puts the file reading bits in recipe directly (unless /root/svc is being
populated during the converge time)

On Sun, Sep 14, 2014 at 1:45 AM, rramkarthik@gmail.com wrote:

I have multiple services that I need to stop , my code is as below ,

ruby_block “stopsvc” do
block do
services = File.readlines("/root/svc")
services.each do |svc|
require
Chef::Provider::Service::Init::Redhat
service “svc” do
action [ :stop, :disable]
end
end
end
end

“can’t convert Class into String” is the error I am seeing . Please point
what
I need ,


#3

If it is inside a recipe you don’t have to require the service provider, it will guess it.
If you really want to enforce it, there’s a provider attribute to the service resource.

For the error message, require take a path to a file without the extension, here it should be require “chef/provider/service/init/redhat”

Envoyé à partir de mon smartphone Sony Xperia™

---- rramkarthik@gmail.com a écrit ----

I have multiple services that I need to stop , my code is as below ,

ruby_block “stopsvc” do
block do
services = File.readlines("/root/svc")
services.each do |svc|
require
Chef::Provider::Service::Init::Redhat
service “svc” do
action [ :stop, :disable]
end
end
end
end

“can’t convert Class into String” is the error I am seeing . Please point what
I need ,


#4

Ranjib,

svc without quote also returns the same error . /root/svc is a static file
. I am not sure about any other method for reading a file to a array and
iterating through the array . Let me know how I can do without using
ruby_block,

Thanks,

On Sun, Sep 14, 2014 at 1:49 AM, Ranjib Dey dey.ranjib@gmail.com wrote:

instead of service “svc” it should be service svc , without quote.

why you are wrapping the service resources inside a ruby_block? You can
puts the file reading bits in recipe directly (unless /root/svc is being
populated during the converge time)

On Sun, Sep 14, 2014 at 1:45 AM, rramkarthik@gmail.com wrote:

I have multiple services that I need to stop , my code is as below ,

ruby_block “stopsvc” do
block do
services = File.readlines("/root/svc")
services.each do |svc|
require
Chef::Provider::Service::Init::Redhat
service “svc” do
action [ :stop, :disable]
end
end
end
end

“can’t convert Class into String” is the error I am seeing . Please point
what
I need ,


#5

require “chef/provider/service/init/redhat” returned load error , or Am I
missing something

On Sun, Sep 14, 2014 at 1:52 AM, Tensibai Zhaoying tensibai@iabis.net
wrote:

If it is inside a recipe you don’t have to require the service provider,
it will guess it.
If you really want to enforce it, there’s a provider attribute to the
service resource.

For the error message, require take a path to a file without the
extension, here it should be require “chef/provider/service/init/redhat”

Envoyé à partir de mon smartphone Sony Xperia™

---- rramkarthik@gmail.com a écrit ----

I have multiple services that I need to stop , my code is as below ,

ruby_block “stopsvc” do
block do
services = File.readlines("/root/svc")
services.each do |svc|
require
Chef::Provider::Service::Init::Redhat
service “svc” do
action [ :stop, :disable]
end
end
end
end

“can’t convert Class into String” is the error I am seeing . Please point
what
I need ,


#6

Use the provider attribute if the service resource and your declaration should use the var without quotes or with #{svc}

See http://docs.getchef.com/resource_service.html for the service resource and provider attribute

Envoyé à partir de mon smartphone Sony Xperia™

---- ram karthik a écrit ----

require “chef/provider/service/init/redhat” returned load error , or Am I missing something

On Sun, Sep 14, 2014 at 1:52 AM, Tensibai Zhaoying tensibai@iabis.net wrote:

If it is inside a recipe you don’t have to require the service provider, it will guess it.
If you really want to enforce it, there’s a provider attribute to the service resource.

For the error message, require take a path to a file without the extension, here it should be require “chef/provider/service/init/redhat”

Envoyé à partir de mon smartphone Sony Xperia™

---- rramkarthik@gmail.com a écrit ----

I have multiple services that I need to stop , my code is as below ,

ruby_block “stopsvc” do
block do
services = File.readlines("/root/svc")
services.each do |svc|
require
Chef::Provider::Service::Init::Redhat
service “svc” do
action [ :stop, :disable]
end
end
end
end

“can’t convert Class into String” is the error I am seeing . Please point what
I need ,


#7

Hi Tensibai,

I have tried “svc” , svc , “#{svc}” - nothing worked . Basically the
contents of the file is as below which leaves scope for adding more
services as required ,

cat /root/svc
auditd
vsftpd

On Sun, Sep 14, 2014 at 9:56 AM, Tensibai Zhaoying tensibai@iabis.net
wrote:

Use the provider attribute if the service resource and your declaration
should use the var without quotes or with #{svc}

See http://docs.getchef.com/resource_service.html for the service
resource and provider attribute

Envoyé à partir de mon smartphone Sony Xperia™

---- ram karthik a écrit ----

require “chef/provider/service/init/redhat” returned load error , or Am I
missing something

On Sun, Sep 14, 2014 at 1:52 AM, Tensibai Zhaoying tensibai@iabis.net
wrote:

If it is inside a recipe you don’t have to require the service provider,
it will guess it.
If you really want to enforce it, there’s a provider attribute to the
service resource.

For the error message, require take a path to a file without the
extension, here it should be require “chef/provider/service/init/redhat”

Envoyé à partir de mon smartphone Sony Xperia™

---- rramkarthik@gmail.com a écrit ----

I have multiple services that I need to stop , my code is as below ,

ruby_block “stopsvc” do
block do
services = File.readlines("/root/svc")
services.each do |svc|
require
Chef::Provider::Service::Init::Redhat
service “svc” do
action [ :stop, :disable]
end
end
end
end

“can’t convert Class into String” is the error I am seeing . Please point
what
I need ,


#8

Hey Ram,

So, there are two to three things wrong with your code:

  1. svc is a variable and shouldn’t be wrapped in quotes, as others have
    mentioned.
  2. require takes a string, not a class, which is the error you’re
    getting. This line isn’t necessary at all, though; you can probably just
    remove it. If you need to explicitly specify a provider for your services,
    use the provider attribute as Tensibai suggested.
  3. If your /root/svc file already contains the required contents at the
    start of the chef run, then there’s no need to complicate things by using a
    ruby_block resource. You really only need a ruby_block if some other
    chef resource is going to be dropping off that file and you want to wait to
    read the contents until as late as possible.

I’m pretty sure what you want is something like this:

services = File.readlines("/root/svc")

services.each do |svc|
service svc.chomp do
provider Chef::Provider::Service::Init::Redhat #This may not
be necessary
action [ :stop, :disable]
endend

I added the .chomp
http://www.ruby-doc.org/core-2.1.2/String.html#method-i-chomp because the
results of File.readlines appear to have new-lines on the end of them. I’m
not 100% sure it’s required, but it won’t hurt.

You might want to consider reading over this
https://docs.getchef.com/just_enough_ruby_for_chef.html for a quick
overview of ruby for chef users.

Hope that helps.

Matt Moretti

On Mon, Sep 15, 2014 at 12:31 AM, ram karthik rramkarthik@gmail.com wrote:

Hi Tensibai,

I have tried “svc” , svc , “#{svc}” - nothing worked . Basically the
contents of the file is as below which leaves scope for adding more
services as required ,

cat /root/svc
auditd
vsftpd

On Sun, Sep 14, 2014 at 9:56 AM, Tensibai Zhaoying tensibai@iabis.net
wrote:

Use the provider attribute if the service resource and your declaration
should use the var without quotes or with #{svc}

See http://docs.getchef.com/resource_service.html for the service
resource and provider attribute

Envoyé à partir de mon smartphone Sony Xperia™

---- ram karthik a écrit ----

require “chef/provider/service/init/redhat” returned load error , or Am I
missing something

On Sun, Sep 14, 2014 at 1:52 AM, Tensibai Zhaoying tensibai@iabis.net
wrote:

If it is inside a recipe you don’t have to require the service provider,
it will guess it.
If you really want to enforce it, there’s a provider attribute to the
service resource.

For the error message, require take a path to a file without the
extension, here it should be require “chef/provider/service/init/redhat”

Envoyé à partir de mon smartphone Sony Xperia™

---- rramkarthik@gmail.com a écrit ----

I have multiple services that I need to stop , my code is as below ,

ruby_block “stopsvc” do
block do
services = File.readlines("/root/svc")
services.each do |svc|
require
Chef::Provider::Service::Init::Redhat
service “svc” do
action [ :stop, :disable]
end
end
end
end

“can’t convert Class into String” is the error I am seeing . Please
point what
I need ,


#9

Thanks Matthew . I removed the ruby block and much of what others said made
sense :-),

Thanks,
Ram

On Sun, Sep 14, 2014 at 10:01 PM, Matthew Moretti werebus@gmail.com wrote:

Hey Ram,

So, there are two to three things wrong with your code:

  1. svc is a variable and shouldn’t be wrapped in quotes, as others
    have mentioned.
  2. require takes a string, not a class, which is the error you’re
    getting. This line isn’t necessary at all, though; you can probably just
    remove it. If you need to explicitly specify a provider for your services,
    use the provider attribute as Tensibai suggested.
  3. If your /root/svc file already contains the required contents at
    the start of the chef run, then there’s no need to complicate things by
    using a ruby_block resource. You really only need a ruby_block if some
    other chef resource is going to be dropping off that file and you want to
    wait to read the contents until as late as possible.

I’m pretty sure what you want is something like this:

services = File.readlines("/root/svc")

services.each do |svc|
service svc.chomp do
provider Chef::Provider::Service::Init::Redhat #This may not be necessary
action [ :stop, :disable]
endend

I added the .chomp
http://www.ruby-doc.org/core-2.1.2/String.html#method-i-chomp because
the results of File.readlines appear to have new-lines on the end of
them. I’m not 100% sure it’s required, but it won’t hurt.

You might want to consider reading over this
https://docs.getchef.com/just_enough_ruby_for_chef.html for a quick
overview of ruby for chef users.

Hope that helps.

Matt Moretti

On Mon, Sep 15, 2014 at 12:31 AM, ram karthik rramkarthik@gmail.com
wrote:

Hi Tensibai,

I have tried “svc” , svc , “#{svc}” - nothing worked . Basically the
contents of the file is as below which leaves scope for adding more
services as required ,

cat /root/svc
auditd
vsftpd

On Sun, Sep 14, 2014 at 9:56 AM, Tensibai Zhaoying tensibai@iabis.net
wrote:

Use the provider attribute if the service resource and your declaration
should use the var without quotes or with #{svc}

See http://docs.getchef.com/resource_service.html for the service
resource and provider attribute

Envoyé à partir de mon smartphone Sony Xperia™

---- ram karthik a écrit ----

require “chef/provider/service/init/redhat” returned load error , or Am
I missing something

On Sun, Sep 14, 2014 at 1:52 AM, Tensibai Zhaoying tensibai@iabis.net
wrote:

If it is inside a recipe you don’t have to require the service
provider, it will guess it.
If you really want to enforce it, there’s a provider attribute to the
service resource.

For the error message, require take a path to a file without the
extension, here it should be require “chef/provider/service/init/redhat”

Envoyé à partir de mon smartphone Sony Xperia™

---- rramkarthik@gmail.com a écrit ----

I have multiple services that I need to stop , my code is as below ,

ruby_block “stopsvc” do
block do
services = File.readlines("/root/svc")
services.each do |svc|
require
Chef::Provider::Service::Init::Redhat
service “svc” do
action [ :stop, :disable]
end
end
end
end

“can’t convert Class into String” is the error I am seeing . Please
point what
I need ,


#10

Thanks Matt, was hard to write a correct answer from the phone :wink:

Envoyé à partir de mon smartphone Sony Xperia™

---- Matthew Moretti a écrit ----

Hey Ram,

So, there are two to three things wrong with your code:

svc is a variable and shouldn’t be wrapped in quotes, as others have mentioned.require takes a string, not a class, which is the error you’re getting. This line isn’t necessary at all, though; you can probably just remove it. If you need to explicitly specify a provider for your services, use the provider attribute as Tensibai suggested.If your /root/svc file already contains the required contents at the start of the chef run, then there’s no need to complicate things by using a ruby_block resource. You really only need a ruby_block if some other chef resource is going to be dropping off that file and you want to wait to read the contents until as late as possible.

I’m pretty sure what you want is something like this:

services = File.readlines("/root/svc") services.each do |svc| service svc.chomp do provider Chef::Provider::Service::Init::Redhat #This may not be necessary action [ :stop, :disable] end end

I added the .chomp because the results of File.readlines appear to have new-lines on the end of them. I’m not 100% sure it’s required, but it won’t hurt.

You might want to consider reading over this for a quick overview of ruby for chef users.

Hope that helps.

Matt Moretti

On Mon, Sep 15, 2014 at 12:31 AM, ram karthik rramkarthik@gmail.com wrote:

Hi Tensibai,

I have tried “svc” , svc , “#{svc}” - nothing worked . Basically the contents of the file is as below which leaves scope for adding more services as required ,

cat /root/svc
auditd
vsftpd

On Sun, Sep 14, 2014 at 9:56 AM, Tensibai Zhaoying tensibai@iabis.net wrote:

Use the provider attribute if the service resource and your declaration should use the var without quotes or with #{svc}

See http://docs.getchef.com/resource_service.html for the service resource and provider attribute

Envoyé à partir de mon smartphone Sony Xperia™

---- ram karthik a écrit ----

require “chef/provider/service/init/redhat” returned load error , or Am I missing something

On Sun, Sep 14, 2014 at 1:52 AM, Tensibai Zhaoying tensibai@iabis.net wrote:

If it is inside a recipe you don’t have to require the service provider, it will guess it.
If you really want to enforce it, there’s a provider attribute to the service resource.

For the error message, require take a path to a file without the extension, here it should be require “chef/provider/service/init/redhat”

Envoyé à partir de mon smartphone Sony Xperia™

---- rramkarthik@gmail.com a écrit ----

I have multiple services that I need to stop , my code is as below ,

ruby_block “stopsvc” do
block do
services = File.readlines("/root/svc")
services.each do |svc|
require
Chef::Provider::Service::Init::Redhat
service “svc” do
action [ :stop, :disable]
end
end
end
end

“can’t convert Class into String” is the error I am seeing . Please point what
I need ,