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
= None # pointer to current DC
27 # call original Docker.__init__
28 Docker
.__init
__(self
, name
, dimage
, **kwargs
)
30 def getNetworkStatus(self
):
32 Helper method to receive information about the virtual networks
33 this compute instance is connected to.
35 # format list of tuples (name, Ip, MAC, isUp, status)
36 return [(str(i
), i
.IP(), i
.MAC(), i
.isUp(), i
.status())
37 for i
in self
.intfList()]
41 Helper method to receive information about this compute instance.
44 status
["name"] = self
.name
45 status
["network"] = self
.getNetworkStatus()
46 status
["image"] = self
.dimage
47 status
["cpu_quota"] = self
.cpu_quota
48 status
["cpu_period"] = self
.cpu_period
49 status
["cpu_shares"] = self
.cpu_shares
50 status
["cpuset"] = self
.cpuset
51 status
["mem_limit"] = self
.mem_limit
52 status
["memswap_limit"] = self
.memswap_limit
53 status
["state"] = self
.dcli
.inspect_container(self
.dc
)["State"]
54 status
["id"] = self
.dcli
.inspect_container(self
.dc
)["Id"]
55 status
["datacenter"] = (None if self
.datacenter
is None
56 else self
.datacenter
.label
)
60 class Datacenter(object):
62 Represents a logical data center to which compute resources
63 (Docker containers) can be added at runtime.
65 Will also implement resource bookkeeping in later versions.
70 def __init__(self
, label
, metadata
={}):
71 self
.net
= None # DCNetwork to which we belong
72 # each node (DC) has a short internal name used by Mininet
73 # this is caused by Mininets naming limitations for swtiches etc.
74 self
.name
= "dc%d" % Datacenter
.DC_COUNTER
75 Datacenter
.DC_COUNTER
+= 1
76 # use this for user defined names that can be longer than self.name
78 # dict to store arbitrary metadata (e.g. latitude and longitude)
79 self
.metadata
= metadata
80 self
.switch
= None # first prototype assumes one "bigswitch" per DC
81 self
.containers
= {} # keep track of running containers
86 def _get_next_dc_dpid(self
):
93 Each data center is represented by a single switch to which
94 compute resources can be connected at run time.
96 TODO: This will be changed in the future to support multiple networks
99 self
.switch
= self
.net
.addSwitch(
100 "%s.s1" % self
.name
, dpid
=hex(self
._get
_next
_dc
_dpid
())[2:])
101 logging
.debug("created data center switch: %s" % str(self
.switch
))
106 def startCompute(self
, name
, image
=None, command
=None, network
=None):
108 Create a new container as compute resource and connect it to this
110 :param name: name (string)
111 :param image: image name (string)
112 :param command: command (string)
113 :param network: networks list({"ip": "10.0.0.254/8"}, {"ip": "11.0.0.254/24"})
116 assert name
is not None
118 if name
in [c
.name
for c
in self
.net
.getAllContainers()]:
119 raise Exception("Container with name %s already exists." % name
)
120 # set default parameter
124 network
= {} # {"ip": "10.0.0.254/8"}
125 if isinstance(network
, dict):
126 network
= [network
] # if we have only one network, put it in a list
127 if isinstance(network
, list):
131 # create the container
132 d
= self
.net
.addDocker("%s" % (name
), dimage
=image
, dcmd
=command
)
133 # connect all given networks
135 # TODO we cannot use TCLink here (see: https://github.com/mpeuster/dockernet/issues/3)
136 self
.net
.addLink(d
, self
.switch
, params1
=nw
, cls
=Link
)
138 self
.containers
[name
] = d
140 return d
# we might use UUIDs for naming later on
142 def stopCompute(self
, name
):
144 Stop and remove a container from this data center.
146 assert name
is not None
147 if name
not in self
.containers
:
148 raise Exception("Container with name %s not found." % name
)
150 link
=None, node1
=self
.containers
[name
], node2
=self
.switch
)
151 self
.net
.removeDocker("%s" % (name
))
152 del self
.containers
[name
]
155 def listCompute(self
):
157 Return a list of all running containers assigned to this
160 return list(self
.containers
.itervalues())
164 Return a dict with status information about this DC.
168 "internalname": self
.name
,
169 "switch": self
.switch
.name
,
170 "n_running_containers": len(self
.containers
),
171 "metadata": self
.metadata