Statistics
| Branch: | Tag: | Revision:

one / src / cluster / ClusterPool.cc @ 6329d8b3

History | View | Annotate | Download (4.35 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2010, 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 "ClusterPool.h"
18
#include "Nebula.h"
19
#include "NebulaLog.h"
20

    
21
#include <stdexcept>
22

    
23
const string ClusterPool::DEFAULT_CLUSTER_NAME = "default";
24
const int    ClusterPool::DEFAULT_CLUSTER_ID   = 0;
25

    
26
/* -------------------------------------------------------------------------- */
27
/* -------------------------------------------------------------------------- */
28

    
29
ClusterPool::ClusterPool(SqlDB * db):PoolSQL(db, Cluster::table)
30
{
31
    // lastOID is set in PoolSQL::init_cb
32
    if (get_lastOID() == -1)
33
    {
34
        int         rc;
35
        Cluster *   cluster;
36
        string      error_str;
37

    
38
        // Build a new Cluster object
39
        cluster = new Cluster(DEFAULT_CLUSTER_ID, DEFAULT_CLUSTER_NAME);
40

    
41
        // Insert the Object in the pool
42
        rc = PoolSQL::allocate(cluster, error_str);
43

    
44
        if(rc != 0)
45
        {
46
            ostringstream oss;
47

    
48
            oss << "Error trying to create default cluster: " << error_str;
49
            NebulaLog::log("CLUSTER",Log::ERROR,oss);
50

    
51
            throw runtime_error(oss.str());
52
        }
53
    }
54
}
55

    
56
/* -------------------------------------------------------------------------- */
57
/* -------------------------------------------------------------------------- */
58

    
59
int ClusterPool::allocate(int * oid, string name, string& error_str)
60
{
61
    Cluster *       cluster;
62
    ostringstream   oss;
63

    
64
    if ( name.empty() )
65
    {
66
        goto error_name;
67
    }
68

    
69
    // Check for duplicates
70
    cluster = get(name, false);
71

    
72
    if( cluster != 0 )
73
    {
74
        goto error_duplicated;
75
    }
76

    
77
    // Build a new Cluster object
78
    cluster = new Cluster(-1, name);
79

    
80
    // Insert the Object in the pool
81
    *oid = PoolSQL::allocate(cluster, error_str);
82

    
83
    return *oid;
84

    
85

    
86
error_name:
87
    oss << "NAME cannot be empty.";
88
    goto error_common;
89

    
90
error_duplicated:
91
    oss << "NAME is already taken by CLUSTER " << cluster->get_oid() << ".";
92

    
93
error_common:
94
    *oid = -1;
95
    error_str = oss.str();
96

    
97
    return *oid;
98
}
99

    
100
/* -------------------------------------------------------------------------- */
101
/* -------------------------------------------------------------------------- */
102

    
103
int ClusterPool::drop(Cluster * cluster)
104
{
105
    int         rc;
106

    
107
    Host*                   host;
108
    vector<int>             hids;
109
    vector<int>::iterator   hid_it;
110

    
111
    Nebula&     nd = Nebula::instance();
112
    HostPool *  hpool = nd.get_hpool();
113

    
114
    int         cluster_id = cluster->get_oid();
115

    
116
    ostringstream where;
117
    where << "cid = " << cluster_id;
118

    
119
    // Return error if cluster is 'default'
120
    if( cluster->get_oid() == DEFAULT_CLUSTER_ID )
121
    {
122
        NebulaLog::log("CLUSTER",Log::WARNING,
123
                       "Default cluster cannot be deleted.");
124

    
125
        return -1;
126
    }
127

    
128
    rc = cluster->drop(db);
129

    
130
    // Move the hosts assigned to the deleted cluster to the default one
131
    if( rc == 0 )
132
    {
133
        hpool->search(hids, where.str());
134

    
135
        for ( hid_it=hids.begin() ; hid_it < hids.end(); hid_it++ )
136
        {
137
            host = hpool->get(*hid_it, true);
138

    
139
            if ( host == 0 )
140
            {
141
                continue;
142
            }
143

    
144
            set_default_cluster(host);
145

    
146
            hpool->update(host);
147

    
148
            host->unlock();
149
        }
150
    }
151

    
152
    return rc;
153
}