# -*-perl-*-

# $Id: HostAtom.plm,v 1.18 2011/02/14 22:27:21 koczan Exp $

# This package provides the afs atom objects.  It inherits almost everything
# from the generic Atom object.

# $Log: HostAtom.plm,v $
# Revision 1.18  2011/02/14 22:27:21  koczan
# Remove the Network weights
#
# Revision 1.17  2010/03/22 22:52:47  koczan
# cleaned up error messages
#
# Revision 1.16  2009/11/11 22:57:49  koczan
# Modified limits for BFQs
#
# Revision 1.15  2009/09/21 15:09:56  koczan
# altered memory limits
#
# Revision 1.14  2008/01/31 17:34:26  thomas
# Don't need sushiremote_wrapper, just sushiremote
#
# Revision 1.13  2005/02/08 21:13:03  thomas
# new version of ssh
#
# Revision 1.12  2004/03/11 21:58:43  thomas
# Clean up ssh calls to use /s/sushi-0.3.19/depend/bin/ssh
#
# Revision 1.11  2004/03/04 20:40:25  thomas
# [CSL #180910]
#
# Revision 1.10  2003/06/09 14:45:11  thomas
# schema version of database
#
# Revision 1.9  2002/10/31 16:54:40  thomas
# - pass kidstatus to checkAtom
# - checkAtom by exit code ONLY (no check of output)
#
# Revision 1.8  2002/10/31 14:39:35  thomas
# Add "Can't do setuid" to errors for LinuxRmtTar
#
# Revision 1.7  2002/10/31 14:36:43  thomas
# Allow 'hostinfo: Command not found.' for BsdRemote
#
# Revision 1.6  2002/10/30 19:41:50  thomas
# - Use gmtime, not ctime when converting to db values
# - Add errors for HostAtom checkDump
#
# Revision 1.5  2002/10/07 19:01:21  thomas
# sushi-0.3.3
#
# Revision 1.4  2002/09/10 20:42:57  thomas
# sushi-0.3.1
#
# Revision 1.3  2002/08/14 17:13:07  thomas
# 0.2.9 release
#
# Revision 1.2  2002/07/10 14:01:28  thomas
# sushi-0.2.4
#
# Revision 1.1  2002/07/08 15:56:33  thomas
# 0.2.3 release
#
# Revision 1.1.1.1  2002/05/21 14:13:31  thomas
# sushi - backup to disk
#

package Sushi::HostAtom;

use strict;
use Net::hostent;
use Data::Dumper;

use base qw(Sushi::Atom);

###############################################################################
# Return the command used to execute the child atom.

sub getChildCmd {
  my $self = shift;
  my $timestamp = shift;

  my @argv = ();
  my $execHost;

  if ($self->{name} =~ /^([^:]+):/) {
    $execHost = $1;
  } else {
    $self->error ("HostAtom couldn't find execute host for " . $self->commonName . "!\n");
  return undef;
  }

  @argv = ('/s/sushi-0.3.19/depend/bin/ssh', '-2', '-x', '-n', '-o', 'gssapiauthentication=no', '-l', 'wsbackup', $execHost);

  $::debug>3 and push @argv, "time";

  push @argv, "/s/sushi-0.3.19/bin/runkeytab", "/s/sushi-0.3.19/bin/sushiremote", '-d', $::debug, $self->{id}, $timestamp;

  return @argv;
}

###############################################################################
# For most host atoms, we need a host acl.
# Use the host case here, and override
# where needed in the specific method.

sub needHostAcl {
  my $self = shift;

  my $host = $self->name;
  $host =~ s/\:.*//;
  return $host;
}

###############################################################################

sub createPts {
  return Sushi::AfsBits::createPts (@_);
}

###############################################################################
# Screw the looking in the output.  Make everything give us a valid
# exit status.
# return 1 for success, 0 for failure

sub checkAtom {
  my ($self) = shift;
  my ($output) = shift;
  my ($status) = shift;

  return (($status>>8) == 0);
}

###############################################################################
# Check the passed fd for correct dump completion
# return 1 for success, 0 for failure
#
#sub checkAtom {
#  my ($self) = shift;
#  my ($output) = shift;
#
#  # Some things are ok for BsdRemote...
#  if ($self->{type} eq 'BsdRemote') {
#    $output =~ s/hostinfo: Command not found\.//g;
#  }
#
#  ($output =~/error|Unknown|ABORTING|EXITED|ATTENTION|abort|Bad|closed|reset|Connection timed out|No route to host|Host is unreachable|Connection refused|Host key verification failed|Cannot open \/dev\/|REMOTE HOST IDENTIFICATION HAS CHANGED|Permission denied|Cannot lstat|Can\'t read next inode|Command not found\.|Someone forgot to make me set uid|Can\'t do setuid/)
#    and return 0;
#  return 1;
#}
#
###############################################################################

sub setConstraints {
  my $self = shift;
  my $hints = shift;
  my $limits = shift;

  # $limits is a table of results from a BFQ of the limits table passed by 
  # from a calling function so we don't have to issue so many queries
  # if defined, then we assume that the table is kosher and use its data
  # if not, then we query the database for more information

  defined (my $cpuname = (split /:/, $self->{name})[0]) or return 0;

  $self->setWeight ( {
		      resource_type  => "Host",
		      resource_alias => $cpuname,
		      resource_name  => $cpuname,
		      weight         => 1
		     } ) or return 0;

  my $limit_exists = 0;
  if (defined $limits) {
    $limit_exists = $limits->{"Host:$cpuname"};
  } else {
    my $db_limits = &Sushi::GenDB::fetch ( {
					    resource_type => "Host",
					    resource_name => $cpuname 
					   },
					   {},
					   $::table_defs->{limits}
					 );
    $limit_exists = scalar @{$db_limits};
  }

  # Add the corresponding host limit if there are none.
  if (!$limit_exists) {
    $self->log(2, "Adding limits for host $cpuname\n");
    &Sushi::GenDB::add (
			{
			 resource_type  => "Host",
			 resource_name  => $cpuname,
			 resource_limit => 2
			},
			{},
			$::table_defs->{limits}
		       );

    $limits->{"Host:$cpuname"} = 1 if defined $limits;
  }

  return $self->setConstraintsDumpType();
}

###############################################################################
# Get the execute Host for the atom.  This comes from the atom name.

sub executeHost {
  my $self = shift;

  if ($self->{name} =~ /^([^:]+):/) {
    my $execHost = $1;
    return $execHost;
  }
  $self->error ("HostAtom::executeHost failed for " . $self->commonName . "!\n");
  return undef;
}

###############################################################################
# No extra constraints for most host atom types.

sub setConstraintsDumpType { return 1; }

###############################################################################

1;
