| peusterm | 9c252b6 | 2016-01-06 16:59:53 +0100 | [diff] [blame] | 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.DEBUG) |
| 11 | |
| 12 | |
| 13 | class ZeroRpcApiEndpoint(object): |
| peusterm | e4e89d3 | 2016-01-07 09:14:54 +0100 | [diff] [blame^] | 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 | """ |
| peusterm | 9c252b6 | 2016-01-06 16:59:53 +0100 | [diff] [blame] | 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.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 | |
| 48 | class MultiDatacenterApi(object): |
| peusterm | e4e89d3 | 2016-01-07 09:14:54 +0100 | [diff] [blame^] | 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 | """ |
| peusterm | 9c252b6 | 2016-01-06 16:59:53 +0100 | [diff] [blame] | 55 | |
| 56 | def __init__(self, dcs): |
| 57 | self.dcs = dcs |
| 58 | |
| 59 | def compute_action_start(self, dc_name, compute_name): |
| peusterm | e4e89d3 | 2016-01-07 09:14:54 +0100 | [diff] [blame^] | 60 | # TODO what to return UUID / IP ? |
| peusterm | 70baaf8 | 2016-01-06 17:14:40 +0100 | [diff] [blame] | 61 | logging.debug("RPC CALL: compute start") |
| 62 | if dc_name in self.dcs: |
| 63 | self.dcs[dc_name].addCompute(compute_name) |
| peusterm | 9c252b6 | 2016-01-06 16:59:53 +0100 | [diff] [blame] | 64 | |
| 65 | def compute_action_stop(self, dc_name, compute_name): |
| 66 | logging.info("compute stop") |
| peusterm | 70baaf8 | 2016-01-06 17:14:40 +0100 | [diff] [blame] | 67 | if dc_name in self.dcs: |
| 68 | self.dcs[dc_name].removeCompute(compute_name) |
| peusterm | 9c252b6 | 2016-01-06 16:59:53 +0100 | [diff] [blame] | 69 | |
| 70 | def compute_list(self): |
| 71 | pass |