2

I am defining a provider as below:

action :start do
 ...
end

action :stop do 
 ...
end

action :restart do
 ...
end

Now instead of rewriting the implementation of stop and start in restart, I would like to call action :stop and then action :start in action :restart, like this:

action :restart do
  action :stop
  action :start
end

Is there a way to achieve this ?

EDIT - As mentioned in Coderanger answer, the solution is:

action :restart do 
  action_stop
  action_start
end
JahMyst
  • 1,616
  • 3
  • 20
  • 39

2 Answers2

4

Call action_start and action_stop.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • I wonder what's wrong, but when I put ```action_stop``` and ```action_start``` in ```action :restart```, ```action :stop``` and ```action :start``` are executed twice. When I just paste the code for start / stop, everything is fine. – JahMyst Mar 01 '16 at 17:55
  • See the following pastebin for comparison purposes: the first calls each action two times, the second works fine. http://pastebin.com/6r94LMNA http://pastebin.com/3WHwQhNF – JahMyst Mar 01 '16 at 18:05
  • I don't know what is in `init` but it would indeed get called twice. – coderanger Mar 01 '16 at 19:25
  • Also you are using a bunch of class variables, I really don't think you mean to be doing that. – coderanger Mar 01 '16 at 19:26
  • This is the content of ```libraries.rb ```defining the ```init``` function: http://pastebin.com/qULUAsgk – JahMyst Mar 01 '16 at 20:17
  • I don't know what you wrote, but it is *not* a Chef resource. Please follow the standard style if you want your code to work. – coderanger Mar 01 '16 at 20:20
  • To be clear, this answer is correct, however what you wrote is not a Chef resource/provider and thus does not follow the same rules. – coderanger Mar 01 '16 at 20:52
0

I'm not sure if this is the correct answer. I have just tried this and it seems that this calls action_stop and action_start in compile time. I was trying to run something like this:

action :create do
  # steps to create resource
  directory '/test' do
    ...
  end

  action_config
end

action :config do   
  ... # configuration   
  template '/test/config' do   
    ...   
  end 
end

It has failed because :config ran first (before directory has been created).

I have tried to put action_config into ruby_block -- which seems to work, but I'm not sure if arguments are passed correctly.