Added proper exception handling, if there are not enough free resources to allocate...
[osm/vim-emu.git] / src / emuvim / dcemulator / node.py
1 """
2 Distributed Cloud Emulator (dcemulator)
3 (c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
4 """
5 from mininet.node import Docker
6 from mininet.link import Link
7 from emuvim.dcemulator.resourcemodel import NotEnoughResourcesAvailable
8 import logging
9 import time
10 import json
11
12 LOG = logging.getLogger("dcemulator")
13 LOG.setLevel(logging.DEBUG)
14
15
16 DCDPID_BASE = 1000 # start of switch dpid's used for data center switches
17
18
19 class EmulatorCompute(Docker):
20 """
21 Emulator specific compute node class.
22 Inherits from Dockernet's Docker host class.
23 Represents a single container connected to a (logical)
24 data center.
25 We can add emulator specific helper functions to it.
26 """
27
28 def __init__(
29 self, name, dimage, **kwargs):
30 self.datacenter = kwargs.get("datacenter") # pointer to current DC
31 self.flavor_name = kwargs.get("flavor_name")
32 LOG.debug("Starting compute instance %r in data center %r" % (name, str(self.datacenter)))
33 # call original Docker.__init__
34 Docker.__init__(self, name, dimage, **kwargs)
35
36 def getNetworkStatus(self):
37 """
38 Helper method to receive information about the virtual networks
39 this compute instance is connected to.
40 """
41 # format list of tuples (name, Ip, MAC, isUp, status)
42 return [(str(i), i.IP(), i.MAC(), i.isUp(), i.status())
43 for i in self.intfList()]
44
45 def getStatus(self):
46 """
47 Helper method to receive information about this compute instance.
48 """
49 status = {}
50 status["name"] = self.name
51 status["network"] = self.getNetworkStatus()
52 status["image"] = self.dimage
53 status["flavor_name"] = self.flavor_name
54 status["cpu_quota"] = self.cpu_quota
55 status["cpu_period"] = self.cpu_period
56 status["cpu_shares"] = self.cpu_shares
57 status["cpuset"] = self.cpuset
58 status["mem_limit"] = self.mem_limit
59 status["memswap_limit"] = self.memswap_limit
60 status["state"] = self.dcli.inspect_container(self.dc)["State"]
61 status["id"] = self.dcli.inspect_container(self.dc)["Id"]
62 status["datacenter"] = (None if self.datacenter is None
63 else self.datacenter.label)
64 return status
65
66
67 class Datacenter(object):
68 """
69 Represents a logical data center to which compute resources
70 (Docker containers) can be added at runtime.
71
72 Will also implement resource bookkeeping in later versions.
73 """
74
75 DC_COUNTER = 1
76
77 def __init__(self, label, metadata={}, resource_log_path=None):
78 self.net = None # DCNetwork to which we belong
79 # each node (DC) has a short internal name used by Mininet
80 # this is caused by Mininets naming limitations for swtiches etc.
81 self.name = "dc%d" % Datacenter.DC_COUNTER
82 Datacenter.DC_COUNTER += 1
83 # use this for user defined names that can be longer than self.name
84 self.label = label
85 # dict to store arbitrary metadata (e.g. latitude and longitude)
86 self.metadata = metadata
87 # path to which resource information should be logged (e.g. for experiments). None = no logging
88 self.resource_log_path = resource_log_path
89 # first prototype assumes one "bigswitch" per DC
90 self.switch = None
91 # keep track of running containers
92 self.containers = {}
93 # pointer to assigned resource model
94 self._resource_model = None
95
96 def __repr__(self):
97 return self.label
98
99 def _get_next_dc_dpid(self):
100 global DCDPID_BASE
101 DCDPID_BASE += 1
102 return DCDPID_BASE
103
104 def create(self):
105 """
106 Each data center is represented by a single switch to which
107 compute resources can be connected at run time.
108
109 TODO: This will be changed in the future to support multiple networks
110 per data center
111 """
112 self.switch = self.net.addSwitch(
113 "%s.s1" % self.name, dpid=hex(self._get_next_dc_dpid())[2:])
114 LOG.debug("created data center switch: %s" % str(self.switch))
115
116 def start(self):
117 pass
118
119 def startCompute(self, name, image=None, command=None, network=None, flavor_name="tiny"):
120 """
121 Create a new container as compute resource and connect it to this
122 data center.
123 :param name: name (string)
124 :param image: image name (string)
125 :param command: command (string)
126 :param network: networks list({"ip": "10.0.0.254/8"}, {"ip": "11.0.0.254/24"})
127 :param flavor_name: name of the flavor for this compute container
128 :return:
129 """
130 assert name is not None
131 # no duplications
132 if name in [c.name for c in self.net.getAllContainers()]:
133 raise Exception("Container with name %s already exists." % name)
134 # set default parameter
135 if image is None:
136 image = "ubuntu:trusty"
137 if network is None:
138 network = {} # {"ip": "10.0.0.254/8"}
139 if isinstance(network, dict):
140 network = [network] # if we have only one network, put it in a list
141 if isinstance(network, list):
142 if len(network) < 1:
143 network.append({})
144
145 # create the container
146 d = self.net.addDocker(
147 "%s" % (name),
148 dimage=image,
149 dcmd=command,
150 datacenter=self,
151 flavor_name=flavor_name
152 )
153
154 # apply resource limits to container if a resource model is defined
155 if self._resource_model is not None:
156 try:
157 self._resource_model.allocate(d)
158 self._resource_model.write_allocation_log(d, self.resource_log_path)
159 except NotEnoughResourcesAvailable as ex:
160 LOG.warning("Allocation of container %r was blocked by resource model." % name)
161 LOG.info(ex.message)
162 # ensure that we remove the container
163 self.net.removeDocker(name)
164 return None
165
166 # connect all given networks
167 # if no --net option is given, network = [{}], so 1 empty dict in the list
168 # this results in 1 default interface with a default ip address
169 for nw in network:
170 # TODO we cannot use TCLink here (see: https://github.com/mpeuster/dockernet/issues/3)
171 self.net.addLink(d, self.switch, params1=nw, cls=Link)
172 # do bookkeeping
173 self.containers[name] = d
174 return d # we might use UUIDs for naming later on
175
176 def stopCompute(self, name):
177 """
178 Stop and remove a container from this data center.
179 """
180 assert name is not None
181 if name not in self.containers:
182 raise Exception("Container with name %s not found." % name)
183 LOG.debug("Stopping compute instance %r in data center %r" % (name, str(self)))
184
185 # call resource model and free resources
186 if self._resource_model is not None:
187 self._resource_model.free(self.containers[name])
188 self._resource_model.write_free_log(self.containers[name], self.resource_log_path)
189
190 # remove links
191 self.net.removeLink(
192 link=None, node1=self.containers[name], node2=self.switch)
193
194 # remove container
195 self.net.removeDocker("%s" % (name))
196 del self.containers[name]
197
198 return True
199
200 def listCompute(self):
201 """
202 Return a list of all running containers assigned to this
203 data center.
204 """
205 return list(self.containers.itervalues())
206
207 def getStatus(self):
208 """
209 Return a dict with status information about this DC.
210 """
211 return {
212 "label": self.label,
213 "internalname": self.name,
214 "switch": self.switch.name,
215 "n_running_containers": len(self.containers),
216 "metadata": self.metadata
217 }
218
219 def assignResourceModel(self, rm):
220 """
221 Assign a resource model to this DC.
222 :param rm: a BaseResourceModel object
223 :return:
224 """
225 if self._resource_model is not None:
226 raise Exception("There is already an resource model assigned to this DC.")
227 self._resource_model = rm
228 self.net.rm_registrar.register(self, rm)
229 LOG.info("Assigned RM: %r to DC: %r" % (rm, self))
230