0001-implement-an-init-method-for-the-nebula-daemon.patch

Robert Schweikert, 12/06/2012 09:46 PM

Download (12.2 KB)

View differences:

include/Nebula.h
303 303
        return "3.9.0";
304 304
    }
305 305

  
306
    void init();
306 307
    void start();
307 308

  
308 309
    void get_configuration_attribute(
share/pkgs/openSUSE/systemd/onedsetup
79 79
fi
80 80

  
81 81
# Start the one daemon
82
$ONED -f 2>&1 &
82
$ONED -i 2>&1 &
83 83
STARTED=$?
84 84
CURPID=$!
85 85

  
......
88 88
    exit 1
89 89
fi
90 90

  
91
# Give oned a chance to do it's thing...
92
sleep 2
91
# Give oned a chance to do it's thing and exit
92
sleep 5
93 93

  
94
# OK we're all done here
94
# Just in case the process gets stuck, kill it
95 95
kill -TERM $CURPID > /dev/null 2>&1
96 96

  
97 97
counter=0
......
105 105
done
106 106

  
107 107
# If the lock file is left over remove it
108
rm -f /var/lol/one/one
108
rm -f /var/lock/one/one
src/nebula/Nebula.cc
24 24
#include <stdexcept>
25 25
#include <libxml/parser.h>
26 26

  
27
#include <errno.h>
27 28
#include <signal.h>
28 29
#include <unistd.h>
29 30
#include <fcntl.h>
......
116 117

  
117 118
int SystemDB::check_db_version()
118 119
{
119
    int             rc;
120 120
    ostringstream   oss;
121 121

  
122 122
    string loaded_db_version = "";
......
133 133
    oss.str("");
134 134
    unset_callback();
135 135

  
136
    if( loaded_db_version == "" )
137
    {
138
        // Table user_pool is present for all OpenNebula versions, and it
139
        // always contains at least the oneadmin user.
140
        oss << "SELECT MAX(oid) FROM user_pool";
141
        rc = db->exec(oss);
142

  
143
        oss.str("");
144

  
145
        if( rc != 0 )   // Database needs bootstrap
146
        {
147
            return -2;
148
        }
149
    }
150

  
151 136
    if( Nebula::db_version() != loaded_db_version )
152 137
    {
153 138
        oss << "Database version mismatch. "
......
265 250
    return 0;
266 251
}
267 252

  
268
/* -------------------------------------------------------------------------- */
269
/* -------------------------------------------------------------------------- */
270

  
271
void Nebula::start()
253
/* ------------------------------------------------------------------------- */
254
/* ------------------------------------------------------------------------- */
255
void Nebula::init()
272 256
{
273
    int             rc;
274
    int             fd;
275
    sigset_t        mask;
276
    int             signal;
277
    char            hn[80];
278
    string          scripts_remote_dir;
279

  
280
    if ( gethostname(hn,79) != 0 )
281
    {
282
        throw runtime_error("Error getting hostname");
283
    }
284

  
285
    hostname = hn;
286

  
257
    int    rc;
258
    string scripts_remote_dir;
287 259
    // -----------------------------------------------------------
288 260
    // Configuration
289 261
    // -----------------------------------------------------------
......
298 270
    }
299 271

  
300 272
    string   config_fname = var_location + "config";
301
    ofstream config_file(config_fname.c_str(), ios_base::trunc & ios_base::out);
273
    ofstream config_file(config_fname.c_str(),ios_base::trunc & ios_base::out);
302 274

  
303 275
    if (config_file.fail() == false)
304 276
    {
......
308 280

  
309 281
    nebula_configuration->get("SCRIPTS_REMOTE_DIR", scripts_remote_dir);
310 282
    hook_location = scripts_remote_dir + "/hooks/";
311

  
283
    
312 284
    // -----------------------------------------------------------
313 285
    // Log system
314 286
    // -----------------------------------------------------------
......
363 335
        int  rc;
364 336

  
365 337
        bool   db_is_sqlite = true;
338
        bool   bootstrap_db = false;
366 339

  
367 340
        string server  = "localhost";
368 341
        string port_str;
......
424 397

  
425 398
        if ( db_is_sqlite )
426 399
        {
427
            string  db_name = var_location + "one.db";
428

  
400
            db_name = var_location + "one.db";
401
            rc = access(db_name.c_str(), R_OK|W_OK|F_OK);
402
            if (rc == -1)
403
            {
404
                if (errno == ENOENT)
405
                {
406
                    bootstrap_db = true;
407
                }
408
                else
409
                {
410
                    throw runtime_error("Could not acces one.db file");
411
                } 
412
            }
429 413
            db = new SqliteDB(db_name);
430 414
        }
431 415
        else
......
433 417
            ostringstream   oss;
434 418

  
435 419
            db = new MySqlDB(server,port,user,passwd,db_name);
436

  
437
            oss << "CREATE DATABASE IF NOT EXISTS " << db_name;
420
            
421
            oss << "USE " << db_name;
438 422
            rc = db->exec(oss);
423
            if ( rc == 1044 ) {
439 424

  
440
            if ( rc != 0 )
441
            {
442
                throw runtime_error("Could not create database.");
443
            }
425
                oss.str("");                
426
                oss << "CREATE DATABASE " << db_name;
427
                rc = db->exec(oss);
444 428

  
445
            oss.str("");
446
            oss << "USE " << db_name;
447
            rc = db->exec(oss);
448
            if ( rc != 0 )
429
                if ( rc == 0 )
430
                {
431
                    bootstrap_db = true;
432
                }
433
                else if ( rc == 1044 )
434
                {
435
                    throw runtime_error("Could not create database.");
436
                }
437
            }
438
            else
449 439
            {
450 440
                throw runtime_error("Could not open database.");
451 441
            }
452 442
        }
453 443

  
454
        // ---------------------------------------------------------------------
444
        // --------------------------------------------------------------------
455 445
        // Prepare the SystemDB and check versions
456
        // ---------------------------------------------------------------------
457

  
458
        NebulaLog::log("ONE",Log::INFO,"Checking database version.");
446
        // --------------------------------------------------------------------
459 447

  
460 448
        system_db = new SystemDB(db);
461 449

  
462
        rc = system_db->check_db_version();
463

  
464
        if( rc == -1 )
465
        {
466
            throw runtime_error("Database version mismatch.");
467
        }
468

  
469
        if( rc == -2 )
470
        {
450
        if (bootstrap_db) {
471 451
            rc = 0;
472

  
473
            NebulaLog::log("ONE",Log::INFO,"Bootstrapping OpenNebula database.");
452
            NebulaLog::log("ONE",Log::INFO,
453
                           "Bootstrapping OpenNebula database.");
474 454

  
475 455
            rc += VirtualMachinePool::bootstrap(db);
476 456
            rc += HostPool::bootstrap(db);
......
497 477
            if ( rc != 0 )
498 478
            {
499 479
                throw runtime_error("Error bootstrapping database.");
500
            }
480
            } 
481
        }
482

  
483
        NebulaLog::log("ONE",Log::INFO,"Checking database version.");
484
        rc = system_db->check_db_version();
485

  
486
        if( rc == -1 )
487
        {
488
            throw runtime_error("Database version mismatch.");
501 489
        }
502 490
    }
503 491
    catch (exception&)
504 492
    {
505 493
        throw;
506 494
    }
495
}
496

  
497
/* -------------------------------------------------------------------------- */
498
/* -------------------------------------------------------------------------- */
499

  
500
void Nebula::start()
501
{
502
    int             rc;
503
    int             fd;
504
    sigset_t        mask;
505
    int             signal;
506
    char            hn[80];
507

  
508
    if ( gethostname(hn,79) != 0 )
509
    {
510
        throw runtime_error("Error getting hostname");
511
    }
512

  
513
    hostname = hn;
514

  
515
    Nebula::init();
507 516

  
508 517
    try
509 518
    {
src/nebula/oned.cc
32 32
"SYNOPSIS\n"
33 33
"  Starts the OpenNebula daemon\n\n"
34 34
"OPTIONS\n"
35
"\t-h\tprints this help.\n"
36 35
"\t-v\tprints OpenNebula version and license\n"
37
"\t-f\tforeground, do not fork the oned daemon\n";
36
"\t-h\tprints this help.\n"
37
"\t-f\tforeground, do not fork the oned daemon\n"
38
"\t-i\tinitialize the dabase and exit.\n";
39

  
38 40

  
39 41
static const char * susage =
40 42
"usage: oned [-h] [-v] [-f]\n";
41 43

  
42
/* -------------------------------------------------------------------------- */
43
/* -------------------------------------------------------------------------- */
44
/* ------------------------------------------------------------------------- */
45
/* ------------------------------------------------------------------------- */
44 46

  
45 47
static void print_license()
46 48
{
47 49
    cout<< "Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org)\n\n"
48
        << Nebula::version() << " is distributed and licensed for use under the"
49
        << " terms of the\nApache License, Version 2.0 "
50
        << Nebula::version() << " is distributed and licensed for use under "
51
        << "the terms of the\nApache License, Version 2.0 "
50 52
        << "(http://www.apache.org/licenses/LICENSE-2.0).\n";
51 53
}
52 54

  
53
/* -------------------------------------------------------------------------- */
54
/* -------------------------------------------------------------------------- */
55
/* ------------------------------------------------------------------------- */
56
/* ------------------------------------------------------------------------- */
57

  
58
static void oned_init()
59
{
60
    try
61
    {
62
        Nebula& nd  = Nebula::instance();
63
        nd.init();   
64
    }
65
    catch (exception &e)
66
    {
67
        cerr << e.what() << endl;
68
 
69
        return;
70
    }
71
}
72

  
73
/* ------------------------------------------------------------------------- */
74
/* ------------------------------------------------------------------------- */
55 75

  
56 76
static void oned_main()
57 77
{
......
68 88
    }
69 89
}
70 90
    
71
/* -------------------------------------------------------------------------- */
72
/* -------------------------------------------------------------------------- */
91
/* ------------------------------------------------------------------------- */
92
/* ------------------------------------------------------------------------- */
73 93
    
74 94
int main(int argc, char **argv)
75 95
{
......
81 101
    string          wd;
82 102
    int             rc;
83 103
            
84
    while((opt = getopt(argc,argv,"vhf")) != -1)
104
    while((opt = getopt(argc,argv,"vhif")) != -1)
85 105
        switch(opt)
86 106
        {
87 107
            case 'v':
......
92 112
                cout << usage;
93 113
                exit(0);
94 114
                break;
115
            case 'i':
116
                oned_init();
117
                exit(0);
118
                break;
95 119
            case 'f':
96 120
                foreground = true;
97 121
                break;        
src/sql/MySqlDB.cc
113 113

  
114 114
        unlock();
115 115

  
116
        return -1;
116
        return err_num;
117 117
    }
118 118

  
119 119

  
......
141 141

  
142 142
            unlock();
143 143

  
144
            return -1;
144
            return err_num;
145 145
        }
146 146

  
147 147
        // Fetch the names of the fields
148
-