# Path: otpsetup/client/models.py
# class ManagedDeployment(models.Model):
#     source = models.CharField(max_length=15)
#     description = models.CharField(max_length=200, null=True, blank=True)
#     group = models.ForeignKey(DeploymentGroup, null=True, blank=True)
#     last_osm_key = models.CharField(max_length=200, null=True, blank=True)
# 
#     def __str__(self):
#         return "[%s] %s (%s)" % (self.id, self.source, self.description)
# 
# class DeploymentHost(models.Model):
#     name = models.CharField(max_length=50, blank=True)
#     instance_id = models.CharField(max_length=20)
#     host_ip = models.CharField(max_length=20)
#     otp_version = models.CharField(max_length=20)
#     auth_password = models.CharField(max_length=20)
#     total_memory = models.BigIntegerField()
#     free_memory = models.BigIntegerField()
#     group = models.ForeignKey(DeploymentGroup, null=True, blank=True)
# 
#     def __str__(self):
#         return "(%s) %s" % (self.id, self.name)
# 
# class DeploymentGroup(models.Model):
#     name = models.CharField(max_length=20)
#     description = models.TextField(max_length=1000, null=True, blank=True)
# 
#     def __str__(self):
#         return self.name
# 
# class GraphBuild(models.Model):
#     deployment = models.ForeignKey(ManagedDeployment)
#     creation_date = models.DateTimeField(default=datetime.now)
#     completion_date = models.DateTimeField(null=True)
#     config = models.TextField(max_length=10000, null=True, blank=True)
#     osm_key = models.CharField(max_length=200, null=True, blank=True)
#     graph_key = models.CharField(max_length=200, null=True, blank=True)
#     graph_size = models.BigIntegerField(null=True)
#     output_key = models.CharField(max_length=200, null=True, blank=True)
#     success = models.BooleanField(default=False)
#     otp_version = models.CharField(max_length=20, null=True, blank=True)
# 
#     def gtfs_count(self):
#         return len(self.gtfsbuildmapping_set.all())
# 
#     def host_count(self):
#         return len(self.buildhostmapping_set.all())
# 
#     def link(self):
#         return "<a href='/admin/client/graphbuild/%s'>[<b>%s</b>]</a>" % (self.id, self.id)
#     link.allow_tags = True
# 
#     def __str__(self):
#         return "Build #%s / Deployment %s" % (self.id, self.deployment)


from otpsetup.client.models import ManagedDeployment, DeploymentHost, DeploymentGroup, GraphBuild
from kombu import Exchange
from otpsetup.shortcuts import DjangoBrokerConnection
import sys, urllib2, json
    source = 'metro-%s' % config['metroId']
    osm_key = None
    try:
        man_dep = ManagedDeployment.objects.get(source=source)
        response += " has existing record."
    except ManagedDeployment.DoesNotExist:
        group = DeploymentGroup.objects.get(name="otpna")
        man_dep = ManagedDeployment(source=source, group=group)
        response += " has no record; created."

    man_dep.description = metro_name
    man_dep.save()

    build = GraphBuild(deployment=man_dep, osm_key=man_dep.last_osm_key, config=config_txt)
    build.save()

    exchange = Exchange("amq.direct", type="direct", durable=True)
    conn = DjangoBrokerConnection()

    publisher = conn.Producer(routing_key="process_gtfs", exchange=exchange)
    publisher.publish({'id' : build.id, 'config' : config_txt})
    response += ' Published process_gtfs message.'

    #publisher = conn.Producer(routing_key="build_managed", exchange=exchange)
    #publisher.publish({'id' : build.id, 'osm_key' : build.osm_key, 'config' : request.REQUEST['data']})
    #html = html + 'published build_managed message<br>'

    return response



# Path: otpsetup/client/models.py
# class ManagedDeployment(models.Model):
#     source = models.CharField(max_length=15)
#     description = models.CharField(max_length=200, null=True, blank=True)
#     group = models.ForeignKey(DeploymentGroup, null=True, blank=True)
#     last_osm_key = models.CharField(max_length=200, null=True, blank=True)
# 
#     def __str__(self):
#         return "[%s] %s (%s)" % (self.id, self.source, self.description)
# 
# class DeploymentHost(models.Model):
#     name = models.CharField(max_length=50, blank=True)
#     instance_id = models.CharField(max_length=20)
#     host_ip = models.CharField(max_length=20)
#     otp_version = models.CharField(max_length=20)
#     auth_password = models.CharField(max_length=20)
#     total_memory = models.BigIntegerField()
#     free_memory = models.BigIntegerField()
#     group = models.ForeignKey(DeploymentGroup, null=True, blank=True)
# 
#     def __str__(self):
#         return "(%s) %s" % (self.id, self.name)
# 
# class DeploymentGroup(models.Model):
#     name = models.CharField(max_length=20)
#     description = models.TextField(max_length=1000, null=True, blank=True)
# 
#     def __str__(self):
#         return self.name
# 
# class GraphBuild(models.Model):
#     deployment = models.ForeignKey(ManagedDeployment)
#     creation_date = models.DateTimeField(default=datetime.now)
#     completion_date = models.DateTimeField(null=True)
#     config = models.TextField(max_length=10000, null=True, blank=True)
#     osm_key = models.CharField(max_length=200, null=True, blank=True)
#     graph_key = models.CharField(max_length=200, null=True, blank=True)
#     graph_size = models.BigIntegerField(null=True)
#     output_key = models.CharField(max_length=200, null=True, blank=True)
#     success = models.BooleanField(default=False)
#     otp_version = models.CharField(max_length=20, null=True, blank=True)
# 
#     def gtfs_count(self):
#         return len(self.gtfsbuildmapping_set.all())
# 
#     def host_count(self):
#         return len(self.buildhostmapping_set.all())
# 
#     def link(self):
#         return "<a href='/admin/client/graphbuild/%s'>[<b>%s</b>]</a>" % (self.id, self.id)
#     link.allow_tags = True
# 
#     def __str__(self):
#         return "Build #%s / Deployment %s" % (self.id, self.deployment)


# Path: otpsetup/deploy_ops.py
from otpsetup.client.models import ManagedDeployment, DeploymentHost, DeploymentGroup, GraphBuild
from kombu import Exchange
from otpsetup.shortcuts import DjangoBrokerConnection
import sys, urllib2, json

    source = 'metro-%s' % config['metroId']
    osm_key = None
    try:
        man_dep = ManagedDeployment.objects.get(source=source)
        response += " has existing record."
    except ManagedDeployment.DoesNotExist:
        group = DeploymentGroup.objects.get(name="otpna")
        man_dep = ManagedDeployment(source=source, group=group)
        response += " has no record; created."

    man_dep.description = metro_name
    man_dep.save()

    build = GraphBuild(deployment=man_dep, osm_key=man_dep.last_osm_key, config=config_txt)
    build.save()

    exchange = Exchange("amq.direct", type="direct", durable=True)
    conn = DjangoBrokerConnection()

    publisher = conn.Producer(routing_key="process_gtfs", exchange=exchange)
    publisher.publish({'id' : build.id, 'config' : config_txt})
    response += ' Published process_gtfs message.'

    #publisher = conn.Producer(routing_key="build_managed", exchange=exchange)
    #publisher.publish({'id' : build.id, 'osm_key' : build.osm_key, 'config' : request.REQUEST['data']})
    #html = html + 'published build_managed message<br>'

    return response



def deploy_build_to_host(build, host):