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
["docker_network"] = self
.dcinfo
['NetworkSettings']['IPAddress']
53 status
["image"] = self
.dimage
54 status
["flavor_name"] = self
.flavor_name
55 status
["cpu_quota"] = self
.cpu_quota
56 status
["cpu_period"] = self
.cpu_period
57 status
["cpu_shares"] = self
.cpu_shares
58 status
["cpuset"] = self
.cpuset
59 status
["mem_limit"] = self
.mem_limit
60 status
["memswap_limit"] = self
.memswap_limit
61 status
["state"] = self
.dcli
.inspect_container(self
.dc
)["State"]
62 status
["id"] = self
.dcli
.inspect_container(self
.dc
)["Id"]
63 status
["datacenter"] = (None if self
.datacenter
is None
64 else self
.datacenter
.label
)
68 class Datacenter(object):
70 Represents a logical data center to which compute resources
71 (Docker containers) can be added at runtime.
73 Will also implement resource bookkeeping in later versions.
78 def __init__(self
, label
, metadata
={}, resource_log_path
=None):
79 self
.net
= None # DCNetwork to which we belong
80 # each node (DC) has a short internal name used by Mininet
81 # this is caused by Mininets naming limitations for swtiches etc.
82 self
.name
= "dc%d" % Datacenter
.DC_COUNTER
83 Datacenter
.DC_COUNTER
+= 1
84 # use this for user defined names that can be longer than self.name
86 # dict to store arbitrary metadata (e.g. latitude and longitude)
87 self
.metadata
= metadata
88 # path to which resource information should be logged (e.g. for experiments). None = no logging
89 self
.resource_log_path
= resource_log_path
90 # first prototype assumes one "bigswitch" per DC
92 # keep track of running containers
94 # pointer to assigned resource model
95 self
._resource
_model
= None
100 def _get_next_dc_dpid(self
):
107 Each data center is represented by a single switch to which
108 compute resources can be connected at run time.
110 TODO: This will be changed in the future to support multiple networks
113 self
.switch
= self
.net
.addSwitch(
114 "%s.s1" % self
.name
, dpid
=hex(self
._get
_next
_dc
_dpid
())[2:])
115 LOG
.debug("created data center switch: %s" % str(self
.switch
))
120 def startCompute(self
, name
, image
=None, command
=None, network
=None, flavor_name
="tiny"):
122 Create a new container as compute resource and connect it to this
124 :param name: name (string)
125 :param image: image name (string)
126 :param command: command (string)
127 :param network: networks list({"ip": "10.0.0.254/8"}, {"ip": "11.0.0.254/24"})
128 :param flavor_name: name of the flavor for this compute container
131 assert name
is not None
133 if name
in [c
.name
for c
in self
.net
.getAllContainers()]:
134 raise Exception("Container with name %s already exists." % name
)
135 # set default parameter
137 image
= "ubuntu:trusty"
139 network
= {} # {"ip": "10.0.0.254/8"}
140 if isinstance(network
, dict):
141 network
= [network
] # if we have only one network, put it in a list
142 if isinstance(network
, list):
146 # create the container
147 d
= self
.net
.addDocker(
152 flavor_name
=flavor_name
155 # apply resource limits to container if a resource model is defined
156 if self
._resource
_model
is not None:
158 self
._resource
_model
.allocate(d
)
159 self
._resource
_model
.write_allocation_log(d
, self
.resource_log_path
)
160 except NotEnoughResourcesAvailable
as ex
:
161 LOG
.warning("Allocation of container %r was blocked by resource model." % name
)
163 # ensure that we remove the container
164 self
.net
.removeDocker(name
)
167 # connect all given networks
168 # if no --net option is given, network = [{}], so 1 empty dict in the list
169 # this results in 1 default interface with a default ip address
171 # TODO we cannot use TCLink here (see: https://github.com/mpeuster/dockernet/issues/3)
172 self
.net
.addLink(d
, self
.switch
, params1
=nw
, cls
=Link
)
174 self
.containers
[name
] = d
175 return d
# we might use UUIDs for naming later on
177 def stopCompute(self
, name
):
179 Stop and remove a container from this data center.
181 assert name
is not None
182 if name
not in self
.containers
:
183 raise Exception("Container with name %s not found." % name
)
184 LOG
.debug("Stopping compute instance %r in data center %r" % (name
, str(self
)))
186 # call resource model and free resources
187 if self
._resource
_model
is not None:
188 self
._resource
_model
.free(self
.containers
[name
])
189 self
._resource
_model
.write_free_log(self
.containers
[name
], self
.resource_log_path
)
193 link
=None, node1
=self
.containers
[name
], node2
=self
.switch
)
196 self
.net
.removeDocker("%s" % (name
))
197 del self
.containers
[name
]
201 def listCompute(self
):
203 Return a list of all running containers assigned to this
206 return list(self
.containers
.itervalues())
210 Return a dict with status information about this DC.
214 "internalname": self
.name
,
215 "switch": self
.switch
.name
,
216 "n_running_containers": len(self
.containers
),
217 "metadata": self
.metadata
220 def assignResourceModel(self
, rm
):
222 Assign a resource model to this DC.
223 :param rm: a BaseResourceModel object
226 if self
._resource
_model
is not None:
227 raise Exception("There is already an resource model assigned to this DC.")
228 self
._resource
_model
= rm
229 self
.net
.rm_registrar
.register(self
, rm
)
230 LOG
.info("Assigned RM: %r to DC: %r" % (rm
, self
))