From 55b7f22404da7a0cfcdbce467e9581f6b8509320 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Thu, 5 Feb 2015 14:18:09 -0500 Subject: Updated tab completion as well as respecting ssh config files. --- bin/COMPLETION_SETUP | 32 -------------------------------- bin/README_SHELL_COMPLETION | 32 ++++++++++++++++++++++++++++++++ bin/_ossh_zsh_completion | 24 ------------------------ bin/ossh | 17 +++++++++-------- bin/ossh_zsh_completion | 24 ++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 64 deletions(-) delete mode 100644 bin/COMPLETION_SETUP create mode 100644 bin/README_SHELL_COMPLETION delete mode 100644 bin/_ossh_zsh_completion create mode 100644 bin/ossh_zsh_completion diff --git a/bin/COMPLETION_SETUP b/bin/COMPLETION_SETUP deleted file mode 100644 index 0183544e6..000000000 --- a/bin/COMPLETION_SETUP +++ /dev/null @@ -1,32 +0,0 @@ -# ossh is an ssh replacement. - - -Ossh uses a dynamic inventory cache in order to lookup hostnames and translate them -to something meaningful such as an IP address or dns name. - -This allows us to treat our servers as cattle and not as pets. - -If you have not run the ossh command and it has not laid down -a cache file the completions will not be available. - -You can populate the cache by running `ossh --list`. This -will populate the cache file and the completions should -become available. - -This zsh script will look at the cached version of your -multi_ec2 results in ~/.ansible/tmp/. It will then parse a few -{host}.{env} out of the json and return them to be completable. - -# BASH -In order to setup bash completion, source the following script: -/path/to/repository/openshift-online-ansible/bin/ossh_bash_completion - -# ZSH -In order to setup zsh completion, you will need to verify -that the _ossh_zsh_completion script is somewhere in the path -of $fpath. - -Once $fpath includes the _ossh_zsh_completion script then you should -run `exec zsh`. This will then allow you to call `ossh host[TAB]` -for a list of completions. - diff --git a/bin/README_SHELL_COMPLETION b/bin/README_SHELL_COMPLETION new file mode 100644 index 000000000..0183544e6 --- /dev/null +++ b/bin/README_SHELL_COMPLETION @@ -0,0 +1,32 @@ +# ossh is an ssh replacement. + + +Ossh uses a dynamic inventory cache in order to lookup hostnames and translate them +to something meaningful such as an IP address or dns name. + +This allows us to treat our servers as cattle and not as pets. + +If you have not run the ossh command and it has not laid down +a cache file the completions will not be available. + +You can populate the cache by running `ossh --list`. This +will populate the cache file and the completions should +become available. + +This zsh script will look at the cached version of your +multi_ec2 results in ~/.ansible/tmp/. It will then parse a few +{host}.{env} out of the json and return them to be completable. + +# BASH +In order to setup bash completion, source the following script: +/path/to/repository/openshift-online-ansible/bin/ossh_bash_completion + +# ZSH +In order to setup zsh completion, you will need to verify +that the _ossh_zsh_completion script is somewhere in the path +of $fpath. + +Once $fpath includes the _ossh_zsh_completion script then you should +run `exec zsh`. This will then allow you to call `ossh host[TAB]` +for a list of completions. + diff --git a/bin/_ossh_zsh_completion b/bin/_ossh_zsh_completion deleted file mode 100644 index f057ca8ce..000000000 --- a/bin/_ossh_zsh_completion +++ /dev/null @@ -1,24 +0,0 @@ -#compdef ossh - -_ossh_known_hosts(){ - if [[ -f ~/.ansible/tmp/multi_ec2_inventory.cache ]]; then - print $(/usr/bin/python -c 'import json,os; z = json.loads(open("%s"%os.path.expanduser("~/.ansible/tmp/multi_ec2_inventory.cache")).read()); print "\n".join(["%s.%s" % (host["ec2_tag_Name"],host["ec2_tag_environment"]) for dns, host in z["_meta"]["hostvars"].items()])') - fi - -} -_ossh(){ - local curcontext="$curcontext" state line - typeset -A opt_args - -#_arguments "*:Hosts:_ossh_known_hosts" - _arguments -s : \ - "*:hosts:->hosts" - - case "$state" in - hosts) - _values 'hosts' $(_ossh_known_hosts) - ;; - esac - -} -_ossh "$@" diff --git a/bin/ossh b/bin/ossh index 1cef1b4fa..7f7acc6c2 100755 --- a/bin/ossh +++ b/bin/ossh @@ -84,6 +84,7 @@ class Ossh(object): '''Determine host name and user name for SSH. ''' self.env = None + self.user = None re_env = re.compile('\.(int|stg|prod|ops)') search = re_env.search(self.args.host) @@ -104,10 +105,6 @@ class Ossh(object): self.host = self.args.host if self.args.login_name: self.user = self.args.login_name - else: - self.user = os.environ['USER'] - - def get_hosts(self): '''Query our host inventory and return a dict where the format @@ -192,8 +189,12 @@ class Ossh(object): ''' try: cmd = '/usr/bin/ssh' - ssh_args = [cmd, '-l%s' % self.user] - #ssh_args = [cmd, ] + + # shell args start with the program name in position 1 + ssh_args = [cmd, ] + + if self.user: + ssh_args.append('-l%s' % self.user) if self.args.verbose: ssh_args.append('-vvv') @@ -211,9 +212,9 @@ class Ossh(object): # Assume we have one and only one. hostname, server_info = result[0] - ip = server_info['ec2_ip_address'] + dns = server_info['ec2_public_dns_name'] - ssh_args.append(ip) + ssh_args.append(dns) #last argument if self.args.command: diff --git a/bin/ossh_zsh_completion b/bin/ossh_zsh_completion new file mode 100644 index 000000000..f057ca8ce --- /dev/null +++ b/bin/ossh_zsh_completion @@ -0,0 +1,24 @@ +#compdef ossh + +_ossh_known_hosts(){ + if [[ -f ~/.ansible/tmp/multi_ec2_inventory.cache ]]; then + print $(/usr/bin/python -c 'import json,os; z = json.loads(open("%s"%os.path.expanduser("~/.ansible/tmp/multi_ec2_inventory.cache")).read()); print "\n".join(["%s.%s" % (host["ec2_tag_Name"],host["ec2_tag_environment"]) for dns, host in z["_meta"]["hostvars"].items()])') + fi + +} +_ossh(){ + local curcontext="$curcontext" state line + typeset -A opt_args + +#_arguments "*:Hosts:_ossh_known_hosts" + _arguments -s : \ + "*:hosts:->hosts" + + case "$state" in + hosts) + _values 'hosts' $(_ossh_known_hosts) + ;; + esac + +} +_ossh "$@" -- cgit v1.2.3