Statistics
| Branch: | Tag: | Revision:

one / src / host / HostHook.cc @ c9d0df87

History | View | Annotate | Download (5.76 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 "HostHook.h"
18
#include "Host.h"
19
#include "Nebula.h"
20

    
21
// -----------------------------------------------------------------------------
22
// -----------------------------------------------------------------------------
23
static void parse_host_arguments(Host *host, string& parsed)
24
{
25
    size_t  found;
26

    
27
    found = parsed.find("$HID");
28

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

    
34
        parsed.replace(found,4,oss.str());
35
    }
36

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

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

    
48
void HostAllocateHook::do_hook(void *arg)
49
{
50
    Host *  host;
51

    
52
    string  parsed_args = args;
53

    
54
    host = static_cast<Host *>(arg);
55

    
56
    if ( host == 0 )
57
    {
58
        return;
59
    }
60
    
61
    parse_host_arguments(host,parsed_args);
62

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

    
67
    if ( hmd != 0 )
68
    {
69
        if ( remote == true )
70
        {
71
            hmd->execute(host->get_oid(),
72
                         name,
73
                         host->get_name(),
74
                         cmd,
75
                         parsed_args);
76
        }
77
        else
78
        {
79
            hmd->execute(host->get_oid(),name,cmd,parsed_args);
80
        }
81
    }
82
}
83

    
84
// -----------------------------------------------------------------------------
85
// -----------------------------------------------------------------------------
86

    
87
map<int,Host::HostState> HostStateMapHook::host_states;
88

    
89
// -----------------------------------------------------------------------------
90

    
91
int HostStateMapHook::get_state(int id, Host::HostState &state)
92
{
93
    map<int,Host::HostState>::iterator it;
94

    
95
    it = host_states.find(id);
96

    
97
    if ( it == host_states.end() )
98
    {
99
        return -1;
100
    }
101

    
102
    state = it->second;
103

    
104
    return 0;
105
}
106

    
107
// -----------------------------------------------------------------------------
108
// -----------------------------------------------------------------------------
109

    
110
void HostStateMapHook::update_state (int id, Host::HostState state)
111
{
112
    map<int,Host::HostState>::iterator it;
113

    
114
    it = host_states.find(id);
115

    
116
    if ( it == host_states.end() )
117
    {
118
        host_states.insert(make_pair(id,state));
119
    }
120
    else
121
    {
122
        it->second = state;
123
    }
124
}
125

    
126
// -----------------------------------------------------------------------------
127

    
128
void HostStateMapHook::remove_host (int id)
129
{
130
    map<int,Host::HostState>::iterator it;
131

    
132
    it = host_states.find(id);
133

    
134
    if ( it != host_states.end() )
135
    {
136
        host_states.erase(it);
137
    }
138
}
139

    
140
// -----------------------------------------------------------------------------
141
// -----------------------------------------------------------------------------
142

    
143
void HostStateHook::do_hook(void *arg)
144
{
145
    Host * host;
146
    int    rc;
147

    
148
    Host::HostState prev_state, cur_state;
149

    
150
    host = static_cast<Host *>(arg);
151

    
152
    if ( host == 0 )
153
    {
154
        return;
155
    }
156

    
157
    rc = get_state(host->get_oid(), prev_state);
158

    
159
    if ( rc != 0 )
160
    {
161
        return;
162
    }
163

    
164
    cur_state = host->get_state();
165

    
166
    if ( prev_state == cur_state ) //Still in the same state
167
    {
168
        return;
169
    }
170

    
171
    if ( cur_state == this->state )
172
    {
173
        string  parsed_args = args;
174

    
175
        parse_host_arguments(host,parsed_args);
176

    
177
        Nebula& ne        = Nebula::instance();
178
        HookManager * hm  = ne.get_hm();
179

    
180
        const HookManagerDriver * hmd = hm->get();
181

    
182
        if ( hmd != 0 )
183
        {
184
            if ( remote == true)
185
            {
186
                hmd->execute(host->get_oid(),
187
                             name,
188
                             host->get_name(),
189
                             cmd,
190
                             parsed_args);
191
            }
192
            else
193
            {
194
                hmd->execute(host->get_oid(),name,cmd,parsed_args);
195
            }
196
        }
197
    }
198
}
199

    
200
// -----------------------------------------------------------------------------
201
// -----------------------------------------------------------------------------
202

    
203
void HostUpdateStateHook::do_hook(void *arg)
204
{
205
    Host * host = static_cast<Host *>(arg);
206

    
207
    if ( host == 0 )
208
    {
209
        return;
210
    }
211

    
212
    update_state(host->get_oid(), host->get_state());
213
}
214

    
215
// -----------------------------------------------------------------------------
216
// -----------------------------------------------------------------------------