Statistics
| Branch: | Tag: | Revision:

one / src / vm_template / VMTemplate.cc @ d0973401

History | View | Annotate | Download (8.24 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 "VMTemplate.h"
18

    
19
#define TO_UPPER(S) transform(S.begin(),S.end(),S.begin(),(int(*)(int))toupper)
20

    
21
/* ************************************************************************ */
22
/* VMTemplate :: Constructor/Destructor                                     */
23
/* ************************************************************************ */
24

    
25
VMTemplate::VMTemplate(int id,
26
                       int _uid,
27
                       int _gid,
28
                       const string& _uname,
29
                       const string& _gname,
30
                       int umask,
31
                       VirtualMachineTemplate * _template_contents):
32
        PoolObjectSQL(id,TEMPLATE,"",_uid,_gid,_uname,_gname,table),
33
        regtime(time(0))
34
{
35
    if (_template_contents != 0)
36
    {
37
        obj_template = _template_contents;
38
    }
39
    else
40
    {
41
        obj_template = new VirtualMachineTemplate;
42
    }
43

    
44
    set_umask(umask);
45
}
46

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

    
50
VMTemplate::~VMTemplate()
51
{
52
    delete obj_template;
53
}
54

    
55
/* ************************************************************************ */
56
/* VMTemplate :: Database Access Functions                                  */
57
/* ************************************************************************ */
58

    
59
const char * VMTemplate::table = "template_pool";
60

    
61
const char * VMTemplate::db_names =
62
        "oid, name, body, uid, gid, owner_u, group_u, other_u";
63

    
64
const char * VMTemplate::db_bootstrap =
65
    "CREATE TABLE IF NOT EXISTS template_pool (oid INTEGER PRIMARY KEY, "
66
    "name VARCHAR(128), body MEDIUMTEXT, uid INTEGER, gid INTEGER, "
67
    "owner_u INTEGER, group_u INTEGER, other_u INTEGER)";
68

    
69
/* ------------------------------------------------------------------------ */
70
/* ------------------------------------------------------------------------ */
71

    
72
int VMTemplate::insert(SqlDB *db, string& error_str)
73
{
74
    // ---------------------------------------------------------------------
75
    // Check default attributes
76
    // ---------------------------------------------------------------------
77

    
78
    erase_template_attribute("NAME", name);
79

    
80
    // ------------------------------------------------------------------------
81
    // Insert the Template
82
    // ------------------------------------------------------------------------
83

    
84
    return insert_replace(db, false, error_str);
85
}
86

    
87
/* ------------------------------------------------------------------------ */
88
/* ------------------------------------------------------------------------ */
89

    
90
int VMTemplate::insert_replace(SqlDB *db, bool replace, string& error_str)
91
{
92
    ostringstream   oss;
93

    
94
    int    rc;
95
    string xml_body;
96

    
97
    char * sql_name;
98
    char * sql_xml;
99

    
100
   // Update the Object
101

    
102
    sql_name = db->escape_str(name.c_str());
103

    
104
    if ( sql_name == 0 )
105
    {
106
        goto error_name;
107
    }
108

    
109
    sql_xml = db->escape_str(to_xml(xml_body).c_str());
110

    
111
    if ( sql_xml == 0 )
112
    {
113
        goto error_body;
114
    }
115

    
116
    if ( validate_xml(sql_xml) != 0 )
117
    {
118
        goto error_xml;
119
    }
120

    
121
    if(replace)
122
    {
123
        oss << "REPLACE";
124
    }
125
    else
126
    {
127
        oss << "INSERT";
128
    }
129

    
130
    // Construct the SQL statement to Insert or Replace
131

    
132
    oss <<" INTO " << table <<" ("<< db_names <<") VALUES ("
133
        <<            oid        << ","
134
        << "'"     << sql_name   << "',"
135
        << "'"     << sql_xml    << "',"
136
        <<            uid        << ","
137
        <<            gid        << ","
138
        <<            owner_u    << ","
139
        <<            group_u    << ","
140
        <<            other_u    << ")";
141

    
142
    rc = db->exec_wr(oss);
143

    
144
    db->free_str(sql_name);
145
    db->free_str(sql_xml);
146

    
147
    return rc;
148

    
149
error_xml:
150
    db->free_str(sql_name);
151
    db->free_str(sql_xml);
152

    
153
    error_str = "Error transforming the Template to XML.";
154

    
155
    goto error_common;
156

    
157
error_body:
158
    db->free_str(sql_name);
159
    goto error_generic;
160

    
161
error_name:
162
    goto error_generic;
163

    
164
error_generic:
165
    error_str = "Error inserting Template in DB.";
166
error_common:
167
    return -1;
168
}
169

    
170
/* ------------------------------------------------------------------------ */
171
/* ------------------------------------------------------------------------ */
172

    
173
int VMTemplate::post_update_template(string& error)
174
{
175
    if ( !parse_sched_action() )
176
    {
177
       error = "Error parsing sched_action, malformed.";
178
       return -1;
179
    }
180
    return 0;
181
}
182

    
183
/* ************************************************************************ */
184
/* VMTemplate :: Misc                                                       */
185
/* ************************************************************************ */
186

    
187
string& VMTemplate::to_xml(string& xml) const
188
{
189
    return to_xml(xml, obj_template);
190
}
191

    
192
/* ------------------------------------------------------------------------ */
193
/* ------------------------------------------------------------------------ */
194

    
195
string& VMTemplate::to_xml(string& xml, const Template* tmpl) const
196
{
197
    ostringstream   oss;
198
    string          template_xml;
199
    string          perm_str;
200

    
201
    oss << "<VMTEMPLATE>"
202
            << "<ID>"       << oid        << "</ID>"
203
            << "<UID>"      << uid        << "</UID>"
204
            << "<GID>"      << gid        << "</GID>"
205
            << "<UNAME>"    << uname      << "</UNAME>"
206
            << "<GNAME>"    << gname      << "</GNAME>"
207
            << "<NAME>"     << name       << "</NAME>"
208
            << perms_to_xml(perm_str)
209
            << "<REGTIME>"  << regtime    << "</REGTIME>"
210
            << tmpl->to_xml(template_xml)
211
        << "</VMTEMPLATE>";
212

    
213
    xml = oss.str();
214

    
215
    return xml;
216
}
217

    
218
/* ------------------------------------------------------------------------ */
219
/* ------------------------------------------------------------------------ */
220

    
221
int VMTemplate::from_xml(const string& xml)
222
{
223
    vector<xmlNodePtr> content;
224
    int rc = 0;
225

    
226
    // Initialize the internal XML object
227
    update_from_str(xml);
228

    
229
    // Get class base attributes
230
    rc += xpath(oid,        "/VMTEMPLATE/ID",      -1);
231
    rc += xpath(uid,        "/VMTEMPLATE/UID",     -1);
232
    rc += xpath(gid,        "/VMTEMPLATE/GID",     -1);
233
    rc += xpath(uname,      "/VMTEMPLATE/UNAME",   "not_found");
234
    rc += xpath(gname,      "/VMTEMPLATE/GNAME",   "not_found");
235
    rc += xpath(name,       "/VMTEMPLATE/NAME",    "not_found");
236
    rc += xpath<time_t>(regtime, "/VMTEMPLATE/REGTIME", 0);
237

    
238
    // Permissions
239
    rc += perms_from_xml();
240

    
241
    // Get associated classes
242
    ObjectXML::get_nodes("/VMTEMPLATE/TEMPLATE", content);
243

    
244
    if (content.empty())
245
    {
246
        return -1;
247
    }
248

    
249
    // Template contents
250
    rc += obj_template->from_xml_node(content[0]);
251

    
252
    ObjectXML::free_nodes(content);
253

    
254
    if (rc != 0)
255
    {
256
        return -1;
257
    }
258

    
259
    return 0;
260
}
261

    
262
/* ------------------------------------------------------------------------ */
263
/* ------------------------------------------------------------------------ */
264

    
265
bool VMTemplate::is_vrouter()
266
{
267
    bool vr;
268

    
269
    get_template_attribute("VROUTER", vr);
270

    
271
    return vr;
272
}
273

    
274
/* ------------------------------------------------------------------------ */
275
/* ------------------------------------------------------------------------ */