Statistics
| Branch: | Tag: | Revision:

one / src / vm / VirtualMachineHook.cc @ c9d0df87

History | View | Annotate | Download (5.88 KB)

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

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

    
24
static void parse_vm_arguments(VirtualMachine *vm, string& parsed)
25
{
26
    size_t  found;
27

    
28
    found = parsed.find("$VMID");
29

    
30
    if ( found !=string::npos )
31
    {
32
        ostringstream oss;
33
        oss << vm->get_oid();
34

    
35
        parsed.replace(found,5,oss.str());
36
    }
37

    
38
    found = parsed.find("$TEMPLATE");
39

    
40
    if ( found != string::npos )
41
    {
42
        string templ;
43
        parsed.replace(found,9,vm->to_xml64(templ));
44
    }
45
}
46

    
47
// -----------------------------------------------------------------------------
48
// -----------------------------------------------------------------------------
49

    
50
void VirtualMachineAllocateHook::do_hook(void *arg)
51
{
52
    VirtualMachine * vm;
53
    string           parsed_args = args;
54

    
55
    vm = static_cast<VirtualMachine *>(arg);
56

    
57
    if ( vm == 0 )
58
    {
59
        return;
60
    }
61

    
62
    parse_vm_arguments(vm, parsed_args);
63

    
64
    Nebula& ne                    = Nebula::instance();
65
    HookManager * hm              = ne.get_hm();
66
    const HookManagerDriver * hmd = hm->get();
67

    
68
    if ( hmd != 0 )
69
    {
70
        hmd->execute(vm->get_oid(),name,cmd,parsed_args);
71
    }
72
}
73

    
74
// -----------------------------------------------------------------------------
75
// -----------------------------------------------------------------------------
76

    
77
map<int,VirtualMachineStateMapHook::VmStates>
78
                                     VirtualMachineStateMapHook::vm_states;
79

    
80
// -----------------------------------------------------------------------------
81

    
82
int VirtualMachineStateMapHook::get_state(int id,
83
        VirtualMachine::LcmState &lcm_state,
84
        VirtualMachine::VmState  &vm_state)
85
{
86
    map<int,VmStates>::iterator it;
87

    
88
    it = vm_states.find(id);
89

    
90
    if ( it == vm_states.end() )
91
    {
92
        return -1;
93
    }
94

    
95
    lcm_state = it->second.lcm;
96
    vm_state  = it->second.vm;
97

    
98
    return 0;
99
}
100

    
101
// -----------------------------------------------------------------------------
102
// -----------------------------------------------------------------------------
103

    
104
void VirtualMachineStateMapHook::update_state (int id,
105
        VirtualMachine::LcmState lcm_state,
106
        VirtualMachine::VmState  vm_state)
107
{
108
    map<int,VmStates>::iterator it;
109

    
110
    it = vm_states.find(id);
111

    
112
    if ( it == vm_states.end() )
113
    {
114
        VmStates states(lcm_state, vm_state);
115

    
116
        vm_states.insert(make_pair(id,states));
117
    }
118
    else
119
    {
120
        if ( vm_state == VirtualMachine::DONE )
121
        {
122
            vm_states.erase(it);
123
        }
124
        else
125
        {
126
            it->second.lcm = lcm_state;
127
            it->second.vm  = vm_state;
128
        }
129
    }
130
}
131

    
132
// -----------------------------------------------------------------------------
133
// -----------------------------------------------------------------------------
134

    
135
void VirtualMachineStateHook::do_hook(void *arg)
136
{
137

    
138
    VirtualMachine * vm;
139
    int              rc;
140

    
141
    VirtualMachine::LcmState prev_lcm, cur_lcm;
142
    VirtualMachine::VmState  prev_vm, cur_vm;
143

    
144
    vm = static_cast<VirtualMachine *>(arg);
145

    
146
    if ( vm == 0 )
147
    {
148
        return;
149
    }
150

    
151
    rc = get_state(vm->get_oid(), prev_lcm, prev_vm);
152

    
153
    if ( rc != 0 )
154
    {
155
        return;
156
    }
157

    
158
    cur_lcm = vm->get_lcm_state();
159
    cur_vm  = vm->get_state();
160

    
161
    if ( prev_lcm == cur_lcm && prev_vm == cur_vm ) //Still in the same state
162
    {
163
        return;
164
    }
165

    
166
    if ( cur_lcm == lcm && cur_vm == this->vm )
167
    {
168
        string  parsed_args = args;
169

    
170
        parse_vm_arguments(vm,parsed_args);
171

    
172
        Nebula& ne        = Nebula::instance();
173
        HookManager * hm  = ne.get_hm();
174

    
175
        const HookManagerDriver * hmd = hm->get();
176

    
177
        if ( hmd != 0 )
178
        {
179
            if ( ! remote )
180
            {
181
                hmd->execute(vm->get_oid(),name,cmd,parsed_args);
182
            }
183
            else if ( vm->hasHistory() )
184
            {
185
                hmd->execute(vm->get_oid(),
186
                     name,
187
                     vm->get_hostname(),
188
                     cmd,
189
                     parsed_args);
190
            }
191
        }
192
    }
193
}
194

    
195
// -----------------------------------------------------------------------------
196
// -----------------------------------------------------------------------------
197

    
198
void VirtualMachineUpdateStateHook::do_hook(void *arg)
199
{
200
    VirtualMachine * vm = static_cast<VirtualMachine *>(arg);
201

    
202
    if ( vm == 0 )
203
    {
204
        return;
205
    }
206

    
207
    update_state(vm->get_oid(), vm->get_lcm_state(), vm->get_state());
208
}
209

    
210
// -----------------------------------------------------------------------------
211
// -----------------------------------------------------------------------------
212