Browse Source

Renamed ossh.py and added bash completion function

Kenny Woodson 10 years ago
parent
commit
fe7d30b762
4 changed files with 47 additions and 5 deletions
  1. 21 0
      bin/COMPLETION_SETUP
  2. 0 4
      bin/ansibleutil.py
  3. 8 1
      bin/ossh.py
  4. 18 0
      bin/ossh_bash_completion

+ 21 - 0
bin/COMPLETION_SETUP

@@ -0,0 +1,21 @@
+# 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.
+
+In order to setup bash completion, source the following script:
+/path/to/repository/openshift-online-ansible/bin/ossh_bash_completion
+
+This bash_completion 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.
+
+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.
+

+ 0 - 4
bin/ansibleutil.py

@@ -57,10 +57,6 @@ class AnsibleUtil(object):
         for dns, host in inv['_meta']['hostvars'].items():
             if host['ec2_tag_environment'] not in inst_by_env:
                 inst_by_env[host['ec2_tag_environment']] = {}
-
-            #if inst_by_env[host['ec2_tag_environment']][host['ec2_tag_Name']]:
-                #raise Exception('Duplicate ec2_tag_Name found: %s' % host['ec2_tag_Name'])
-
             host_id = "%s:%s" % (host['ec2_tag_Name'],host['ec2_id'])
             inst_by_env[host['ec2_tag_environment']][host_id] = host
 

+ 8 - 1
bin/ossh.py

@@ -25,6 +25,11 @@ class Ossh(object):
     def __init__(self):
         self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)))
         self.parse_cli_args()
+
+        if self.args.host == '' and not self.args.list:
+            self.parser.print_help()
+            return
+
         self.ansible = ansibleutil.AnsibleUtil()
 
         self.host_inventory = self.get_hosts()
@@ -57,7 +62,6 @@ class Ossh(object):
                           action="store_true", help="Verbose?")
         parser.add_argument('--list', default=False,
                           action="store_true", help="list out hosts")
-        parser.add_argument('host')
         parser.add_argument('-c', '--command', action='store',
                             help='Command to run on remote host')
         parser.add_argument('-l', '--login_name', action='store',
@@ -66,8 +70,11 @@ class Ossh(object):
         parser.add_argument('-o', '--ssh_opts', action='store',
                             help='options to pass to SSH.\n \
                                   "-o ForwardX11 yes"')
+        parser.add_argument('host', nargs='?', default='')
 
         self.args = parser.parse_args()
+        self.parser = parser
+
 
     def process_host(self):
         '''Determine host name and user name for SSH.

+ 18 - 0
bin/ossh_bash_completion

@@ -0,0 +1,18 @@
+__ossh_known_hosts(){
+    if [[ -f ~/.ansible/tmp/multi_ec2_inventory.cache ]]; then
+      /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 cur prev known_hosts
+    COMPREPLY=()
+    cur="${COMP_WORDS[COMP_CWORD]}"
+    prev="${COMP_WORDS[COMP_CWORD-1]}"
+    known_hosts="$(__ossh_known_hosts)"
+    COMPREPLY=( $(compgen -W "${known_hosts}" -- ${cur}))
+
+    return 0
+}
+complete -F _ossh ossh