Statistics
| Branch: | Tag: | Revision:

one / src / rm / RequestManager.cc @ ae53d437

History | View | Annotate | Download (15.2 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             */
3
/*                                                                            */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
5
/* not use this file except in compliance with the License. You may obtain    */
6
/* a copy of the License at                                                   */
7
/*                                                                            */
8
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
9
/*                                                                            */
10
/* Unless required by applicable law or agreed to in writing, software        */
11
/* distributed under the License is distributed on an "AS IS" BASIS,          */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
13
/* See the License for the specific language governing permissions and        */
14
/* limitations under the License.                                             */
15
/* -------------------------------------------------------------------------- */
16

    
17
#include "RequestManager.h"
18
#include "NebulaLog.h"
19
#include <cerrno>
20

    
21
#include <sys/signal.h>
22
#include <sys/socket.h>
23
#include <netinet/in.h>
24
#include <netinet/tcp.h>
25
#include <unistd.h>
26
#include <fcntl.h>
27
#include <string.h> 
28
#include <cstring>
29
   
30
/* -------------------------------------------------------------------------- */
31
/* -------------------------------------------------------------------------- */
32

    
33
extern "C" void * rm_action_loop(void *arg)
34
{
35
    RequestManager *  rm;
36

    
37
    if ( arg == 0 )
38
    {
39
        return 0;
40
    }
41

    
42
    NebulaLog::log("ReM",Log::INFO,"Request Manager started.");
43

    
44
    rm = static_cast<RequestManager *>(arg);
45
    
46
    rm->am.loop(0,0);
47

    
48
    NebulaLog::log("ReM",Log::INFO,"Request Manager stopped.");
49
    
50
    return 0;
51
}
52

    
53
/* -------------------------------------------------------------------------- */
54
/* -------------------------------------------------------------------------- */
55
    
56
extern "C" void * rm_xml_server_loop(void *arg)
57
{
58
    RequestManager *    rm;
59
        
60
    if ( arg == 0 )
61
    {
62
        return 0;
63
    }
64

    
65
    rm = static_cast<RequestManager *>(arg);
66
 
67
    // Set cancel state for the thread
68
    
69
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,0);
70

    
71
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,0);
72
      
73
    //Start the server
74
            
75
    rm->AbyssServer = new xmlrpc_c::serverAbyss(xmlrpc_c::serverAbyss::constrOpt()
76
        .registryP(&rm->RequestManagerRegistry)
77
        .logFileName(rm->xml_log_file)
78
        .socketFd(rm->socket_fd));
79
        
80
    rm->AbyssServer->run();
81

    
82
    return 0;
83
}
84

    
85
/* -------------------------------------------------------------------------- */
86
/* -------------------------------------------------------------------------- */
87

    
88
int RequestManager::setup_socket()
89
{
90
    int                 rc;
91
    int                 yes = 1;
92
    struct sockaddr_in  rm_addr;
93
    
94
    socket_fd = socket(AF_INET, SOCK_STREAM, 0);
95
    
96
    if ( socket_fd == -1 )
97
    {
98
        ostringstream oss;
99

    
100
        oss << "Can not open server socket: " << strerror(errno);
101
        NebulaLog::log("ReM",Log::ERROR,oss);
102
       
103
        return -1; 
104
    }
105
  
106
    rc = setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); 
107

    
108
    if ( rc == -1 )
109
    {
110
        ostringstream oss;
111

    
112
        oss << "Can not set socket options: " << strerror(errno);
113
        NebulaLog::log("ReM",Log::ERROR,oss);
114
        
115
        close(socket_fd);
116
               
117
        return -1;        
118
    }
119
    
120
    fcntl(socket_fd,F_SETFD,FD_CLOEXEC); // Close socket in MADs
121
    
122
    rm_addr.sin_family      = AF_INET;
123
    rm_addr.sin_port        = htons(port);
124
    rm_addr.sin_addr.s_addr = INADDR_ANY;
125

    
126
    rc = bind(socket_fd,(struct sockaddr *) &(rm_addr),sizeof(struct sockaddr));
127

    
128
    if ( rc == -1) 
129
    {
130
        ostringstream oss;
131

    
132
        oss << "Can not bind to port " << port << " : " << strerror(errno);
133
        NebulaLog::log("ReM",Log::ERROR,oss);
134
       
135
        close(socket_fd);
136
            
137
        return -1;
138
    }
139

    
140
    return 0;
141
}
142

    
143
/* -------------------------------------------------------------------------- */
144
/* -------------------------------------------------------------------------- */
145

    
146
int RequestManager::start()
147
{
148
    pthread_attr_t  pattr;
149
    ostringstream   oss;
150
    
151
    NebulaLog::log("ReM",Log::INFO,"Starting Request Manager...");
152
    
153
    int rc = setup_socket();
154
    
155
    if ( rc != 0 )
156
    {
157
        return -1;
158
    }
159
    
160
    register_xml_methods();
161
    
162
    pthread_attr_init (&pattr);
163
    pthread_attr_setdetachstate (&pattr, PTHREAD_CREATE_JOINABLE);
164
    
165
    pthread_create(&rm_thread,&pattr,rm_action_loop,(void *)this);
166
    
167
    pthread_attr_init (&pattr);
168
    pthread_attr_setdetachstate (&pattr, PTHREAD_CREATE_JOINABLE);
169
    
170
    oss << "Starting XML-RPC server, port " << port << " ...";
171
    NebulaLog::log("ReM",Log::INFO,oss);
172
    
173
    pthread_create(&rm_xml_server_thread,&pattr,rm_xml_server_loop,(void *)this);
174

    
175
    return 0;
176
}
177

    
178
/* -------------------------------------------------------------------------- */
179
/* -------------------------------------------------------------------------- */
180
  
181
void RequestManager::do_action(
182
        const string &  action,
183
        void *          arg)
184
{
185
    if (action == ACTION_FINALIZE)
186
    {
187
        NebulaLog::log("ReM",Log::INFO,"Stopping Request Manager...");
188
        
189
        pthread_cancel(rm_xml_server_thread); 
190

    
191
        pthread_join(rm_xml_server_thread,0);
192

    
193
        NebulaLog::log("ReM",Log::INFO,"XML-RPC server stopped.");
194

    
195
        delete AbyssServer;
196
        
197
        if ( socket_fd != -1 )
198
        {
199
            close(socket_fd);
200
        }
201
    }
202
    else
203
    {
204
        ostringstream oss;
205
        oss << "Unknown action name: " << action;
206
        
207
        NebulaLog::log("ReM", Log::ERROR, oss);
208
    }    
209
};
210

    
211
/* -------------------------------------------------------------------------- */
212
/* -------------------------------------------------------------------------- */
213
        
214
void RequestManager::register_xml_methods()
215
{
216
    xmlrpc_c::methodPtr vm_allocate(new 
217
        RequestManager::VirtualMachineAllocate(vmpool,vnpool,ipool,tpool,upool));
218
        
219
    xmlrpc_c::methodPtr vm_deploy(new 
220
        RequestManager::VirtualMachineDeploy(vmpool,hpool,upool));
221
        
222
    xmlrpc_c::methodPtr vm_migrate(new 
223
        RequestManager::VirtualMachineMigrate(vmpool,hpool,upool));
224
        
225
    xmlrpc_c::methodPtr vm_action(new 
226
        RequestManager::VirtualMachineAction(vmpool,upool));
227

    
228
    xmlrpc_c::methodPtr vm_savedisk(new
229
        RequestManager::VirtualMachineSaveDisk(vmpool,upool,ipool));
230

    
231
    xmlrpc_c::methodPtr vm_info(new 
232
        RequestManager::VirtualMachineInfo(vmpool,upool));
233

    
234
    xmlrpc_c::methodPtr vm_pool_info(new
235
        RequestManager::VirtualMachinePoolInfo(vmpool,upool));
236

    
237
    xmlrpc_c::methodPtr template_allocate(new
238
        RequestManager::TemplateAllocate(tpool,upool));
239

    
240
    xmlrpc_c::methodPtr template_delete(new
241
        RequestManager::TemplateDelete(tpool, upool));
242

    
243
    xmlrpc_c::methodPtr template_info(new
244
        RequestManager::TemplateInfo(tpool, upool));
245

    
246
    xmlrpc_c::methodPtr template_update(new
247
        RequestManager::TemplateUpdate(tpool, upool));
248

    
249
    xmlrpc_c::methodPtr template_rm_attribute(new
250
        RequestManager::TemplateRemoveAttribute(tpool, upool));
251

    
252
    xmlrpc_c::methodPtr template_publish(new
253
        RequestManager::TemplatePublish(tpool, upool));
254

    
255
    xmlrpc_c::methodPtr template_pool_info(new
256
        RequestManager::TemplatePoolInfo(tpool,upool));
257

    
258
    xmlrpc_c::methodPtr host_allocate(new 
259
        RequestManager::HostAllocate(hpool,upool));
260
        
261
    xmlrpc_c::methodPtr host_info(new 
262
        RequestManager::HostInfo(hpool, upool));
263

    
264
    xmlrpc_c::methodPtr hostpool_info(new 
265
        RequestManager::HostPoolInfo(hpool,upool));
266
        
267
    xmlrpc_c::methodPtr host_delete(new 
268
        RequestManager::HostDelete(hpool,upool));
269

    
270
    xmlrpc_c::methodPtr host_enable(new 
271
        RequestManager::HostEnable(hpool,upool));
272

    
273
    xmlrpc_c::methodPtr cluster_allocate(new 
274
        RequestManager::ClusterAllocate(upool,cpool));
275

    
276
    xmlrpc_c::methodPtr cluster_info(new 
277
        RequestManager::ClusterInfo(upool,cpool));
278

    
279
    xmlrpc_c::methodPtr cluster_delete(new 
280
        RequestManager::ClusterDelete(upool,cpool));
281

    
282
    xmlrpc_c::methodPtr cluster_add(new 
283
        RequestManager::ClusterAdd(hpool,upool,cpool));
284

    
285
    xmlrpc_c::methodPtr cluster_remove(new 
286
        RequestManager::ClusterRemove(hpool,upool,cpool));
287

    
288
    xmlrpc_c::methodPtr clusterpool_info(new 
289
        RequestManager::ClusterPoolInfo(upool,cpool));
290

    
291
    xmlrpc_c::methodPtr group_allocate(new 
292
        RequestManager::GroupAllocate(upool,gpool));
293

    
294
    xmlrpc_c::methodPtr group_info(new 
295
        RequestManager::GroupInfo(upool,gpool));
296

    
297
    xmlrpc_c::methodPtr group_delete(new 
298
        RequestManager::GroupDelete(upool,gpool));
299

    
300
    xmlrpc_c::methodPtr grouppool_info(new 
301
        RequestManager::GroupPoolInfo(upool,gpool));
302

    
303
    xmlrpc_c::methodPtr vn_allocate(new 
304
        RequestManager::VirtualNetworkAllocate(vnpool,upool));
305
        
306
    xmlrpc_c::methodPtr vn_info(new 
307
        RequestManager::VirtualNetworkInfo(vnpool,upool));
308
        
309
    xmlrpc_c::methodPtr vnpool_info(new 
310
        RequestManager::VirtualNetworkPoolInfo(vnpool,upool));
311
        
312
    xmlrpc_c::methodPtr vn_publish(new    
313
        RequestManager::VirtualNetworkPublish(vnpool, upool));
314

    
315
    xmlrpc_c::methodPtr vn_delete(new 
316
        RequestManager::VirtualNetworkDelete(vnpool, upool));
317

    
318
    xmlrpc_c::methodPtr vn_addleases(new
319
        RequestManager::VirtualNetworkAddLeases(vnpool, upool));
320

    
321
    xmlrpc_c::methodPtr vn_rmleases(new
322
        RequestManager::VirtualNetworkRemoveLeases(vnpool, upool));
323

    
324
    xmlrpc_c::methodPtr user_allocate(new    
325
        RequestManager::UserAllocate(upool));
326

    
327
    xmlrpc_c::methodPtr user_delete(new    
328
        RequestManager::UserDelete(upool));
329

    
330
    xmlrpc_c::methodPtr user_info(new    
331
        RequestManager::UserInfo(upool));
332

    
333
    xmlrpc_c::methodPtr user_change_password(new
334
        RequestManager::UserChangePassword(upool));
335

    
336
    xmlrpc_c::methodPtr userpool_info(new    
337
        RequestManager::UserPoolInfo(upool));
338
        
339
    xmlrpc_c::methodPtr image_allocate(new    
340
        RequestManager::ImageAllocate(ipool, upool));
341
        
342
    xmlrpc_c::methodPtr image_delete(new    
343
        RequestManager::ImageDelete(ipool, upool));
344
        
345
    xmlrpc_c::methodPtr image_info(new    
346
        RequestManager::ImageInfo(ipool, upool));
347
        
348
    xmlrpc_c::methodPtr image_update(new    
349
        RequestManager::ImageUpdate(ipool, upool));
350
    
351
    xmlrpc_c::methodPtr image_rm_attribute(new    
352
        RequestManager::ImageRemoveAttribute(ipool, upool));
353
        
354
    xmlrpc_c::methodPtr image_publish(new    
355
        RequestManager::ImagePublish(ipool, upool));
356
        
357
    xmlrpc_c::methodPtr image_persistent(new    
358
        RequestManager::ImagePersistent(ipool, upool));
359
        
360
    xmlrpc_c::methodPtr image_enable(new    
361
        RequestManager::ImageEnable(ipool, upool));
362
        
363
    xmlrpc_c::methodPtr imagepool_info(new    
364
        RequestManager::ImagePoolInfo(ipool, upool));
365

    
366
    /* VM related methods  */    
367
        
368
    RequestManagerRegistry.addMethod("one.vm.allocate", vm_allocate);
369
    RequestManagerRegistry.addMethod("one.vm.deploy", vm_deploy);
370
    RequestManagerRegistry.addMethod("one.vm.action", vm_action);
371
    RequestManagerRegistry.addMethod("one.vm.migrate", vm_migrate);
372
    RequestManagerRegistry.addMethod("one.vm.info", vm_info);
373
    RequestManagerRegistry.addMethod("one.vm.savedisk", vm_savedisk);
374

    
375
    RequestManagerRegistry.addMethod("one.vmpool.info", vm_pool_info);
376

    
377
    /* VM Template related methods*/
378

    
379
    RequestManagerRegistry.addMethod("one.template.allocate",template_allocate);
380
    RequestManagerRegistry.addMethod("one.template.delete", template_delete);
381
    RequestManagerRegistry.addMethod("one.template.info", template_info);
382
    RequestManagerRegistry.addMethod("one.template.update", template_update);
383
    RequestManagerRegistry.addMethod("one.template.rmattr", template_rm_attribute);
384
    RequestManagerRegistry.addMethod("one.template.publish", template_publish);
385

    
386
    RequestManagerRegistry.addMethod("one.templatepool.info",template_pool_info);
387

    
388
    /* Host related methods*/
389
     
390
    RequestManagerRegistry.addMethod("one.host.allocate", host_allocate);   
391
    RequestManagerRegistry.addMethod("one.host.info", host_info);
392
    RequestManagerRegistry.addMethod("one.host.delete", host_delete);
393
    RequestManagerRegistry.addMethod("one.host.enable", host_enable);
394
    
395
    RequestManagerRegistry.addMethod("one.hostpool.info", hostpool_info); 
396

    
397
    /* Cluster related methods */
398

    
399
    RequestManagerRegistry.addMethod("one.cluster.allocate", cluster_allocate);
400
    RequestManagerRegistry.addMethod("one.cluster.info", cluster_info);
401
    RequestManagerRegistry.addMethod("one.cluster.delete", cluster_delete);
402
    RequestManagerRegistry.addMethod("one.cluster.add", cluster_add);
403
    RequestManagerRegistry.addMethod("one.cluster.remove", cluster_remove);
404

    
405
    RequestManagerRegistry.addMethod("one.clusterpool.info", clusterpool_info);
406

    
407
    /* Group related methods */
408

    
409
    RequestManagerRegistry.addMethod("one.group.allocate",  group_allocate);
410
    RequestManagerRegistry.addMethod("one.group.info",      group_info);
411
    RequestManagerRegistry.addMethod("one.group.delete",    group_delete);
412

    
413
    RequestManagerRegistry.addMethod("one.grouppool.info",  grouppool_info);
414

    
415
    /* Network related methods*/
416

    
417
    RequestManagerRegistry.addMethod("one.vn.allocate", vn_allocate);   
418
    RequestManagerRegistry.addMethod("one.vn.info", vn_info); 
419
    RequestManagerRegistry.addMethod("one.vn.publish", vn_publish);
420
    RequestManagerRegistry.addMethod("one.vn.delete", vn_delete);
421
    RequestManagerRegistry.addMethod("one.vn.addleases", vn_addleases);
422
    RequestManagerRegistry.addMethod("one.vn.rmleases", vn_rmleases);
423

    
424
    RequestManagerRegistry.addMethod("one.vnpool.info", vnpool_info); 
425
    
426
    
427
    /* User related methods*/
428
        
429
    RequestManagerRegistry.addMethod("one.user.allocate", user_allocate);
430
    RequestManagerRegistry.addMethod("one.user.delete", user_delete);
431
    RequestManagerRegistry.addMethod("one.user.info", user_info);
432
    RequestManagerRegistry.addMethod("one.user.passwd", user_change_password);
433

    
434
    RequestManagerRegistry.addMethod("one.userpool.info", userpool_info);
435
    
436
    /* Image related methods*/
437
    
438
    RequestManagerRegistry.addMethod("one.image.allocate", image_allocate);
439
    RequestManagerRegistry.addMethod("one.image.delete", image_delete);
440
    RequestManagerRegistry.addMethod("one.image.info", image_info);
441
    RequestManagerRegistry.addMethod("one.image.update", image_update);     
442
    RequestManagerRegistry.addMethod("one.image.rmattr", image_rm_attribute);
443
    RequestManagerRegistry.addMethod("one.image.publish", image_publish);
444
    RequestManagerRegistry.addMethod("one.image.persistent", image_persistent);
445
    RequestManagerRegistry.addMethod("one.image.enable", image_enable);    
446

    
447
    RequestManagerRegistry.addMethod("one.imagepool.info", imagepool_info);
448
    
449
};
450

    
451
/* -------------------------------------------------------------------------- */
452
/* -------------------------------------------------------------------------- */
453