blob: 336126c72c422a876ddc268b58639778940b355d [file] [log] [blame]
peustermcbcd4c22015-12-28 11:33:42 +01001"""
2Distributed Cloud Emulator (dcemulator)
3(c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
4"""
peusterm7aae6852016-01-12 14:53:18 +01005from mininet.node import Docker
peustermcbcd4c22015-12-28 11:33:42 +01006import logging
7
8
9DCDPID_BASE = 1000 # start of switch dpid's used for data center switches
10
11
peusterm7aae6852016-01-12 14:53:18 +010012class EmulatorCompute(Docker):
13 """
14 Emulator specific compute node class.
15 Inherits from Dockernet's Docker host class.
16 Represents a single container connected to a (logical)
17 data center.
18 We can add emulator specific helper functions to it.
19 """
20
21 def __init__(
22 self, name, dimage, **kwargs):
23 logging.debug("Create EmulatorCompute instance: %s" % name)
peusterm2ec74e12016-01-13 11:17:53 +010024 self.datacenter = None # pointer to current DC
peusterm7aae6852016-01-12 14:53:18 +010025
26 # call original Docker.__init__
27 Docker.__init__(self, name, dimage, **kwargs)
28
29 def getNetworkStatus(self):
30 """
31 Helper method to receive information about the virtual networks
32 this compute instance is connected to.
33 """
peusterm056fd452016-01-12 15:32:25 +010034 # 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()]
peusterm7aae6852016-01-12 14:53:18 +010037
38 def getStatus(self):
39 """
40 Helper method to receive information about this compute instance.
41 """
peusterm056fd452016-01-12 15:32:25 +010042 status = {}
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"]
peusterm2ec74e12016-01-13 11:17:53 +010054 status["datacenter"] = (None if self.datacenter is None
peusterma47db032016-02-04 14:55:29 +010055 else self.datacenter.label)
peusterm056fd452016-01-12 15:32:25 +010056 return status
peusterm7aae6852016-01-12 14:53:18 +010057
58
peustermcbcd4c22015-12-28 11:33:42 +010059class Datacenter(object):
60 """
61 Represents a logical data center to which compute resources
62 (Docker containers) can be added at runtime.
peusterme4e89d32016-01-07 09:14:54 +010063
64 Will also implement resource bookkeeping in later versions.
peustermcbcd4c22015-12-28 11:33:42 +010065 """
66
peusterma47db032016-02-04 14:55:29 +010067 DC_COUNTER = 1
68
peusterm53504942016-02-04 16:09:28 +010069 def __init__(self, label, metadata={}):
peustermcbcd4c22015-12-28 11:33:42 +010070 self.net = None # DCNetwork to which we belong
peusterma47db032016-02-04 14:55:29 +010071 # 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
peusterm53504942016-02-04 16:09:28 +010075 # use this for user defined names that can be longer than self.name
76 self.label = label
77 # dict to store arbitrary metadata (e.g. latitude and longitude)
78 self.metadata = metadata
peustermcbcd4c22015-12-28 11:33:42 +010079 self.switch = None # first prototype assumes one "bigswitch" per DC
peusterma2ad9ff2016-01-11 17:10:07 +010080 self.containers = {} # keep track of running containers
peustermcbcd4c22015-12-28 11:33:42 +010081
82 def _get_next_dc_dpid(self):
83 global DCDPID_BASE
84 DCDPID_BASE += 1
85 return DCDPID_BASE
86
87 def create(self):
88 """
89 Each data center is represented by a single switch to which
90 compute resources can be connected at run time.
91
peusterm9c252b62016-01-06 16:59:53 +010092 TODO: This will be changed in the future to support multiple networks
peustermcbcd4c22015-12-28 11:33:42 +010093 per data center
94 """
peusterm293cbc32016-01-13 17:05:28 +010095 self.switch = self.net.addSwitch(
peustermcbcd4c22015-12-28 11:33:42 +010096 "%s.s1" % self.name, dpid=hex(self._get_next_dc_dpid())[2:])
97 logging.debug("created data center switch: %s" % str(self.switch))
98
99 def start(self):
100 pass
101
peusterm7f8e8402016-02-28 18:38:10 +0100102 def startCompute(self, name, image=None, command=None, network=None):
peusterme4e89d32016-01-07 09:14:54 +0100103 """
104 Create a new container as compute resource and connect it to this
105 data center.
peusterm7f8e8402016-02-28 18:38:10 +0100106 :param name: name (string)
107 :param image: image name (string)
108 :param command: command (string)
109 :param network: networks list({"ip": "10.0.0.254/8"}, {"ip": "11.0.0.254/24"})
110 :return:
peusterme4e89d32016-01-07 09:14:54 +0100111 """
peusterm4e98b632016-01-12 14:08:07 +0100112 assert name is not None
peusterm9165ef92016-01-13 13:50:39 +0100113 # no duplications
peustermbd44f4a2016-01-13 14:53:30 +0100114 if name in [c.name for c in self.net.getAllContainers()]:
peusterm9165ef92016-01-13 13:50:39 +0100115 raise Exception("Container with name %s already exists." % name)
peusterm4e98b632016-01-12 14:08:07 +0100116 # set default parameter
117 if image is None:
118 image = "ubuntu"
119 if network is None:
120 network = {} # {"ip": "10.0.0.254/8"}
peusterm7f8e8402016-02-28 18:38:10 +0100121 if isinstance(network, dict):
122 network = [network] # if we have only one network, put it in a list
123 if isinstance(network, list):
124 if len(network) < 1:
125 network.append({})
126
127 # create the container
stevenvanrosseme55975e2016-02-17 11:42:55 +0100128 d = self.net.addDocker("%s" % (name), dimage=image, dcmd=command)
peusterm7f8e8402016-02-28 18:38:10 +0100129 # connect all given networks
130 for nw in network:
131 self.net.addLink(d, self.switch, params1=nw)
peusterm2ec74e12016-01-13 11:17:53 +0100132 # do bookkeeping
peusterma2ad9ff2016-01-11 17:10:07 +0100133 self.containers[name] = d
peusterm2ec74e12016-01-13 11:17:53 +0100134 d.datacenter = self
peustermfa4bcc72016-01-15 11:08:09 +0100135 return d # we might use UUIDs for naming later on
peustermcbcd4c22015-12-28 11:33:42 +0100136
peusterm7aae6852016-01-12 14:53:18 +0100137 def stopCompute(self, name):
peusterma2ad9ff2016-01-11 17:10:07 +0100138 """
139 Stop and remove a container from this data center.
140 """
peusterm9165ef92016-01-13 13:50:39 +0100141 assert name is not None
142 if name not in self.containers:
143 raise Exception("Container with name %s not found." % name)
peusterma2ad9ff2016-01-11 17:10:07 +0100144 self.net.removeLink(
145 link=None, node1=self.containers[name], node2=self.switch)
peustermc3b977e2016-01-12 10:09:35 +0100146 self.net.removeDocker("%s" % (name))
peusterma2ad9ff2016-01-11 17:10:07 +0100147 del self.containers[name]
peusterm4e98b632016-01-12 14:08:07 +0100148 return True
149
150 def listCompute(self):
151 """
152 Return a list of all running containers assigned to this
153 data center.
154 """
peusterm5aa8cf22016-01-15 11:12:17 +0100155 return list(self.containers.itervalues())
peustermd313dc12016-02-04 15:36:02 +0100156
157 def getStatus(self):
158 """
159 Return a dict with status information about this DC.
160 """
peusterm53504942016-02-04 16:09:28 +0100161 return {
162 "label": self.label,
163 "internalname": self.name,
164 "switch": self.switch.name,
165 "n_running_containers": len(self.containers),
166 "metadata": self.metadata
167 }