return docker configuration data on compute start
[osm/vim-emu.git] / src / emuvim / api / zerorpc / compute.py
1 """
2 Distributed Cloud Emulator (dcemulator)
3 (c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
4 """
5
6 import logging
7 import threading
8 import zerorpc
9
10 logging.basicConfig(level=logging.INFO)
11
12
13 class ZeroRpcApiEndpoint(object):
14 """
15 Simple API endpoint that offers a zerorpc-based
16 interface. This interface will be used by the
17 default command line client.
18 It can be used as a reference to implement
19 REST interfaces providing the same semantics,
20 like e.g. OpenStack compute API.
21 """
22
23 def __init__(self, listenip, port):
24 self.dcs = {}
25 self.ip = listenip
26 self.port = port
27 logging.debug("Created API endpoint %s(%s:%d)" % (
28 self.__class__.__name__, self.ip, self.port))
29
30 def connectDatacenter(self, dc):
31 self.dcs[dc.label] = dc
32 logging.info("Connected DC(%s) to API endpoint %s(%s:%d)" % (
33 dc.label, self.__class__.__name__, self.ip, self.port))
34
35 def start(self):
36 thread = threading.Thread(target=self._api_server_thread, args=())
37 thread.daemon = True
38 thread.start()
39 logging.debug("Started API endpoint %s(%s:%d)" % (
40 self.__class__.__name__, self.ip, self.port))
41
42 def _api_server_thread(self):
43 s = zerorpc.Server(MultiDatacenterApi(self.dcs))
44 s.bind("tcp://%s:%d" % (self.ip, self.port))
45 s.run()
46
47
48 class MultiDatacenterApi(object):
49 """
50 Just pass through the corresponding request to the
51 selected data center. Do not implement provisioning
52 logic here because will will have multiple API
53 endpoint implementations at the end.
54 """
55
56 def __init__(self, dcs):
57 self.dcs = dcs
58
59 def compute_action_start(self, dc_label, compute_name, image, network=None, command=None):
60 """
61 Start a new compute instance: A docker container
62 :param dc_label: name of the DC
63 :param compute_name: compute container name
64 :param image: image name
65 :param command: command to execute
66 :param network: list of all interface of the vnf, with their parameters (id=id1,ip=x.x.x.x/x),...
67 :return: networks list({"id":"input","ip": "10.0.0.254/8"}, {"id":"output","ip": "11.0.0.254/24"})
68 """
69 # TODO what to return UUID / given name / internal name ?
70 logging.debug("RPC CALL: compute start")
71 try:
72 c = self.dcs.get(dc_label).startCompute(
73 compute_name, image=image, command=command, network=network)
74 #return str(c.name)
75 # return docker inspect dict
76 return c.getStatus()
77 except Exception as ex:
78 logging.exception("RPC error.")
79 return ex.message
80
81 def compute_action_stop(self, dc_label, compute_name):
82 logging.debug("RPC CALL: compute stop")
83 try:
84 return self.dcs.get(dc_label).stopCompute(compute_name)
85 except Exception as ex:
86 logging.exception("RPC error.")
87 return ex.message
88
89 def compute_list(self, dc_label):
90 logging.debug("RPC CALL: compute list")
91 try:
92 if dc_label is None:
93 # return list with all compute nodes in all DCs
94 all_containers = []
95 for dc in self.dcs.itervalues():
96 all_containers += dc.listCompute()
97 return [(c.name, c.getStatus())
98 for c in all_containers]
99 else:
100 # return list of compute nodes for specified DC
101 return [(c.name, c.getStatus())
102 for c in self.dcs.get(dc_label).listCompute()]
103 except Exception as ex:
104 logging.exception("RPC error.")
105 return ex.message
106
107 def compute_status(self, dc_label, compute_name):
108 logging.debug("RPC CALL: compute status")
109 try:
110 return self.dcs.get(
111 dc_label).containers.get(compute_name).getStatus()
112 except Exception as ex:
113 logging.exception("RPC error.")
114 return ex.message
115
116 def datacenter_list(self):
117 logging.debug("RPC CALL: datacenter list")
118 try:
119 return [d.getStatus() for d in self.dcs.itervalues()]
120 except Exception as ex:
121 logging.exception("RPC error.")
122 return ex.message
123
124 def datacenter_status(self, dc_label):
125 logging.debug("RPC CALL: datacenter status")
126 try:
127 return self.dcs.get(dc_label).getStatus()
128 except Exception as ex:
129 logging.exception("RPC error.")
130 return ex.message