blob: d50693012b266550268ccc4fe6095fecb1749606 [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
55 else self.datacenter.name)
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
67 def __init__(self, name):
68 self.net = None # DCNetwork to which we belong
69 self.name = name
70 self.switch = None # first prototype assumes one "bigswitch" per DC
peusterma2ad9ff2016-01-11 17:10:07 +010071 self.containers = {} # keep track of running containers
peustermcbcd4c22015-12-28 11:33:42 +010072
73 def _get_next_dc_dpid(self):
74 global DCDPID_BASE
75 DCDPID_BASE += 1
76 return DCDPID_BASE
77
78 def create(self):
79 """
80 Each data center is represented by a single switch to which
81 compute resources can be connected at run time.
82
peusterm9c252b62016-01-06 16:59:53 +010083 TODO: This will be changed in the future to support multiple networks
peustermcbcd4c22015-12-28 11:33:42 +010084 per data center
85 """
peusterm293cbc32016-01-13 17:05:28 +010086 self.switch = self.net.addSwitch(
peustermcbcd4c22015-12-28 11:33:42 +010087 "%s.s1" % self.name, dpid=hex(self._get_next_dc_dpid())[2:])
88 logging.debug("created data center switch: %s" % str(self.switch))
89
90 def start(self):
91 pass
92
peusterm7aae6852016-01-12 14:53:18 +010093 def startCompute(self, name, image=None, network=None):
peusterme4e89d32016-01-07 09:14:54 +010094 """
95 Create a new container as compute resource and connect it to this
96 data center.
peusterm4e98b632016-01-12 14:08:07 +010097
98 TODO: This interface will change to support multiple networks to which
99 a single container can be connected.
peusterme4e89d32016-01-07 09:14:54 +0100100 """
peusterm4e98b632016-01-12 14:08:07 +0100101 assert name is not None
peusterm9165ef92016-01-13 13:50:39 +0100102 # no duplications
peustermbd44f4a2016-01-13 14:53:30 +0100103 if name in [c.name for c in self.net.getAllContainers()]:
peusterm9165ef92016-01-13 13:50:39 +0100104 raise Exception("Container with name %s already exists." % name)
peusterm4e98b632016-01-12 14:08:07 +0100105 # set default parameter
106 if image is None:
107 image = "ubuntu"
108 if network is None:
109 network = {} # {"ip": "10.0.0.254/8"}
110 # create the container and connect it to the given network
111 d = self.net.addDocker("%s" % (name), dimage=image)
112 self.net.addLink(d, self.switch, params1=network)
peusterm2ec74e12016-01-13 11:17:53 +0100113 # do bookkeeping
peusterma2ad9ff2016-01-11 17:10:07 +0100114 self.containers[name] = d
peusterm2ec74e12016-01-13 11:17:53 +0100115 d.datacenter = self
peustermfa4bcc72016-01-15 11:08:09 +0100116 return d # we might use UUIDs for naming later on
peustermcbcd4c22015-12-28 11:33:42 +0100117
peusterm7aae6852016-01-12 14:53:18 +0100118 def stopCompute(self, name):
peusterma2ad9ff2016-01-11 17:10:07 +0100119 """
120 Stop and remove a container from this data center.
121 """
peusterm9165ef92016-01-13 13:50:39 +0100122 assert name is not None
123 if name not in self.containers:
124 raise Exception("Container with name %s not found." % name)
peusterma2ad9ff2016-01-11 17:10:07 +0100125 self.net.removeLink(
126 link=None, node1=self.containers[name], node2=self.switch)
peustermc3b977e2016-01-12 10:09:35 +0100127 self.net.removeDocker("%s" % (name))
peusterma2ad9ff2016-01-11 17:10:07 +0100128 del self.containers[name]
peusterm4e98b632016-01-12 14:08:07 +0100129 return True
130
131 def listCompute(self):
132 """
133 Return a list of all running containers assigned to this
134 data center.
135 """
136 return self.containers.itervalues()