Statistics
| Branch: | Tag: | Revision:

one / src / rm / RequestManager.cc @ d42f3d33

History | View | Annotate | Download (14.9 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
#include "RequestManagerUpdateTemplate.h"
28

    
29
#include "RequestManagerVirtualNetwork.h"
30
#include "RequestManagerVirtualMachine.h"
31
#include "RequestManagerVMTemplate.h"
32

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

    
45
extern "C" void * rm_action_loop(void *arg)
46
{
47
    RequestManager *  rm;
48

    
49
    if ( arg == 0 )
50
    {
51
        return 0;
52
    }
53

    
54
    NebulaLog::log("ReM",Log::INFO,"Request Manager started.");
55

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

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

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

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

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

    
94
    return 0;
95
}
96

    
97
/* -------------------------------------------------------------------------- */
98
/* -------------------------------------------------------------------------- */
99

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

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

    
120
    if ( rc == -1 )
121
    {
122
        ostringstream oss;
123

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

    
138
    rc = bind(socket_fd,(struct sockaddr *) &(rm_addr),sizeof(struct sockaddr));
139

    
140
    if ( rc == -1) 
141
    {
142
        ostringstream oss;
143

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

    
152
    return 0;
153
}
154

    
155
/* -------------------------------------------------------------------------- */
156
/* -------------------------------------------------------------------------- */
157

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

    
187
    return 0;
188
}
189

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

    
203
        pthread_join(rm_xml_server_thread,0);
204

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

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

    
223
/* -------------------------------------------------------------------------- */
224
/* -------------------------------------------------------------------------- */
225
        
226
void RequestManager::register_xml_methods()
227
{
228
    // VirtualMachine Methods
229
    xmlrpc_c::methodPtr template_instantiate(new VMTemplateInstantiate());
230

    
231
    // VirtualMachine Methods
232
    xmlrpc_c::methodPtr vm_deploy(new VirtualMachineDeploy());
233
    xmlrpc_c::methodPtr vm_migrate(new VirtualMachineMigrate());
234
    xmlrpc_c::methodPtr vm_action(new VirtualMachineAction()); 
235
    xmlrpc_c::methodPtr vm_savedisk(new VirtualMachineSaveDisk());
236

    
237
    // VirtualNetwork Methods
238
    xmlrpc_c::methodPtr vn_addleases(new VirtualNetworkAddLeases());
239
    xmlrpc_c::methodPtr vn_rmleases(new VirtualNetworkRemoveLeases());
240

    
241
    // Update Template Methods
242
    xmlrpc_c::methodPtr image_update(new ImageUpdateTemplate());
243
    xmlrpc_c::methodPtr template_update(new TemplateUpdateTemplate());
244
    xmlrpc_c::methodPtr host_update(new HostUpdateTemplate());
245

    
246
    // Allocate Methods
247
    xmlrpc_c::methodPtr vm_allocate(new VirtualMachineAllocate());
248
    xmlrpc_c::methodPtr image_allocate(new ImageAllocate());
249
    xmlrpc_c::methodPtr vn_allocate(new VirtualNetworkAllocate());
250
    xmlrpc_c::methodPtr group_allocate(new GroupAllocate());
251
    xmlrpc_c::methodPtr template_allocate(new TemplateAllocate());
252
    xmlrpc_c::methodPtr host_allocate(new HostAllocate());
253
    xmlrpc_c::methodPtr cluster_allocate(new ClusterAllocate());
254
    xmlrpc_c::methodPtr user_allocate(new  UserAllocate());
255

    
256
    // Publish Methods
257
    xmlrpc_c::methodPtr template_publish(new TemplatePublish());
258
    xmlrpc_c::methodPtr vn_publish(new VirtualNetworkPublish());
259
    xmlrpc_c::methodPtr image_publish(new ImagePublish());
260

    
261
    // Delete Methods
262
    xmlrpc_c::methodPtr host_delete(new HostDelete());
263
    xmlrpc_c::methodPtr template_delete(new TemplateDelete());
264
    xmlrpc_c::methodPtr cluster_delete(new ClusterDelete());
265
    xmlrpc_c::methodPtr group_delete(new GroupDelete());
266
    xmlrpc_c::methodPtr vn_delete(new VirtualNetworkDelete());
267
    xmlrpc_c::methodPtr user_delete(new UserDelete());
268
    xmlrpc_c::methodPtr image_delete(new ImageDelete());
269

    
270
    // Info Methods
271
    xmlrpc_c::methodPtr vm_info(new VirtualMachineInfo());
272
    xmlrpc_c::methodPtr host_info(new HostInfo());
273
    xmlrpc_c::methodPtr template_info(new TemplateInfo());
274
    xmlrpc_c::methodPtr cluster_info(new ClusterInfo());
275
    xmlrpc_c::methodPtr group_info(new GroupInfo());
276
    xmlrpc_c::methodPtr vn_info(new VirtualNetworkInfo());
277
    xmlrpc_c::methodPtr user_info(new UserInfo());
278
    xmlrpc_c::methodPtr image_info(new ImageInfo());
279

    
280
    // PoolInfo Methods 
281

    
282
    xmlrpc_c::methodPtr hostpool_info(new HostPoolInfo());
283
    xmlrpc_c::methodPtr clusterpool_info(new ClusterPoolInfo());
284
    xmlrpc_c::methodPtr grouppool_info(new GroupPoolInfo());
285
    xmlrpc_c::methodPtr userpool_info(new UserPoolInfo());
286

    
287
    // PoolInfo Methods with Filtering
288

    
289
    xmlrpc_c::methodPtr vm_pool_info(new VirtualMachinePoolInfo());
290
    xmlrpc_c::methodPtr template_pool_info(new TemplatePoolInfo());
291
    xmlrpc_c::methodPtr vnpool_info(new VirtualNetworkPoolInfo());
292
    xmlrpc_c::methodPtr imagepool_info(new ImagePoolInfo());
293

    
294
/*     
295
    xmlrpc_c::methodPtr vm_chown(new
296
        RequestManager::GenericChown(this,VM));
297

298

299
    xmlrpc_c::methodPtr template_chown(new
300
        RequestManager::GenericChown(this,TEMPLATE));
301

302

303
    xmlrpc_c::methodPtr host_enable(new 
304
        RequestManager::HostEnable(hpool,upool));
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
    xmlrpc_c::methodPtr vn_chown(new
315
        RequestManager::GenericChown(this,NET));
316

317

318
    xmlrpc_c::methodPtr user_change_password(new
319
        RequestManager::UserChangePassword(upool));
320

321
    xmlrpc_c::methodPtr user_chown(new
322
        RequestManager::GenericChown(this,USER));
323
        
324
    xmlrpc_c::methodPtr image_persistent(new    
325
        RequestManager::ImagePersistent(ipool, upool));
326
        
327
    xmlrpc_c::methodPtr image_enable(new    
328
        RequestManager::ImageEnable(ipool, upool));
329

330
    xmlrpc_c::methodPtr image_chown(new
331
        RequestManager::GenericChown(this,IMAGE));
332
*/
333
    /* VM related methods  */    
334
/*        
335

336
    RequestManagerRegistry.addMethod("one.vm.chown", vm_chown);
337
*/
338
    RequestManagerRegistry.addMethod("one.vm.deploy", vm_deploy);
339
    RequestManagerRegistry.addMethod("one.vm.action", vm_action);
340
    RequestManagerRegistry.addMethod("one.vm.migrate", vm_migrate);
341
    RequestManagerRegistry.addMethod("one.vm.savedisk", vm_savedisk);
342
    RequestManagerRegistry.addMethod("one.vm.allocate", vm_allocate);
343
    RequestManagerRegistry.addMethod("one.vm.info", vm_info);
344

    
345
    RequestManagerRegistry.addMethod("one.vmpool.info", vm_pool_info);
346

    
347
    /* VM Template related methods*/
348
/*
349
    RequestManagerRegistry.addMethod("one.template.chown", template_chown);
350
*/
351

    
352
    RequestManagerRegistry.addMethod("one.template.update", template_update);
353
    RequestManagerRegistry.addMethod("one.template.instantiate",template_instantiate);
354
    RequestManagerRegistry.addMethod("one.template.allocate",template_allocate);
355
    RequestManagerRegistry.addMethod("one.template.publish", template_publish);
356
    RequestManagerRegistry.addMethod("one.template.delete", template_delete);
357
    RequestManagerRegistry.addMethod("one.template.info", template_info);
358

    
359
    RequestManagerRegistry.addMethod("one.templatepool.info",template_pool_info);
360

    
361
    /* Host related methods*/
362
/*     
363
    RequestManagerRegistry.addMethod("one.host.enable", host_enable);
364
*/    
365
    RequestManagerRegistry.addMethod("one.host.update", host_update);
366
    RequestManagerRegistry.addMethod("one.host.allocate", host_allocate);   
367
    RequestManagerRegistry.addMethod("one.host.delete", host_delete);
368
    RequestManagerRegistry.addMethod("one.host.info", host_info);
369

    
370
    RequestManagerRegistry.addMethod("one.hostpool.info", hostpool_info); 
371

    
372
    /* Cluster related methods */
373
/*
374
    RequestManagerRegistry.addMethod("one.cluster.add", cluster_add);
375
    RequestManagerRegistry.addMethod("one.cluster.remove", cluster_remove);
376
*/
377
    RequestManagerRegistry.addMethod("one.cluster.allocate", cluster_allocate);
378
    RequestManagerRegistry.addMethod("one.cluster.delete", cluster_delete);
379
    RequestManagerRegistry.addMethod("one.cluster.info", cluster_info);
380

    
381
    RequestManagerRegistry.addMethod("one.clusterpool.info", clusterpool_info);
382

    
383
    /* Group related methods */
384
    RequestManagerRegistry.addMethod("one.group.allocate",  group_allocate);
385
    RequestManagerRegistry.addMethod("one.group.delete",    group_delete);
386
    RequestManagerRegistry.addMethod("one.group.info",      group_info);
387

    
388
    RequestManagerRegistry.addMethod("one.grouppool.info",  grouppool_info);
389

    
390
    /* Network related methods*/
391
/*
392
    RequestManagerRegistry.addMethod("one.vn.chown", vn_chown);
393
*/
394
    RequestManagerRegistry.addMethod("one.vn.addleases", vn_addleases);
395
    RequestManagerRegistry.addMethod("one.vn.rmleases", vn_rmleases);
396
    RequestManagerRegistry.addMethod("one.vn.allocate", vn_allocate);   
397
    RequestManagerRegistry.addMethod("one.vn.publish", vn_publish);
398
    RequestManagerRegistry.addMethod("one.vn.delete", vn_delete);
399
    RequestManagerRegistry.addMethod("one.vn.info", vn_info); 
400

    
401
    RequestManagerRegistry.addMethod("one.vnpool.info", vnpool_info); 
402
    
403
    
404
    /* User related methods*/
405
/*        
406
    RequestManagerRegistry.addMethod("one.user.passwd", user_change_password);
407
    RequestManagerRegistry.addMethod("one.user.chown", user_chown);
408
*/
409
    RequestManagerRegistry.addMethod("one.user.allocate", user_allocate);
410
    RequestManagerRegistry.addMethod("one.user.delete", user_delete);
411
    RequestManagerRegistry.addMethod("one.user.info", user_info);
412

    
413
    RequestManagerRegistry.addMethod("one.userpool.info", userpool_info);
414
    
415
    /* Image related methods*/
416
/*    
417
    RequestManagerRegistry.addMethod("one.image.persistent", image_persistent);
418
    RequestManagerRegistry.addMethod("one.image.enable", image_enable);    
419
    RequestManagerRegistry.addMethod("one.image.chown", image_chown);
420

421
*/  
422
    RequestManagerRegistry.addMethod("one.image.update", image_update);     
423
    RequestManagerRegistry.addMethod("one.image.allocate", image_allocate);
424
    RequestManagerRegistry.addMethod("one.image.publish", image_publish);
425
    RequestManagerRegistry.addMethod("one.image.delete", image_delete);
426
    RequestManagerRegistry.addMethod("one.image.info", image_info);
427

    
428
    RequestManagerRegistry.addMethod("one.imagepool.info", imagepool_info);
429
};
430

    
431
/* -------------------------------------------------------------------------- */
432
/* -------------------------------------------------------------------------- */
433