0001-create-a-way-to-initialize-without-service-start.patch

Robert Schweikert, 10/05/2013 07:48 PM

Download (9.83 KB)

View differences:

include/Nebula.h
343 343
     */
344 344
    void start();
345 345

  
346
    /**
347
     *  Initialize the database
348
     */
349
    void init();
350

  
346 351
    // -----------------------------------------------------------------------
347 352
    // Configuration attributes (read from oned.conf)
348 353
    // -----------------------------------------------------------------------
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

  
......
89 89
fi
90 90

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

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

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

  
107 108
# If the lock file is left over remove it
108
rm -f /var/lol/one/one
109
rm -f /var/lock/one/one
src/nebula/Nebula.cc
23 23
#include <stdlib.h>
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. "
......
268 253
/* -------------------------------------------------------------------------- */
269 254
/* -------------------------------------------------------------------------- */
270 255

  
271
void Nebula::start()
256
void Nebula::init()
272 257
{
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;
258
    int    rc;
259
    string scripts_remote_dir;
286 260

  
287 261
    // -----------------------------------------------------------
288 262
    // Configuration
......
384 358
        int  rc;
385 359

  
386 360
        bool   db_is_sqlite = true;
361
        bool   bootstrap_db = false;
387 362

  
388 363
        string server  = "localhost";
389 364
        string port_str;
......
445 420

  
446 421
        if ( db_is_sqlite )
447 422
        {
448
            string  db_name = var_location + "one.db";
449

  
423
            db_name = var_location + "one.db";
424
            rc = access(db_name.c_str(), R_OK|W_OK|F_OK);
425
            if (rc == -1)
426
            {
427
                if (errno == ENOENT)
428
                {
429
                    bootstrap_db = true;
430
                }
431
                else
432
                {
433
                    throw runtime_error("Could not acces one.db file");
434
                } 
435
            }
450 436
            db = new SqliteDB(db_name);
451 437
        }
452 438
        else
......
454 440
            ostringstream   oss;
455 441

  
456 442
            db = new MySqlDB(server,port,user,passwd,db_name);
443

  
444
            oss << "USE " << db_name;
445
            rc = db->exec(oss);
446

  
447
            if ( rc == 1044 )
448
            {
449
                oss.str("");                
450
                oss << "CREATE DATABASE " << db_name;
451
                rc = db->exec(oss);
452

  
453
                if ( rc == 0 )
454
                {
455
                    bootstrap_db = true;
456
                }
457
                else if ( rc == 1044 )
458
                {
459
                    throw runtime_error("Could not create database.");
460
                }
461
            }
462
            else if ( rc != 0 )
463
            {
464
                throw runtime_error("Could not open database.");
465
            }
457 466
        }
458 467

  
459 468
        // ---------------------------------------------------------------------
460 469
        // Prepare the SystemDB and check versions
461 470
        // ---------------------------------------------------------------------
462 471

  
463
        NebulaLog::log("ONE",Log::INFO,"Checking database version.");
464

  
465 472
        system_db = new SystemDB(db);
466 473

  
467
        rc = system_db->check_db_version();
468

  
469
        if( rc == -1 )
470
        {
471
            throw runtime_error("Database version mismatch.");
472
        }
473

  
474
        if( rc == -2 )
475
        {
474
        if (bootstrap_db) {
476 475
            rc = 0;
477

  
478
            NebulaLog::log("ONE",Log::INFO,"Bootstrapping OpenNebula database.");
476
            NebulaLog::log("ONE",Log::INFO,
477
                           "Bootstrapping OpenNebula database.");
479 478

  
480 479
            rc += VirtualMachinePool::bootstrap(db);
481 480
            rc += HostPool::bootstrap(db);
......
502 501
            if ( rc != 0 )
503 502
            {
504 503
                throw runtime_error("Error bootstrapping database.");
505
            }
504
            } 
505
        }
506

  
507
        NebulaLog::log("ONE",Log::INFO,"Checking database version.");
508
        rc = system_db->check_db_version();
509

  
510
        if( rc == -1 )
511
        {
512
            throw runtime_error("Database version mismatch.");
506 513
        }
507 514
    }
508 515
    catch (exception&)
509 516
    {
510 517
        throw;
511 518
    }
519
}
520

  
521
/* -------------------------------------------------------------------------- */
522
/* -------------------------------------------------------------------------- */
523

  
524
void Nebula::start()
525
{
526
    int             rc;
527
    int             fd;
528
    sigset_t        mask;
529
    int             signal;
530
    char            hn[80];
531

  
532
    if ( gethostname(hn,79) != 0 )
533
    {
534
        throw runtime_error("Error getting hostname");
535
    }
536

  
537
    hostname = hn;
538

  
539
    Nebula::init();
512 540

  
513 541
    try
514 542
    {
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";
38 39

  
39 40
static const char * susage =
40 41
"usage: oned [-h] [-v] [-f]\n";
......
50 51
        << "(http://www.apache.org/licenses/LICENSE-2.0).\n";
51 52
}
52 53

  
54
/* ------------------------------------------------------------------------- */
55
/* ------------------------------------------------------------------------- */
56

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

  
53 72
/* -------------------------------------------------------------------------- */
54 73
/* -------------------------------------------------------------------------- */
55 74

  
......
81 100
    string          wd;
82 101
    int             rc;
83 102
            
84
    while((opt = getopt(argc,argv,"vhf")) != -1)
103
    while((opt = getopt(argc,argv,"vhif")) != -1)
85 104
        switch(opt)
86 105
        {
87 106
            case 'v':
......
92 111
                cout << usage;
93 112
                exit(0);
94 113
                break;
114
            case 'i':
115
                oned_init();
116
                exit(0);
117
                break;
95 118
            case 'f':
96 119
                foreground = true;
97 120
                break;        
src/sql/MySqlDB.cc
180 180

  
181 181
        free_db_connection(db);
182 182

  
183
        return -1;
183
        return err_num;
184 184
    }
185 185

  
186 186

  
......
208 208

  
209 209
            free_db_connection(db);
210 210

  
211
            return -1;
211
            return err_num;
212 212
        }
213 213

  
214 214
        // Fetch the names of the fields
215
-