Initial resource limitation model API and classes.
[osm/vim-emu.git] / src / emuvim / dcemulator / node.py
1 """
2 Distributed Cloud Emulator (dcemulator)
3 (c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
4 """
5 from mininet.node import Docker
6 from mininet.link import Link
7 import logging
8
9
10 DCDPID_BASE = 1000 # start of switch dpid's used for data center switches
11
12
13 class 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)
25 self.datacenter = kwargs.get("datacenter") # pointer to current DC
26 self.flavor_name = kwargs.get("flavor_name")
27
28 # call original Docker.__init__
29 Docker.__init__(self, name, dimage, **kwargs)
30
31 def getNetworkStatus(self):
32 """
33 Helper method to receive information about the virtual networks
34 this compute instance is connected to.
35 """
36 # format list of tuples (name, Ip, MAC, isUp, status)
37 return [(str(i), i.IP(), i.MAC(), i.isUp(), i.status())
38 for i in self.intfList()]
39
40 def getStatus(self):
41 """
42 Helper method to receive information about this compute instance.
43 """
44 status = {}
45 status["name"] = self.name
46 status["network"] = self.getNetworkStatus()
47 status["image"] = self.dimage
48 status["cpu_quota"] = self.cpu_quota
49 status["cpu_period"] = self.cpu_period
50 status["cpu_shares"] = self.cpu_shares
51 status["cpuset"] = self.cpuset
52 status["mem_limit"] = self.mem_limit
53 status["memswap_limit"] = self.memswap_limit
54 status["state"] = self.dcli.inspect_container(self.dc)["State"]
55 status["id"] = self.dcli.inspect_container(self.dc)["Id"]
56 status["datacenter"] = (None if self.datacenter is None
57 else self.datacenter.label)
58 return status
59
60
61 class Datacenter(object):
62 """
63 Represents a logical data center to which compute resources
64 (Docker containers) can be added at runtime.
65
66 Will also implement resource bookkeeping in later versions.
67 """
68
69 DC_COUNTER = 1
70
71 def __init__(self, label, metadata={}):
72 self.net = None # DCNetwork to which we belong
73 # each node (DC) has a short internal name used by Mininet
74 # this is caused by Mininets naming limitations for swtiches etc.
75 self.name = "dc%d" % Datacenter.DC_COUNTER
76 Datacenter.DC_COUNTER += 1
77 # use this for user defined names that can be longer than self.name
78 self.label = label
79 # dict to store arbitrary metadata (e.g. latitude and longitude)
80 self.metadata = metadata
81 # first prototype assumes one "bigswitch" per DC
82 self.switch = None
83 # keep track of running containers
84 self.containers = {}
85 # pointer to assigned resource model
86 self._resource_model = None
87
88 def __repr__(self):
89 return self.label
90
91 def _get_next_dc_dpid(self):
92 global DCDPID_BASE
93 DCDPID_BASE += 1
94 return DCDPID_BASE
95
96 def create(self):
97 """
98 Each data center is represented by a single switch to which
99 compute resources can be connected at run time.
100
101 TODO: This will be changed in the future to support multiple networks
102 per data center
103 """
104 self.switch = self.net.addSwitch(
105 "%s.s1" % self.name, dpid=hex(self._get_next_dc_dpid())[2:])
106 logging.debug("created data center switch: %s" % str(self.switch))
107
108 def start(self):
109 pass
110
111 def startCompute(self, name, image=None, command=None, network=None, flavor_name="tiny"):
112 """
113 Create a new container as compute resource and connect it to this
114 data center.
115 :param name: name (string)
116 :param image: image name (string)
117 :param command: command (string)
118 :param network: networks list({"ip": "10.0.0.254/8"}, {"ip": "11.0.0.254/24"})
119 :param flavor_name: name of the flavor for this compute container
120 :return:
121 """
122 assert name is not None
123 # no duplications
124 if name in [c.name for c in self.net.getAllContainers()]:
125 raise Exception("Container with name %s already exists." % name)
126 # set default parameter
127 if image is None:
128 image = "ubuntu"
129 if network is None:
130 network = {} # {"ip": "10.0.0.254/8"}
131 if isinstance(network, dict):
132 network = [network] # if we have only one network, put it in a list
133 if isinstance(network, list):
134 if len(network) < 1:
135 network.append({})
136
137 # allocate in resource resource model and compute resource limits for new container
138 if self._resource_model is not None:
139 # TODO pass resource limits to new container (cf. Dockernet API)
140 cpu_limit, mem_limit, disk_limit = self._resource_model.allocate(name, flavor_name)
141 # create the container
142 d = self.net.addDocker(
143 "%s" % (name),
144 dimage=image,
145 dcmd=command,
146 datacenter=self,
147 flavor_name=flavor_name)
148 # connect all given networks
149 for nw in network:
150 # TODO we cannot use TCLink here (see: https://github.com/mpeuster/dockernet/issues/3)
151 self.net.addLink(d, self.switch, params1=nw, cls=Link)
152 # do bookkeeping
153 self.containers[name] = d
154 return d # we might use UUIDs for naming later on
155
156 def stopCompute(self, name):
157 """
158 Stop and remove a container from this data center.
159 """
160 assert name is not None
161 if name not in self.containers:
162 raise Exception("Container with name %s not found." % name)
163 self.net.removeLink(
164 link=None, node1=self.containers[name], node2=self.switch)
165 self.net.removeDocker("%s" % (name))
166 del self.containers[name]
167 # call resource model and free resources
168 if self._resource_model is not None:
169 self._resource_model.free(name)
170 return True
171
172 def listCompute(self):
173 """
174 Return a list of all running containers assigned to this
175 data center.
176 """
177 return list(self.containers.itervalues())
178
179 def getStatus(self):
180 """
181 Return a dict with status information about this DC.
182 """
183 return {
184 "label": self.label,
185 "internalname": self.name,
186 "switch": self.switch.name,
187 "n_running_containers": len(self.containers),
188 "metadata": self.metadata
189 }
190
191 def assignResourceModel(self, rm):
192 if self._resource_model is not None:
193 raise Exception("There is already an resource model assigned to this DC.")
194 self._resource_model = rm
195 self.net.rm_registrar.register(self, rm)
196 logging.info("Assigned RM: %r to DC: %r" % (rm, self))
197