import logging
import os

from django.conf import settings
from paramiko import AutoAddPolicy, SSHClient

from .base import Runner

logger = logging.getLogger(__name__)


class SshRunner(Runner):
    def get_commands(self, task):
        return task.config['commands']

    @staticmethod
    def set_up_client(task):
        client = SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(AutoAddPolicy())
        client.connect(
            hostname=task.config['hostname'],
            username=task.config['username'],
            key_filename=os.path.expanduser(settings.PRIVATE_KEY_FILENAME),
        )

        return client

    @staticmethod
    def run_command(command, client):
        result = {}
        channel = client.get_transport().open_session()
        channel.set_combine_stderr(True)

        channel.exec_command(command)
        result['stdout'] = channel.recv(1024 * 1024 * 1024).decode()
        result['exit_code'] = channel.recv_exit_status()
        return result

    def run_task(self, task):
        task.result = {}
        try:
            client = self.set_up_client(task)

            for command in self.get_commands(task):
                task.result[command] = self.run_command(command, client)
                task.save()

            client.close()
        except FileNotFoundError as error:
            logger.exception(error)
            task.result['ssh'] = {'exit_code': 1, 'error': str(error)}
            task.save()

@@ -717,19 +717,27 @@
 , client
+, config
 ):%0A
-
         
@@ -846,16 +846,200 @@
 (True)%0A%0A
+        if 'path' in config:%0A            result%5B'path'%5D = config%5B'path'%5D%0A            channel.exec_command('cd %7Bpath%7D && %7Bcommand%7D'.format(command=command, **config))%0A        else:%0A    
         
@@ -1068,16 +1068,17 @@
 ommand)%0A
+%0A
         
@@ -1456,16 +1456,29 @@
 , client
+, task.config
 )%0A      

Add support for path config in SSH tasks