
from provisioning.models import ChromaManager, ChromaAppliance, Node

import settings
from django.core.management.base import BaseCommand
from boto.ec2.connection import EC2Connection

from fabric.operations import open_shell


class Command(BaseCommand):
    args = "list|open <id>|ssh <id>|terminate all"
    help = "Utility command to manage instances"
    can_import_settings = True
    def handle(self, *args, **options):
        if not args or args[0] == 'list':
            for m in ChromaManager.objects.all():
                print m.id, 'manager', m.node.ec2_id, m.node.name
            for a in ChromaAppliance.objects.all():
                print a.id, 'appliance', a.node.ec2_id, a.node.name

        elif args[0] == 'terminate' and args[1] == 'all':
            conn = EC2Connection(settings.AWS_KEY_ID, settings.AWS_SECRET)
            conn.terminate_instances([i.ec2_id for i in Node.objects.all()])
            Node.objects.all().delete()

        elif args[0] == 'open':
            manager_id = int(args[1])
            manager = ChromaManager.objects.get(id = manager_id)
            from provisioning.lib.chroma_ops import ChromaManagerOps
            ops = ChromaManagerOps(manager)
            from subprocess import call
            url = "http://%s/" % ops.ec2_session.instance.ip_address
            print "Opening %s..." % url
            call(["open", url])

        elif args[0] == 'ssh':
            manager_id = int(args[1])
            manager = ChromaManager.objects.get(id = manager_id)
            with manager.get_session().fabric_settings():
                open_shell()

            
                
                

@@ -1531,12 +1531,17 @@
     
-with
+session =
  man
@@ -1562,55 +1562,239 @@
 on()
-.fabric_settings():%0A                open_shell(
+%0A            import os%0A            SSH_BIN = %22/usr/bin/ssh%22%0A            os.execvp(SSH_BIN, %5B%22ssh%22, %22-i%22, settings.AWS_SSH_PRIVATE_KEY, %0A                                %22%25s@%25s%22 %25 (manager.node.username, session.instance.ip_address)%5D
 )%0A%0A 

Use real ssh instead of open_shell