blob: 8a0813165658a0511a18bb3932db9756be736523 [file] [log] [blame]
peusterm9c252b62016-01-06 16:59:53 +01001"""
2Distributed Cloud Emulator (dcemulator)
3(c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
4"""
5
6import logging
7import threading
8import zerorpc
9
peustermbd44f4a2016-01-13 14:53:30 +010010logging.basicConfig(level=logging.INFO)
peusterm9c252b62016-01-06 16:59:53 +010011
12
13class ZeroRpcApiEndpoint(object):
peusterme4e89d32016-01-07 09:14:54 +010014 """
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 """
peusterm9c252b62016-01-06 16:59:53 +010022
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.name] = dc
32 logging.info("Connected DC(%s) to API endpoint %s(%s:%d)" % (
33 dc.name, 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
48class MultiDatacenterApi(object):
peusterme4e89d32016-01-07 09:14:54 +010049 """
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 """
peusterm9c252b62016-01-06 16:59:53 +010055
56 def __init__(self, dcs):
57 self.dcs = dcs
58
59 def compute_action_start(self, dc_name, compute_name):
peusterm4e98b632016-01-12 14:08:07 +010060 # TODO what to return UUID / given name / internal name ?
peusterm70baaf82016-01-06 17:14:40 +010061 logging.debug("RPC CALL: compute start")
peusterm7aae6852016-01-12 14:53:18 +010062 try:
peustermfa4bcc72016-01-15 11:08:09 +010063 c = self.dcs.get(dc_name).startCompute(compute_name)
64 return str(c.name)
peusterm7aae6852016-01-12 14:53:18 +010065 except Exception as ex:
66 logging.exception("RPC error.")
67 return ex.message
peusterm9c252b62016-01-06 16:59:53 +010068
69 def compute_action_stop(self, dc_name, compute_name):
peustermbd44f4a2016-01-13 14:53:30 +010070 logging.debug("RPC CALL: compute stop")
peusterm7aae6852016-01-12 14:53:18 +010071 try:
72 return self.dcs.get(dc_name).stopCompute(compute_name)
73 except Exception as ex:
74 logging.exception("RPC error.")
75 return ex.message
peusterm9c252b62016-01-06 16:59:53 +010076
peusterm4e98b632016-01-12 14:08:07 +010077 def compute_list(self, dc_name):
peustermbd44f4a2016-01-13 14:53:30 +010078 logging.debug("RPC CALL: compute list")
peusterm7aae6852016-01-12 14:53:18 +010079 try:
peusterm2ec74e12016-01-13 11:17:53 +010080 if dc_name is None:
81 # return list with all compute nodes in all DCs
82 all_containers = []
83 for dc in self.dcs.itervalues():
84 all_containers += dc.listCompute()
85 return [(c.name, c.getStatus())
86 for c in all_containers]
87 else:
88 # return list of compute nodes for specified DC
89 return [(c.name, c.getStatus())
90 for c in self.dcs.get(dc_name).listCompute()]
peusterm7aae6852016-01-12 14:53:18 +010091 except Exception as ex:
92 logging.exception("RPC error.")
93 return ex.message
peusterm4e98b632016-01-12 14:08:07 +010094
95 def compute_status(self, dc_name, compute_name):
peustermbd44f4a2016-01-13 14:53:30 +010096 logging.debug("RPC CALL: compute status")
peusterm7aae6852016-01-12 14:53:18 +010097 try:
98 return self.dcs.get(
99 dc_name).containers.get(compute_name).getStatus()
100 except Exception as ex:
101 logging.exception("RPC error.")
102 return ex.message