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
11 LOG
= logging
.getLogger("dcemulator")
12 LOG
.setLevel(logging
.DEBUG
)
15 DCDPID_BASE
= 1000 # start of switch dpid's used for data center switches
18 class EmulatorCompute(Docker
):
20 Emulator specific compute node class.
21 Inherits from Dockernet's Docker host class.
22 Represents a single container connected to a (logical)
24 We can add emulator specific helper functions to it.
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
)
35 def getNetworkStatus(self
):
37 Helper method to receive information about the virtual networks
38 this compute instance is connected to.
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()]
46 Helper method to receive information about this compute instance.
49 status
["name"] = self
.name
50 status
["network"] = self
.getNetworkStatus()
51 status
["image"] = self
.dimage
52 status
["cpu_quota"] = self
.cpu_quota
53 status
["cpu_period"] = self
.cpu_period
54 status
["cpu_shares"] = self
.cpu_shares
55 status
["cpuset"] = self
.cpuset
56 status
["mem_limit"] = self
.mem_limit
57 status
["memswap_limit"] = self
.memswap_limit
58 status
["state"] = self
.dcli
.inspect_container(self
.dc
)["State"]
59 status
["id"] = self
.dcli
.inspect_container(self
.dc
)["Id"]
60 status
["datacenter"] = (None if self
.datacenter
is None
61 else self
.datacenter
.label
)
65 class Datacenter(object):
67 Represents a logical data center to which compute resources
68 (Docker containers) can be added at runtime.
70 Will also implement resource bookkeeping in later versions.
75 def __init__(self
, label
, metadata
={}, resource_log_path
=None):
76 self
.net
= None # DCNetwork to which we belong
77 # each node (DC) has a short internal name used by Mininet
78 # this is caused by Mininets naming limitations for swtiches etc.
79 self
.name
= "dc%d" % Datacenter
.DC_COUNTER
80 Datacenter
.DC_COUNTER
+= 1
81 # use this for user defined names that can be longer than self.name
83 # dict to store arbitrary metadata (e.g. latitude and longitude)
84 self
.metadata
= metadata
85 # path to which resource information should be logged (e.g. for experiments). None = no logging
86 self
.resource_log_path
= resource_log_path
87 # first prototype assumes one "bigswitch" per DC
89 # keep track of running containers
91 # pointer to assigned resource model
92 self
._resource
_model
= None
97 def _get_next_dc_dpid(self
):
104 Each data center is represented by a single switch to which
105 compute resources can be connected at run time.
107 TODO: This will be changed in the future to support multiple networks
110 self
.switch
= self
.net
.addSwitch(
111 "%s.s1" % self
.name
, dpid
=hex(self
._get
_next
_dc
_dpid
())[2:])
112 LOG
.debug("created data center switch: %s" % str(self
.switch
))
117 def startCompute(self
, name
, image
=None, command
=None, network
=None, flavor_name
="tiny"):
119 Create a new container as compute resource and connect it to this
121 :param name: name (string)
122 :param image: image name (string)
123 :param command: command (string)
124 :param network: networks list({"ip": "10.0.0.254/8"}, {"ip": "11.0.0.254/24"})
125 :param flavor_name: name of the flavor for this compute container
128 assert name
is not None
130 if name
in [c
.name
for c
in self
.net
.getAllContainers()]:
131 raise Exception("Container with name %s already exists." % name
)
132 # set default parameter
136 network
= {} # {"ip": "10.0.0.254/8"}
137 if isinstance(network
, dict):
138 network
= [network
] # if we have only one network, put it in a list
139 if isinstance(network
, list):
143 # create the container
144 d
= self
.net
.addDocker(
149 flavor_name
=flavor_name
152 # apply resource limits to container if a resource model is defined
153 if self
._resource
_model
is not None:
154 self
._resource
_model
.allocate(d
)
156 # connect all given networks
157 # if no --net option is given, network = [{}], so 1 empty dict in the list
158 # this results in 1 default interface with a default ip address
160 # TODO we cannot use TCLink here (see: https://github.com/mpeuster/dockernet/issues/3)
161 self
.net
.addLink(d
, self
.switch
, params1
=nw
, cls
=Link
)
163 self
.containers
[name
] = d
165 # TODO re-enable logging
167 # write resource log if a path is given
168 if self.resource_log_path is not None:
172 l["compute"] = d.getStatus()
173 l["flavor_name"] = flavor_name
174 l["action"] = "allocate"
175 l["cpu_limit"] = cpu_limit
176 l["mem_limit"] = mem_limit
177 l["disk_limit"] = disk_limit
178 l["rm_state"] = None if self._resource_model is None else self._resource_model.get_state_dict()
180 with open(self.resource_log_path, "a") as f:
181 f.write("%s\n" % json.dumps(l))
183 return d
# we might use UUIDs for naming later on
185 def stopCompute(self
, name
):
187 Stop and remove a container from this data center.
189 assert name
is not None
190 if name
not in self
.containers
:
191 raise Exception("Container with name %s not found." % name
)
192 LOG
.debug("Stopping compute instance %r in data center %r" % (name
, str(self
)))
194 # call resource model and free resources
195 if self
._resource
_model
is not None:
196 self
._resource
_model
.free(self
.containers
[name
])
200 link
=None, node1
=self
.containers
[name
], node2
=self
.switch
)
203 self
.net
.removeDocker("%s" % (name
))
204 del self
.containers
[name
]
206 # TODO re-enable logging
208 # write resource log if a path is given
209 if self.resource_log_path is not None:
213 l["flavor_name"] = None
218 l["rm_state"] = None if self._resource_model is None else self._resource_model.get_state_dict()
220 with open(self.resource_log_path, "a") as f:
221 f.write("%s\n" % json.dumps(l))
225 def listCompute(self
):
227 Return a list of all running containers assigned to this
230 return list(self
.containers
.itervalues())
234 Return a dict with status information about this DC.
238 "internalname": self
.name
,
239 "switch": self
.switch
.name
,
240 "n_running_containers": len(self
.containers
),
241 "metadata": self
.metadata
244 def assignResourceModel(self
, rm
):
246 Assign a resource model to this DC.
247 :param rm: a BaseResourceModel object
250 if self
._resource
_model
is not None:
251 raise Exception("There is already an resource model assigned to this DC.")
252 self
._resource
_model
= rm
253 self
.net
.rm_registrar
.register(self
, rm
)
254 LOG
.info("Assigned RM: %r to DC: %r" % (rm
, self
))