2 Distributed Cloud Emulator (dcemulator)
3 (c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
10 logging
.basicConfig(level
=logging
.INFO
)
13 class ZeroRpcApiEndpoint(object):
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.
23 def __init__(self
, listenip
, port
):
27 logging
.debug("Created API endpoint %s(%s:%d)" % (
28 self
.__class
__.__name
__, self
.ip
, self
.port
))
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
))
36 thread
= threading
.Thread(target
=self
._api
_server
_thread
, args
=())
39 logging
.debug("Started API endpoint %s(%s:%d)" % (
40 self
.__class
__.__name
__, self
.ip
, self
.port
))
42 def _api_server_thread(self
):
43 s
= zerorpc
.Server(MultiDatacenterApi(self
.dcs
))
44 s
.bind("tcp://%s:%d" % (self
.ip
, self
.port
))
48 class MultiDatacenterApi(object):
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.
56 def __init__(self
, dcs
):
59 def compute_action_start(self
, dc_label
, compute_name
, image
, command
, network
):
60 # network e.g. {"ip": "10.0.0.254/8"}
61 # TODO what to return UUID / given name / internal name ?
62 logging
.debug("RPC CALL: compute start")
64 c
= self
.dcs
.get(dc_label
).startCompute(
65 compute_name
, image
=image
, command
=command
, network
=network
)
67 except Exception as ex
:
68 logging
.exception("RPC error.")
71 def compute_action_stop(self
, dc_label
, compute_name
):
72 logging
.debug("RPC CALL: compute stop")
74 return self
.dcs
.get(dc_label
).stopCompute(compute_name
)
75 except Exception as ex
:
76 logging
.exception("RPC error.")
79 def compute_list(self
, dc_label
):
80 logging
.debug("RPC CALL: compute list")
83 # return list with all compute nodes in all DCs
85 for dc
in self
.dcs
.itervalues():
86 all_containers
+= dc
.listCompute()
87 return [(c
.name
, c
.getStatus())
88 for c
in all_containers
]
90 # return list of compute nodes for specified DC
91 return [(c
.name
, c
.getStatus())
92 for c
in self
.dcs
.get(dc_label
).listCompute()]
93 except Exception as ex
:
94 logging
.exception("RPC error.")
97 def compute_status(self
, dc_label
, compute_name
):
98 logging
.debug("RPC CALL: compute status")
101 dc_label
).containers
.get(compute_name
).getStatus()
102 except Exception as ex
:
103 logging
.exception("RPC error.")
106 def datacenter_list(self
):
107 logging
.debug("RPC CALL: datacenter list")
109 return [d
.getStatus() for d
in self
.dcs
.itervalues()]
110 except Exception as ex
:
111 logging
.exception("RPC error.")
114 def datacenter_status(self
, dc_label
):
115 logging
.debug("RPC CALL: datacenter status")
117 return self
.dcs
.get(dc_label
).getStatus()
118 except Exception as ex
:
119 logging
.exception("RPC error.")