1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
6 * Alvaro Lopez Ortega <alvaro@alobbs.com>
7 * Stefan de Konink <stefan@konink.de>
9 * Copyright (C) 2001-2008 Alvaro Lopez Ortega
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 // #define LINUX_CMDLINE "root=/dev/xvda ro ip=%s:1.2.3.4:%s:%s::eth0:off"
28 #define VIRT_INTERFACE_XML \
29 " <interface type='bridge'>" \
30 " <target dev='vif1.0' />" \
31 " <source bridge='xenbr0'/>" \
32 " <mac address='%s'/>" \
33 " <ip address='%s' />" \
34 " <script path='vif-bridge'/>" \
37 //" <target dev='vif1.0'/>" \
39 #define VIRT_DISK_XML \
40 " <disk type='file' device='disk'>" \
41 " <driver name='tap' type='aio' />" \
42 " <source file='%s'/>" \
43 " <target dev='%s' bus='xen'/>" \
48 #define VIRT_DOMAIN_XML \
49 "<domain type='xen'>" \
50 " <name>%s_%s</name>" \
52 " <type>linux</type>" \
53 " <kernel>/usr/lib/xen/boot/linux-2.6.20-xen-r6</kernel>" \
54 " <cmdline> root=/dev/xvda ro</cmdline>" \
56 " <memory>%d</memory>" \
58 " <on_poweroff>destroy</on_poweroff>" \
59 " <on_reboot>restart</on_reboot>" \
60 " <on_crash>destroy</on_crash>" \
66 #define VIRT_STORAGE_XML \
67 "<volume type='%s'>" \
68 " <name>%s_%s</name>" \
69 " <allocation>%lu</allocation>" \
70 " <capacity unit='%s'>%lu</capacity>" \
72 " <path>%s_%s</path>" \
74 " <owner>0744</owner>" \
75 " <group>0744</group>" \
76 " <mode>0744</mode>" \
77 " <label>%s_%s</label>" \
83 #include "handler_virt.h"
84 #include "handler_avahi.h"
85 #include <cherokee/cherokee.h>
87 /* Plug-in initialization
89 * In this function you can use any of these:
90 * http_delete | http_get | http_post | http_put
92 * For a full list: cherokee_http_method_t
94 * It is what your handler to be implements.
97 PLUGIN_INFO_HANDLER_EASIEST_INIT (virt
, http_get
| http_post
);
100 /* Methods implementation
103 props_free (cherokee_handler_virt_props_t
*props
)
106 cherokee_buffer_mrproper(&props
->xsl
);
107 cherokee_buffer_mrproper(&props
->virt
);
108 return cherokee_module_props_free_base (MODULE_PROPS(props
));
113 cherokee_handler_virt_configure (cherokee_config_node_t
*conf
, cherokee_server_t
*srv
, cherokee_module_props_t
**_props
)
116 cherokee_handler_virt_props_t
*props
;
118 /* Instance a new property object
121 if (*_props
== NULL
) {
122 CHEROKEE_NEW_STRUCT (n
, handler_virt_props
);
124 cherokee_handler_avahi_props_init_base (PROP_AVAHI(n
), MODULE_PROPS_FREE(props_free
));
126 /* Look at handler_virt.h
127 * This is an virt of configuration.
129 n
->authenticate
= true; /* by default we are secure! */
130 n
->read_only
= true; /* by default we are secure! */
131 cherokee_buffer_init (&n
->xsl
); /* we support a custom header */
132 cherokee_buffer_init (&n
->virt
); /* your first xenserver */
134 *_props
= MODULE_PROPS(n
);
137 props
= PROP_VIRT(*_props
);
139 cherokee_config_node_foreach (i
, conf
) {
140 cherokee_config_node_t
*subconf
= CONFIG_NODE(i
);
142 if (equal_buf_str (&subconf
->key
, "authenticate")) {
143 props
->authenticate
= atoi(subconf
->val
.buf
);
145 else if (equal_buf_str (&subconf
->key
, "read_only")) {
146 props
->read_only
= atoi(subconf
->val
.buf
);
148 else if (equal_buf_str (&subconf
->key
, "xsl")) {
149 cherokee_buffer_add_buffer (&props
->xsl
, &subconf
->val
);
151 else if (equal_buf_str (&subconf
->key
, "virt")) {
152 cherokee_buffer_add_buffer (&props
->virt
, &subconf
->val
);
160 return cherokee_handler_avahi_configure (conf
, srv
, _props
);
165 cherokee_handler_virt_init (cherokee_handler_virt_t
*hdl
)
171 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
172 cherokee_buffer_init(&hdl
->user
);
173 cherokee_buffer_init(&hdl
->vm
);
175 hdl
->action
= nothing
;
177 cherokee_buffer_add (&conn
->pathinfo
,
178 conn
->request
.buf
+ conn
->web_directory
.len
,
179 conn
->request
.len
- conn
->web_directory
.len
);
181 this = conn
->pathinfo
.buf
+ 1;
182 next
= strchr(this, '/');
185 if ((!next
&& (this && (len
= strlen(this)) == 0)) || (next
&& ((len
= next
- this) == 0)) )
186 hdl
->action
= showall
;
188 cherokee_buffer_add (&hdl
->user
, this, len
);
191 if (HDL_VIRT_PROPS(hdl
)->authenticate
) {
192 if (!conn
->validator
||
194 (!cherokee_buffer_cmp_buf(&conn
->validator
->user
, &hdl
->user
) &&
195 !(isroot
= cherokee_buffer_cmp (&conn
->validator
->user
, "root", 4))))) {
196 hdl
->action
= nothing
; /* just in case */
197 conn
->error_code
= http_unauthorized
;
204 if (hdl
->action
== showall
) {
206 hdl
->action
= nothing
;
207 conn
->error_code
= http_unauthorized
;
210 return virt_build_page(hdl
);
216 hdl
->action
= showuservms
;
217 return virt_build_page(hdl
);
220 next
= strchr(this, '/');
222 if (!next
&& (this && (len
= strlen(this)) == 0) || (next
&& ((len
= next
- this) == 0)) ) {
223 hdl
->action
= showuservms
;
224 return virt_build_page(hdl
);
228 cherokee_buffer_add (&hdl
->vm
, this, len
);
231 hdl
->action
= domainGetXMLDesc
;
232 return virt_build_page(hdl
);
235 next
= strchr(this, '/');
237 if (!next
&& (this && (len
= strlen(this)) == 0) || (next
&& ((len
= next
- this) == 0)) ) {
238 hdl
->action
= domainGetXMLDesc
;
239 return virt_build_page(hdl
);
243 /* TODO: it would be nice to filter read_only methods already on this point */
244 hdl
->action
= not_implemented
;
245 switch (conn
->header
.method
) {
247 if (strncmp(this, "virDomain", 9) == 0) {
248 if (strncmp(this+9, "Get", 3) == 0) {
249 if (strcmp(this+12, "ID") == 0) hdl
->action
= domainGetID
;
250 else if (strcmp(this+12, "Name") == 0) hdl
->action
= domainGetName
;
251 else if (strcmp(this+12, "MaxMemory") == 0) hdl
->action
= domainGetMaxMemory
;
252 else if (strcmp(this+12, "MaxVcpus") == 0) hdl
->action
= domainGetMaxVcpus
;
253 else if (strcmp(this+12, "OSType") == 0) hdl
->action
= domainGetOSType
;
254 else if (strcmp(this+12, "UUID") == 0) hdl
->action
= domainGetUUID
;
255 else if (strcmp(this+12, "UUIDString") == 0) hdl
->action
= domainGetUUIDString
;
256 else if (strcmp(this+12, "XMLDesc") == 0) hdl
->action
= domainGetXMLDesc
;
258 else if (strcmp(this+9, "Create") == 0) hdl
->action
= domainCreate
;
259 else if (strcmp(this+9, "Destroy") == 0) hdl
->action
= domainDestroy
;
260 else if (strcmp(this+9, "Reboot") == 0) hdl
->action
= domainReboot
;
261 else if (strcmp(this+9, "Shutdown") == 0) hdl
->action
= domainShutdown
;
263 else if (strcmp(this+9, "AttachDevice") == 0) hdl
->action
= domainAttachDevice_args
;
265 else if (strcmp(this+9, "DefineXML") == 0) hdl
->action
= domainDefineXML_args
;
266 else if (strcmp(this+9, "Undefine") == 0) hdl
->action
= domainUndefine
;
268 else if (strncmp(this, "virStorage", 10) == 0) {
269 if (strncmp(this+10, "Vol", 3) == 0) {
270 if (strcmp(this+13, "CreateXML") == 0) hdl
->action
= storageVolCreateXML_args
;
271 else if (strcmp(this+13, "Delete") == 0) hdl
->action
= storageVolDelete_args
;
272 else if (strcmp(this+13, "CloneXML") == 0) hdl
->action
= storageVolCloneXML_args
;
273 else if (strcmp(this+13, "GetXMLDesc") == 0) hdl
->action
= storageVolGetXMLDesc
;
280 cherokee_post_get_len (&conn
->post
, &postl
);
282 if (postl
<= 0 || postl
>= (INT_MAX
-1)) {
283 conn
->error_code
= http_bad_request
;
287 if (strncmp(this, "virDomain", 9) == 0) {
288 if (strcmp(this+9, "AttachDevice") == 0) hdl
->action
= domainAttachDevice
;
289 else if (strcmp(this+9, "DetachDevice") == 0) hdl
->action
= domainDetachDevice
;
290 else if (strcmp(this+9, "DefineXML") == 0) hdl
->action
= domainDefineXML
;
292 else if (strncmp(this, "virStorage", 10) == 0) {
293 if (strncmp(this+10, "Vol", 3) == 0) {
294 if (strcmp(this+13, "CreateXML") == 0) hdl
->action
= storageVolCreateXML
;
305 if (hdl
->action
<= 0) {
306 conn
->error_code
= http_bad_request
;
311 return virt_build_page(hdl
);
315 cherokee_handler_virt_free (cherokee_handler_virt_t
*hdl
)
317 cherokee_buffer_mrproper (&hdl
->buffer
);
318 cherokee_buffer_mrproper (&hdl
->user
);
319 cherokee_buffer_mrproper (&hdl
->vm
);
325 cherokee_handler_virt_step (cherokee_handler_virt_t
*hdl
, cherokee_buffer_t
*buffer
)
327 cherokee_buffer_add_buffer (buffer
, &hdl
->buffer
);
328 return ret_eof_have_data
;
332 cherokee_handler_virt_add_headers (cherokee_handler_virt_t
*hdl
, cherokee_buffer_t
*buffer
)
334 cherokee_buffer_add_va (buffer
, "Content-Length: %d"CRLF
, hdl
->buffer
.len
);
336 if (hdl
->action
> xml
)
337 cherokee_buffer_add_str (buffer
, "Content-Type: application/xml"CRLF
);
339 cherokee_buffer_add_str (buffer
, "Content-Type: text/plain"CRLF
);
346 while_func_entries (cherokee_buffer_t *key, void *value, void *param) {
347 virConnectPtr conn = NULL;
348 cherokee_buffer_t uri = CHEROKEE_BUF_INIT;
349 cherokee_buffer_t *buf = (cherokee_buffer_t *)param;
351 cherokee_buffer_add_va (&uri, "xen://%s/", value);
353 if (HDL_VIRT_PROPS(hdl)->read_only == FALSE && !(conn = virConnectOpen (uri.buf))) {
354 if (!(conn = virConnectOpen (uri.buf))) {
359 if (!conn && !(conn = virConnectOpenReadOnly (uri.buf))) {
363 if (!(dom = virDomainLookupByName(conn, (const char *) key->buf))) {
366 char *xml = virDomainGetXMLDesc(dom, 0);
367 cherokee_buffer_add(buf, xml, strlen(xml));
369 virConnectClose(conn);
372 cherokee_buffer_mrproper(&uri);
377 static ret_t
save_xml(cherokee_handler_virt_t
*hdl
, virDomainPtr result
, cherokee_buffer_t
*buf
) {
378 ret_t ret
= ret_error
;
380 cherokee_buffer_t path
= CHEROKEE_BUF_INIT
;
381 cherokee_buffer_add_va (&path
, "/mnt/netapp/users/%s", hdl
->user
.buf
);
383 mkdir(path
.buf
, 0755);
384 cherokee_buffer_add_va (&path
, "/%s", hdl
->vm
.buf
);
386 mkdir(path
.buf
, 0755);
387 cherokee_buffer_add_str (&path
, "/index.xml");
389 if ((fd
= fopen(path
.buf
, "w")) == NULL
) {
392 char *output
= virDomainGetXMLDesc(result
, 0);
393 fwrite(output
, strlen(output
), sizeof(char), fd
);
396 cherokee_buffer_add(buf
, output
, strlen(output
));
400 cherokee_buffer_mrproper(&path
);
406 virt_virt_function(cherokee_handler_virt_t
*hdl
, virDomainPtr dom
, virConnectPtr virConn
) {
407 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
408 cherokee_buffer_t
*buf
= &HDL_AVAHI(hdl
)->buffer
;
410 switch (hdl
->action
) {
412 case domainUndefine
: {
414 if ((result
= virDomainUndefine(dom
)) != 0) {
415 conn
->error_code
= http_internal_error
;
418 cherokee_buffer_add_long10(buf
, result
);
422 case domainAttachDevice_args
: {
426 ret
= cherokee_connection_parse_args (conn
);
427 if (unlikely(ret
< ret_ok
))
430 ret
= cherokee_avl_get_ptr (conn
->arguments
, "type", &temp
);
433 cherokee_buffer_t xml
= CHEROKEE_BUF_INIT
;
435 if (strcmp(temp
, "disk") == 0) {
437 if ((ret
= cherokee_avl_get_ptr (conn
->arguments
, "device", &device
)) == ret_ok
) {
439 if ((ret
= cherokee_avl_get_ptr (conn
->arguments
, "file", &file
)) == ret_ok
) {
440 cherokee_buffer_add_va (&xml
, VIRT_DISK_XML
, file
, device
);
442 void *poolstr
, *volumestr
;
443 if ((ret
= cherokee_avl_get_ptr (conn
->arguments
, "pool", &poolstr
)) == ret_ok
&&
444 (ret
= cherokee_avl_get_ptr (conn
->arguments
, "volume", &volumestr
)) == ret_ok
) {
445 cherokee_buffer_t fullvol
= CHEROKEE_BUF_INIT
;
446 virStoragePoolPtr pool
;
447 virStorageVolPtr volume
;
449 pool
= virStoragePoolLookupByName(virConn
, poolstr
);
451 conn
->error_code
= http_not_found
;
455 cherokee_buffer_add_va (&fullvol
, "%s_%s", hdl
->user
.buf
, volumestr
);
456 volume
= virStorageVolLookupByName(pool
, fullvol
.buf
);
458 cherokee_buffer_mrproper(&fullvol
);
459 virStoragePoolFree(pool
);
461 if (volume
== NULL
) {
462 conn
->error_code
= http_not_found
;
465 file
= virStorageVolGetPath(volume
);
466 cherokee_buffer_add_va (&xml
, VIRT_DISK_XML
, file
, device
);
469 virStorageVolFree(volume
);
472 conn
->error_code
= http_bad_request
;
479 else if (strcmp(temp
, "interface") == 0) {
481 if ((ret
= cherokee_avl_get_ptr (conn
->arguments
, "mac", &mac
)) == ret_ok
&& (ret
= cherokee_avl_get_ptr (conn
->arguments
, "ip", &ip
)) == ret_ok
) {
482 cherokee_buffer_add_va (&xml
, VIRT_INTERFACE_XML
, mac
, ip
);
486 cherokee_buffer_t domu
= CHEROKEE_BUF_INIT
;
487 cherokee_buffer_add_va (&domu
, "%s_%s", hdl
->user
.buf
, hdl
->vm
.buf
);
488 if (getNewMac("dv28", domu
.buf
, &mac
, &ip
) == 0)
489 cherokee_buffer_add_va (&xml
, VIRT_INTERFACE_XML
, mac
, ip
);
490 cherokee_buffer_mrproper(&domu
);
494 if (ret
== ret_ok
&& (result
= virDomainAttachDevice(dom
, (const char *) xml
.buf
)) == 0) {
497 conn
->error_code
= http_internal_error
;
501 cherokee_buffer_add_long10(buf
, result
);
502 cherokee_buffer_mrproper(&xml
);
504 conn
->error_code
= http_bad_request
;
511 case domainAttachDevice
: {
514 cherokee_buffer_t post
= CHEROKEE_BUF_INIT
;
515 cherokee_post_get_len (&conn
->post
, &postl
);
516 cherokee_post_walk_read (&conn
->post
, &post
, (cuint_t
) postl
);
517 if ((result
= virDomainAttachDevice(dom
, (const char *) post
.buf
)) != 0)
518 conn
->error_code
= http_internal_error
;
520 cherokee_buffer_mrproper(&post
);
521 cherokee_buffer_add_long10(buf
, result
);
526 case domainGetXMLDesc
: {
527 char *xml
= virDomainGetXMLDesc(dom
, 0);
528 cherokee_buffer_add(buf
, xml
, strlen(xml
));
534 case domainDetachDevice
: {
537 cherokee_buffer_t post
= CHEROKEE_BUF_INIT
;
538 cherokee_post_get_len (&conn
->post
, &postl
);
539 cherokee_post_walk_read (&conn
->post
, &post
, (cuint_t
) postl
);
540 if ((result
= virDomainDetachDevice(dom
, (const char *) post
.buf
)) != 0) {
541 conn
->error_code
= http_internal_error
;
543 cherokee_buffer_mrproper(&post
);
544 cherokee_buffer_add_long10(buf
, result
);
549 cherokee_buffer_add_ulong10(buf
, virDomainGetID(dom
));
552 case domainGetMaxMemory
: {
553 cherokee_buffer_add_ulong10(buf
, virDomainGetMaxMemory (dom
));
556 case domainGetMaxVcpus
: {
557 cherokee_buffer_add_long10(buf
, virDomainGetMaxVcpus (dom
));
560 case domainGetName
: {
561 const char *name
= virDomainGetName (dom
);
562 cherokee_buffer_add(buf
, name
, strlen(name
));
565 case domainGetUUID
: {
566 unsigned char uuid
[VIR_UUID_BUFLEN
];
567 if (virDomainGetUUID(dom
, uuid
) == 0) {
568 cherokee_buffer_add_str(buf
, uuid
);
570 conn
->error_code
= http_internal_error
;
575 case domainGetUUIDString
: {
576 unsigned char uuid
[VIR_UUID_STRING_BUFLEN
];
577 if (virDomainGetUUIDString(dom
, uuid
) == 0) {
578 cherokee_buffer_add_str(buf
, uuid
);
580 conn
->error_code
= http_internal_error
;
587 cherokee_buffer_add_long10(buf
, virDomainCreate (dom
));
591 case domainDestroy
: {
592 cherokee_buffer_add_long10(buf
, virDomainDestroy (dom
));
597 cherokee_buffer_add_long10(buf
, virDomainReboot (dom
, 0));
601 case domainShutdown
: {
602 cherokee_buffer_add_long10(buf
, virDomainShutdown (dom
));
607 case domainGetOSType
: {
608 char *ostype
= virDomainGetOSType(dom
);
609 cherokee_buffer_add(buf
, ostype
, strlen(ostype
));
618 if (hdl
->action
== domainUndefine
) {
619 /* Remove VM data from filesystem */
620 cherokee_buffer_t path
= CHEROKEE_BUF_INIT
;
621 cherokee_buffer_add_va (&path
, "/mnt/netapp/users/%s/%s/index.xml", hdl
->user
.buf
, hdl
->vm
.buf
);
622 unlink(path
.buf
); /* TODO: instead of delet replace */
623 cherokee_buffer_mrproper(&path
);
625 save_xml(hdl
, dom
, NULL
);
635 virt_virt_new(cherokee_handler_virt_t
*hdl
, virConnectPtr virConn
)
637 cherokee_buffer_t
*buf
= &HDL_AVAHI(hdl
)->buffer
;
638 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
639 cherokee_buffer_t xml
= CHEROKEE_BUF_INIT
;
641 virStoragePoolPtr pool
;
642 virStorageVolPtr volume
;
644 switch (hdl
->action
) {
645 case storageVolDelete_args
: {
646 cherokee_buffer_t fullvol
= CHEROKEE_BUF_INIT
;
649 ret
= cherokee_connection_parse_args (conn
);
651 if (unlikely(ret
< ret_ok
)) {
652 conn
->error_code
= http_internal_error
;
656 ret
= cherokee_avl_get_ptr (conn
->arguments
, "pool", &temp
);
657 if (unlikely(ret
< ret_ok
)) {
658 conn
->error_code
= http_bad_request
;
662 pool
= virStoragePoolLookupByName(virConn
, temp
);
665 conn
->error_code
= http_not_found
;
669 ret
= cherokee_avl_get_ptr (conn
->arguments
, "volume", &temp
);
670 if (unlikely(ret
< ret_ok
)) {
671 conn
->error_code
= http_bad_request
;
675 cherokee_buffer_add_va (&fullvol
, "%s_%s", hdl
->user
.buf
, temp
);
676 printf("%s\n", fullvol
.buf
);
677 volume
= virStorageVolLookupByName(pool
, fullvol
.buf
);
679 cherokee_buffer_mrproper(&fullvol
);
680 virStoragePoolFree(pool
);
682 if (volume
== NULL
) {
683 conn
->error_code
= http_not_found
;
687 cherokee_buffer_add_long10(buf
, virStorageVolDelete(volume
, 0));
691 virStorageVolFree(volume
);
696 case storageVolGetXMLDesc
: {
697 cherokee_buffer_t fullvol
= CHEROKEE_BUF_INIT
;
700 ret
= cherokee_connection_parse_args (conn
);
702 if (unlikely(ret
< ret_ok
)) {
703 conn
->error_code
= http_internal_error
;
707 ret
= cherokee_avl_get_ptr (conn
->arguments
, "pool", &temp
);
708 if (unlikely(ret
< ret_ok
)) {
709 conn
->error_code
= http_bad_request
;
713 pool
= virStoragePoolLookupByName(virConn
, temp
);
716 conn
->error_code
= http_not_found
;
720 virStoragePoolRefresh(pool
, 0); /* TODO: might be better to do it outside */
722 ret
= cherokee_avl_get_ptr (conn
->arguments
, "volume", &temp
);
723 if (unlikely(ret
< ret_ok
)) {
724 conn
->error_code
= http_bad_request
;
728 cherokee_buffer_add_va (&fullvol
, "%s_%s", hdl
->user
.buf
, temp
);
729 printf("%s\n", fullvol
.buf
);
730 volume
= virStorageVolLookupByName(pool
, fullvol
.buf
);
732 cherokee_buffer_mrproper(&fullvol
);
733 virStoragePoolFree(pool
);
735 if (volume
== NULL
) {
736 conn
->error_code
= http_not_found
;
740 char *xml
= virStorageVolGetXMLDesc(volume
, 0);
741 virStorageVolFree(volume
);
742 cherokee_buffer_add(buf
, xml
, strlen(xml
));
748 case storageVolCloneXML_args
: {
752 ret
= cherokee_connection_parse_args (conn
);
753 if (unlikely(ret
< ret_ok
)) {
754 conn
->error_code
= http_internal_error
;
758 ret
= cherokee_avl_get_ptr (conn
->arguments
, "pool", &temp
);
759 if (unlikely(ret
< ret_ok
)) {
760 conn
->error_code
= http_bad_request
;
764 pool
= virStoragePoolLookupByName(virConn
, temp
);
767 conn
->error_code
= http_not_found
;
771 ret
= cherokee_avl_get_ptr (conn
->arguments
, "volume", &temp
);
772 if (unlikely(ret
< ret_ok
)) {
773 conn
->error_code
= http_bad_request
;
777 volume
= virStorageVolLookupByName(pool
, temp
);
779 if (volume
== NULL
) {
780 conn
->error_code
= http_not_found
;
784 source
= virStorageVolGetKey(volume
);
786 ret
= cherokee_avl_get_ptr (conn
->arguments
, "name", &name
);
787 if (unlikely(ret
< ret_ok
)) {
788 virStorageVolFree(volume
);
789 virStoragePoolFree(pool
);
790 conn
->error_code
= http_bad_request
;
794 cherokee_buffer_t busy
= CHEROKEE_BUF_INIT
;
795 cherokee_buffer_add_va (&busy
, "/mnt/images/queue/%s_%s.queued", hdl
->user
.buf
, name
);
797 if (unlikely(symlink(source
, busy
.buf
) == 1)) {
798 virStorageVolFree(volume
);
799 virStoragePoolFree(pool
);
800 conn
->error_code
= http_internal_error
;
804 cherokee_buffer_mrproper(&busy
);
805 cherokee_buffer_add_str(buf
, "QUEUED");
810 case storageVolCreateXML_args
: {
811 void *temp
, *type
, *name
, *unit
;
812 unsigned long int capacity
, allocation
;
815 ret
= cherokee_connection_parse_args (conn
);
816 if (unlikely(ret
< ret_ok
)) {
817 conn
->error_code
= http_internal_error
;
821 ret
= cherokee_avl_get_ptr (conn
->arguments
, "pool", &temp
);
822 if (unlikely(ret
< ret_ok
)) {
823 conn
->error_code
= http_bad_request
;
827 pool
= virStoragePoolLookupByName(virConn
, temp
);
830 conn
->error_code
= http_not_found
;
834 ret
= cherokee_avl_get_ptr (conn
->arguments
, "name", &name
);
835 if (unlikely(ret
< ret_ok
)) {
836 conn
->error_code
= http_bad_request
;
840 ret
= cherokee_avl_get_ptr (conn
->arguments
, "type", &type
);
841 if (unlikely(ret
< ret_ok
)) {
842 conn
->error_code
= http_bad_request
;
846 ret
= cherokee_avl_get_ptr (conn
->arguments
, "unit", &unit
);
847 if (unlikely(ret
< ret_ok
)) {
848 conn
->error_code
= http_bad_request
;
852 ret
= cherokee_avl_get_ptr (conn
->arguments
, "allocation", &temp
);
853 if (unlikely(ret
< ret_ok
)) {
854 conn
->error_code
= http_bad_request
;
859 allocation
= strtoul(temp
, NULL
, 10);
860 /* TODO: errno check */
862 ret
= cherokee_avl_get_ptr (conn
->arguments
, "capacity", &temp
);
863 if (unlikely(ret
< ret_ok
)) {
864 conn
->error_code
= http_bad_request
;
868 capacity
= strtoul(temp
, NULL
, 10);
869 /* TODO: errno check */
871 cherokee_buffer_add_va (&xml
, VIRT_STORAGE_XML
, type
, hdl
->user
.buf
, name
, allocation
, unit
, capacity
, hdl
->user
.buf
, name
, hdl
->user
.buf
, name
);
872 printf("%s", xml
.buf
);
877 case domainDefineXML_args
: {
880 unsigned long vcpu
= 0, interface
= 0, memory
= 0;
882 cherokee_buffer_t xml_interfaces
= CHEROKEE_BUF_INIT
;
885 ret
= cherokee_connection_parse_args (conn
);
886 if (unlikely(ret
< ret_ok
))
889 ret
= cherokee_avl_get_ptr (conn
->arguments
, "vcpu", &temp
);
891 vcpu
= strtoul(temp
, (char **) NULL
, 10);
893 if (ret
!= ret_ok
|| errno
== ERANGE
|| vcpu
== 0) {
894 conn
->error_code
= http_internal_error
;
899 ret
= cherokee_avl_get_ptr (conn
->arguments
, "memory", &temp
);
901 memory
= strtoul(temp
, (char **) NULL
, 10);
903 if (ret
!= ret_ok
|| errno
== ERANGE
|| memory
== 0) {
904 conn
->error_code
= http_internal_error
;
909 ret
= cherokee_avl_get_ptr (conn
->arguments
, "interface", &temp
);
911 interface
= strtoul(temp
, (char **) NULL
, 10);
913 if (ret
!= ret_ok
|| errno
== ERANGE
) {
914 conn
->error_code
= http_internal_error
;
918 cherokee_buffer_t domu
= CHEROKEE_BUF_INIT
;
919 cherokee_buffer_add_va (&domu
, "%s_%s", hdl
->user
.buf
, hdl
->vm
.buf
);
921 for (i
= 0; i
< interface
; i
++) {
924 if (getNewMac("dv28", domu
.buf
, &mac
, &ip
) == 0)
925 cherokee_buffer_add_va (&xml_interfaces
, VIRT_INTERFACE_XML
, mac
, ip
);
928 cherokee_buffer_mrproper(&domu
);
930 cherokee_buffer_add_va (&xml
, VIRT_DOMAIN_XML
,
931 hdl
->user
.buf
, hdl
->vm
.buf
, memory
, vcpu
, xml_interfaces
.buf
);
933 cherokee_buffer_mrproper(&xml_interfaces
);
937 case storageVolCreateXML
:
938 case domainDefineXML
: {
940 cherokee_post_get_len (&conn
->post
, &postl
);
941 cherokee_post_walk_read (&conn
->post
, &xml
, (cuint_t
) postl
);
946 switch (hdl
->action
) {
947 case domainDefineXML_args
:
948 case domainDefineXML
: {
949 result
= virDomainDefineXML(virConn
, (const char *) xml
.buf
);
950 // printf("%s\n", xml.buf);
951 cherokee_buffer_mrproper(&xml
);
953 if (result
== NULL
) {
954 /* TODO: vrij maken eventuele uitgegeven macs! */
955 conn
->error_code
= http_internal_error
;
959 save_xml(hdl
, result
, buf
);
964 case storageVolCreateXML_args
:
965 case storageVolCreateXML
: {
966 virStorageVolPtr vol
= virStorageVolCreateXML(pool
, xml
.buf
, 0);
967 virStoragePoolFree(pool
);
968 cherokee_buffer_mrproper(&xml
);
971 cherokee_buffer_add_long10(buf
, -1);
975 cherokee_buffer_add_long10(buf
, 0);
985 virt_virt_do(cherokee_handler_virt_t
*hdl
, cherokee_buffer_t
*domu
, cherokee_buffer_t
*uri
)
987 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
989 ret_t ret
= ret_error
;
990 virConnectPtr virConn
= NULL
;
993 if (HDL_VIRT_PROPS(hdl
)->read_only
== FALSE
)
994 virConn
= virConnectOpen (uri
->buf
);
996 if (!virConn
&& !(virConn
= virConnectOpenReadOnly (uri
->buf
))) {
997 conn
->error_code
= http_service_unavailable
;
1001 switch (hdl
->action
) {
1002 case storageVolDelete_args
:
1003 case storageVolGetXMLDesc
:
1004 case storageVolCloneXML_args
:
1005 case storageVolCreateXML_args
:
1006 case storageVolCreateXML
:
1007 case domainDefineXML_args
:
1008 case domainDefineXML
: {
1009 ret
= virt_virt_new(hdl
, virConn
);
1015 if ((dom
= virDomainLookupByName(virConn
, domu
->buf
)) == NULL
) {
1016 conn
->error_code
= http_not_found
;
1018 ret
= virt_virt_function(hdl
, dom
, virConn
);
1024 virConnectClose(virConn
);
1029 virt_while (cherokee_buffer_t
*key
, void *value
, void *param
) {
1030 cherokee_handler_virt_t
* hdl
= param
;
1031 // cherokee_buffer_t uri = CHEROKEE_BUF_INIT;
1032 // cherokee_buffer_add_va (&uri, "xen://%s/", ((cherokee_buffer_t *) value)->buf);
1033 // virt_virt_do((cherokee_handler_virt_t *) param, key, &uri);
1034 // cherokee_buffer_mrproper(&uri);
1036 cherokee_buffer_add_va (&hdl
->buffer
, "<domain><name>%s</name></domain>", key
->buf
);
1042 virt_while_user (cherokee_buffer_t
*key
, void *value
, void *param
) {
1043 cherokee_handler_virt_t
*hdl
= param
;
1044 if (key
->len
> hdl
->user
.len
&& key
->buf
[hdl
->user
.len
] == '_' && strncmp(key
->buf
, hdl
->user
.buf
, hdl
->user
.len
) == 0)
1045 return virt_while (key
, value
, param
);
1051 virt_build_page (cherokee_handler_virt_t
*hdl
)
1054 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
1055 cherokee_buffer_t uri
= CHEROKEE_BUF_INIT
;
1056 cherokee_buffer_t domu
= CHEROKEE_BUF_INIT
;
1058 if (hdl
->action
> xml
&& HDL_VIRT_PROPS(hdl
)->xsl
.len
> 0)
1059 cherokee_buffer_add_va (&hdl
->buffer
, "<?xml version=\"1.0\"?>\n<?xml-stylesheet type=\"text/xsl\" href=\"%s\"?>\n", HDL_VIRT_PROPS(hdl
)->xsl
.buf
);
1062 /* First, block the event loop */
1063 avahi_threaded_poll_lock(HDL_AVAHI_PROPS(hdl
)->threaded_poll
);
1064 switch (hdl
->action
) {
1066 cherokee_buffer_add_str (&hdl
->buffer
, "<domains>");
1067 cherokee_avl_while(&HDL_AVAHI_PROPS(hdl
)->entries
, virt_while
, hdl
, NULL
, NULL
);
1068 cherokee_buffer_add_str (&hdl
->buffer
, "</domains>");
1074 cherokee_buffer_add_str (&hdl
->buffer
, "<domains>");
1075 cherokee_avl_while(&HDL_AVAHI_PROPS(hdl
)->entries
, virt_while_user
, hdl
, NULL
, NULL
);
1076 cherokee_buffer_add_str (&hdl
->buffer
, "</domains>");
1082 cherokee_buffer_t
*hostname
= NULL
;
1083 cherokee_buffer_add_va (&domu
, "%s_%s", hdl
->user
.buf
, hdl
->vm
.buf
);
1084 ret
= cherokee_avl_get_ptr(&HDL_AVAHI_PROPS(hdl
)->entries
, domu
.buf
, (void **) &hostname
);
1086 if (ret
== ret_not_found
) {
1087 virDomainPtr virDom
;
1088 virConnectPtr virConn
;
1089 cherokee_buffer_add_va (&uri
, "xen://%s/", HDL_VIRT_PROPS(hdl
)->virt
); // TODO: change!
1091 /* If we have the read only parameter, we will set up a connection to the
1092 * Hypervisor here. */
1093 if (HDL_VIRT_PROPS(hdl
)->read_only
== FALSE
)
1094 virConn
= virConnectOpen (uri
.buf
);
1096 /* We should already have a connection (read only) or build a new connection
1097 * if this doesn't work, we can safely assume our services is fubar */
1098 if (!virConn
&& !(virConn
= virConnectOpenReadOnly (uri
.buf
))) {
1099 conn
->error_code
= http_service_unavailable
;
1103 /* We lookup if there is a domain somewhere with this name */
1104 if ((virDom
= virDomainLookupByName(virConn
, domu
.buf
)) == NULL
) {
1105 /* If the domain is not found on the network the only possible
1106 * command that is possible would be to Define it */
1107 if (hdl
->action
== domainDefineXML_args
|| hdl
->action
== domainDefineXML
) {
1110 /* Otherwise we don't have anything to do and we should
1111 * return an error */
1112 hdl
->action
= nothing
;
1113 conn
->error_code
= http_not_found
;
1117 /* We don't want te recreate things that already found on the network
1118 * first undefine them! */
1119 if (hdl
->action
== domainDefineXML_args
|| hdl
->action
== domainDefineXML
) {
1120 hdl
->action
= nothing
;
1121 conn
->error_code
= http_bad_request
;
1124 /* Everything is ok, because nothing is found. */
1128 /* Domain wasn't NULL, so we should free it here */
1129 virDomainFree(virDom
);
1131 virConnectClose (virConn
);
1132 } else if (ret
== ret_ok
) {
1133 cherokee_buffer_add_va (&uri
, "xen://%s/", hostname
->buf
);
1134 printf("%s\n", uri
.buf
);
1136 hdl
->action
= http_internal_error
;
1137 hdl
->action
= nothing
;
1143 /* Finally, unblock the event loop */
1144 avahi_threaded_poll_unlock(HDL_AVAHI_PROPS(hdl
)->threaded_poll
);
1146 if (ret
== ret_ok
&& hdl
->action
!= showall
&& hdl
->action
!= showuservms
) {
1147 ret
= virt_virt_do(hdl
, &domu
, &uri
);
1150 cherokee_buffer_mrproper(&domu
);
1151 cherokee_buffer_mrproper(&uri
);
1159 cherokee_handler_virt_new (cherokee_handler_t
**hdl
, cherokee_connection_t
*cnt
, cherokee_module_props_t
*props
)
1162 CHEROKEE_NEW_STRUCT (n
, handler_virt
);
1164 /* Init the base class
1167 cherokee_handler_init_base(HANDLER(n
), cnt
, HANDLER_PROPS(props
), PLUGIN_INFO_HANDLER_PTR(virt
));
1169 MODULE(n
)->init
= (handler_func_init_t
) cherokee_handler_virt_init
;
1170 MODULE(n
)->free
= (module_func_free_t
) cherokee_handler_virt_free
;
1171 HANDLER(n
)->step
= (handler_func_step_t
) cherokee_handler_virt_step
;
1172 HANDLER(n
)->add_headers
= (handler_func_add_headers_t
) cherokee_handler_virt_add_headers
;
1174 HANDLER(n
)->support
= hsupport_length
| hsupport_range
;
1176 ret
= cherokee_buffer_init (&n
->buffer
);
1177 if (unlikely(ret
!= ret_ok
))
1180 ret
= cherokee_buffer_ensure_size (&n
->buffer
, 4*1024);
1181 if (unlikely(ret
!= ret_ok
))