Statistics
| Branch: | Tag: | Revision:

one / src / scheduler / include / VirtualMachinePoolXML.h @ 1f571426

History | View | Annotate | Download (5.77 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

    
18
#ifndef VM_POOL_XML_H_
19
#define VM_POOL_XML_H_
20

    
21
#include "PoolXML.h"
22
#include "VirtualMachineXML.h"
23

    
24
using namespace std;
25

    
26
class VirtualMachinePoolXML : public PoolXML
27
{
28
public:
29

    
30
    VirtualMachinePoolXML(Client*        client,
31
                          unsigned int   machines_limit,
32
                          bool           _live_resched):
33
        PoolXML(client, machines_limit), live_resched(_live_resched){};
34

    
35
    virtual ~VirtualMachinePoolXML(){};
36

    
37
    /**
38
     * Retrieves the pending and rescheduling VMs
39
     *
40
     * @return   0 on success
41
     *          -1 on error
42
     *          -2 if no VMs need to be scheduled
43
     */
44
    int set_up();
45

    
46
    /**
47
     *  Gets an object from the pool
48
     *   @param oid the object unique identifier
49
     *
50
     *   @return a pointer to the object, 0 in case of failure
51
     */
52
    VirtualMachineXML * get(int oid) const
53
    {
54
        return static_cast<VirtualMachineXML *>(PoolXML::get(oid));
55
    };
56

    
57
    /**
58
     *  Dispatch a VM to the given host
59
     *    @param vid the VM id
60
     *    @param hid the id of the target host
61
     *    @param resched the machine is going to be rescheduled
62
     */
63
    int dispatch(int vid, int hid, int dsid, bool resched) const;
64

    
65
    /**
66
     *  Update the VM template
67
     *    @param vid the VM id
68
     *    @param st the template string
69
     *
70
     *    @return 0 on success, -1 otherwise
71
     */
72
    int update(int vid, const string &st) const;
73

    
74
    /**
75
     *  Update the VM template
76
     *      @param the VM
77
     *
78
     *      @return 0 on success, -1 otherwise
79
     */
80
    int update(VirtualMachineXML * vm) const
81
    {
82
        string xml;
83

    
84
        return update(vm->get_oid(), vm->get_template(xml));
85
    };
86

    
87
    /**
88
     *  Returns a vector of matched hosts
89
     */
90
    const vector<Resource *> get_vm_resources()
91
    {
92
        return vm_resources.get_resources();
93
    }
94

    
95
    /**
96
     *  Sort the VMs in the pool
97
     */
98
    void sort_vm_resources()
99
    {
100
        vm_resources.sort_resources();
101
    }
102

    
103
protected:
104

    
105
    int get_suitable_nodes(vector<xmlNodePtr>& content)
106
    {
107
        // Pending or ((running or unknown) and resched))
108
        return get_nodes("/VM_POOL/VM[STATE=1 or "
109
            "((LCM_STATE=3 or LCM_STATE=16) and RESCHED=1)]", content);
110
    }
111

    
112
    virtual void add_object(xmlNodePtr node);
113

    
114
    virtual int load_info(xmlrpc_c::value &result);
115

    
116
    /**
117
     * Do live migrations to resched VMs
118
     */
119
    bool live_resched;
120

    
121
private:
122
    /**
123
     *  Stores the list of vms, and it associated user prioty vm_resources.
124
     */
125
    ResourceMatch vm_resources;
126
};
127

    
128
/* -------------------------------------------------------------------------- */
129
/* -------------------------------------------------------------------------- */
130

    
131
class VirtualMachineActionsPoolXML : public VirtualMachinePoolXML
132
{
133
public:
134

    
135
    VirtualMachineActionsPoolXML(Client*       client,
136
                                 unsigned int  machines_limit):
137
        VirtualMachinePoolXML(client, machines_limit, false){};
138

    
139
    virtual ~VirtualMachineActionsPoolXML(){};
140

    
141
    /**
142
     * Retrieves the VMs with pending actions
143
     *
144
     * @return   0 on success
145
     *          -1 on error
146
     *          -2 if no VMs with pending actions
147
     */
148
    int set_up();
149

    
150
    /**
151
     * Calls one.vm.action
152
     *
153
     * @param vid The VM id
154
     * @param action Action argument (terminate, hold, release...)
155
     * @param error_msg Error reason, if any
156
     *
157
     * @return 0 on success, -1 otherwise
158
     */
159
    int action(int vid, const string &action, string &error_msg) const;
160

    
161
protected:
162

    
163
    int get_suitable_nodes(vector<xmlNodePtr>& content)
164
    {
165
        ostringstream oss;
166

    
167
        oss << "/VM_POOL/VM/USER_TEMPLATE/SCHED_ACTION[TIME < " << time(0)
168
            << " and not(DONE > 0)]/../..";
169

    
170
        return get_nodes(oss.str().c_str(), content);
171
    }
172
};
173

    
174
/* -------------------------------------------------------------------------- */
175
/* -------------------------------------------------------------------------- */
176

    
177
class VirtualMachineRolePoolXML : public VirtualMachinePoolXML
178
{
179
public:
180

    
181
    VirtualMachineRolePoolXML(Client * client, unsigned int machines_limit):
182
        VirtualMachinePoolXML(client, machines_limit, false){};
183

    
184
    virtual ~VirtualMachineRolePoolXML(){};
185

    
186
    /**
187
     * Retrieves the VMs part of a role
188
     *
189
     * @return   0 on success
190
     *          -1 on error
191
     *          -2 if no VMs in a role
192
     */
193
    int set_up();
194

    
195
protected:
196

    
197
    int get_suitable_nodes(vector<xmlNodePtr>& content)
198
    {
199
        ostringstream oss;
200

    
201
        oss << "/VM_POOL/VM[TEMPLATE/VMGROUP/ROLE]";
202

    
203
        return get_nodes(oss.str().c_str(), content);
204
    }
205
};
206
#endif /* VM_POOL_XML_H_ */