Statistics
| Branch: | Tag: | Revision:

one / src / rm / RequestManager.cc @ af3eed60

History | View | Annotate | Download (15 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 "RequestManagerPoolInfoFilter.h"
22
#include "RequestManagerPoolInfo.h"
23
#include "RequestManagerInfo.h"
24
#include "RequestManagerDelete.h"
25
#include "RequestManagerPublish.h"
26
#include "RequestManagerAllocate.h"
27

    
28
#include "RequestManagerVirtualNetwork.h"
29
#include "RequestManagerVirtualMachine.h"
30

    
31
#include <sys/signal.h>
32
#include <sys/socket.h>
33
#include <netinet/in.h>
34
#include <netinet/tcp.h>
35
#include <unistd.h>
36
#include <fcntl.h>
37
#include <string.h> 
38
#include <cstring>
39
   
40
/* -------------------------------------------------------------------------- */
41
/* -------------------------------------------------------------------------- */
42

    
43
extern "C" void * rm_action_loop(void *arg)
44
{
45
    RequestManager *  rm;
46

    
47
    if ( arg == 0 )
48
    {
49
        return 0;
50
    }
51

    
52
    NebulaLog::log("ReM",Log::INFO,"Request Manager started.");
53

    
54
    rm = static_cast<RequestManager *>(arg);
55
    
56
    rm->am.loop(0,0);
57

    
58
    NebulaLog::log("ReM",Log::INFO,"Request Manager stopped.");
59
    
60
    return 0;
61
}
62

    
63
/* -------------------------------------------------------------------------- */
64
/* -------------------------------------------------------------------------- */
65
    
66
extern "C" void * rm_xml_server_loop(void *arg)
67
{
68
    RequestManager *    rm;
69
        
70
    if ( arg == 0 )
71
    {
72
        return 0;
73
    }
74

    
75
    rm = static_cast<RequestManager *>(arg);
76
 
77
    // Set cancel state for the thread
78
    
79
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,0);
80

    
81
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,0);
82
      
83
    //Start the server
84
            
85
    rm->AbyssServer = new xmlrpc_c::serverAbyss(xmlrpc_c::serverAbyss::constrOpt()
86
        .registryP(&rm->RequestManagerRegistry)
87
        .logFileName(rm->xml_log_file)
88
        .socketFd(rm->socket_fd));
89
        
90
    rm->AbyssServer->run();
91

    
92
    return 0;
93
}
94

    
95
/* -------------------------------------------------------------------------- */
96
/* -------------------------------------------------------------------------- */
97

    
98
int RequestManager::setup_socket()
99
{
100
    int                 rc;
101
    int                 yes = 1;
102
    struct sockaddr_in  rm_addr;
103
    
104
    socket_fd = socket(AF_INET, SOCK_STREAM, 0);
105
    
106
    if ( socket_fd == -1 )
107
    {
108
        ostringstream oss;
109

    
110
        oss << "Can not open server socket: " << strerror(errno);
111
        NebulaLog::log("ReM",Log::ERROR,oss);
112
       
113
        return -1; 
114
    }
115
  
116
    rc = setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); 
117

    
118
    if ( rc == -1 )
119
    {
120
        ostringstream oss;
121

    
122
        oss << "Can not set socket options: " << strerror(errno);
123
        NebulaLog::log("ReM",Log::ERROR,oss);
124
        
125
        close(socket_fd);
126
               
127
        return -1;        
128
    }
129
    
130
    fcntl(socket_fd,F_SETFD,FD_CLOEXEC); // Close socket in MADs
131
    
132
    rm_addr.sin_family      = AF_INET;
133
    rm_addr.sin_port        = htons(port);
134
    rm_addr.sin_addr.s_addr = INADDR_ANY;
135

    
136
    rc = bind(socket_fd,(struct sockaddr *) &(rm_addr),sizeof(struct sockaddr));
137

    
138
    if ( rc == -1) 
139
    {
140
        ostringstream oss;
141

    
142
        oss << "Can not bind to port " << port << " : " << strerror(errno);
143
        NebulaLog::log("ReM",Log::ERROR,oss);
144
       
145
        close(socket_fd);
146
            
147
        return -1;
148
    }
149

    
150
    return 0;
151
}
152

    
153
/* -------------------------------------------------------------------------- */
154
/* -------------------------------------------------------------------------- */
155

    
156
int RequestManager::start()
157
{
158
    pthread_attr_t  pattr;
159
    ostringstream   oss;
160
    
161
    NebulaLog::log("ReM",Log::INFO,"Starting Request Manager...");
162
    
163
    int rc = setup_socket();
164
    
165
    if ( rc != 0 )
166
    {
167
        return -1;
168
    }
169
    
170
    register_xml_methods();
171
    
172
    pthread_attr_init (&pattr);
173
    pthread_attr_setdetachstate (&pattr, PTHREAD_CREATE_JOINABLE);
174
    
175
    pthread_create(&rm_thread,&pattr,rm_action_loop,(void *)this);
176
    
177
    pthread_attr_init (&pattr);
178
    pthread_attr_setdetachstate (&pattr, PTHREAD_CREATE_JOINABLE);
179
    
180
    oss << "Starting XML-RPC server, port " << port << " ...";
181
    NebulaLog::log("ReM",Log::INFO,oss);
182
    
183
    pthread_create(&rm_xml_server_thread,&pattr,rm_xml_server_loop,(void *)this);
184

    
185
    return 0;
186
}
187

    
188
/* -------------------------------------------------------------------------- */
189
/* -------------------------------------------------------------------------- */
190
  
191
void RequestManager::do_action(
192
        const string &  action,
193
        void *          arg)
194
{
195
    if (action == ACTION_FINALIZE)
196
    {
197
        NebulaLog::log("ReM",Log::INFO,"Stopping Request Manager...");
198
        
199
        pthread_cancel(rm_xml_server_thread); 
200

    
201
        pthread_join(rm_xml_server_thread,0);
202

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

    
205
        delete AbyssServer;
206
        
207
        if ( socket_fd != -1 )
208
        {
209
            close(socket_fd);
210
        }
211
    }
212
    else
213
    {
214
        ostringstream oss;
215
        oss << "Unknown action name: " << action;
216
        
217
        NebulaLog::log("ReM", Log::ERROR, oss);
218
    }    
219
};
220

    
221
/* -------------------------------------------------------------------------- */
222
/* -------------------------------------------------------------------------- */
223
        
224
void RequestManager::register_xml_methods()
225
{
226
    // VirtualMachine Methods
227
    xmlrpc_c::methodPtr vm_deploy(new VirtualMachineDeploy());
228
    xmlrpc_c::methodPtr vm_migrate(new VirtualMachineMigrate());
229
    xmlrpc_c::methodPtr vm_action(new VirtualMachineAction()); 
230
    xmlrpc_c::methodPtr vm_savedisk(new VirtualMachineSaveDisk());
231

    
232
    // VirtualNetwork Methods
233
    xmlrpc_c::methodPtr vn_addleases(new VirtualNetworkAddLeases());
234
    xmlrpc_c::methodPtr vn_rmleases(new VirtualNetworkRemoveLeases());
235

    
236
    // Allocate Methods
237
    xmlrpc_c::methodPtr vm_allocate(new VirtualMachineAllocate());
238
    xmlrpc_c::methodPtr image_allocate(new ImageAllocate());
239
    xmlrpc_c::methodPtr vn_allocate(new VirtualNetworkAllocate());
240
    xmlrpc_c::methodPtr group_allocate(new GroupAllocate());
241
    xmlrpc_c::methodPtr template_allocate(new TemplateAllocate());
242
    xmlrpc_c::methodPtr host_allocate(new HostAllocate());
243
    xmlrpc_c::methodPtr cluster_allocate(new ClusterAllocate());
244
    xmlrpc_c::methodPtr user_allocate(new  UserAllocate());
245

    
246
    // Publish Methods
247
    xmlrpc_c::methodPtr template_publish(new TemplatePublish());
248
    xmlrpc_c::methodPtr vn_publish(new VirtualNetworkPublish());
249
    xmlrpc_c::methodPtr image_publish(new ImagePublish());
250

    
251
    // Delete Methods
252
    xmlrpc_c::methodPtr host_delete(new HostDelete());
253
    xmlrpc_c::methodPtr template_delete(new TemplateDelete());
254
    xmlrpc_c::methodPtr cluster_delete(new ClusterDelete());
255
    xmlrpc_c::methodPtr group_delete(new GroupDelete());
256
    xmlrpc_c::methodPtr vn_delete(new VirtualNetworkDelete());
257
    xmlrpc_c::methodPtr user_delete(new UserDelete());
258
    xmlrpc_c::methodPtr image_delete(new ImageDelete());
259

    
260
    // Info Methods
261
    xmlrpc_c::methodPtr vm_info(new VirtualMachineInfo());
262
    xmlrpc_c::methodPtr host_info(new HostInfo());
263
    xmlrpc_c::methodPtr template_info(new TemplateInfo());
264
    xmlrpc_c::methodPtr cluster_info(new ClusterInfo());
265
    xmlrpc_c::methodPtr group_info(new GroupInfo());
266
    xmlrpc_c::methodPtr vn_info(new VirtualNetworkInfo());
267
    xmlrpc_c::methodPtr user_info(new UserInfo());
268
    xmlrpc_c::methodPtr image_info(new ImageInfo());
269

    
270
    // PoolInfo Methods 
271

    
272
    xmlrpc_c::methodPtr hostpool_info(new HostPoolInfo());
273
    xmlrpc_c::methodPtr clusterpool_info(new ClusterPoolInfo());
274
    xmlrpc_c::methodPtr grouppool_info(new GroupPoolInfo());
275
    xmlrpc_c::methodPtr userpool_info(new UserPoolInfo());
276

    
277
    // PoolInfo Methods with Filtering
278

    
279
    xmlrpc_c::methodPtr vm_pool_info(new VirtualMachinePoolInfo());
280
    xmlrpc_c::methodPtr template_pool_info(new TemplatePoolInfo());
281
    xmlrpc_c::methodPtr vnpool_info(new VirtualNetworkPoolInfo());
282
    xmlrpc_c::methodPtr imagepool_info(new ImagePoolInfo());
283

    
284
/*     
285
    xmlrpc_c::methodPtr vm_chown(new
286
        RequestManager::GenericChown(this,VM));
287

288

289
    xmlrpc_c::methodPtr template_update(new
290
        RequestManager::TemplateUpdate(tpool, upool));
291

292
    xmlrpc_c::methodPtr template_rm_attribute(new
293
        RequestManager::TemplateRemoveAttribute(tpool, upool));
294

295
    xmlrpc_c::methodPtr template_chown(new
296
        RequestManager::GenericChown(this,TEMPLATE));
297

298
        
299
        
300

301
    xmlrpc_c::methodPtr host_enable(new 
302
        RequestManager::HostEnable(hpool,upool));
303

304

305

306

307
    xmlrpc_c::methodPtr cluster_add(new 
308
        RequestManager::ClusterAdd(hpool,upool,cpool));
309

310
    xmlrpc_c::methodPtr cluster_remove(new 
311
        RequestManager::ClusterRemove(hpool,upool,cpool));
312

313

314

315

316
        
317
        
318

319

320

321
    xmlrpc_c::methodPtr vn_chown(new
322
        RequestManager::GenericChown(this,NET));
323

324

325

326

327
    xmlrpc_c::methodPtr user_change_password(new
328
        RequestManager::UserChangePassword(upool));
329

330
    xmlrpc_c::methodPtr user_chown(new
331
        RequestManager::GenericChown(this,USER));
332
        
333
        
334
        
335
        
336
    xmlrpc_c::methodPtr image_update(new    
337
        RequestManager::ImageUpdate(ipool, upool));
338
    
339
    xmlrpc_c::methodPtr image_rm_attribute(new    
340
        RequestManager::ImageRemoveAttribute(ipool, upool));
341
        
342
    xmlrpc_c::methodPtr image_persistent(new    
343
        RequestManager::ImagePersistent(ipool, upool));
344
        
345
    xmlrpc_c::methodPtr image_enable(new    
346
        RequestManager::ImageEnable(ipool, upool));
347

348
    xmlrpc_c::methodPtr image_chown(new
349
        RequestManager::GenericChown(this,IMAGE));
350
*/
351
    /* VM related methods  */    
352
/*        
353

354
    RequestManagerRegistry.addMethod("one.vm.chown", vm_chown);
355
*/
356
    RequestManagerRegistry.addMethod("one.vm.deploy", vm_deploy);
357
    RequestManagerRegistry.addMethod("one.vm.action", vm_action);
358
    RequestManagerRegistry.addMethod("one.vm.migrate", vm_migrate);
359
    RequestManagerRegistry.addMethod("one.vm.savedisk", vm_savedisk);
360
    RequestManagerRegistry.addMethod("one.vm.allocate", vm_allocate);
361
    RequestManagerRegistry.addMethod("one.vm.info", vm_info);
362

    
363
    RequestManagerRegistry.addMethod("one.vmpool.info", vm_pool_info);
364

    
365
    /* VM Template related methods*/
366
/*
367
    RequestManagerRegistry.addMethod("one.template.update", template_update);
368
    RequestManagerRegistry.addMethod("one.template.rmattr", template_rm_attribute);
369
    RequestManagerRegistry.addMethod("one.template.chown", template_chown);
370
*/
371
    RequestManagerRegistry.addMethod("one.template.allocate",template_allocate);
372
    RequestManagerRegistry.addMethod("one.template.publish", template_publish);
373
    RequestManagerRegistry.addMethod("one.template.delete", template_delete);
374
    RequestManagerRegistry.addMethod("one.template.info", template_info);
375

    
376
    RequestManagerRegistry.addMethod("one.templatepool.info",template_pool_info);
377

    
378
    /* Host related methods*/
379
/*     
380
    RequestManagerRegistry.addMethod("one.host.enable", host_enable);
381
*/    
382
    RequestManagerRegistry.addMethod("one.host.allocate", host_allocate);   
383
    RequestManagerRegistry.addMethod("one.host.delete", host_delete);
384
    RequestManagerRegistry.addMethod("one.host.info", host_info);
385

    
386
    RequestManagerRegistry.addMethod("one.hostpool.info", hostpool_info); 
387

    
388
    /* Cluster related methods */
389
/*
390
    RequestManagerRegistry.addMethod("one.cluster.add", cluster_add);
391
    RequestManagerRegistry.addMethod("one.cluster.remove", cluster_remove);
392
*/
393
    RequestManagerRegistry.addMethod("one.cluster.allocate", cluster_allocate);
394
    RequestManagerRegistry.addMethod("one.cluster.delete", cluster_delete);
395
    RequestManagerRegistry.addMethod("one.cluster.info", cluster_info);
396

    
397
    RequestManagerRegistry.addMethod("one.clusterpool.info", clusterpool_info);
398

    
399
    /* Group related methods */
400
    RequestManagerRegistry.addMethod("one.group.allocate",  group_allocate);
401
    RequestManagerRegistry.addMethod("one.group.delete",    group_delete);
402
    RequestManagerRegistry.addMethod("one.group.info",      group_info);
403

    
404
    RequestManagerRegistry.addMethod("one.grouppool.info",  grouppool_info);
405

    
406
    /* Network related methods*/
407
/*
408
    RequestManagerRegistry.addMethod("one.vn.chown", vn_chown);
409
*/
410
    RequestManagerRegistry.addMethod("one.vn.addleases", vn_addleases);
411
    RequestManagerRegistry.addMethod("one.vn.rmleases", vn_rmleases);
412
    RequestManagerRegistry.addMethod("one.vn.allocate", vn_allocate);   
413
    RequestManagerRegistry.addMethod("one.vn.publish", vn_publish);
414
    RequestManagerRegistry.addMethod("one.vn.delete", vn_delete);
415
    RequestManagerRegistry.addMethod("one.vn.info", vn_info); 
416

    
417
    RequestManagerRegistry.addMethod("one.vnpool.info", vnpool_info); 
418
    
419
    
420
    /* User related methods*/
421
/*        
422
    RequestManagerRegistry.addMethod("one.user.passwd", user_change_password);
423
    RequestManagerRegistry.addMethod("one.user.chown", user_chown);
424
*/
425
    RequestManagerRegistry.addMethod("one.user.allocate", user_allocate);
426
    RequestManagerRegistry.addMethod("one.user.delete", user_delete);
427
    RequestManagerRegistry.addMethod("one.user.info", user_info);
428

    
429
    RequestManagerRegistry.addMethod("one.userpool.info", userpool_info);
430
    
431
    /* Image related methods*/
432
/*    
433
    RequestManagerRegistry.addMethod("one.image.update", image_update);     
434
    RequestManagerRegistry.addMethod("one.image.rmattr", image_rm_attribute);
435
    RequestManagerRegistry.addMethod("one.image.persistent", image_persistent);
436
    RequestManagerRegistry.addMethod("one.image.enable", image_enable);    
437
    RequestManagerRegistry.addMethod("one.image.chown", image_chown);
438

439
*/  
440
    RequestManagerRegistry.addMethod("one.image.allocate", image_allocate);
441
    RequestManagerRegistry.addMethod("one.image.publish", image_publish);
442
    RequestManagerRegistry.addMethod("one.image.delete", image_delete);
443
    RequestManagerRegistry.addMethod("one.image.info", image_info);
444

    
445
    RequestManagerRegistry.addMethod("one.imagepool.info", imagepool_info);
446
};
447

    
448
/* -------------------------------------------------------------------------- */
449
/* -------------------------------------------------------------------------- */
450