Statistics
| Branch: | Tag: | Revision:

one / src / scheduler / src / pool / VirtualMachinePoolXML.cc @ 1f571426

History | View | Annotate | Download (9.68 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 "VirtualMachinePoolXML.h"
18
#include <stdexcept>
19
#include <iomanip>
20

    
21
int VirtualMachinePoolXML::set_up()
22
{
23
    ostringstream   oss;
24

    
25
    int rc = PoolXML::set_up();
26

    
27
    if ( rc == 0 )
28
    {
29
        map<int, ObjectXML*>::iterator it;
30

    
31
        if (objects.empty())
32
        {
33
            return -2;
34
        }
35

    
36
        vm_resources.clear();
37

    
38
        for ( it = objects.begin(); it != objects.end(); ++it )
39
        {
40
            vm_resources.add_resource(it->first);
41
        } 
42

    
43
        if (NebulaLog::log_level() >= Log::DDDEBUG)
44
        {
45

    
46
            oss << "Pending/rescheduling VM and capacity requirements:" << endl;
47

    
48
            oss << right << setw(8)  << "ACTION"    << " "
49
                << right << setw(8)  << "VM"        << " "
50
                << right << setw(4)  << "CPU"       << " "
51
                << right << setw(11) << "Memory"    << " "
52
                << right << setw(3)  << "PCI"       << " "
53
                << right << setw(11) << "System DS" << " "
54
                << " Image DS" << endl
55
                << setw(60) << setfill('-') << "-" << setfill(' ') << endl;
56

    
57
            for (it = objects.begin() ; it != objects.end() ; ++it)
58
            {
59
                int cpu, mem;
60
                long long disk;
61
                vector<VectorAttribute *> pci;
62

    
63
                string action = "DEPLOY";
64

    
65
                VirtualMachineXML * vm;
66

    
67
                vm = static_cast<VirtualMachineXML *>(it->second);
68

    
69
                vm->get_requirements(cpu, mem, disk, pci);
70

    
71
                if (vm->is_resched())
72
                {
73
                    action = "RESCHED";
74
                }
75
                else if (vm->is_resume())
76
                {
77
                    action = "RESUME";
78
                }
79

    
80
                oss << right << setw(8)  << action      << " "
81
                    << right << setw(8)  << it->first   << " "
82
                    << right << setw(4)  << cpu         << " "
83
                    << right << setw(11) << mem         << " "
84
                    << right << setw(3)  << pci.size()  << " "
85
                    << right << setw(11) << disk        << " ";
86

    
87
                map<int,long long> ds_usage = vm->get_storage_usage();
88
                map<int,long long>::const_iterator ds_it;
89

    
90
                for ( ds_it = ds_usage.begin(); ds_it != ds_usage.end(); ds_it++)
91
                {
92
                    oss << " DS " << ds_it->first << ": " << ds_it->second <<" ";
93
                }
94

    
95
                oss << endl;
96
            }
97
        }
98
        else
99
        {
100
            oss << "Found " << objects.size() << " pending/rescheduling VMs.";
101
        }
102

    
103
        NebulaLog::log("VM",Log::DEBUG,oss);
104
    }
105

    
106
    return rc;
107
}
108

    
109
/* -------------------------------------------------------------------------- */
110
/* -------------------------------------------------------------------------- */
111

    
112
void VirtualMachinePoolXML::add_object(xmlNodePtr node)
113
{
114
    if ( node == 0 || node->children == 0 || node->children->next==0 )
115
    {
116
        NebulaLog::log("VM",Log::ERROR,
117
                       "XML Node does not represent a valid Virtual Machine");
118
        return;
119
    }
120

    
121
    VirtualMachineXML* vm = new VirtualMachineXML(node);
122

    
123
    objects.insert(pair<int,ObjectXML*>(vm->get_oid(),vm));
124
}
125

    
126
/* -------------------------------------------------------------------------- */
127
/* -------------------------------------------------------------------------- */
128

    
129
int VirtualMachinePoolXML::load_info(xmlrpc_c::value &result)
130
{
131
    try
132
    {
133
        client->call("one.vmpool.info", "iiii", &result, -2, -1, -1, -1);
134

    
135
        return 0;
136
    }
137
    catch (exception const& e)
138
    {
139
        ostringstream   oss;
140
        oss << "Exception raised: " << e.what();
141

    
142
        NebulaLog::log("VM", Log::ERROR, oss);
143

    
144
        return -1;
145
    }
146
}
147

    
148
/* -------------------------------------------------------------------------- */
149
/* -------------------------------------------------------------------------- */
150

    
151
int VirtualMachinePoolXML::dispatch(int vid, int hid, int dsid, bool resched) const
152
{
153
    xmlrpc_c::value deploy_result;
154

    
155
    VirtualMachineXML* vm = get(vid);
156

    
157
    if (vm != 0 && vm->clear_log())
158
    {
159
        update(vm);
160
    }
161

    
162
    try
163
    {
164
        if (resched == true)
165
        {
166
            client->call("one.vm.migrate",// methodName
167
                         "iibb",          // arguments format
168
                         &deploy_result,  // resultP
169
                         vid,             // argument 1 (VM)
170
                         hid,             // argument 2 (HOST)
171
                         live_resched,    // argument 3 (LIVE)
172
                         false);          // argument 4 (ENFORCE)
173
        }
174
        else
175
        {
176
            client->call("one.vm.deploy", // methodName
177
                         "iibi",          // arguments format
178
                         &deploy_result,  // resultP
179
                         vid,             // argument 1 (VM)
180
                         hid,             // argument 2 (HOST)
181
                         false,           // argument 3 (ENFORCE)
182
                         dsid);           // argument 5 (SYSTEM SD)
183
        }
184
    }
185
    catch (exception const& e)
186
    {
187
        ostringstream   oss;
188

    
189
        oss << "Exception raised: " << e.what() << '\n';
190

    
191
        NebulaLog::log("VM",Log::ERROR,oss);
192

    
193
        return -1;
194
    }
195

    
196
    vector<xmlrpc_c::value> values =
197
                    xmlrpc_c::value_array(deploy_result).vectorValueValue();
198

    
199
    bool success = xmlrpc_c::value_boolean(values[0]);
200

    
201
    if ( !success )
202
    {
203
        ostringstream oss;
204
        string message = xmlrpc_c::value_string(values[1]);
205

    
206
        oss << "Error deploying virtual machine " << vid
207
            << " to HID: " << hid << ". Reason: " << message;
208

    
209
        NebulaLog::log("VM",Log::ERROR,oss);
210

    
211
        return -1;
212
    }
213

    
214
    return 0;
215
}
216

    
217
/* -------------------------------------------------------------------------- */
218
/* -------------------------------------------------------------------------- */
219

    
220
int VirtualMachinePoolXML::update(int vid, const string &st) const
221
{
222
    xmlrpc_c::value result;
223
    bool            success;
224

    
225
    try
226
    {
227
        client->call("one.vm.update", "is", &result, vid, st.c_str());
228
    }
229
    catch (exception const& e)
230
    {
231
        return -1;
232
    }
233

    
234
    vector<xmlrpc_c::value> values =
235
            xmlrpc_c::value_array(result).vectorValueValue();
236

    
237
    success = xmlrpc_c::value_boolean(values[0]);
238

    
239
    if (!success)
240
    {
241
        return -1;
242
    }
243

    
244
    return 0;
245
}
246

    
247
/* -------------------------------------------------------------------------- */
248
/* -------------------------------------------------------------------------- */
249

    
250
int VirtualMachineActionsPoolXML::set_up()
251
{
252
    ostringstream   oss;
253
    int             rc;
254

    
255
    rc = PoolXML::set_up();
256

    
257
    if ( rc == 0 )
258
    {
259
        if (objects.empty())
260
        {
261
            return -2;
262
        }
263

    
264
        oss.str("");
265
        oss << "VMs with scheduled actions:" << endl;
266

    
267
        map<int,ObjectXML*>::iterator it;
268

    
269
        for (it=objects.begin();it!=objects.end();it++)
270
        {
271
            oss << " " << it->first;
272
        }
273

    
274
        NebulaLog::log("VM",Log::DEBUG,oss);
275
    }
276

    
277
    return rc;
278
}
279

    
280
/* -------------------------------------------------------------------------- */
281
/* -------------------------------------------------------------------------- */
282

    
283
int VirtualMachineActionsPoolXML::action(
284
        int             vid,
285
        const string&   action,
286
        string&         error_msg) const
287
{
288
    xmlrpc_c::value result;
289
    bool            success;
290

    
291
    try
292
    {
293
        if (action == "snapshot-create")
294
        {
295
            client->call("one.vm.snapshotcreate", "is", &result, vid, "");
296
        }
297
        else
298
        {
299
            client->call("one.vm.action", "si", &result, action.c_str(), vid);
300
        }
301
    }
302
    catch (exception const& e)
303
    {
304
        return -1;
305
    }
306

    
307
    vector<xmlrpc_c::value> values =
308
            xmlrpc_c::value_array(result).vectorValueValue();
309

    
310
    success = xmlrpc_c::value_boolean(values[0]);
311

    
312
    if (!success)
313
    {
314
        error_msg = xmlrpc_c::value_string(  values[1] );
315

    
316
        return -1;
317
    }
318

    
319
    return 0;
320
}
321

    
322
/* -------------------------------------------------------------------------- */
323
/* -------------------------------------------------------------------------- */
324

    
325
int VirtualMachineRolePoolXML::set_up()
326
{
327
    int rc = PoolXML::set_up();
328

    
329
    if ( rc == 0 )
330
    {
331
        ostringstream oss;
332

    
333
        oss << "VMs in VMGroups:" << endl;
334

    
335
        map<int,ObjectXML*>::iterator it;
336

    
337
        for (it=objects.begin();it!=objects.end();it++)
338
        {
339
            oss << " " << it->first;
340
        }
341

    
342
        NebulaLog::log("VM", Log::DEBUG, oss);
343
    }
344

    
345
    return rc;
346
}
347