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