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
10 DCDPID_BASE
= 1000 # start of switch dpid's used for data center switches
13 class EmulatorCompute(Docker
):
15 Emulator specific compute node class.
16 Inherits from Dockernet's Docker host class.
17 Represents a single container connected to a (logical)
19 We can add emulator specific helper functions to it.
23 self
, name
, dimage
, **kwargs
):
24 logging
.debug("Create EmulatorCompute instance: %s" % name
)
25 self
.datacenter
= kwargs
.get("datacenter") # pointer to current DC
26 self
.flavor_name
= kwargs
.get("flavor_name")
28 # call original Docker.__init__
29 Docker
.__init
__(self
, name
, dimage
, **kwargs
)
31 def getNetworkStatus(self
):
33 Helper method to receive information about the virtual networks
34 this compute instance is connected to.
36 # format list of tuples (name, Ip, MAC, isUp, status)
37 return [(str(i
), i
.IP(), i
.MAC(), i
.isUp(), i
.status())
38 for i
in self
.intfList()]
42 Helper method to receive information about this compute instance.
45 status
["name"] = self
.name
46 status
["network"] = self
.getNetworkStatus()
47 status
["image"] = self
.dimage
48 status
["cpu_quota"] = self
.cpu_quota
49 status
["cpu_period"] = self
.cpu_period
50 status
["cpu_shares"] = self
.cpu_shares
51 status
["cpuset"] = self
.cpuset
52 status
["mem_limit"] = self
.mem_limit
53 status
["memswap_limit"] = self
.memswap_limit
54 status
["state"] = self
.dcli
.inspect_container(self
.dc
)["State"]
55 status
["id"] = self
.dcli
.inspect_container(self
.dc
)["Id"]
56 status
["datacenter"] = (None if self
.datacenter
is None
57 else self
.datacenter
.label
)
61 class Datacenter(object):
63 Represents a logical data center to which compute resources
64 (Docker containers) can be added at runtime.
66 Will also implement resource bookkeeping in later versions.
71 def __init__(self
, label
, metadata
={}):
72 self
.net
= None # DCNetwork to which we belong
73 # each node (DC) has a short internal name used by Mininet
74 # this is caused by Mininets naming limitations for swtiches etc.
75 self
.name
= "dc%d" % Datacenter
.DC_COUNTER
76 Datacenter
.DC_COUNTER
+= 1
77 # use this for user defined names that can be longer than self.name
79 # dict to store arbitrary metadata (e.g. latitude and longitude)
80 self
.metadata
= metadata
81 # first prototype assumes one "bigswitch" per DC
83 # keep track of running containers
85 # pointer to assigned resource model
86 self
._resource
_model
= None
91 def _get_next_dc_dpid(self
):
98 Each data center is represented by a single switch to which
99 compute resources can be connected at run time.
101 TODO: This will be changed in the future to support multiple networks
104 self
.switch
= self
.net
.addSwitch(
105 "%s.s1" % self
.name
, dpid
=hex(self
._get
_next
_dc
_dpid
())[2:])
106 logging
.debug("created data center switch: %s" % str(self
.switch
))
111 def startCompute(self
, name
, image
=None, command
=None, network
=None, flavor_name
="tiny"):
113 Create a new container as compute resource and connect it to this
115 :param name: name (string)
116 :param image: image name (string)
117 :param command: command (string)
118 :param network: networks list({"ip": "10.0.0.254/8"}, {"ip": "11.0.0.254/24"})
119 :param flavor_name: name of the flavor for this compute container
122 assert name
is not None
124 if name
in [c
.name
for c
in self
.net
.getAllContainers()]:
125 raise Exception("Container with name %s already exists." % name
)
126 # set default parameter
130 network
= {} # {"ip": "10.0.0.254/8"}
131 if isinstance(network
, dict):
132 network
= [network
] # if we have only one network, put it in a list
133 if isinstance(network
, list):
137 # allocate in resource resource model and compute resource limits for new container
138 if self
._resource
_model
is not None:
139 # TODO pass resource limits to new container (cf. Dockernet API) Issue #47
140 (cpu_limit
, mem_limit
, disk_limit
) = alloc
= self
._resource
_model
.allocate(name
, flavor_name
)
141 logging
.info("Allocation result: %r" % str(alloc
))
142 # create the container
143 d
= self
.net
.addDocker(
148 flavor_name
=flavor_name
)
149 # connect all given networks
151 # TODO we cannot use TCLink here (see: https://github.com/mpeuster/dockernet/issues/3)
152 self
.net
.addLink(d
, self
.switch
, params1
=nw
, cls
=Link
)
154 self
.containers
[name
] = d
155 return d
# we might use UUIDs for naming later on
157 def stopCompute(self
, name
):
159 Stop and remove a container from this data center.
161 assert name
is not None
162 if name
not in self
.containers
:
163 raise Exception("Container with name %s not found." % name
)
165 link
=None, node1
=self
.containers
[name
], node2
=self
.switch
)
166 self
.net
.removeDocker("%s" % (name
))
167 del self
.containers
[name
]
168 # call resource model and free resources
169 if self
._resource
_model
is not None:
170 self
._resource
_model
.free(name
)
173 def listCompute(self
):
175 Return a list of all running containers assigned to this
178 return list(self
.containers
.itervalues())
182 Return a dict with status information about this DC.
186 "internalname": self
.name
,
187 "switch": self
.switch
.name
,
188 "n_running_containers": len(self
.containers
),
189 "metadata": self
.metadata
192 def assignResourceModel(self
, rm
):
193 if self
._resource
_model
is not None:
194 raise Exception("There is already an resource model assigned to this DC.")
195 self
._resource
_model
= rm
196 self
.net
.rm_registrar
.register(self
, rm
)
197 logging
.info("Assigned RM: %r to DC: %r" % (rm
, self
))