Statistics
| Branch: | Tag: | Revision:

one / src / rm / RequestManager.cc @ bfaabf35

History | View | Annotate | Download (14.5 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
#include "RequestManagerChown.h"
29

    
30
#include "RequestManagerVirtualNetwork.h"
31
#include "RequestManagerVirtualMachine.h"
32
#include "RequestManagerVMTemplate.h"
33
#include "RequestManagerHost.h"
34
#include "RequestManagerImage.h"
35
#include "RequestManagerUser.h"
36
#include "RequestManagerAcl.h"
37

    
38
#include <sys/signal.h>
39
#include <sys/socket.h>
40
#include <netinet/in.h>
41
#include <netinet/tcp.h>
42
#include <unistd.h>
43
#include <fcntl.h>
44
#include <string.h> 
45
#include <cstring>
46
   
47
/* -------------------------------------------------------------------------- */
48
/* -------------------------------------------------------------------------- */
49

    
50
extern "C" void * rm_action_loop(void *arg)
51
{
52
    RequestManager *  rm;
53

    
54
    if ( arg == 0 )
55
    {
56
        return 0;
57
    }
58

    
59
    NebulaLog::log("ReM",Log::INFO,"Request Manager started.");
60

    
61
    rm = static_cast<RequestManager *>(arg);
62
    
63
    rm->am.loop(0,0);
64

    
65
    NebulaLog::log("ReM",Log::INFO,"Request Manager stopped.");
66
    
67
    return 0;
68
}
69

    
70
/* -------------------------------------------------------------------------- */
71
/* -------------------------------------------------------------------------- */
72
    
73
extern "C" void * rm_xml_server_loop(void *arg)
74
{
75
    RequestManager *    rm;
76
        
77
    if ( arg == 0 )
78
    {
79
        return 0;
80
    }
81

    
82
    rm = static_cast<RequestManager *>(arg);
83
 
84
    // Set cancel state for the thread
85
    
86
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,0);
87

    
88
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,0);
89
      
90
    //Start the server
91
            
92
    rm->AbyssServer = new xmlrpc_c::serverAbyss(xmlrpc_c::serverAbyss::constrOpt()
93
        .registryP(&rm->RequestManagerRegistry)
94
        .logFileName(rm->xml_log_file)
95
        .socketFd(rm->socket_fd));
96
        
97
    rm->AbyssServer->run();
98

    
99
    return 0;
100
}
101

    
102
/* -------------------------------------------------------------------------- */
103
/* -------------------------------------------------------------------------- */
104

    
105
int RequestManager::setup_socket()
106
{
107
    int                 rc;
108
    int                 yes = 1;
109
    struct sockaddr_in  rm_addr;
110
    
111
    socket_fd = socket(AF_INET, SOCK_STREAM, 0);
112
    
113
    if ( socket_fd == -1 )
114
    {
115
        ostringstream oss;
116

    
117
        oss << "Can not open server socket: " << strerror(errno);
118
        NebulaLog::log("ReM",Log::ERROR,oss);
119
       
120
        return -1; 
121
    }
122
  
123
    rc = setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); 
124

    
125
    if ( rc == -1 )
126
    {
127
        ostringstream oss;
128

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

    
143
    rc = bind(socket_fd,(struct sockaddr *) &(rm_addr),sizeof(struct sockaddr));
144

    
145
    if ( rc == -1) 
146
    {
147
        ostringstream oss;
148

    
149
        oss << "Can not bind to port " << port << " : " << strerror(errno);
150
        NebulaLog::log("ReM",Log::ERROR,oss);
151
       
152
        close(socket_fd);
153
            
154
        return -1;
155
    }
156

    
157
    return 0;
158
}
159

    
160
/* -------------------------------------------------------------------------- */
161
/* -------------------------------------------------------------------------- */
162

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

    
192
    return 0;
193
}
194

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

    
208
        pthread_join(rm_xml_server_thread,0);
209

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

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

    
228
/* -------------------------------------------------------------------------- */
229
/* -------------------------------------------------------------------------- */
230
        
231
void RequestManager::register_xml_methods()
232
{
233
    // User Methods
234
    xmlrpc_c::methodPtr user_change_password(new UserChangePassword());
235
    xmlrpc_c::methodPtr user_add_group(new UserAddGroup());
236
    xmlrpc_c::methodPtr user_del_group(new UserDelGroup());
237

    
238
    // VMTemplate Methods
239
    xmlrpc_c::methodPtr template_instantiate(new VMTemplateInstantiate());
240

    
241
    // VirtualMachine Methods
242
    xmlrpc_c::methodPtr vm_deploy(new VirtualMachineDeploy());
243
    xmlrpc_c::methodPtr vm_migrate(new VirtualMachineMigrate());
244
    xmlrpc_c::methodPtr vm_action(new VirtualMachineAction()); 
245
    xmlrpc_c::methodPtr vm_savedisk(new VirtualMachineSaveDisk());
246

    
247
    // VirtualNetwork Methods
248
    xmlrpc_c::methodPtr vn_addleases(new VirtualNetworkAddLeases());
249
    xmlrpc_c::methodPtr vn_rmleases(new VirtualNetworkRemoveLeases());
250

    
251
    // Update Template Methods
252
    xmlrpc_c::methodPtr image_update(new ImageUpdateTemplate());
253
    xmlrpc_c::methodPtr template_update(new TemplateUpdateTemplate());
254
    xmlrpc_c::methodPtr host_update(new HostUpdateTemplate());
255

    
256
    // Allocate Methods
257
    xmlrpc_c::methodPtr vm_allocate(new VirtualMachineAllocate());
258
    xmlrpc_c::methodPtr image_allocate(new ImageAllocate());
259
    xmlrpc_c::methodPtr vn_allocate(new VirtualNetworkAllocate());
260
    xmlrpc_c::methodPtr group_allocate(new GroupAllocate());
261
    xmlrpc_c::methodPtr template_allocate(new TemplateAllocate());
262
    xmlrpc_c::methodPtr host_allocate(new HostAllocate());
263
    xmlrpc_c::methodPtr user_allocate(new  UserAllocate());
264

    
265
    // Publish Methods
266
    xmlrpc_c::methodPtr template_publish(new TemplatePublish());
267
    xmlrpc_c::methodPtr vn_publish(new VirtualNetworkPublish());
268
    xmlrpc_c::methodPtr image_publish(new ImagePublish());
269

    
270
    // Delete Methods
271
    xmlrpc_c::methodPtr host_delete(new HostDelete());
272
    xmlrpc_c::methodPtr template_delete(new TemplateDelete());
273
    xmlrpc_c::methodPtr group_delete(new GroupDelete());
274
    xmlrpc_c::methodPtr vn_delete(new VirtualNetworkDelete());
275
    xmlrpc_c::methodPtr user_delete(new UserDelete());
276
    xmlrpc_c::methodPtr image_delete(new ImageDelete());
277

    
278
    // Info Methods
279
    xmlrpc_c::methodPtr vm_info(new VirtualMachineInfo());
280
    xmlrpc_c::methodPtr host_info(new HostInfo());
281
    xmlrpc_c::methodPtr template_info(new TemplateInfo());
282
    xmlrpc_c::methodPtr group_info(new GroupInfo());
283
    xmlrpc_c::methodPtr vn_info(new VirtualNetworkInfo());
284
    xmlrpc_c::methodPtr user_info(new UserInfo());
285
    xmlrpc_c::methodPtr image_info(new ImageInfo());
286

    
287
    // PoolInfo Methods 
288

    
289
    xmlrpc_c::methodPtr hostpool_info(new HostPoolInfo());
290
    xmlrpc_c::methodPtr grouppool_info(new GroupPoolInfo());
291
    xmlrpc_c::methodPtr userpool_info(new UserPoolInfo());
292

    
293
    // PoolInfo Methods with Filtering
294

    
295
    xmlrpc_c::methodPtr vm_pool_info(new VirtualMachinePoolInfo());
296
    xmlrpc_c::methodPtr template_pool_info(new TemplatePoolInfo());
297
    xmlrpc_c::methodPtr vnpool_info(new VirtualNetworkPoolInfo());
298
    xmlrpc_c::methodPtr imagepool_info(new ImagePoolInfo());
299

    
300
    // Host Methods
301
    xmlrpc_c::methodPtr host_enable(new HostEnable());
302

    
303
    // Image Methods
304
    xmlrpc_c::methodPtr image_persistent(new ImagePersistent());
305
    xmlrpc_c::methodPtr image_enable(new ImageEnable());
306

    
307
    // Chown Methods
308
    xmlrpc_c::methodPtr vm_chown(new VirtualMachineChown());
309
    xmlrpc_c::methodPtr template_chown(new TemplateChown());
310
    xmlrpc_c::methodPtr vn_chown(new VirtualNetworkChown());
311
    xmlrpc_c::methodPtr image_chown(new ImageChown());
312
    xmlrpc_c::methodPtr user_chown(new UserChown());
313

    
314
    // ACL Methods
315
    xmlrpc_c::methodPtr acl_addrule(new AclAddRule());
316
    xmlrpc_c::methodPtr acl_delrule(new AclDelRule());
317
    xmlrpc_c::methodPtr acl_info(new AclInfo());
318

    
319
    /* VM related methods  */    
320
    RequestManagerRegistry.addMethod("one.vm.deploy", vm_deploy);
321
    RequestManagerRegistry.addMethod("one.vm.action", vm_action);
322
    RequestManagerRegistry.addMethod("one.vm.migrate", vm_migrate);
323
    RequestManagerRegistry.addMethod("one.vm.savedisk", vm_savedisk);
324
    RequestManagerRegistry.addMethod("one.vm.allocate", vm_allocate);
325
    RequestManagerRegistry.addMethod("one.vm.info", vm_info);
326
    RequestManagerRegistry.addMethod("one.vm.chown", vm_chown);
327

    
328
    RequestManagerRegistry.addMethod("one.vmpool.info", vm_pool_info);
329

    
330
    /* VM Template related methods*/
331
    RequestManagerRegistry.addMethod("one.template.update", template_update);
332
    RequestManagerRegistry.addMethod("one.template.instantiate",template_instantiate);
333
    RequestManagerRegistry.addMethod("one.template.allocate",template_allocate);
334
    RequestManagerRegistry.addMethod("one.template.publish", template_publish);
335
    RequestManagerRegistry.addMethod("one.template.delete", template_delete);
336
    RequestManagerRegistry.addMethod("one.template.info", template_info);
337
    RequestManagerRegistry.addMethod("one.template.chown", template_chown);
338

    
339
    RequestManagerRegistry.addMethod("one.templatepool.info",template_pool_info);
340

    
341
    /* Host related methods*/
342
    RequestManagerRegistry.addMethod("one.host.enable", host_enable);
343
    RequestManagerRegistry.addMethod("one.host.update", host_update);
344
    RequestManagerRegistry.addMethod("one.host.allocate", host_allocate);   
345
    RequestManagerRegistry.addMethod("one.host.delete", host_delete);
346
    RequestManagerRegistry.addMethod("one.host.info", host_info);
347

    
348
    RequestManagerRegistry.addMethod("one.hostpool.info", hostpool_info); 
349

    
350
    /* Group related methods */
351
    RequestManagerRegistry.addMethod("one.group.allocate",  group_allocate);
352
    RequestManagerRegistry.addMethod("one.group.delete",    group_delete);
353
    RequestManagerRegistry.addMethod("one.group.info",      group_info);
354

    
355
    RequestManagerRegistry.addMethod("one.grouppool.info",  grouppool_info);
356

    
357
    /* Network related methods*/
358
    RequestManagerRegistry.addMethod("one.vn.addleases", vn_addleases);
359
    RequestManagerRegistry.addMethod("one.vn.rmleases", vn_rmleases);
360
    RequestManagerRegistry.addMethod("one.vn.allocate", vn_allocate);   
361
    RequestManagerRegistry.addMethod("one.vn.publish", vn_publish);
362
    RequestManagerRegistry.addMethod("one.vn.delete", vn_delete);
363
    RequestManagerRegistry.addMethod("one.vn.info", vn_info); 
364
    RequestManagerRegistry.addMethod("one.vn.chown", vn_chown);
365

    
366
    RequestManagerRegistry.addMethod("one.vnpool.info", vnpool_info); 
367
    
368
    
369
    /* User related methods*/
370
    RequestManagerRegistry.addMethod("one.user.allocate", user_allocate);
371
    RequestManagerRegistry.addMethod("one.user.delete", user_delete);
372
    RequestManagerRegistry.addMethod("one.user.info", user_info);
373
    RequestManagerRegistry.addMethod("one.user.addgroup", user_add_group);
374
    RequestManagerRegistry.addMethod("one.user.delgroup", user_del_group);
375
    RequestManagerRegistry.addMethod("one.user.passwd", user_change_password);
376
    RequestManagerRegistry.addMethod("one.user.chgrp", user_chown);
377

    
378
    RequestManagerRegistry.addMethod("one.userpool.info", userpool_info);
379
    
380
    /* Image related methods*/
381

    
382
    RequestManagerRegistry.addMethod("one.image.persistent", image_persistent);
383
    RequestManagerRegistry.addMethod("one.image.enable", image_enable);
384
    RequestManagerRegistry.addMethod("one.image.update", image_update);     
385
    RequestManagerRegistry.addMethod("one.image.allocate", image_allocate);
386
    RequestManagerRegistry.addMethod("one.image.publish", image_publish);
387
    RequestManagerRegistry.addMethod("one.image.delete", image_delete);
388
    RequestManagerRegistry.addMethod("one.image.info", image_info);
389
    RequestManagerRegistry.addMethod("one.image.chown", image_chown);
390

    
391
    RequestManagerRegistry.addMethod("one.imagepool.info", imagepool_info);
392

    
393
    /* ACL related methods */
394
    RequestManagerRegistry.addMethod("one.acl.addrule", acl_addrule);
395
    RequestManagerRegistry.addMethod("one.acl.delrule", acl_delrule);
396
    RequestManagerRegistry.addMethod("one.acl.info",    acl_info);
397
};
398

    
399
/* -------------------------------------------------------------------------- */
400
/* -------------------------------------------------------------------------- */
401