2 Distributed Cloud Emulator (dcemulator)
3 (c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
5 from mininet
.node
import Docker
6 from mininet
.link
import Link
7 from emuvim
.dcemulator
.resourcemodel
import NotEnoughResourcesAvailable
12 LOG
= logging
.getLogger("dcemulator")
13 LOG
.setLevel(logging
.DEBUG
)
16 DCDPID_BASE
= 1000 # start of switch dpid's used for data center switches
19 class EmulatorCompute(Docker
):
21 Emulator specific compute node class.
22 Inherits from Dockernet's Docker host class.
23 Represents a single container connected to a (logical)
25 We can add emulator specific helper functions to it.
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
)
36 def getNetworkStatus(self
):
38 Helper method to receive information about the virtual networks
39 this compute instance is connected to.
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()]
47 Helper method to receive information about this compute instance.
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
)
67 class Datacenter(object):
69 Represents a logical data center to which compute resources
70 (Docker containers) can be added at runtime.
72 Will also implement resource bookkeeping in later versions.
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
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
91 # keep track of running containers
93 # pointer to assigned resource model
94 self
._resource
_model
= None
99 def _get_next_dc_dpid(self
):
106 Each data center is represented by a single switch to which
107 compute resources can be connected at run time.
109 TODO: This will be changed in the future to support multiple networks
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
))
119 def startCompute(self
, name
, image
=None, command
=None, network
=None, flavor_name
="tiny"):
121 Create a new container as compute resource and connect it to this
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
130 assert name
is not None
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
136 image
= "ubuntu:trusty"
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):
145 # create the container
146 d
= self
.net
.addDocker(
151 flavor_name
=flavor_name
154 # apply resource limits to container if a resource model is defined
155 if self
._resource
_model
is not None:
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
)
162 # ensure that we remove the container
163 self
.net
.removeDocker(name
)
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
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
)
173 self
.containers
[name
] = d
174 return d
# we might use UUIDs for naming later on
176 def stopCompute(self
, name
):
178 Stop and remove a container from this data center.
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
)))
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
)
192 link
=None, node1
=self
.containers
[name
], node2
=self
.switch
)
195 self
.net
.removeDocker("%s" % (name
))
196 del self
.containers
[name
]
200 def listCompute(self
):
202 Return a list of all running containers assigned to this
205 return list(self
.containers
.itervalues())
209 Return a dict with status information about this DC.
213 "internalname": self
.name
,
214 "switch": self
.switch
.name
,
215 "n_running_containers": len(self
.containers
),
216 "metadata": self
.metadata
219 def assignResourceModel(self
, rm
):
221 Assign a resource model to this DC.
222 :param rm: a BaseResourceModel object
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
))