mirror of
https://github.com/duncs/clusterssh.git
synced 2025-07-03 09:53:23 +00:00
Add in bash expansions on hostnames
Allow hostnames to be expanded using bash where a { is in the host name definition Github issue #53
This commit is contained in:
parent
b9738f7142
commit
b330457f99
6 changed files with 150 additions and 2 deletions
|
@ -153,6 +153,8 @@ sub register_host {
|
|||
my ( $self, $node, @tags ) = @_;
|
||||
$self->debug( 2, "Registering node $node on tags:", join( ' ', @tags ) );
|
||||
|
||||
@tags = $self->expand_glob( 'node', $node, @tags );
|
||||
|
||||
foreach my $tag (@tags) {
|
||||
if ( $self->{tags}->{$tag} ) {
|
||||
$self->{tags}->{$tag}
|
||||
|
@ -170,6 +172,11 @@ sub register_host {
|
|||
sub register_tag {
|
||||
my ( $self, $tag, @nodes ) = @_;
|
||||
|
||||
#warn "b4 nodes=@nodes";
|
||||
@nodes = $self->expand_glob( 'tag', $tag, @nodes );
|
||||
|
||||
#warn "af nodes=@nodes";
|
||||
|
||||
$self->debug( 2, "Registering tag $tag: ", join( ' ', @nodes ) );
|
||||
|
||||
$self->{tags}->{$tag} = \@nodes;
|
||||
|
@ -177,6 +184,37 @@ sub register_tag {
|
|||
return $self;
|
||||
}
|
||||
|
||||
sub expand_glob {
|
||||
my ( $self, $type, $name, @items ) = @_;
|
||||
|
||||
my @expanded;
|
||||
|
||||
# skip expanding anything that appears to have nasty metachars
|
||||
if ( !grep {m/[\`\!\$;]/} @items ) {
|
||||
if ( grep {m/[{]/} @items ) {
|
||||
|
||||
#@expanded = split / /, `/bin/bash -c 'shopt -s extglob\n echo @items'`;
|
||||
my $cmd = $self->parent->config->{shell_expansion};
|
||||
$cmd =~ s/%items%/@items/;
|
||||
@expanded = split / /, `$cmd`;
|
||||
chomp(@expanded);
|
||||
}
|
||||
else {
|
||||
@expanded = map { glob $_ } @items;
|
||||
}
|
||||
}
|
||||
else {
|
||||
warn(
|
||||
$self->loc(
|
||||
"Bad characters picked up in [_1] '[_2]': [_3]",
|
||||
$type, $name, join( ' ', @items )
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return @expanded;
|
||||
}
|
||||
|
||||
sub get_tag {
|
||||
my ( $self, $tag ) = @_;
|
||||
|
||||
|
@ -287,6 +325,10 @@ Return an array of all available tag names
|
|||
|
||||
Returns a hash of all tag data.
|
||||
|
||||
=item @tags = $cluster->expand_glob( $type, $name, @items );
|
||||
|
||||
Use shell expansion against each item in @items, where $type is either 'node', or 'tag' and $name is the node or tag name. These attributes are presented to the user in the event of an issue with the expanion to track down the source.
|
||||
|
||||
=back
|
||||
|
||||
=head1 AUTHOR
|
||||
|
|
|
@ -102,6 +102,8 @@ my %default_config = (
|
|||
|
||||
send_menu_xml_file => $ENV{HOME} . '/.clusterssh/send_menu',
|
||||
|
||||
shell_expansion => "/bin/bash -c 'shopt -s extglob\n echo %items%'",
|
||||
|
||||
# don't set username here as takes precendence over ssh config
|
||||
user => '',
|
||||
);
|
||||
|
|
|
@ -563,11 +563,23 @@ would replace the <Alt-n> with the client's name in each window.}
|
|||
output $self->loc(
|
||||
q{All comments (marked by a #) and blank lines are ignored. Tags may be nested, but be aware of using recursive tags as they are not checked for.}
|
||||
);
|
||||
output $self->loc(q{Servers can be defined using bash shell expansion:});
|
||||
output 'C<< webservers websvr{a,b,c} >>';
|
||||
output $self->loc(q{would be expanded to});
|
||||
output 'C<< webservers websvra websvrb websvrc >>';
|
||||
output $self->loc(q{and});
|
||||
output 'C<< webservers websvr{6..9} >>';
|
||||
output $self->loc(q{would be expanded to});
|
||||
output 'C<< webservers websvr6 websvr7 websvr8 websvr9 >>';
|
||||
output $self->loc(
|
||||
q{B<NOTE:> this requires [_1] to be installed on your system (see [_2] configuration option },
|
||||
'/bin/bash', 'C<shell_expansion>'
|
||||
);
|
||||
output $self->loc(
|
||||
q{Extra cluster files may also be specified either as an option on the command line (see [_1]) or in the user's [_2] file (see [_3] configuration option).},
|
||||
'C<cluster-file>',
|
||||
'F<$HOME/.clusterssh/config>',
|
||||
'C<extra_cluster_file>'
|
||||
'L</extra_cluster_file>'
|
||||
);
|
||||
output $self->loc(
|
||||
'B<NOTE:> the last tag read overwrites any pre-existing tag of that name.'
|
||||
|
@ -760,6 +772,13 @@ B<NOTE:> Any "generic" change to the method (e.g., specifying the ssh port to us
|
|||
q{Number of pixels from the screen's side to reserve when calculating screen geometry for tiling. Setting this to something like 50 will help keep cssh from positioning windows over your window manager's menu bar if it draws one at that side of the screen.}
|
||||
);
|
||||
|
||||
output
|
||||
q{=item shell_expansion = /bin/bash -c 'shopt -s extglob\n echo %items%'"};
|
||||
output $self->loc(
|
||||
q{Command used to expand a given string (provided by the macro [_1]) - used for expanding host names when a [_2] is in the name. See [_3]},
|
||||
'C<%items%>', 'C<{>', 'L<bash/EXPANSION>'
|
||||
);
|
||||
|
||||
output '=item terminal = /path/to/xterm';
|
||||
output $self->loc(q{Path to the X-Windows terminal used for the client.});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue