Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many ips #14

Merged
merged 5 commits into from Feb 19, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion overmind/api/provisioning.py
Expand Up @@ -161,7 +161,7 @@ class SizeHandler(BaseHandler):

class NodeHandler(BaseHandler):
fields = ('id', 'name', 'node_id', 'provider', 'image', 'location', 'size',
'public_ip', 'private_ip', 'created_by', 'state', 'environment',
'public_ips', 'private_ips', 'created_by', 'state', 'environment',
'destroyed_by', 'created_at', 'destroyed_at')
model = Node

Expand Down
2 changes: 1 addition & 1 deletion overmind/provisioning/controllers.py
Expand Up @@ -97,7 +97,7 @@ def create_node(self, form):
return e, None

return None, {
'public_ip': node.public_ip[0],
'public_ips': node.public_ips,
'node_id': node.id,
'state': node.state,
'extra': node.extra,
Expand Down
75 changes: 67 additions & 8 deletions overmind/provisioning/models.py
Expand Up @@ -3,6 +3,7 @@
from provisioning.provider_meta import PROVIDERS
import logging, datetime
import simplejson as json
from IPy import IP

provider_meta_keys = PROVIDERS.keys()
provider_meta_keys.sort()
Expand Down Expand Up @@ -96,7 +97,6 @@ def import_nodes(self):
if not self.supports('list'): return
self.create_connection()
nodes = self.conn.get_nodes()

# Import nodes not present in the DB
for node in nodes:
try:
Expand Down Expand Up @@ -126,11 +126,10 @@ def import_nodes(self):
n.size = Size.objects.get(size_id=size_id, provider=self)
except Size.DoesNotExist:
n.size = None

n.save()
# Import/Update node info
n.public_ip = node.public_ip[0]
if len(node.private_ip):
n.private_ip = node.private_ip[0]
n.sync_ips(node.public_ips, public=True)
n.sync_ips(node.private_ips, public=False)
n.state = get_state(node.state)
n.save_extra_data(node.extra)
n.save()
Expand Down Expand Up @@ -323,6 +322,21 @@ class Meta:
unique_together = ('provider', 'size_id')


class NodeIP(models.Model):
INET_FAMILIES = (
('inet4', 4),
('inet6', 6),
)
node = models.ForeignKey('Node', related_name='ips')
address = models.IPAddressField()
is_public = models.BooleanField(default=True)
version = models.IntegerField(choices=INET_FAMILIES, default=4)
position = models.IntegerField()
interface_name = models.CharField(max_length=32, blank=True)

def __unicode__(self):
return "%s" % (self.address)

class Node(models.Model):
STATE_CHOICES = (
(u'Begin', u'Begin'),
Expand Down Expand Up @@ -353,8 +367,6 @@ class Node(models.Model):
state = models.CharField(
default='Begin', max_length=20, choices=STATE_CHOICES
)
public_ip = models.CharField(max_length=25)
private_ip = models.CharField(max_length=25, blank=True)
hostname = models.CharField(max_length=25, blank=True)
_extra_data = models.TextField(blank=True)

Expand All @@ -366,7 +378,54 @@ class Node(models.Model):
destroyed_by = models.CharField(max_length=25, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
destroyed_at = models.DateTimeField(null=True)


@property
def public_ips(self):
return self.ips.filter(is_public=True)


@property
def private_ips(self):
return self.ips.filter(is_public=False)

# Backward compatibility properties
@property
def public_ip(self):
public_ips = self.ips.filter(is_public=True).filter(version=4)
if len(public_ips):
return public_ips[0].address
return ''

@property
def private_ip(self):
private_ips = self.ips.filter(is_public=False)
if len(private_ips):
return private_ips[0].address
return ''

# helper for related ips creation
def sync_ips(self, ips, public=True):
"""Sync IP for a node"""
previous = self.ips.filter(is_public=public).order_by('position')
addrs = []
for i in ips:
addr = IP(i)
addrs.append(addr)
new_ips = [x.strFullsize() for x in addrs]
for p in previous:
if p.address in new_ips:
p.position = new_ips.index(p.address)
else:
p.delete()
for a in addrs:
if a.strFullsize() not in [x.address for x in previous]:
# Create new nodeip object
NodeIP.objects.create(
address=a.strFullsize(),
position=new_ips.index(a.strFullsize()),
version=a.version(), is_public=public, node=self
)

class Meta:
unique_together = (('provider', 'name'), ('provider', 'node_id'))

Expand Down
2 changes: 1 addition & 1 deletion overmind/templates/overview.html
Expand Up @@ -27,7 +27,7 @@
<h2>Nodes<span class="actions"><a href="/provider/update/">update</a></span></h2>
<br />
<ul class="node">
{% for row in nodes %}<li id="n_{{ row.node.provider.id }}_{{ row.node.id }}"><span class="name" data-tooltip="{{ row.data }}">{{ row.node.provider }} - {{ row.node.name }} - {{ row.node.public_ip }} - {{ row.node.state }}</span><span class="actions">{% for a in row.actions %}<a href="{% if a.confirmation %}javascript:confirmation('{{ a.confirmation }}', '{% endif %}/node/{{ row.node.id }}/{{ a.action }}{% if a.confirmation %}');{% endif %}">{{ a.label }}</a>{% endfor %}</span></li>
{% for row in nodes %}<li id="n_{{ row.node.provider.id }}_{{ row.node.id }}"><span class="name" data-tooltip="{{ row.data }}">{{ row.node.provider }} - {{ row.node.name }} - {{ row.node.public_ips.0 }} - {{ row.node.state }}</span><span class="actions">{% for a in row.actions %}<a href="{% if a.confirmation %}javascript:confirmation('{{ a.confirmation }}', '{% endif %}/node/{{ row.node.id }}/{{ a.action }}{% if a.confirmation %}');{% endif %}">{{ a.label }}</a>{% endfor %}</span></li>
{% endfor %}
</ul>
{% endblock %}
Expand Down