Created
June 26, 2014 18:27
-
-
Save krimdomu/46efdc68b4067e9b792c to your computer and use it in GitHub Desktop.
patch system and reboot
This file contains 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
use Rex -feature => ['0.45']; | |
use Rex::Commands::SimpleCheck; | |
user "root"; | |
password "box"; | |
group servers => "10.211.55.168", "10.211.55.169"; | |
# this task is flagged with "no_ssh". so it won't create a ssh connection | |
# to the server, but the connection() method works as usual | |
desc "Run all tasks to patch a system."; | |
no_ssh | |
task "patch_system", | |
group => "servers", | |
make { | |
# run the patch tasks on the current server | |
run_task $_, on => connection->server | |
for qw/stop_services apply_patches reboot_host | |
wait_for_comeback check_services/; | |
# after running the patches, login to the cluster and check the | |
# availbility of the patched system | |
run_task "check_cluster", | |
on => "loadbalancer01", | |
params => { check_server => connection->server }; | |
}; | |
# do something to stop the services | |
task "stop_services", make { | |
service foo => "stop"; | |
sleep 3; # give the init script some time | |
my ($service) = grep { $_->{cmd} =~ m/foo/ } ps; | |
if ($service) { | |
# service still running | |
# kill it | |
kill $service->{pid}, -9; | |
} | |
}; | |
# apply the patches / update the system | |
task "apply_patches", make { | |
update_package_db; | |
update_system; | |
}; | |
# reboot the host | |
task "reboot_host", make { | |
run "/sbin/reboot"; | |
}; | |
# after comming back up, check if the services are running | |
task "check_services", make { | |
sleep 30; # give the server time to finish boot | |
if ( !service foo => "status" ) { | |
service foo => "start"; | |
} | |
my ($service) = grep { $_->{cmd} =~ m/foo/ } ps; | |
if ( !$service ) { | |
# service is NOT running | |
} | |
}; | |
task "check_cluster", make { | |
my $param = shift; | |
my $server = $param->{server}; | |
# maybe query ipvsadm | |
my @cluster_status = grep { $_ =~ m/$server/ } run "ipvsadm -Ln"; | |
# do something to check the cluster | |
# for example query an url | |
use Mojo::UserAgent; | |
my $ua = Mojo::UserAgent->new; | |
my $content = $ua->get("https://user:pass\@loadbalancer/status")->res->body; | |
my $server = connection->server; | |
while ( $content !~ m/$server/ ) { | |
sleep 5; # ask in 5 seconds again | |
$content = $ua->get("https://user:pass\@loadbalancer/status")->res->body; | |
} | |
}; | |
# don't open a ssh connection for this task | |
no_ssh task "wait_for_comeback", make { | |
sleep 30; # give the server time to reboot, wait a bit until | |
# checking for comeback | |
my $server = connection->server; | |
# waiting for openssh to start | |
print "Waiting for openssh to start on $server..."; | |
while ( !is_port_open $server, 22 ) { | |
print "."; | |
sleep 1; | |
} | |
print "\n"; | |
}; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment