Statistics
| Branch: | Tag: | Revision:

one / include / Nebula.h @ 1772a358

History | View | Annotate | Download (19.8 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems                */
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
#ifndef NEBULA_H_
18
#define NEBULA_H_
19

    
20
#include "SqlDB.h"
21
#include "SystemDB.h"
22

    
23
#include "NebulaTemplate.h"
24

    
25
#include "VirtualMachinePool.h"
26
#include "VirtualNetworkPool.h"
27
#include "HostPool.h"
28
#include "UserPool.h"
29
#include "VMTemplatePool.h"
30
#include "GroupPool.h"
31
#include "DatastorePool.h"
32
#include "ClusterPool.h"
33
#include "DocumentPool.h"
34
#include "ZonePool.h"
35
#include "SecurityGroupPool.h"
36
#include "VdcPool.h"
37
#include "VirtualRouterPool.h"
38
#include "MarketPlacePool.h"
39
#include "MarketPlaceAppPool.h"
40

    
41
#include "VirtualMachineManager.h"
42
#include "LifeCycleManager.h"
43
#include "InformationManager.h"
44
#include "TransferManager.h"
45
#include "DispatchManager.h"
46
#include "RequestManager.h"
47
#include "HookManager.h"
48
#include "AuthManager.h"
49
#include "AclManager.h"
50
#include "ImageManager.h"
51
#include "MarketPlaceManager.h"
52
#include "IPAMManager.h"
53

    
54
#include "DefaultQuotas.h"
55

    
56
#include "Callbackable.h"
57

    
58

    
59
/**
60
 *  This is the main class for the OpenNebula daemon oned. It stores references
61
 *  to the main modules and data pools. It also includes functions to bootstrap
62
 *  the system and start all its components.
63
 */
64
class Nebula
65
{
66
public:
67

    
68
    static Nebula& instance()
69
    {
70
        static Nebula nebulad;
71

    
72
        return nebulad;
73
    };
74

    
75
    // --------------------------------------------------------------
76
    // Pool Accessors
77
    // --------------------------------------------------------------
78

    
79
    VirtualMachinePool * get_vmpool()
80
    {
81
        return vmpool;
82
    };
83

    
84
    HostPool * get_hpool()
85
    {
86
        return hpool;
87
    };
88

    
89
    VirtualNetworkPool * get_vnpool()
90
    {
91
        return vnpool;
92
    };
93

    
94
    UserPool * get_upool()
95
    {
96
        return upool;
97
    };
98

    
99
    ImagePool * get_ipool()
100
    {
101
        return ipool;
102
    };
103

    
104
    GroupPool * get_gpool()
105
    {
106
        return gpool;
107
    };
108

    
109
    VMTemplatePool * get_tpool()
110
    {
111
        return tpool;
112
    };
113

    
114
    DatastorePool * get_dspool()
115
    {
116
        return dspool;
117
    };
118

    
119
    ClusterPool * get_clpool()
120
    {
121
        return clpool;
122
    };
123

    
124
    DocumentPool * get_docpool()
125
    {
126
        return docpool;
127
    };
128

    
129
    ZonePool * get_zonepool()
130
    {
131
        return zonepool;
132
    };
133

    
134
    SecurityGroupPool * get_secgrouppool()
135
    {
136
        return secgrouppool;
137
    };
138

    
139
    VdcPool * get_vdcpool()
140
    {
141
        return vdcpool;
142
    };
143

    
144
    VirtualRouterPool * get_vrouterpool()
145
    {
146
        return vrouterpool;
147
    };
148

    
149
    MarketPlacePool * get_marketpool()
150
    {
151
        return marketpool;
152
    };
153

    
154
    MarketPlaceAppPool * get_apppool()
155
    {
156
        return apppool;
157
    };
158

    
159
    // --------------------------------------------------------------
160
    // Manager Accessors
161
    // --------------------------------------------------------------
162

    
163
    VirtualMachineManager * get_vmm()
164
    {
165
        return vmm;
166
    };
167

    
168
    LifeCycleManager * get_lcm()
169
    {
170
        return lcm;
171
    };
172

    
173
    InformationManager * get_im()
174
    {
175
        return im;
176
    };
177

    
178
    TransferManager * get_tm()
179
    {
180
        return tm;
181
    };
182

    
183
    DispatchManager * get_dm()
184
    {
185
        return dm;
186
    };
187

    
188
    HookManager * get_hm()
189
    {
190
        return hm;
191
    };
192

    
193
    AuthManager * get_authm()
194
    {
195
        return authm;
196
    };
197

    
198
    ImageManager * get_imagem()
199
    {
200
        return imagem;
201
    };
202

    
203
    AclManager * get_aclm()
204
    {
205
        return aclm;
206
    };
207

    
208
    MarketPlaceManager * get_marketm()
209
    {
210
        return marketm;
211
    };
212

    
213
    IPAMManager * get_ipamm()
214
    {
215
        return ipamm;
216
    };
217

    
218
    // --------------------------------------------------------------
219
    // Environment & Configuration
220
    // --------------------------------------------------------------
221

    
222
    /**
223
     *  Returns the value of LOG->DEBUG_LEVEL in oned.conf file
224
     *      @return the debug level, to instantiate Log'ers
225
     */
226
    Log::MessageType get_debug_level() const;
227

    
228
    /**
229
     *  Returns the value of LOG->SYSTEM in oned.conf file
230
     *      @return the logging system CERR, FILE_TS or SYSLOG
231
     */
232
    NebulaLog::LogType get_log_system() const;
233

    
234
    /**
235
     *  Returns the value of ONE_LOCATION env variable. When this variable is
236
     *  not defined the nebula location is "/".
237
     *      @return the nebula location.
238
     */
239
    const string& get_nebula_location()
240
    {
241
        return nebula_location;
242
    };
243

    
244
    /**
245
     *  Returns the path where mad executables are stored, if ONE_LOCATION is
246
     *  defined this path points to $ONE_LOCATION/bin, otherwise it is
247
     *  /usr/lib/one/mads.
248
     *      @return the mad execs location.
249
     */
250
    const string& get_mad_location()
251
    {
252
        return mad_location;
253
    };
254

    
255
    /**
256
     *  Returns the path where defaults for mads are stored, if ONE_LOCATION is
257
     *  defined this path points to $ONE_LOCATION/etc, otherwise it is /etc/one
258
     *      @return the mad defaults location.
259
     */
260
    const string& get_defaults_location()
261
    {
262
        return etc_location;
263
    };
264

    
265
    /**
266
     *  Returns the path where logs (oned.log, schedd.log,...) are generated
267
     *  if ONE_LOCATION is defined this path points to $ONE_LOCATION/var,
268
     *  otherwise it is /var/log/one.
269
     *      @return the log location.
270
     */
271
    const string& get_log_location()
272
    {
273
        return log_location;
274
    };
275

    
276
    /**
277
     *  Returns the default var location. When ONE_LOCATION is defined this path
278
     *  points to $ONE_LOCATION/var, otherwise it is /var/lib/one.
279
     *      @return the log location.
280
     */
281
    const string& get_var_location()
282
    {
283
        return var_location;
284
    };
285

    
286
    /**
287
     *
288
     *
289
     */
290
    void get_ds_location(string& dsloc);
291

    
292
    /**
293
     *  Returns the default vms location. When ONE_LOCATION is defined this path
294
     *  points to $ONE_LOCATION/var/vms, otherwise it is /var/lib/one/vms. This
295
     *  location stores vm related files: deployment, transfer, context, and
296
     *  logs (in self-contained mode only)
297
     *      @return the vms location.
298
     */
299
    const string& get_vms_location()
300
    {
301
        return vms_location;
302
    };
303

    
304
    /**
305
     *  Returns the path of the log file for a VM, depending where OpenNebula is
306
     *  installed,
307
     *     $ONE_LOCATION/var/$VM_ID/vm.log
308
     *  or
309
     *     /var/log/one/$VM_ID.log
310
     *  @return the log location for the VM.
311
     */
312
    string get_vm_log_filename(int oid);
313

    
314
    /**
315
     *  Returns the name of the host running oned
316
     *    @return the name
317
     */
318
    const string& get_nebula_hostname()
319
    {
320
        return hostname;
321
    };
322

    
323
    /**
324
     *  Returns the version of oned
325
     *    @return the version
326
     */
327
    static string version()
328
    {
329
        return "OpenNebula " + code_version();
330
    };
331

    
332
    /**
333
     *  Returns the version of oned
334
     * @return
335
     */
336
    static string code_version()
337
    {
338
        return "5.0.1"; // bump version
339
    }
340

    
341
    /**
342
     * Version needed for the DB, shared tables
343
     * @return
344
     */
345
    static string shared_db_version()
346
    {
347
        return "4.90.0";
348
    }
349

    
350
    /**
351
     * Version needed for the DB, local tables
352
     * @return
353
     */
354
    static string local_db_version()
355
    {
356
        return "4.90.0";
357
    }
358

    
359
    /**
360
     *  Starts all the modules and services for OpenNebula
361
     */
362
    void start(bool bootstrap_only=false);
363

    
364
    /**
365
     *  Initialize the database
366
     */
367
    void bootstrap_db()
368
    {
369
        start(true);
370
    }
371

    
372
    // --------------------------------------------------------------
373
    // Federation
374
    // --------------------------------------------------------------
375

    
376
    bool is_federation_enabled()
377
    {
378
        return federation_enabled;
379
    };
380

    
381
    bool is_federation_master()
382
    {
383
        return federation_master;
384

    
385
    };
386

    
387
    bool is_federation_slave()
388
    {
389
        return federation_enabled && !federation_master;
390
    };
391

    
392
    int get_zone_id()
393
    {
394
        return zone_id;
395
    };
396

    
397
    const string& get_master_oned()
398
    {
399
        return master_oned;
400
    };
401

    
402
    // -----------------------------------------------------------------------
403
    // Configuration attributes (read from oned.conf)
404
    // -----------------------------------------------------------------------
405

    
406
    /**
407
     *  Gets a configuration attribute for oned
408
     *    @param name of the attribute
409
     *    @param value of the attribute
410
     */
411
    template<typename T>
412
    void get_configuration_attribute(const string& name, T& value) const
413
    {
414
        nebula_configuration->get(name, value);
415
    };
416

    
417
    /**
418
     *  Gets a DS configuration attribute
419
     */
420
    int get_ds_conf_attribute(
421
        const std::string& ds_name,
422
        const VectorAttribute* &value) const
423
    {
424
        return get_conf_attribute("DS_MAD_CONF", ds_name, value);
425
    };
426

    
427
    /**
428
     *  Gets a TM configuration attribute
429
     */
430
    int get_tm_conf_attribute(
431
        const string& tm_name,
432
        const VectorAttribute* &value) const
433
    {
434
        return get_conf_attribute("TM_MAD_CONF", tm_name, value);
435
    };
436

    
437
    /**
438
     *  Gets a Market configuration attribute
439
     */
440
    int get_market_conf_attribute(
441
        const string& mk_name,
442
        const VectorAttribute* &value) const
443
    {
444
        return get_conf_attribute("MARKET_MAD_CONF", mk_name, value);
445
    };
446

    
447
    /**
448
     *  Gets an Auth driver configuration attribute
449
     */
450
    int get_auth_conf_attribute(
451
        const string& driver_name,
452
        const VectorAttribute* &value) const
453
    {
454
        return get_conf_attribute("AUTH_MAD_CONF", driver_name, value);
455
    };
456

    
457
    /**
458
     *  Gets an XML document with all of the configuration attributes
459
     *    @return the XML
460
     */
461
    string get_configuration_xml() const
462
    {
463
        string xml;
464
        return nebula_configuration->to_xml(xml);
465
    };
466

    
467
    // -----------------------------------------------------------------------
468
    // Default Quotas
469
    // -----------------------------------------------------------------------
470

    
471
    /**
472
     *  Get the default quotas for OpenNebula users
473
     *    @return the default quotas
474
     */
475
    const DefaultQuotas& get_default_user_quota()
476
    {
477
        return default_user_quota;
478
    };
479

    
480
    /**
481
     *  Set the default quotas for OpenNebula users
482
     *    @param tmpl template with the default quotas
483
     *    @param error describes the error if any
484
     *
485
     *    @return 0 if success
486
     */
487
    int set_default_user_quota(Template *tmpl, string& error)
488
    {
489
        int rc = default_user_quota.set(tmpl, error);
490

    
491
        if ( rc == 0 )
492
        {
493
            rc = default_user_quota.update();
494
        }
495

    
496
        return rc;
497
    };
498

    
499
    /**
500
     *  Get the default quotas for OpenNebula for groups
501
     *    @return the default quotas
502
     */
503
    const DefaultQuotas& get_default_group_quota()
504
    {
505
        return default_group_quota;
506
    };
507

    
508
    /**
509
     *  Set the default quotas for OpenNebula groups
510
     *    @param tmpl template with the default quotas
511
     *    @param error describes the error if any
512
     *
513
     *    @return 0 if success
514
     */
515
    int set_default_group_quota(Template *tmpl, string& error)
516
    {
517
        int rc = default_group_quota.set(tmpl, error);
518

    
519
        if ( rc == 0 )
520
        {
521
            rc = default_group_quota.update();
522
        }
523

    
524
        return rc;
525
    };
526

    
527
    // -----------------------------------------------------------------------
528
    // System attributes
529
    // -----------------------------------------------------------------------
530
    /**
531
     *  Reads a System attribute from the DB
532
     *    @param attr_name name of the attribute
533
     *    @param cb Callback that will receive the attribute in XML
534
     *    @return 0 on success
535
     */
536
    int select_sys_attribute(const string& attr_name, string& attr_xml)
537
    {
538
        return system_db->select_sys_attribute(attr_name, attr_xml);
539
    };
540

    
541
    /**
542
     *  Writes a system attribute in the database.
543
     *    @param db pointer to the db
544
     *    @return 0 on success
545
     */
546
    int insert_sys_attribute(
547
        const string& attr_name,
548
        const string& xml_attr,
549
        string&       error_str)
550
    {
551
        return system_db->insert_sys_attribute(attr_name, xml_attr, error_str);
552
    };
553

    
554
    /**
555
     *  Updates the system attribute in the database.
556
     *    @param db pointer to the db
557
     *    @return 0 on success
558
     */
559
    int update_sys_attribute(
560
        const string& attr_name,
561
        const string& xml_attr,
562
        string&       error_str)
563
    {
564
        return system_db->update_sys_attribute(attr_name, xml_attr, error_str);
565
    };
566

    
567
private:
568

    
569
    // -----------------------------------------------------------------------
570
    //Constructors and = are private to only access the class through instance
571
    // -----------------------------------------------------------------------
572

    
573
    Nebula():nebula_configuration(0),
574
        default_user_quota( "DEFAULT_USER_QUOTAS",
575
                            "/DEFAULT_USER_QUOTAS/DATASTORE_QUOTA",
576
                            "/DEFAULT_USER_QUOTAS/NETWORK_QUOTA",
577
                            "/DEFAULT_USER_QUOTAS/IMAGE_QUOTA",
578
                            "/DEFAULT_USER_QUOTAS/VM_QUOTA"),
579
        default_group_quota("DEFAULT_GROUP_QUOTAS",
580
                            "/DEFAULT_GROUP_QUOTAS/DATASTORE_QUOTA",
581
                            "/DEFAULT_GROUP_QUOTAS/NETWORK_QUOTA",
582
                            "/DEFAULT_GROUP_QUOTAS/IMAGE_QUOTA",
583
                            "/DEFAULT_GROUP_QUOTAS/VM_QUOTA"),
584
        system_db(0), db(0),
585
        vmpool(0), hpool(0), vnpool(0), upool(0), ipool(0), gpool(0), tpool(0),
586
        dspool(0), clpool(0), docpool(0), zonepool(0),
587
        secgrouppool(0), vdcpool(0), vrouterpool(0), marketpool(0), apppool(0),
588
        lcm(0), vmm(0), im(0), tm(0), dm(0), rm(0), hm(0), authm(0),
589
        aclm(0), imagem(0), marketm(0), ipamm(0)
590
    {
591
        const char * nl = getenv("ONE_LOCATION");
592

    
593
        if (nl == 0) //OpenNebula installed under root directory
594
        {
595
            nebula_location = "/";
596

    
597
            mad_location     = "/usr/lib/one/mads/";
598
            etc_location     = "/etc/one/";
599
            log_location     = "/var/log/one/";
600
            var_location     = "/var/lib/one/";
601
            remotes_location = "/var/lib/one/remotes/";
602
            vms_location     = "/var/lib/one/vms/";
603
        }
604
        else
605
        {
606
            nebula_location = nl;
607

    
608
            if ( nebula_location.at(nebula_location.size()-1) != '/' )
609
            {
610
                nebula_location += "/";
611
            }
612

    
613
            mad_location     = nebula_location + "lib/mads/";
614
            etc_location     = nebula_location + "etc/";
615
            log_location     = nebula_location + "var/";
616
            var_location     = nebula_location + "var/";
617
            remotes_location = nebula_location + "var/remotes/";
618
            vms_location     = nebula_location + "var/vms/";
619
        }
620
    };
621

    
622
    ~Nebula()
623
    {
624
        delete vmpool;
625
        delete vnpool;
626
        delete hpool;
627
        delete upool;
628
        delete ipool;
629
        delete gpool;
630
        delete tpool;
631
        delete dspool;
632
        delete clpool;
633
        delete docpool;
634
        delete zonepool;
635
        delete secgrouppool;
636
        delete vdcpool;
637
        delete vrouterpool;
638
        delete marketpool;
639
        delete apppool;
640
        delete vmm;
641
        delete lcm;
642
        delete im;
643
        delete tm;
644
        delete dm;
645
        delete rm;
646
        delete hm;
647
        delete authm;
648
        delete aclm;
649
        delete imagem;
650
        delete marketm;
651
        delete ipamm;
652
        delete nebula_configuration;
653
        delete db;
654
        delete system_db;
655
    };
656

    
657
    Nebula& operator=(Nebula const&){return *this;};
658

    
659
    // ---------------------------------------------------------------
660
    // Environment variables
661
    // ---------------------------------------------------------------
662

    
663
    string  nebula_location;
664

    
665
    string  mad_location;
666
    string  etc_location;
667
    string  log_location;
668
    string  var_location;
669
    string  hook_location;
670
    string  remotes_location;
671
    string  vms_location;
672

    
673
    string  hostname;
674

    
675
    // ---------------------------------------------------------------
676
    // Configuration
677
    // ---------------------------------------------------------------
678

    
679
    OpenNebulaTemplate * nebula_configuration;
680

    
681
    // ---------------------------------------------------------------
682
    // Federation
683
    // ---------------------------------------------------------------
684

    
685
    bool    federation_enabled;
686
    bool    federation_master;
687
    int     zone_id;
688
    string  master_oned;
689

    
690
    // ---------------------------------------------------------------
691
    // Default quotas
692
    // ---------------------------------------------------------------
693

    
694
    DefaultQuotas default_user_quota;
695
    DefaultQuotas default_group_quota;
696

    
697
    // ---------------------------------------------------------------
698
    // The system database
699
    // ---------------------------------------------------------------
700

    
701
    SystemDB * system_db;
702

    
703
    // ---------------------------------------------------------------
704
    // Nebula Pools
705
    // ---------------------------------------------------------------
706

    
707
    SqlDB              * db;
708
    VirtualMachinePool * vmpool;
709
    HostPool           * hpool;
710
    VirtualNetworkPool * vnpool;
711
    UserPool           * upool;
712
    ImagePool          * ipool;
713
    GroupPool          * gpool;
714
    VMTemplatePool     * tpool;
715
    DatastorePool      * dspool;
716
    ClusterPool        * clpool;
717
    DocumentPool       * docpool;
718
    ZonePool           * zonepool;
719
    SecurityGroupPool  * secgrouppool;
720
    VdcPool            * vdcpool;
721
    VirtualRouterPool  * vrouterpool;
722
    MarketPlacePool    * marketpool;
723
    MarketPlaceAppPool * apppool;
724

    
725
    // ---------------------------------------------------------------
726
    // Nebula Managers
727
    // ---------------------------------------------------------------
728

    
729
    LifeCycleManager *      lcm;
730
    VirtualMachineManager * vmm;
731
    InformationManager *    im;
732
    TransferManager *       tm;
733
    DispatchManager *       dm;
734
    RequestManager *        rm;
735
    HookManager *           hm;
736
    AuthManager *           authm;
737
    AclManager *            aclm;
738
    ImageManager *          imagem;
739
    MarketPlaceManager *    marketm;
740
    IPAMManager *           ipamm;
741

    
742
    // ---------------------------------------------------------------
743
    // Implementation functions
744
    // ---------------------------------------------------------------
745

    
746
    friend void nebula_signal_handler (int sig);
747

    
748
    // ---------------------------------------------------------------
749
    // Helper functions
750
    // ---------------------------------------------------------------
751

    
752
    /**
753
     *  Gets a Generic configuration attribute
754
     *  @param key String that identifies the configuration parameter group name
755
     *  @param name Name of the specific configuration parameter
756
     *  @param value Value of the specific configuration parameter
757
     *  @return a reference to the generated string
758
     */
759

    
760
    int get_conf_attribute(
761
        const std::string& key,
762
        const std::string& name,
763
        const VectorAttribute* &value) const;
764

    
765
};
766

    
767
#endif /*NEBULA_H_*/