/
im_service.py
executable file
·256 lines (210 loc) · 8.86 KB
/
im_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#! /usr/bin/env python
#
# IM - Infrastructure Manager
# Copyright (C) 2011 - GRyCAP - Universitat Politecnica de Valencia
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
if sys.version_info <= (2, 6):
print "Must use python 2.6 or greater"
sys.exit(1)
import logging
import os
import signal
from IM.request import Request, AsyncXMLRPCServer, get_system_queue
from IM.config import Config
from IM.InfrastructureManager import InfrastructureManager
from IM.ServiceRequests import IMBaseRequest
from IM import __version__ as version
logger = logging.getLogger('InfrastructureManager')
def WaitRequest(request):
"""
Wait for the specified request
"""
request.wait()
success = (request.status() == Request.STATUS_PROCESSED)
return (success, request.get())
"""
API functions.
They create the specified request and wait for it.
"""
def AddResource(inf_id, radl_data, auth_data, context = True):
request = IMBaseRequest.create_request(IMBaseRequest.ADD_RESOURCE, (inf_id, radl_data, auth_data, context))
return WaitRequest(request)
def RemoveResource(inf_id, vm_list, auth_data, context = True):
request = IMBaseRequest.create_request(IMBaseRequest.REMOVE_RESOURCE, (inf_id, vm_list, auth_data, context))
return WaitRequest(request)
def GetVMInfo(inf_id, vm_id, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.GET_VM_INFO, (inf_id, vm_id, auth_data))
return WaitRequest(request)
def GetVMProperty(inf_id, vm_id, property_name, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.GET_VM_PROPERTY, (inf_id, vm_id, property_name, auth_data))
return WaitRequest(request)
def AlterVM(inf_id, vm_id, radl, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.ALTER_VM, (inf_id, vm_id, radl, auth_data))
return WaitRequest(request)
def GetInfrastructureInfo(inf_id, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.GET_INFRASTRUCTURE_INFO,(inf_id, auth_data))
return WaitRequest(request)
def StopInfrastructure(inf_id, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.STOP_INFRASTRUCTURE,(inf_id, auth_data))
return WaitRequest(request)
def StartInfrastructure(inf_id, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.START_INFRASTRUCTURE,(inf_id, auth_data))
return WaitRequest(request)
def DestroyInfrastructure(inf_id, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.DESTROY_INFRASTRUCTURE,(inf_id, auth_data))
# This function take a lot of time in some connectors. We can make it async: return (True, "")
return WaitRequest(request)
def CreateInfrastructure(radl_data, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.CREATE_INFRASTRUCTURE,(radl_data, auth_data))
return WaitRequest(request)
def GetInfrastructureList(auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.GET_INFRASTRUCTURE_LIST,(auth_data))
return WaitRequest(request)
def Reconfigure(inf_id, radl_data, auth_data, vm_list = None):
request = IMBaseRequest.create_request(IMBaseRequest.RECONFIGURE,(inf_id, radl_data, auth_data, vm_list))
return WaitRequest(request)
def ImportInfrastructure(str_inf, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.IMPORT_INFRASTRUCTURE,(str_inf, auth_data))
return WaitRequest(request)
def ExportInfrastructure(inf_id, delete, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.EXPORT_INFRASTRUCTURE,(inf_id, delete, auth_data))
return WaitRequest(request)
def GetInfrastructureRADL(inf_id, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.GET_INFRASTRUCTURE_RADL,(inf_id, auth_data))
return WaitRequest(request)
def GetVMContMsg(inf_id, vm_id, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.GET_VM_CONT_MSG,(inf_id, vm_id, auth_data))
return WaitRequest(request)
def GetInfrastructureContMsg(inf_id, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.GET_INFRASTRUCTURE_CONT_MSG,(inf_id, auth_data))
return WaitRequest(request)
def StopVM(inf_id, vm_id, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.STOP_VM,(inf_id, vm_id, auth_data))
return WaitRequest(request)
def StartVM(inf_id, vm_id, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.START_VM,(inf_id, vm_id, auth_data))
return WaitRequest(request)
def GetInfrastructureState(inf_id, auth_data):
request = IMBaseRequest.create_request(IMBaseRequest.GET_INFRASTRUCTURE_STATE,(inf_id, auth_data))
return WaitRequest(request)
def GetVersion():
request = IMBaseRequest.create_request(IMBaseRequest.GET_VERSION, None)
return WaitRequest(request)
def launch_daemon():
"""
Launch the IM daemon
"""
if os.path.isfile(Config.DATA_FILE) or Config.DATA_DB:
InfrastructureManager.load_data()
if Config.XMLRCP_SSL:
# if specified launch the secure version
import ssl
from IM.request import AsyncSSLXMLRPCServer
server = AsyncSSLXMLRPCServer(Config.XMLRCP_ADDRESS, Config.XMLRCP_PORT, Config.XMLRCP_SSL_KEYFILE,
Config.XMLRCP_SSL_CERTFILE, Config.XMLRCP_SSL_CA_CERTS,
cert_reqs=ssl.CERT_OPTIONAL)
else:
# otherwise the standard XML-RPC service
server = AsyncXMLRPCServer((Config.XMLRCP_ADDRESS, Config.XMLRCP_PORT))
# Register the API functions
server.register_function(CreateInfrastructure)
server.register_function(DestroyInfrastructure)
server.register_function(StartInfrastructure)
server.register_function(StopInfrastructure)
server.register_function(GetInfrastructureInfo)
server.register_function(GetVMInfo)
server.register_function(GetVMProperty)
server.register_function(AlterVM)
server.register_function(RemoveResource)
server.register_function(AddResource)
server.register_function(GetInfrastructureList)
server.register_function(Reconfigure)
server.register_function(ExportInfrastructure)
server.register_function(ImportInfrastructure)
server.register_function(GetInfrastructureRADL)
server.register_function(GetInfrastructureContMsg)
server.register_function(GetVMContMsg)
server.register_function(StartVM)
server.register_function(StopVM)
server.register_function(GetInfrastructureState)
server.register_function(GetVersion)
InfrastructureManager.logger.info('************ Start Infrastructure Manager daemon (v.%s) ************' % version)
# Launch the API XMLRPC thread
server.serve_forever_in_thread()
if Config.ACTIVATE_REST:
# If specified launch the REST server
import IM.REST
IM.REST.run_in_thread(host=Config.REST_ADDRESS, port=Config.REST_PORT)
# Start the messages queue
get_system_queue().timed_process_loop(None, 1, exit_callback = im_stop)
def config_logging():
"""
Init the logging info
"""
try:
# First look at /etc/im/logging.conf file
logging.config.fileConfig('/etc/im/logging.conf')
except Exception, ex:
print ex
log_dir = os.path.dirname(Config.LOG_FILE)
if not os.path.isdir(log_dir):
os.makedirs(log_dir)
fileh = logging.handlers.RotatingFileHandler(filename=Config.LOG_FILE, maxBytes=Config.LOG_FILE_MAX_SIZE, backupCount=3)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fileh.setFormatter(formatter)
try:
log_level = eval("logging." + Config.LOG_LEVEL)
except:
log_level = logging.DEBUG
logging.RootLogger.propagate = 0
logging.root.setLevel(logging.ERROR)
log = logging.getLogger('ConfManager')
log.setLevel(log_level)
log.propagate = 0
log.addHandler(fileh)
log = logging.getLogger('CloudConnector')
log.setLevel(log_level)
log.propagate = 0
log.addHandler(fileh)
log = logging.getLogger('InfrastructureManager')
log.setLevel(log_level)
log.propagate = 0
log.addHandler(fileh)
def im_stop():
"""
Function to safely stop the service
"""
try:
# Assure that the IM data are correctly saved
InfrastructureManager.logger.info('Stopping Infrastructure Manager daemon...')
InfrastructureManager.stop()
if Config.ACTIVATE_REST:
# we have to stop the REST server
import IM.REST
IM.REST.stop()
except:
InfrastructureManager.logger.exception("Error stopping Infrastructure Manager daemon")
InfrastructureManager.logger.info('************ Infrastructure Manager daemon stopped ************')
sys.exit(0)
def signal_term_handler(signal, frame):
"""
Callback function to catch the system signals
"""
im_stop()
if __name__ == "__main__":
signal.signal(signal.SIGTERM, signal_term_handler)
config_logging()
launch_daemon()