Resolving tags by external command

Start coding up resolving tags by an external command instead of a cluster or tag file
This commit is contained in:
Duncan Ferguson 2013-03-25 13:13:03 +00:00
parent 06a5f929ed
commit f1546a0d7d
7 changed files with 96 additions and 16 deletions

View file

@ -2,6 +2,7 @@
- Refactured file loading code
- Add in 'tags' file handling
- Fix bug whereby cluster files were read in multiple times
- Add in resolving tags by external command
2013-03-05 Duncan Ferguson <duncan_ferguson@user.sf.net> - v4.01_05
- New option (-m, --unique-servers) to remove repeated servers when opening terminals (Thanks to Oliver Meissner)

View file

@ -397,7 +397,7 @@ sub resolve_names(@) {
if ( $self->config->{use_all_a_records}
&& $dirty !~ m/^(\d{1,3}\.?){4}$/
&& ! @tag_list )
&& !@tag_list )
{
my $hostobj = gethostbyname($dirty);
if ( defined($hostobj) ) {
@ -412,9 +412,9 @@ sub resolve_names(@) {
}
}
}
if ( @tag_list ) {
if (@tag_list) {
logmsg( 3, '... it is a cluster' );
foreach my $node ( @tag_list) {
foreach my $node (@tag_list) {
if ($username) {
$node =~ s/^(.*)@//;
$node = $username . '@' . $node;
@ -425,12 +425,34 @@ sub resolve_names(@) {
}
}
# now run everything through the external command, if one is defined
if ( $self->config->{external_cluster_command} ) {
$self->debug( 4, 'External cluster command defined' );
# use a second array here in case of failure so previously worked
# out entries are not lost
my @new_servers;
eval {
@new_servers
= $self->cluster->get_external_clusters(
$self->config->{external_cluster_command}, @servers );
};
if ($@) {
warn 'Failure running external cluster command "',
$self->config->{external_cluster_command}, '": ', $@;
}
else {
@servers = @new_servers;
}
}
# now clean the array up
@servers = grep { $_ !~ m/^$/ } @servers;
if ($self->config->{unique_servers}) {
if ( $self->config->{unique_servers} ) {
logmsg( 3, 'removing duplicate server names' );
@servers=remove_repeated_servers(@servers);
@servers = remove_repeated_servers(@servers);
}
logmsg( 3, 'leaving with ', $_ ) foreach (@servers);
@ -439,9 +461,9 @@ sub resolve_names(@) {
}
sub remove_repeated_servers {
my %all=();
@all{@_}=1;
return (keys %all);
my %all = ();
@all{@_} = 1;
return ( keys %all );
}
sub change_main_window_title() {
@ -620,7 +642,8 @@ sub open_client_windows(@) {
my $server_object = App::ClusterSSH::Host->parse_host_string($_);
my $username = $server_object->get_username();
$username = $self->config->{user} if ( !$username && $self->config->{user} );
$username = $self->config->{user}
if ( !$username && $self->config->{user} );
my $port = $server_object->get_port();
$port = $self->config->{port} if ( $self->config->{port} );
my $server = $server_object->get_hostname();
@ -1875,18 +1898,25 @@ sub run {
load_keyboard_map();
# read in normal cluster files
$self->config->{extra_cluster_file} .= ','.$options{'cluster-file'} if($options{'cluster-file'});
$self->config->{extra_tag_file} .= ','.$options{'tag-file'} if($options{'tag-file'});
$self->cluster->get_cluster_entries( split /,/, $self->config->{extra_cluster_file} || '' );
$self->cluster->get_tag_entries( split /,/, $self->config->{extra_tag_file}|| '' );
$self->config->{extra_cluster_file} .= ',' . $options{'cluster-file'}
if ( $options{'cluster-file'} );
$self->config->{extra_tag_file} .= ',' . $options{'tag-file'}
if ( $options{'tag-file'} );
$self->cluster->get_cluster_entries( split /,/,
$self->config->{extra_cluster_file} || '' );
$self->cluster->get_tag_entries( split /,/,
$self->config->{extra_tag_file} || '' );
if ( $options{'list'} ) {
print( 'Available cluster tags:', $/ );
print "\t", $_, $/ foreach ( sort( $self->cluster->list_tags ) );
$self->debug(4, "Full clusters dump: ",$self->_dump_args_hash( $self->cluster->dump_tags ) );
$self->debug(
4,
"Full clusters dump: ",
$self->_dump_args_hash( $self->cluster->dump_tags )
);
exit_prog();
}

View file

@ -45,6 +45,23 @@ sub get_tag_entries {
return $self;
}
sub get_external_clusters {
my ( $self, $external_command, @tags ) = @_;
$self->debug(3, 'Running tags through external command');
$self->debug(4, 'External command: ', $external_command);
$self->debug(3, 'Tags: ', join(',',@tags));
############################
###########################
###########################
###########################
die "catchall while testing",$/;
return $self;
}
sub read_tag_file {
my ( $self, $filename ) = @_;
$self->debug( 2, 'Reading tags from file ', $filename );

View file

@ -68,7 +68,8 @@ my %default_config = (
ssh => 'ssh',
ssh_args => "",
extra_cluster_file => "",
extra_cluster_file => '',
external_cluster_command => '',
unmap_on_redraw => "no", # Debian #329440

View file

@ -70,6 +70,7 @@ Readonly::Hash my %default_config => {
ssh_args => "",
extra_cluster_file => "",
external_cluster_command => '',
unmap_on_redraw => "no",
@ -484,6 +485,7 @@ console=console
console_args=
console_position=
debug=0
external_cluster_command=
extra_cluster_file=
history_height=10
history_width=40

View file

@ -76,6 +76,10 @@ $expected{tag50} = [ 'host30', ];
$cluster1->read_tag_file( $Bin . '/30cluster.tag1' );
test_expected( 'tag 1', %expected );
# now checks agains running an external command
done_testing();
sub test_expected {

25
t/external_cluster_command Executable file
View file

@ -0,0 +1,25 @@
#!/usr/bin/perl
use strict;
use warnings;
# test script for proving external command for fetching tags works
my %tag_lookup = (
tag100 => [ qw/ host100 host110 host105 / ],
tag200 => [ qw/ host200 host250 host125 /],
tag300 => [ qw/ tag100 tag200 host300 host301 / ],
);
my @lookup=@ARGV;
for (@lookup) {
if($tag_lookup{$_}) {
push(@lookup, @{ $tag_lookup{$_} });
$_= '';
}
}
@lookup = grep { $_ !~ m/^$/ } sort @lookup;
print "@lookup",$/;