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