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