Statistics
| Branch: | Tag: | Revision:

one / src / test / Nebula.cc @ ae53d437

History | View | Annotate | Download (10.3 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 "Nebula.h"
18
#include "NebulaLog.h"
19
#include "VirtualMachine.h"
20
#include "SqliteDB.h"
21
#include "MySqlDB.h"
22

    
23
#include "OneUnitTest.h"
24
#include "NebulaTest.h"
25

    
26
#include <stdlib.h>
27
#include <stdexcept>
28
#include <libxml/parser.h>
29

    
30
#include <signal.h>
31
#include <unistd.h>
32
#include <fcntl.h>
33
#include <sys/types.h>
34
#include <sys/stat.h>
35
#include <pthread.h>
36

    
37
using namespace std;
38

    
39
/* -------------------------------------------------------------------------- */
40
/* -------------------------------------------------------------------------- */
41

    
42
void Nebula::start()
43
{
44
    int             rc;
45
    sigset_t        mask;
46
    time_t          timer_period;
47

    
48
    NebulaTest *    tester;
49

    
50
    tester = NebulaTest::instance();
51

    
52
    // Because Nebula is accessed only using ::instance(), it can't be
53
    // deleted. Tests use the start method several times before the
54
    // destructor is invoked, so this clean-up is necessary
55
    if ( vmpool != 0)
56
    {
57
        delete vmpool;
58
    }
59

    
60
    if ( vnpool != 0)
61
    {
62
        delete vnpool;
63
    }
64

    
65
    if ( hpool != 0)
66
    {
67
        delete hpool;
68
    }
69

    
70
    if ( upool != 0)
71
    {
72
        delete upool;
73
    }
74

    
75
    if ( ipool != 0)
76
    {
77
        delete ipool;
78
    }
79

    
80
    if ( cpool != 0)
81
    {
82
        delete cpool;
83
    }
84

    
85
    if ( tpool != 0)
86
    {
87
        delete tpool;
88
    }
89

    
90
    if ( gpool != 0)
91
    {
92
        delete gpool;
93
    }
94

    
95
    if ( vmm != 0)
96
    {
97
        delete vmm;
98
    }
99

    
100
    if ( lcm != 0)
101
    {
102
        delete lcm;
103
    }
104

    
105
    if ( im != 0)
106
    {
107
        delete im;
108
    }
109

    
110
    if ( tm != 0)
111
    {
112
        delete tm;
113
    }
114

    
115
    if ( dm != 0)
116
    {
117
        delete dm;
118
    }
119

    
120
    if ( rm != 0)
121
    {
122
        delete rm;
123
    }
124

    
125
    if ( hm != 0)
126
    {
127
        delete hm;
128
    }
129

    
130
    if ( authm != 0)
131
    {
132
        delete authm;
133
    }
134

    
135
    if ( nebula_configuration != 0)
136
    {
137
        delete nebula_configuration;
138
    }
139

    
140
    // -----------------------------------------------------------
141
    // Configuration
142
    // -----------------------------------------------------------
143

    
144
    // A self-contained structure in current directory is assumed
145
    nebula_location = "./";
146

    
147
    mad_location     = nebula_location + "lib/mads/";
148
    etc_location     = nebula_location + "etc/";
149
    log_location     = nebula_location + "var/";
150
    var_location     = nebula_location + "var/";
151
    hook_location    = nebula_location + "share/hooks/";
152
    remotes_location = nebula_location + "lib/remotes/";
153

    
154
    if ( nebula_configuration != 0)
155
    {
156
        delete nebula_configuration;
157
    }
158

    
159
    xmlInitParser();
160

    
161
    // -----------------------------------------------------------
162
    // Pools
163
    // -----------------------------------------------------------
164

    
165
    try
166
    {
167
        vector<const Attribute *> dbs;
168

    
169
        db = OneUnitTest::get_db();
170

    
171
        NebulaLog::log("ONE",Log::INFO,"Bootstraping OpenNebula database.");
172

    
173
        VirtualMachinePool::bootstrap(db);
174
        HostPool::bootstrap(db);
175
        VirtualNetworkPool::bootstrap(db);
176
        UserPool::bootstrap(db);
177
        ImagePool::bootstrap(db);
178
        ClusterPool::bootstrap(db);
179
        VMTemplatePool::bootstrap(db);
180
        GroupPool::bootstrap(db);
181
    }
182
    catch (exception&)
183
    {
184
        throw;
185
    }
186

    
187
    // -----------------------------------------------------------
188

    
189
    try
190
    {
191
        string  mac_prefix = "00:00";
192
        int     size = 1;
193
        string  default_image_type;
194
        string  default_device_prefix;
195

    
196
        if (tester->need_vm_pool)
197
        {
198
            vmpool = tester->create_vmpool(db,hook_location);
199
        }
200

    
201
        if (tester->need_host_pool)
202
        {
203
            hpool  = tester->create_hpool(db,hook_location);
204
        }
205

    
206
        if (tester->need_vnet_pool)
207
        {
208
            vnpool = tester->create_vnpool(db,mac_prefix,size);
209
        }
210

    
211
        if (tester->need_user_pool)
212
        {
213
            upool  = tester->create_upool(db);
214
        }
215

    
216
        if (tester->need_image_pool)
217
        {
218
            ipool  = tester->create_ipool(db,
219
                                          default_image_type,
220
                                          default_device_prefix);
221
        }
222

    
223
        if (tester->need_cluster_pool)
224
        {
225
            cpool  = tester->create_cpool(db);
226
        }
227

    
228
        if (tester->need_template_pool)
229
        {
230
            tpool  = tester->create_tpool(db);
231
        }
232

    
233
        if (tester->need_group_pool)
234
        {
235
            gpool  = tester->create_gpool(db);
236
        }
237
    }
238
    catch (exception&)
239
    {
240
        throw;
241
    }
242

    
243
    // Set pointer to null, to prevent its deletion on the destructor
244
    db = 0;
245

    
246
    // -----------------------------------------------------------
247
    //Managers
248
    // -----------------------------------------------------------
249

    
250
    timer_period = 0;
251
    rc = 0;
252

    
253
    sigfillset(&mask);
254

    
255
    pthread_sigmask(SIG_BLOCK, &mask, NULL);
256

    
257
    MadManager::mad_manager_system_init();
258

    
259
    // ---- Virtual Machine Manager ----
260
    if (tester->need_vmm)
261
    {
262
        try
263
        {
264
            time_t poll_period = 0;
265

    
266
            vmm = tester->create_vmm(vmpool,hpool,timer_period,poll_period);
267
        }
268
        catch (bad_alloc&)
269
        {
270
            throw;
271
        }
272

    
273
        if( vmm != 0)
274
        {
275
            rc = vmm->start();
276
        }
277

    
278
        if ( rc != 0 )
279
        {
280
            throw runtime_error("Could not start the Virtual Machine Manager");
281
        }
282
    }
283

    
284
    // ---- Life-cycle Manager ----
285
    if (tester->need_lcm)
286
    {
287
        try
288
        {
289
            lcm = tester->create_lcm(vmpool,hpool);
290
        }
291
        catch (bad_alloc&)
292
        {
293
            throw;
294
        }
295

    
296
        if( lcm != 0 )
297
        {
298
            rc = lcm->start();
299
        }
300

    
301
        if ( rc != 0 )
302
        {
303
            throw runtime_error("Could not start the Life-cycle Manager");
304
        }
305
    }
306

    
307
    // ---- Information Manager ----
308
    if (tester->need_im)
309
    {
310
        try
311
        {
312
            im = tester->create_im(hpool,timer_period,remotes_location);
313
        }
314
        catch (bad_alloc&)
315
        {
316
            throw;
317
        }
318

    
319
        if( im != 0 )
320
        {
321
            rc = im->start();
322
        }
323

    
324
        if ( rc != 0 )
325
        {
326
            throw runtime_error("Could not start the Information Manager");
327
        }
328
    }
329

    
330
    // ---- Transfer Manager ----
331
    if (tester->need_tm)
332
    {
333
        try
334
        {
335
            tm = tester->create_tm(vmpool, hpool);
336
        }
337
        catch (bad_alloc&)
338
        {
339
            throw;
340
        }
341

    
342
        if( tm != 0 )
343
        {
344
            rc = tm->start();
345
        }
346

    
347
        if ( rc != 0 )
348
        {
349
            throw runtime_error("Could not start the Transfer Manager");
350
        }
351
    }
352

    
353
    // ---- Dispatch Manager ----
354
    if ( tester->need_dm )
355
    {
356
        try
357
        {
358
            dm = tester->create_dm(vmpool,hpool);
359
        }
360
        catch (bad_alloc&)
361
        {
362
            throw;
363
        }
364

    
365
        if( dm != 0 )
366
        {
367
            rc = dm->start();
368
        }
369

    
370
        if ( rc != 0 )
371
        {
372
           throw runtime_error("Could not start the Dispatch Manager");
373
        }
374
    }
375

    
376
    // ---- Request Manager ----
377
    if (tester->need_rm)
378
    {
379
        try
380
        {
381
            rm = tester->create_rm(vmpool,hpool,vnpool,upool,ipool,cpool,tpool,
382
                                   gpool,log_location + "one_xmlrpc.log");
383
        }
384
        catch (bad_alloc&)
385
        {
386
            NebulaLog::log("ONE", Log::ERROR, "Error starting RM");
387
            throw;
388
        }
389

    
390
        if( rm != 0 )
391
        {
392
            rc = rm->start();
393
        }
394

    
395
        if ( rc != 0 )
396
        {
397
           throw runtime_error("Could not start the Request Manager");
398
        }
399
    }
400

    
401
    // ---- Hook Manager ----
402
    if (tester->need_hm)
403
    {
404
        try
405
        {
406
            hm = tester->create_hm(vmpool);
407
        }
408
        catch (bad_alloc&)
409
        {
410
            throw;
411
        }
412

    
413
        if( hm != 0 )
414
        {
415
            rc = hm->start();
416
        }
417

    
418
        if ( rc != 0 )
419
        {
420
           throw runtime_error("Could not start the Hook Manager");
421
        }
422
    }
423

    
424
    // ---- Auth Manager ----
425
    if (tester->need_authm)
426
    {
427
        try
428
        {
429
            authm = tester->create_authm(timer_period);
430
        }
431
        catch (bad_alloc&)
432
        {
433
            throw;
434
        }
435

    
436
        if (authm != 0)
437
        {
438
            rc = authm->start();
439

    
440
            if ( rc != 0 )
441
            {
442
              throw runtime_error("Could not start the Auth Manager");
443
            }
444
        }
445
    }
446

    
447
    // ---- Auth Manager ----
448
    if (tester->need_imagem)
449
    {
450
        try
451
        {
452
            imagem = tester->create_imagem(ipool);
453
        }
454
        catch (bad_alloc&)
455
        {
456
            throw;
457
        }
458

    
459
        if (imagem != 0)
460
        {
461
            rc = imagem->start();
462

    
463
            if ( rc != 0 )
464
            {
465
              throw runtime_error("Could not start the Image Manager");
466
            }
467
        }
468
    }
469

    
470
    // -----------------------------------------------------------
471
    // Load mads
472
    // -----------------------------------------------------------
473

    
474
    sleep(2);
475

    
476
    if( vmm != 0 )
477
    {
478
        vmm->load_mads(0);
479
    }
480

    
481
    if( im != 0 )
482
    {
483
        im->load_mads(0);
484
    }
485

    
486
    if( tm != 0 )
487
    {
488
        tm->load_mads(0);
489
    }
490

    
491
    if( hm != 0 )
492
    {
493
        hm->load_mads(0);
494
    }
495

    
496
    if( authm != 0 )
497
    {
498
        authm->load_mads(0);
499
    }
500
};