-
-
Save dreamcat4/261363 to your computer and use it in GitHub Desktop.
restart chef runs and continue from where it was
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
before_restart "stage 1" do | |
puts "hi we are executing stage 1..." | |
puts "stage 1 done" | |
end | |
before_restart "stage 2" do | |
puts "hi we are executing stage 2..." | |
puts "stage 2 done" | |
end | |
before_restart "stage 3" do | |
puts "hi we are executing stage 3..." | |
puts "stage 3 done" | |
end | |
puts "continue" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cookbook_name | |
"cookbook" | |
end | |
def recipe_name | |
"recipe" | |
end | |
module Chef | |
module Restart | |
def before_restart(name=nil,&blk) | |
Chef::Restart.before_restart(name,&blk) | |
end | |
private | |
def self.moduleinit | |
@run = {} | |
@counter = {} | |
end | |
moduleinit | |
def self.before_restart(name=nil,&blk) | |
@name = "chef_run_#{cookbook_name}_#{recipe_name}" | |
@run[@name] ||= Run.new(@name) | |
incr_count() | |
if env == count.to_s | |
yield | |
incr_env() | |
@run[@name].restart! | |
end | |
end | |
def self.incr_count | |
@counter[@name] ||= 0 | |
@counter[@name] += 1 | |
end | |
def self.count | |
@counter[@name] | |
end | |
def self.incr_env | |
ENV[@name] ||= "0" | |
ENV[@name] = (ENV[@name].to_i+1).to_s | |
end | |
def self.env | |
ENV[@name] ||= "1" | |
end | |
class Run | |
def initialize(name) | |
@name = name | |
end | |
def restart!(msg="Restarting #{File.basename $0}, #{@name}...") | |
if fork # we're the parent | |
puts msg | |
# Chef::Log.info(msg) | |
sleep unspecified_shutdown_time = 2 | |
exit 0 | |
else | |
# in child | |
sleep 1 until(Process.ppid() == 1) | |
exec($0+" "+$*.join(" ")) | |
end | |
end | |
end | |
end | |
end | |
class Object | |
include Chef::Restart | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def restart(recipe,&blk) | |
if fork # we're the parent | |
puts "Restarting!" | |
sleep unspecified_shutdown_time = 2 | |
exit 0 | |
else | |
# in child | |
sleep 1 until(Process.ppid() == 1) | |
ENV["chef_run_#{recipe}"] = (ENV["chef_run_#{recipe}"].to_i+1).to_s | |
exec($0+" "+$*.join(" ")) | |
end | |
end | |
puts $0+" "+$*.inspect | |
recipe="recipe" | |
case ENV["chef_run_#{recipe}"] ||= "1" | |
when "1" | |
puts "first execution run" | |
restart recipe | |
when "2" | |
puts "second execution run" | |
restart recipe | |
when "3" | |
puts "third execution run" | |
restart recipe | |
end | |
puts "continue" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment