Skip to content

Commit

Permalink
Add option -n, --node to ssh and sftp commands
Browse files Browse the repository at this point in the history
This commit add an option to ssh and sftp subcommands to allow specify the hostname of the node where you want to ssh, and provide a mean to override the `ssh_to` option in the configuration file.
  • Loading branch information
arcimboldo committed May 23, 2015
1 parent 559d39b commit fd6d12c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
4 changes: 2 additions & 2 deletions elasticluster/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ def main(self):
try:
return self.params.func()
except MultipleInvalid as ex:
print("Error parsing configuration files: %s" % str.join(', ', [str(e) for e in ex.errors]))
print("Multiple errors: %s" % str.join(', ', [str(e) for e in ex.errors]))
except Invalid as ex:
print("Error parsing configuration files: %s" % ex)
print("Error: %s" % ex)
print("Exiting.")
sys.exit(1)

Expand Down
37 changes: 29 additions & 8 deletions elasticluster/subcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# stdlib imports
from abc import ABCMeta, abstractmethod
from fnmatch import fnmatch
from voluptuous import Invalid
import os
import sys

Expand Down Expand Up @@ -495,7 +496,7 @@ def execute(self):
"""
Lists all nodes within the specified cluster with certain
information like id and ip.
"""
"""
configurator = Configurator.fromConfig(
self.params.config, storage_path=self.params.storage,
include_config_dirs=True)
Expand Down Expand Up @@ -568,6 +569,10 @@ def setup(self, subparsers):
parser.add_argument('cluster', help='name of the cluster')
parser.add_argument('-v', '--verbose', action='count', default=0,
help="Increase verbosity.")
parser.add_argument('-n', '--node', metavar='HOSTNAME', dest='ssh_to',
help="Name of node you want to ssh to. By "
"default, the first node of the `ssh_to` option "
"group is used.")
parser.add_argument('ssh_args', metavar='args', nargs='*',
help="Execute the following command on the remote "
"machine instead of opening an interactive shell.")
Expand All @@ -585,7 +590,15 @@ def execute(self):
(cluster_name, ex))
return

frontend = cluster.get_frontend_node()
if self.params.ssh_to:
try:
nodes = dict((n.name,n) for n in cluster.get_all_nodes())
frontend = nodes[self.params.ssh_to]
except KeyError:
raise Invalid(
"Hostname %s not found in cluster %s" % (self.params.ssh_to, cluster_name))
else:
frontend = cluster.get_frontend_node()
try:
# ensure we can connect to the host
if not frontend.preferred_ip:
Expand Down Expand Up @@ -625,6 +638,10 @@ def setup(self, subparsers):
description=self.__doc__)
parser.set_defaults(func=self)
parser.add_argument('cluster', help='name of the cluster')
parser.add_argument('-n', '--node', metavar='HOSTNAME', dest='ssh_to',
help="Name of node you want to ssh to. By "
"default, the first node of the `ssh_to` option "
"group is used.")
parser.add_argument('-v', '--verbose', action='count', default=0,
help="Increase verbosity.")
parser.add_argument('sftp_args', metavar='args', nargs='*',
Expand All @@ -644,12 +661,15 @@ def execute(self):
(cluster_name, ex))
return

try:
cluster.ssh_to = configurator.cluster_conf[cluster.extra['template']]['cluster'].get('ssh_to', cluster.ssh_to)
if self.params.ssh_to:
try:
nodes = dict((n.name,n) for n in cluster.get_all_nodes())
frontend = nodes[self.params.ssh_to]
except KeyError:
raise Invalid(
"Hostname %s not found in cluster %s" % (self.params.ssh_to, cluster_name))
else:
frontend = cluster.get_frontend_node()
except NodeNotFound, ex:
log.error("Unable to connect to the frontend node: %s" % str(ex))
sys.exit(1)
host = frontend.connection_ip()
username = frontend.image_user
knownhostsfile = cluster.known_hosts_file if cluster.known_hosts_file \
Expand All @@ -676,7 +696,8 @@ def setup(self, subparsers):
parser.add_argument('cluster', help='name of the cluster')
parser.add_argument('-v', '--verbose', action='count', default=0,
help="Increase verbosity.")
parser.add_argument('-a', '--append', metavar='FILE', help='append configuration to file FILE')
parser.add_argument('-a', '--append', metavar='FILE',
help='append configuration to file FILE')

def execute(self):
"""
Expand Down

0 comments on commit fd6d12c

Please sign in to comment.