# -*- coding: utf-8 -*-
__author__ = 'John Moylan'

import sys

from paramiko import SSHClient, SSHConfig, AutoAddPolicy

from pyvarnish.settings import SSH_CONFIG


class Varnish_admin():

    def __init__(self, server=''):
        self.server = server
        self.conf = self.config()

    def config(self):
        sshconfig = SSHConfig()
        try:
            sshconfig.parse(open(SSH_CONFIG))
        except IOError:
            print "your app needs to have a valid " \
                  "ssh config file location in settings.py"
            sys.exit(1)
        return sshconfig.lookup(self.server)

    def runcmd(self, cmd):
        try:
            client = SSHClient()
            client.load_system_host_keys()
            client.set_missing_host_key_policy(AutoAddPolicy())
            client.connect(self.conf['hostname'],
                            port = int(self.conf['port']),
                            username = self.conf['user'],
                            key_filename = self.conf['identityfile'],
                            password = None,)
            stdin, stdout, stderr = client.exec_command(cmd)
            return ''.join([i.rstrip('\r\n ').lstrip() for i in stdout.readlines()])
        finally:
            client.close()
# -*- coding: utf-8 -*-
__author__ = 'John Moylan'

import sys

from paramiko import SSHClient, SSHConfig, AutoAddPolicy

from pyvarnish.settings import SSH_CONFIG


class Varnish_admin():

    def __init__(self, server=''):
        self.server = server
        self.conf = {
            'hostname': server,
            'port': 22,
            # If these are None, Paramiko will figure out the correct values.
            'user': None,
            'identityfile': None,
        }
        self.conf.update(self.config())

    def config(self):
        sshconfig = SSHConfig()
        try:
            sshconfig.parse(open(SSH_CONFIG))
        except IOError:
            print "your app needs to have a valid " \
                  "ssh config file location in settings.py"
            sys.exit(1)
        conf = sshconfig.lookup(self.server)
        if 'port' in conf:
            conf['port'] = int(conf['port'])
        
        return conf

    def runcmd(self, cmd):
        try:
            client = SSHClient()
            client.load_system_host_keys()
            client.set_missing_host_key_policy(AutoAddPolicy())
            client.connect(self.conf['hostname'],
                            port = self.conf['port'],
                            username = self.conf['user'],
                            key_filename = self.conf['identityfile'],)
            stdin, stdout, stderr = client.exec_command(cmd)
            return ''.join([i.rstrip('\r\n ').lstrip() for i in stdout.readlines()])
        finally:
            client.close()


Make the SSH configuration more resilient.
Make the SSH configuration more resilient.

If you don't have certain values specified in your ~/.ssh/config, use
the defaults instead of erroring out.
