blob: 74540ab3304359db3adca2483d561d91e1e8ece0 [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
peustermea8db832016-03-08 10:25:58 +01006from mininet.link import Link
peustermcbcd4c22015-12-28 11:33:42 +01007import logging
8
9
10DCDPID_BASE = 1000 # start of switch dpid's used for data center switches
11
12
peusterm7aae6852016-01-12 14:53:18 +010013class EmulatorCompute(Docker):
14 """
15 Emulator specific compute node class.
16 Inherits from Dockernet's Docker host class.
17 Represents a single container connected to a (logical)
18 data center.
19 We can add emulator specific helper functions to it.
20 """
21
22 def __init__(
23 self, name, dimage, **kwargs):
24 logging.debug("Create EmulatorCompute instance: %s" % name)
peusterm2ec74e12016-01-13 11:17:53 +010025 self.datacenter = None # pointer to current DC
peusterm7aae6852016-01-12 14:53:18 +010026
27 # call original Docker.__init__
28 Docker.__init__(self, name, dimage, **kwargs)
29
30 def getNetworkStatus(self):
31 """
32 Helper method to receive information about the virtual networks
33 this compute instance is connected to.
34 """
peusterm056fd452016-01-12 15:32:25 +010035 # 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()]
peusterm7aae6852016-01-12 14:53:18 +010038
39 def getStatus(self):
40 """
41 Helper method to receive information about this compute instance.
42 """
peusterm056fd452016-01-12 15:32:25 +010043 status = {}
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"]
peusterm2ec74e12016-01-13 11:17:53 +010055 status["datacenter"] = (None if self.datacenter is None
peusterma47db032016-02-04 14:55:29 +010056 else self.datacenter.label)
peusterm056fd452016-01-12 15:32:25 +010057 return status
peusterm7aae6852016-01-12 14:53:18 +010058
59
peustermcbcd4c22015-12-28 11:33:42 +010060class Datacenter(object):
61 """
62 Represents a logical data center to which compute resources
63 (Docker containers) can be added at runtime.
peusterme4e89d32016-01-07 09:14:54 +010064
65 Will also implement resource bookkeeping in later versions.
peustermcbcd4c22015-12-28 11:33:42 +010066 """
67
peusterma47db032016-02-04 14:55:29 +010068 DC_COUNTER = 1
69
peusterm53504942016-02-04 16:09:28 +010070 def __init__(self, label, metadata={}):
peustermcbcd4c22015-12-28 11:33:42 +010071 self.net = None # DCNetwork to which we belong
peusterma47db032016-02-04 14:55:29 +010072 # 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
peusterm53504942016-02-04 16:09:28 +010076 # use this for user defined names that can be longer than self.name
77 self.label = label
78 # dict to store arbitrary metadata (e.g. latitude and longitude)
79 self.metadata = metadata
peustermcbcd4c22015-12-28 11:33:42 +010080 self.switch = None # first prototype assumes one "bigswitch" per DC
peusterma2ad9ff2016-01-11 17:10:07 +010081 self.containers = {} # keep track of running containers
peustermcbcd4c22015-12-28 11:33:42 +010082
83 def _get_next_dc_dpid(self):
84 global DCDPID_BASE
85 DCDPID_BASE += 1
86 return DCDPID_BASE
87
88 def create(self):
89 """
90 Each data center is represented by a single switch to which
91 compute resources can be connected at run time.
92
peusterm9c252b62016-01-06 16:59:53 +010093 TODO: This will be changed in the future to support multiple networks
peustermcbcd4c22015-12-28 11:33:42 +010094 per data center
95 """
peusterm293cbc32016-01-13 17:05:28 +010096 self.switch = self.net.addSwitch(
peustermcbcd4c22015-12-28 11:33:42 +010097 "%s.s1" % self.name, dpid=hex(self._get_next_dc_dpid())[2:])
98 logging.debug("created data center switch: %s" % str(self.switch))
99
100 def start(self):
101 pass
102
peusterm7f8e8402016-02-28 18:38:10 +0100103 def startCompute(self, name, image=None, command=None, network=None):
peusterme4e89d32016-01-07 09:14:54 +0100104 """
105 Create a new container as compute resource and connect it to this
106 data center.
peusterm7f8e8402016-02-28 18:38:10 +0100107 :param name: name (string)
108 :param image: image name (string)
109 :param command: command (string)
110 :param network: networks list({"ip": "10.0.0.254/8"}, {"ip": "11.0.0.254/24"})
111 :return:
peusterme4e89d32016-01-07 09:14:54 +0100112 """
peusterm4e98b632016-01-12 14:08:07 +0100113 assert name is not None
peusterm9165ef92016-01-13 13:50:39 +0100114 # no duplications
peustermbd44f4a2016-01-13 14:53:30 +0100115 if name in [c.name for c in self.net.getAllContainers()]:
peusterm9165ef92016-01-13 13:50:39 +0100116 raise Exception("Container with name %s already exists." % name)
peusterm4e98b632016-01-12 14:08:07 +0100117 # set default parameter
118 if image is None:
119 image = "ubuntu"
120 if network is None:
121 network = {} # {"ip": "10.0.0.254/8"}
peusterm7f8e8402016-02-28 18:38:10 +0100122 if isinstance(network, dict):
123 network = [network] # if we have only one network, put it in a list
124 if isinstance(network, list):
125 if len(network) < 1:
126 network.append({})
127
128 # create the container
stevenvanrosseme55975e2016-02-17 11:42:55 +0100129 d = self.net.addDocker("%s" % (name), dimage=image, dcmd=command)
peusterm7f8e8402016-02-28 18:38:10 +0100130 # connect all given networks
131 for nw in network:
peustermea8db832016-03-08 10:25:58 +0100132 # TODO we cannot use TCLink here (see: https://github.com/mpeuster/dockernet/issues/3)
133 self.net.addLink(d, self.switch, params1=nw, cls=Link)
peusterm2ec74e12016-01-13 11:17:53 +0100134 # do bookkeeping
peusterma2ad9ff2016-01-11 17:10:07 +0100135 self.containers[name] = d
peusterm2ec74e12016-01-13 11:17:53 +0100136 d.datacenter = self
peustermfa4bcc72016-01-15 11:08:09 +0100137 return d # we might use UUIDs for naming later on
peustermcbcd4c22015-12-28 11:33:42 +0100138
peusterm7aae6852016-01-12 14:53:18 +0100139 def stopCompute(self, name):
peusterma2ad9ff2016-01-11 17:10:07 +0100140 """
141 Stop and remove a container from this data center.
142 """
peusterm9165ef92016-01-13 13:50:39 +0100143 assert name is not None
144 if name not in self.containers:
145 raise Exception("Container with name %s not found." % name)
peusterma2ad9ff2016-01-11 17:10:07 +0100146 self.net.removeLink(
147 link=None, node1=self.containers[name], node2=self.switch)
peustermc3b977e2016-01-12 10:09:35 +0100148 self.net.removeDocker("%s" % (name))
peusterma2ad9ff2016-01-11 17:10:07 +0100149 del self.containers[name]
peusterm4e98b632016-01-12 14:08:07 +0100150 return True
151
152 def listCompute(self):
153 """
154 Return a list of all running containers assigned to this
155 data center.
156 """
peusterm5aa8cf22016-01-15 11:12:17 +0100157 return list(self.containers.itervalues())
peustermd313dc12016-02-04 15:36:02 +0100158
159 def getStatus(self):
160 """
161 Return a dict with status information about this DC.
162 """
peusterm53504942016-02-04 16:09:28 +0100163 return {
164 "label": self.label,
165 "internalname": self.name,
166 "switch": self.switch.name,
167 "n_running_containers": len(self.containers),
168 "metadata": self.metadata
169 }