cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
public_ip.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.cm.views.user.network
22 # @author Maciej Nabo┼╝ny <mn@mnabozny.pl>
23 #
24 #
25 # Database model describing public ip addresses, which could be mapped on vm ip
26 # lease (Lease entity). Attached ips are redirected by nodes, on which vm are
27 # running. This is done by one-to-one NAT (SNAT+DNAT)
28 #
29 
30 import subprocess
31 
32 from django.db import models
33 
34 from cm.utils import log
35 from cm.utils.exception import CMException
36 
37 
38 class PublicIP(models.Model):
39  address = models.IPAddressField()
40  lease = models.ForeignKey('Lease', blank=True, null=True)
41  user = models.ForeignKey('User', blank=True, null=True, related_name='public_ips')
42 
43  class Meta:
44  app_label = 'cm'
45 
46  def __unicode__(self):
47  return self.address
48 
49  @property
50  ##
51  #
52  # @returns{dict} this PublicLease's data
53  # \n fields:
54  # @dictkey{id,int} this PublicLease's id
55  # @dictkey{ip,string} IP address corresponding to this PublicLease
56  # @dictkey{lease_id,int} id of the wrapped Lease
57  # @dictkey{vm_name,string} VM, to which IP is attached
58  # @dictkey{user_id,int} owner, if there is any
59  #
60  def dict(self):
61  d = {}
62  d['ip_id'] = self.id
63  d['public_ip_id'] = self.id
64  d['address'] = self.address
65  if self.lease:
66  d['lease_id'] = self.lease.id
67  if self.lease.vm:
68  d['vm_name'] = self.lease.vm.name if self.lease and self.lease.vm else ''
69  else:
70  d['vm_name'] = ''
71  else:
72  d['lease_id'] = ''
73  d['vm_name'] = ''
74 
75  if self.user:
76  d['user_id'] = self.user.id
77  else:
78  d['user_id'] = ''
79  return d
80 
81  @property
82  def mac(self):
83  # TODO where we use it?
84  return ''
85 
86  def assign(self, lease):
87  if lease.vm == None:
88  raise CMException('lease_not_attached')
89 
90  self.lease = lease
91  self.save()
92 
93  log.debug(0, "Attaching ip with comand: %s" % str(['ssh',
94  '-i',
95  '/var/lib/cc1/.ssh/id_rsa',
96  '%s@%s' % (lease.vm.node.username, lease.vm.node.address),
97  'sudo /usr/sbin/cc1_node_public_ip attach %d %s %s' % (lease.vm.id, lease.vm_address, self.address)]))
98 
99  p = subprocess.Popen(['ssh',
100  '-i',
101  '/var/lib/cc1/.ssh/id_rsa',
102  '%s@%s' % (lease.vm.node.username, lease.vm.node.address),
103  'sudo /usr/sbin/cc1_node_public_ip attach %d %s %s' % (lease.vm.id, lease.vm_address, self.address)],
104  stdout=subprocess.PIPE)
105  p.wait()
106  log.debug(self.user.id, p.stdout.read())
107 
108  if p.returncode != 0:
109  log.error(self.user.id, "SSH error: %d" % p.returncode)
110  raise CMException('public_ip_failed')
111 
112  def unassign(self):
113  if self.lease == None:
114  raise CMException('public_ip_not_attached')
115 
116  if self.lease.vm == None:
117  raise CMException('lease_not_attached')
118 
119  log.debug(0, "Detaching ip with comand: %s" % str(['ssh',
120  '-i',
121  '/var/lib/cc1/.ssh/id_rsa',
122  '%s@%s' % (self.lease.vm.node.username, self.lease.vm.node.address),
123  'sudo /usr/sbin/cc1_node_public_ip detach %d %s %s' % (self.lease.vm.id, self.lease.vm_address, self.address)]))
124  p = subprocess.Popen(['ssh',
125  '-i',
126  '/var/lib/cc1/.ssh/id_rsa',
127  '%s@%s' % (self.lease.vm.node.username, self.lease.vm.node.address),
128  'sudo /usr/sbin/cc1_node_public_ip detach %d %s %s' % (self.lease.vm.id, self.lease.vm_address, self.address)],
129  stdout=subprocess.PIPE)
130  p.wait()
131  log.debug(self.user.id, p.stdout.read())
132 
133  self.lease = None
134  self.save()
135 
136  if p.returncode != 0:
137  raise CMException('public_ip_failed')
138