#!/usr/bin/perl
# 
# This program has no specific purpose. It's a general program for performing certain special tasks that 
# can't be done otherwise in a reliable or efficient way.
# 
# Exit codes;
# 0 = Normal exit.
# 1 = No database connection.


use strict;
use warnings;
use Anvil::Tools;
require POSIX;
use Text::Diff;
use Data::Dumper;

my $THIS_FILE           =  ($0 =~ /^.*\/(.*)$/)[0];
my $running_directory   =  ($0 =~ /^(.*?)\/$THIS_FILE$/)[0];
if (($running_directory =~ /^\./) && ($ENV{PWD}))
{
	$running_directory =~ s/^\./$ENV{PWD}/;
}

# Turn off buffering so that the pinwheel will display while waiting for the SSH call(s) to complete.
$| = 1;

my $anvil = Anvil::Tools->new();

# Read switches (target ([user@]host[:port]) and the file with the target's password.
$anvil->Get->switches({list => [
	"resource", 
	"task"], man => $THIS_FILE});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => $anvil->data->{switches}});
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0115", variables => { program => $THIS_FILE }});

$anvil->Database->connect();
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, secure => 0, key => "log_0132"});
if (not $anvil->data->{sys}{database}{connections})
{
	# No databases, update the job, sleep for a bit and then exit. The daemon will pick it up and try 
	# again after we exit.
	$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 0, priority => "err", key => "error_0306"});
	sleep 10;
	$anvil->nice_exit({exit_code => 1});
}

if ($anvil->data->{switches}{'job-uuid'})
{
	$anvil->Job->clear();
	$anvil->Job->get_job_details({debug => 2});
	$anvil->Job->update_progress({
		progress         => 1,
		job_picked_up_by => $$, 
		job_picked_up_at => time, 
		message          => "message_0311", 
	});
}

if ($anvil->data->{switches}{task} eq "refresh-drbd-resource")
{
	refresh_drbd_resource($anvil);
}

$anvil->nice_exit({exit_code => 0});


#############################################################################################################
# Functions                                                                                                 #
#############################################################################################################

# This function is needed to call 'drbdadm adjust <res>' in a background call from a remote host. This is
# needed for adding new volumes to an existing resource, as the call from 'drbdadm adjust <res>' won't return
# until the call is run on all hosts.
sub refresh_drbd_resource
{
	my ($anvil) = @_;
	
	my $resource = $anvil->data->{switches}{resource};
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { resource => $resource }});
	
	if (not $resource)
	{
		# No resource.
		$anvil->Job->update_progress({
			progress   => 100,
			message    => "error_0419", 
			job_status => "failed", 
		});
		$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 0, priority => 'err', key => "error_0419"});
		$anvil->nice_exit({exit_code => 1});
	}
	
	$anvil->Job->update_progress({
		progress => 10,
		message  => "message_0312,!!resource!".$resource."!!", 
	});
	$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 2, key => "message_0312", variables => { resource => $resource }});
	
	my $shell_call = $anvil->data->{path}{exe}{drbdadm}." adjust ".$resource;
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { shell_call => $shell_call }});
	my ($output, $return_code) = $anvil->System->call({
		shell_call => $shell_call,
		background => 1,
	});
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 
		output      => $output,
		return_code => $return_code, 
	}});
	
	$anvil->Job->update_progress({
		progress => 100,
		message  => "message_0313,!!resource!".$resource."!!", 
	});
	$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 2, key => "message_0313", variables => { resource => $resource }});
	
	$anvil->nice_exit({exit_code => 0});
	
	return(0);
}
