Statistics
| Branch: | Tag: | Revision:

one / src / market / MarketPlaceAppPool.cc @ 6368bb51

History | View | Annotate | Download (9.52 KB)

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

    
21
/* -------------------------------------------------------------------------- */
22
/* -------------------------------------------------------------------------- */
23

    
24
static int master_allocate(MarketPlaceApp * mp, string& error)
25
{
26
    Client * client = Client::client();
27

    
28
    xmlrpc_c::value         result;
29
    vector<xmlrpc_c::value> values;
30

    
31
    std::string        mp_xml;
32
    std::ostringstream oss("Cannot allocate marketapp at federation master: ",
33
            std::ios::ate);
34

    
35
    mp->to_xml(mp_xml);
36

    
37
    try
38
    {
39
        client->call("one.marketapp.allocatedb", "s", &result, mp_xml.c_str());
40
    }
41
    catch (exception const& e)
42
    {
43
        oss << e.what();
44
        error = oss.str();
45

    
46
        return -1;
47
    }
48

    
49
    values = xmlrpc_c::value_array(result).vectorValueValue();
50

    
51
    if ( xmlrpc_c::value_boolean(values[0]) == false )
52
    {
53
        std::string error_xml = xmlrpc_c::value_string(values[1]);
54

    
55
        oss << error_xml;
56
        error = oss.str();
57

    
58
        return -1;
59
    }
60

    
61
    int oid = xmlrpc_c::value_int(values[1]);
62

    
63
    return oid;
64
}
65

    
66
/* -------------------------------------------------------------------------- */
67

    
68
int MarketPlaceAppPool:: allocate(
69
            int                uid,
70
            int                gid,
71
            const std::string& uname,
72
            const std::string& gname,
73
            int                umask,
74
            MarketPlaceAppTemplate * apptemplate,
75
            int                mp_id,
76
            const std::string& mp_name,
77
            int *              oid,
78
            std::string&       error_str)
79
{
80
    MarketPlaceApp * mp;
81
    MarketPlaceApp * mp_aux = 0;
82

    
83
    std::string name;
84

    
85
    std::ostringstream oss;
86

    
87
    // -------------------------------------------------------------------------
88
    // Build the marketplace app object
89
    // -------------------------------------------------------------------------
90
    mp = new MarketPlaceApp(uid, gid, uname, gname, umask, apptemplate);
91

    
92
    mp->market_id   = mp_id;
93
    mp->market_name = mp_name;
94
    mp->zone_id     = Nebula::instance().get_zone_id();
95

    
96
    mp->state = MarketPlaceApp::INIT;
97

    
98
    mp->get_template_attribute("NAME", name);
99

    
100
    if ( !PoolObjectSQL::name_is_valid(name, error_str) )
101
    {
102
        goto error_name;
103
    }
104

    
105
    mp_aux = get(name, uid, false);
106

    
107
    if( mp_aux != 0 )
108
    {
109
        goto error_duplicated;
110
    }
111

    
112
    if ( mp->parse_template(error_str) != 0 )
113
    {
114
        goto error_template;
115
    }
116

    
117
    // -------------------------------------------------------------------------
118
    // Insert the object in the Database
119
    // -------------------------------------------------------------------------
120
    if (Nebula::instance().is_federation_slave())
121
    {
122
        *oid = master_allocate(mp, error_str);
123

    
124
        delete mp;
125

    
126
        return *oid;
127
    }
128

    
129
    *oid = PoolSQL::allocate(mp, error_str);
130

    
131
    return *oid;
132

    
133
error_duplicated:
134
    oss << "NAME is already taken by MARKETPLACEAPP " << mp_aux->get_oid();
135
    error_str = oss.str();
136

    
137
error_name:
138
error_template:
139
    delete mp;
140
    *oid = -1;
141

    
142
    return *oid;
143
}
144

    
145
/* -------------------------------------------------------------------------- */
146
/* -------------------------------------------------------------------------- */
147

    
148
int MarketPlaceAppPool::drop(PoolObjectSQL * objsql, std::string& error_msg)
149
{
150
    if (Nebula::instance().is_federation_slave())
151
    {
152
        Client * client = Client::client();
153

    
154
        xmlrpc_c::value result;
155
        vector<xmlrpc_c::value> values;
156

    
157
        std::ostringstream oss("Cannot drop marketapp at federation master: ",
158
                std::ios::ate);
159
        try
160
        {
161
            client->call("one.marketapp.dropdb", "i", &result, objsql->get_oid());
162
        }
163
        catch (exception const& e)
164
        {
165
            oss << e.what();
166

    
167
            error_msg = oss.str();
168
            return -1;
169
        }
170

    
171
        values = xmlrpc_c::value_array(result).vectorValueValue();
172

    
173
        if ( xmlrpc_c::value_boolean(values[0]) == false )
174
        {
175
            std::string error = xmlrpc_c::value_string(values[1]);
176

    
177
            oss << error;
178

    
179
            error_msg = oss.str();
180
            return -1;
181
        }
182

    
183
        return 0;
184
    }
185

    
186
    return PoolSQL::drop(objsql, error_msg);
187
}
188

    
189
/* -------------------------------------------------------------------------- */
190
/* -------------------------------------------------------------------------- */
191

    
192
int MarketPlaceAppPool::import(const std::string& t64, int mp_id,
193
        const std::string& mp_name, int& app_id, std::string& error_str)
194
{
195
    // -------------------------------------------------------------------------
196
    // Build the marketplace app object
197
    // -------------------------------------------------------------------------
198
    MarketPlaceApp * app = new MarketPlaceApp(UserPool::ONEADMIN_ID,
199
        GroupPool::ONEADMIN_ID, UserPool::oneadmin_name, GroupPool::ONEADMIN_NAME
200
        ,0133, 0);
201

    
202
    if ( app->from_template64(t64, error_str) != 0 )
203
    {
204
        app->lock();
205
        delete app;
206

    
207
        return -1;
208
    }
209

    
210
    app->market_id   = mp_id;
211
    app->market_name = mp_name;
212
        app->zone_id     = Nebula::instance().get_zone_id();
213

    
214
    if ( !PoolObjectSQL::name_is_valid(app->name, error_str) )
215
    {
216
        std::ostringstream oss;
217

    
218
        oss << "imported-" << app->get_origin_id();
219
        app->name = oss.str();
220

    
221
        if ( !PoolObjectSQL::name_is_valid(app->name, error_str) )
222
        {
223
            error_str = "Cannot generate a valida name for app";
224
            return -1;
225
        }
226
    }
227

    
228
    MarketPlaceApp * mp_aux = get(app->name, 0, true);
229

    
230
    if( mp_aux != 0 ) //Marketplace app already imported
231
    {
232
        app_id = mp_aux->oid;
233

    
234
        if ( mp_aux->version != app->version || mp_aux->md5 != app->md5 )
235
        {
236
            mp_aux->from_template64(t64, error_str);
237
            update(mp_aux);
238
        }
239

    
240
        mp_aux->unlock();
241

    
242
        app->lock();
243
        delete app;
244

    
245
        return -2;
246
    }
247

    
248
    // -------------------------------------------------------------------------
249
    // Insert the object in the Database
250
    // -------------------------------------------------------------------------
251
    if (Nebula::instance().is_federation_slave())
252
    {
253
        app_id = master_allocate(app, error_str);
254

    
255
        app->lock();
256
        delete app;
257

    
258
        return app_id;
259
    }
260

    
261
    app_id = PoolSQL::allocate(app, error_str);
262

    
263
    return app_id;
264
}
265

    
266
/* -------------------------------------------------------------------------- */
267
/* -------------------------------------------------------------------------- */
268

    
269
int MarketPlaceAppPool::update(PoolObjectSQL * objsql)
270
{
271
    if (Nebula::instance().is_federation_slave())
272
    {
273
        std::string tmpl_xml;
274
        Client * client = Client::client();
275

    
276
        xmlrpc_c::value result;
277
        vector<xmlrpc_c::value> values;
278

    
279
        std::ostringstream oss("Cannot update marketapp at federation master: ",
280
                std::ios::ate);
281

    
282
        try
283
        {
284
            client->call("one.marketapp.updatedb", "is", &result,
285
                objsql->get_oid(), objsql->to_xml(tmpl_xml).c_str());
286
        }
287
        catch (exception const& e)
288
        {
289
            oss << e.what();
290
            NebulaLog::log("MKP", Log::ERROR, oss);
291

    
292
            return -1;
293
        }
294

    
295
        values = xmlrpc_c::value_array(result).vectorValueValue();
296

    
297
        if ( xmlrpc_c::value_boolean(values[0]) == false )
298
        {
299
            std::string error = xmlrpc_c::value_string(values[1]);
300

    
301
            oss << error;
302
            NebulaLog::log("MKP", Log::ERROR, oss);
303

    
304
            return -1;
305
        }
306

    
307
        return 0;
308
    }
309

    
310
    return PoolSQL::update(objsql);
311
}
312

    
313
/* -------------------------------------------------------------------------- */
314
/* -------------------------------------------------------------------------- */
315
const int MarketPlaceAppPool::MAX_MISSING_MONITORS = 3;
316

    
317
bool MarketPlaceAppPool::test_map_check(int app_id)
318
{
319
    map<int, int>::iterator it = map_check.find(app_id);
320

    
321
    if ( it == map_check.end() )
322
    {
323
        return false;
324
    }
325

    
326
    it->second++;
327

    
328
    bool to_delete = it->second >= MAX_MISSING_MONITORS;
329

    
330
    if ( to_delete )
331
    {
332
        map_check.erase(it); 
333
    }
334

    
335
    return to_delete;
336
}
337

    
338
void MarketPlaceAppPool::reset_map_check(int app_id)
339
{
340
    map<int, int>::iterator it = map_check.find(app_id);
341

    
342
    if ( it == map_check.end() )
343
    {
344
        map_check.insert(make_pair(app_id, -1));
345
    }
346
    else
347
    {
348
        it->second = -1;
349
    }
350
}