# 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
    #html = html + 'published build_managed message<br>'

    return response


def deploy_build_to_host(build, host):
    exchange = Exchange("amq.direct", type="direct", durable=True)
    conn = DjangoBrokerConnection()
    publisher = conn.Producer(routing_key="deploy_graph_multi", exchange=exchange)
    publisher.publish({"request_id" : build.id, "instance_id" : host.instance_id, "graph_key" : build.graph_key})


def update_memory(host):
    try:
        response = urllib2.urlopen('http://%s:8080/memcheck/total' % host.host_ip)
        host.total_memory = int(response.read().strip())   
 
        response = urllib2.urlopen('http://%s:8080/memcheck/free' % host.host_ip)
        host.free_memory = int(response.read().strip())        

        host.save()
    except:
        sys.stderr.write("warning: memory utilization for deployment host % could not be accessed" % host.id)


def deploy_once(build):

    group = build.deployment.group

    # check for existing instance

# 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

    #html = html + 'published build_managed message<br>'

    return response


def deploy_build_to_host(build, host):
    exchange = Exchange("amq.direct", type="direct", durable=True)
    conn = DjangoBrokerConnection()
    publisher = conn.Producer(routing_key="deploy_graph_multi", exchange=exchange)
    publisher.publish({"request_id" : build.id, "instance_id" : host.instance_id, "graph_key" : build.graph_key})


def update_memory(host):
    try:
        response = urllib2.urlopen('http://%s:8080/memcheck/total' % host.host_ip)
        host.total_memory = int(response.read().strip())   
 
        response = urllib2.urlopen('http://%s:8080/memcheck/free' % host.host_ip)
        host.free_memory = int(response.read().strip())        

        host.save()
    except:
        sys.stderr.write("warning: memory utilization for deployment host % could not be accessed" % host.id)


def deploy_once(build):

    group = build.deployment.group

    # check for existing instance

    for host in DeploymentHost.objects.all():