2 Distributed Cloud Emulator (dcemulator)
3 (c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
5 from mininet
.node
import Docker
9 DCDPID_BASE
= 1000 # start of switch dpid's used for data center switches
12 class EmulatorCompute(Docker
):
14 Emulator specific compute node class.
15 Inherits from Dockernet's Docker host class.
16 Represents a single container connected to a (logical)
18 We can add emulator specific helper functions to it.
22 self
, name
, dimage
, **kwargs
):
23 logging
.debug("Create EmulatorCompute instance: %s" % name
)
24 self
.datacenter
= None # pointer to current DC
26 # call original Docker.__init__
27 Docker
.__init
__(self
, name
, dimage
, **kwargs
)
29 def getNetworkStatus(self
):
31 Helper method to receive information about the virtual networks
32 this compute instance is connected to.
34 # format list of tuples (name, Ip, MAC, isUp, status)
35 return [(str(i
), i
.IP(), i
.MAC(), i
.isUp(), i
.status())
36 for i
in self
.intfList()]
40 Helper method to receive information about this compute instance.
43 status
["name"] = self
.name
44 status
["network"] = self
.getNetworkStatus()
45 status
["image"] = self
.dimage
46 status
["cpu_quota"] = self
.cpu_quota
47 status
["cpu_period"] = self
.cpu_period
48 status
["cpu_shares"] = self
.cpu_shares
49 status
["cpuset"] = self
.cpuset
50 status
["mem_limit"] = self
.mem_limit
51 status
["memswap_limit"] = self
.memswap_limit
52 status
["state"] = self
.dcli
.inspect_container(self
.dc
)["State"]
53 status
["id"] = self
.dcli
.inspect_container(self
.dc
)["Id"]
54 status
["datacenter"] = (None if self
.datacenter
is None
55 else self
.datacenter
.label
)
59 class Datacenter(object):
61 Represents a logical data center to which compute resources
62 (Docker containers) can be added at runtime.
64 Will also implement resource bookkeeping in later versions.
69 def __init__(self
, label
, metadata
={}):
70 self
.net
= None # DCNetwork to which we belong
71 # each node (DC) has a short internal name used by Mininet
72 # this is caused by Mininets naming limitations for swtiches etc.
73 self
.name
= "dc%d" % Datacenter
.DC_COUNTER
74 Datacenter
.DC_COUNTER
+= 1
75 # use this for user defined names that can be longer than self.name
77 # dict to store arbitrary metadata (e.g. latitude and longitude)
78 self
.metadata
= metadata
79 self
.switch
= None # first prototype assumes one "bigswitch" per DC
80 self
.containers
= {} # keep track of running containers
82 def _get_next_dc_dpid(self
):
89 Each data center is represented by a single switch to which
90 compute resources can be connected at run time.
92 TODO: This will be changed in the future to support multiple networks
95 self
.switch
= self
.net
.addSwitch(
96 "%s.s1" % self
.name
, dpid
=hex(self
._get
_next
_dc
_dpid
())[2:])
97 logging
.debug("created data center switch: %s" % str(self
.switch
))
102 def startCompute(self
, name
, image
=None, network
=None):
104 Create a new container as compute resource and connect it to this
107 TODO: This interface will change to support multiple networks to which
108 a single container can be connected.
110 assert name
is not None
112 if name
in [c
.name
for c
in self
.net
.getAllContainers()]:
113 raise Exception("Container with name %s already exists." % name
)
114 # set default parameter
118 network
= {} # {"ip": "10.0.0.254/8"}
119 # create the container and connect it to the given network
120 d
= self
.net
.addDocker("%s" % (name
), dimage
=image
)
121 self
.net
.addLink(d
, self
.switch
, params1
=network
)
123 self
.containers
[name
] = d
125 return d
# we might use UUIDs for naming later on
127 def stopCompute(self
, name
):
129 Stop and remove a container from this data center.
131 assert name
is not None
132 if name
not in self
.containers
:
133 raise Exception("Container with name %s not found." % name
)
135 link
=None, node1
=self
.containers
[name
], node2
=self
.switch
)
136 self
.net
.removeDocker("%s" % (name
))
137 del self
.containers
[name
]
140 def listCompute(self
):
142 Return a list of all running containers assigned to this
145 return list(self
.containers
.itervalues())
149 Return a dict with status information about this DC.
153 "internalname": self
.name
,
154 "switch": self
.switch
.name
,
155 "n_running_containers": len(self
.containers
),
156 "metadata": self
.metadata