Creator
[handlervirt.git] / handler_virt.c
blob3543b3c92c9f29343f7c646d8151917017a708b5
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* Cherokee
5 * Authors:
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
23 * USA
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 " <source bridge='xenbr0'/>" \
31 " <mac address='%s'/>" \
32 " <ip address='%s' />" \
33 " <script path='vif-bridge'/>" \
34 " </interface>"
36 //" <target dev='vif1.0'/>" \
38 #define VIRT_DISK_XML \
39 " <disk type='file' device='disk'>" \
40 " <driver name='tap' type='qcow' />" \
41 " <source file='%s'/>" \
42 " <target dev='%s' bus='xen'/>" \
43 " </disk>"
47 #define VIRT_DOMAIN_XML \
48 "<domain type='xen'>" \
49 " <name>%s_%s</name>" \
50 " <os>" \
51 " <type>linux</type>" \
52 " <kernel>/usr/lib/xen/boot/linux-2.6.20-xen-r6</kernel>" \
53 " <cmdline> root=/dev/xvda ro</cmdline>" \
54 " </os>" \
55 " <memory>%d</memory>" \
56 " <vcpu>%d</vcpu>" \
57 " <on_poweroff>destroy</on_poweroff>" \
58 " <on_reboot>restart</on_reboot>" \
59 " <on_crash>destroy</on_crash>" \
60 " <devices>" \
61 " %s" \
62 " </devices>" \
63 "</domain>"
65 #define VIRT_STORAGE_XML \
66 "<volume type='%s'>" \
67 " <name>%s</name>" \
68 " <allocation>%lu</allocation>" \
69 " <capacity unit='%s'>%lu</capacity>" \
70 " <target>" \
71 " <path>%s/%s/images/%s</path>" \
72 " <permissions>" \
73 " <owner>0744</owner>" \
74 " <group>0744</group>" \
75 " <mode>0744</mode>" \
76 " <label>%s_%s</label>" \
77 " </permissions>" \
78 " </target>" \
79 "</volume>"
82 #include "handler_virt.h"
83 #include "handler_avahi.h"
84 #include <cherokee/cherokee.h>
86 /* Plug-in initialization
88 * In this function you can use any of these:
89 * http_delete | http_get | http_post | http_put
91 * For a full list: cherokee_http_method_t
93 * It is what your handler to be implements.
96 PLUGIN_INFO_HANDLER_EASIEST_INIT (virt, http_get | http_post);
99 /* Methods implementation
101 static ret_t
102 props_free (cherokee_handler_virt_props_t *props)
105 cherokee_buffer_mrproper(&props->xsl);
106 cherokee_buffer_mrproper(&props->virt);
107 return cherokee_module_props_free_base (MODULE_PROPS(props));
111 ret_t
112 cherokee_handler_virt_configure (cherokee_config_node_t *conf, cherokee_server_t *srv, cherokee_module_props_t **_props)
114 cherokee_list_t *i;
115 cherokee_handler_virt_props_t *props;
117 /* Instance a new property object
120 if (*_props == NULL) {
121 CHEROKEE_NEW_STRUCT (n, handler_virt_props);
123 cherokee_handler_avahi_props_init_base (PROP_AVAHI(n), MODULE_PROPS_FREE(props_free));
125 /* Look at handler_virt.h
126 * This is an virt of configuration.
128 n->authenticate = true; /* by default we are secure! */
129 n->read_only = true; /* by default we are secure! */
130 cherokee_buffer_init (&n->xsl); /* we support a custom header */
131 cherokee_buffer_init (&n->virt); /* your first xenserver */
133 *_props = MODULE_PROPS(n);
136 props = PROP_VIRT(*_props);
138 cherokee_config_node_foreach (i, conf) {
139 cherokee_config_node_t *subconf = CONFIG_NODE(i);
141 if (equal_buf_str (&subconf->key, "authenticate")) {
142 props->authenticate = atoi(subconf->val.buf);
144 else if (equal_buf_str (&subconf->key, "read_only")) {
145 props->read_only = atoi(subconf->val.buf);
147 else if (equal_buf_str (&subconf->key, "xsl")) {
148 cherokee_buffer_add_buffer (&props->xsl, &subconf->val);
150 else if (equal_buf_str (&subconf->key, "virt")) {
151 cherokee_buffer_add_buffer (&props->virt, &subconf->val);
156 /* Init base class
159 return cherokee_handler_avahi_configure (conf, srv, _props);
163 ret_t
164 cherokee_handler_virt_init (cherokee_handler_virt_t *hdl)
166 int isroot = false;
167 int len;
168 char *this, *next;
170 cherokee_connection_t *conn = HANDLER_CONN(hdl);
171 cherokee_buffer_init(&hdl->user);
172 cherokee_buffer_init(&hdl->vm);
174 hdl->action = nothing;
176 cherokee_buffer_add (&conn->pathinfo,
177 conn->request.buf + conn->web_directory.len,
178 conn->request.len - conn->web_directory.len);
180 this = conn->pathinfo.buf + 1;
181 next = strchr(this, '/');
184 if ((!next && (this && (len = strlen(this)) == 0)) || (next && ((len = next - this) == 0)) )
185 hdl->action = showall;
186 else {
187 cherokee_buffer_add (&hdl->user, this, len);
190 if (HDL_VIRT_PROPS(hdl)->authenticate) {
191 if (!conn->validator ||
192 (conn->validator &&
193 (!cherokee_buffer_cmp_buf(&conn->validator->user, &hdl->user) &&
194 !(isroot = cherokee_buffer_cmp (&conn->validator->user, "root", 4))))) {
195 hdl->action = nothing; /* just in case */
196 conn->error_code = http_unauthorized;
197 return ret_error;
199 } else {
200 isroot = true;
203 if (hdl->action == showall) {
204 if (!isroot) {
205 hdl->action = nothing;
206 conn->error_code = http_unauthorized;
207 return ret_error;
208 } else {
209 return virt_build_page(hdl);
214 if (!next) {
215 hdl->action = showuservms;
216 return virt_build_page(hdl);
217 } else {
218 this = next + 1;
219 next = strchr(this, '/');
221 if (!next && (this && (len = strlen(this)) == 0) || (next && ((len = next - this) == 0)) ) {
222 hdl->action = showuservms;
223 return virt_build_page(hdl);
227 cherokee_buffer_add (&hdl->vm, this, len);
229 if (!next) {
230 hdl->action = domainGetXMLDesc;
231 return virt_build_page(hdl);
232 } else {
233 this = next + 1;
234 next = strchr(this, '/');
236 if (!next && (this && (len = strlen(this)) == 0) || (next && ((len = next - this) == 0)) ) {
237 hdl->action = domainGetXMLDesc;
238 return virt_build_page(hdl);
242 hdl->action = not_implemented;
243 switch (conn->header.method) {
244 case http_get:
245 if (strncmp(this, "virDomain", 9) == 0) {
246 if (strncmp(this+9, "Get", 3) == 0) {
247 if (strcmp(this+12, "ID") == 0) hdl->action = domainGetID;
248 else if (strcmp(this+12, "Name") == 0) hdl->action = domainGetName;
249 else if (strcmp(this+12, "MaxMemory") == 0) hdl->action = domainGetMaxMemory;
250 else if (strcmp(this+12, "MaxVcpus") == 0) hdl->action = domainGetMaxVcpus;
251 else if (strcmp(this+12, "OSType") == 0) hdl->action = domainGetOSType;
252 else if (strcmp(this+12, "UUID") == 0) hdl->action = domainGetUUID;
253 else if (strcmp(this+12, "UUIDString") == 0) hdl->action = domainGetUUIDString;
254 else if (strcmp(this+12, "XMLDesc") == 0) hdl->action = domainGetXMLDesc;
256 else if (strcmp(this+9, "Create") == 0) hdl->action = domainCreate;
257 else if (strcmp(this+9, "Destroy") == 0) hdl->action = domainDestroy;
258 else if (strcmp(this+9, "Reboot") == 0) hdl->action = domainReboot;
259 else if (strcmp(this+9, "Shutdown") == 0) hdl->action = domainShutdown;
261 else if (strcmp(this+9, "AttachDevice") == 0) hdl->action = domainAttachDevice_args;
263 else if (strcmp(this+9, "DefineXML") == 0) hdl->action = domainDefineXML_args;
264 else if (strcmp(this+9, "Undefine") == 0) hdl->action = domainUndefine;
266 else if (strncmp(this, "virStorage", 10) == 0) {
267 if (strncmp(this+10, "Vol", 3) == 0) {
268 if (strcmp(this+13, "CreateXML") == 0) hdl->action = storageVolCreateXML_args;
271 break;
273 case http_post: {
274 off_t postl;
275 cherokee_post_get_len (&conn->post, &postl);
277 if (postl <= 0 || postl >= (INT_MAX-1)) {
278 conn->error_code = http_bad_request;
279 return ret_error;
282 if (strncmp(this, "virDomain", 9) == 0) {
283 if (strcmp(this+9, "AttachDevice") == 0) hdl->action = domainAttachDevice;
284 else if (strcmp(this+9, "DetachDevice") == 0) hdl->action = domainDetachDevice;
285 else if (strcmp(this+9, "DefineXML") == 0) hdl->action = domainDefineXML;
287 else if (strncmp(this, "virStorage", 10) == 0) {
288 if (strncmp(this+10, "Vol", 3) == 0) {
289 if (strcmp(this+13, "CreateXML") == 0) hdl->action = storageVolCreateXML;
293 break;
296 default:
297 return ret_error;
300 if (hdl->action <= 0) {
301 conn->error_code = http_bad_request;
302 return ret_error;
306 return virt_build_page(hdl);
309 ret_t
310 cherokee_handler_virt_free (cherokee_handler_virt_t *hdl)
312 cherokee_buffer_mrproper (&hdl->buffer);
313 cherokee_buffer_mrproper (&hdl->user);
314 cherokee_buffer_mrproper (&hdl->vm);
316 return ret_ok;
319 ret_t
320 cherokee_handler_virt_step (cherokee_handler_virt_t *hdl, cherokee_buffer_t *buffer)
322 cherokee_buffer_add_buffer (buffer, &hdl->buffer);
323 return ret_eof_have_data;
326 ret_t
327 cherokee_handler_virt_add_headers (cherokee_handler_virt_t *hdl, cherokee_buffer_t *buffer)
329 cherokee_buffer_add_va (buffer, "Content-Length: %d"CRLF, hdl->buffer.len);
331 if (hdl->action > xml)
332 cherokee_buffer_add_str (buffer, "Content-Type: application/xml"CRLF);
333 else
334 cherokee_buffer_add_str (buffer, "Content-Type: text/plain"CRLF);
336 return ret_ok;
340 static ret_t
341 while_func_entries (cherokee_buffer_t *key, void *value, void *param) {
342 virConnectPtr conn = NULL;
343 cherokee_buffer_t uri = CHEROKEE_BUF_INIT;
344 cherokee_buffer_t *buf = (cherokee_buffer_t *)param;
346 cherokee_buffer_add_va (&uri, "xen://%s/", value);
348 if (HDL_VIRT_PROPS(hdl)->read_only == FALSE && !(conn = virConnectOpen (uri.buf))) {
349 if (!(conn = virConnectOpen (uri.buf))) {
350 return ret_error;
354 if (!conn && !(conn = virConnectOpenReadOnly (uri.buf))) {
355 return ret_error;
356 } else {
357 virDomainPtr dom;
358 if (!(dom = virDomainLookupByName(conn, (const char *) key->buf))) {
359 return ret_error;
360 } else {
361 char *xml = virDomainGetXMLDesc(dom, 0);
362 cherokee_buffer_add(buf, xml, strlen(xml));
364 virConnectClose(conn);
367 cherokee_buffer_mrproper(&uri);
369 return ret_ok;
372 static ret_t
373 virt_virt_function(cherokee_handler_virt_t *hdl, virDomainPtr dom) {
374 cherokee_connection_t *conn = HANDLER_CONN(hdl);
375 cherokee_buffer_t *buf = &HDL_AVAHI(hdl)->buffer;
377 switch (hdl->action) {
379 case domainUndefine: {
380 int result;
381 if ((result = virDomainUndefine(dom)) != 0) {
382 conn->error_code = http_internal_error;
385 cherokee_buffer_add_long10(buf, result);
386 break;
389 case domainAttachDevice_args: {
390 int result;
391 void *temp;
392 ret_t ret;
393 ret = cherokee_connection_parse_args (conn);
394 if (unlikely(ret < ret_ok))
395 return ret;
397 ret = cherokee_avl_get_ptr (conn->arguments, "type", &temp);
399 if (ret == ret_ok) {
400 cherokee_buffer_t xml = CHEROKEE_BUF_INIT;
402 if (strcmp(temp, "disk") == 0) {
403 void *file, *device;
405 if ((ret = cherokee_avl_get_ptr (conn->arguments, "file", &file)) == ret_ok && (ret = cherokee_avl_get_ptr (conn->arguments, "device", &device)) == ret_ok) {
406 cherokee_buffer_add_va (&xml, VIRT_DISK_XML, file, device);
407 } else {
408 ret = ret_error;
412 else if (strcmp(temp, "interface") == 0) {
413 void *mac, *ip;
414 if ((ret = cherokee_avl_get_ptr (conn->arguments, "mac", &mac)) == ret_ok && (ret = cherokee_avl_get_ptr (conn->arguments, "ip", &ip)) == ret_ok) {
415 cherokee_buffer_add_va (&xml, VIRT_INTERFACE_XML, mac, ip);
416 } else {
417 char *mac = NULL;
418 char *ip = NULL;
419 cherokee_buffer_t domu = CHEROKEE_BUF_INIT;
420 cherokee_buffer_add_va (&domu, "%s_%s", hdl->user.buf, hdl->vm.buf);
421 getNewMac("dv28", domu.buf, &mac, &ip);
422 cherokee_buffer_mrproper(&domu);
423 cherokee_buffer_add_va (&xml, VIRT_INTERFACE_XML, mac, ip);
429 if (ret == ret_ok && (result = virDomainAttachDevice(dom, (const char *) xml.buf)) == 0) {
430 ret = ret_ok;
431 } else {
432 conn->error_code = http_internal_error;
433 ret = ret_error;
436 cherokee_buffer_add_long10(buf, result);
437 cherokee_buffer_mrproper(&xml);
440 return ret;
443 case domainAttachDevice: {
444 off_t postl;
445 int result;
446 cherokee_buffer_t post = CHEROKEE_BUF_INIT;
447 cherokee_post_get_len (&conn->post, &postl);
448 cherokee_post_walk_read (&conn->post, &post, (cuint_t) postl);
449 if ((result = virDomainAttachDevice(dom, (const char *) post.buf)) != 0)
450 conn->error_code = http_internal_error;
452 cherokee_buffer_mrproper(&post);
453 cherokee_buffer_add_long10(buf, result);
454 break;
458 case domainGetXMLDesc: {
459 char *xml = virDomainGetXMLDesc(dom, 0);
460 cherokee_buffer_add(buf, xml, strlen(xml));
461 free(xml);
462 break;
466 case domainDetachDevice: {
467 off_t postl;
468 int result;
469 cherokee_buffer_t post = CHEROKEE_BUF_INIT;
470 cherokee_post_get_len (&conn->post, &postl);
471 cherokee_post_walk_read (&conn->post, &post, (cuint_t) postl);
472 if ((result = virDomainDetachDevice(dom, (const char *) post.buf)) != 0) {
473 conn->error_code = http_internal_error;
475 cherokee_buffer_mrproper(&post);
476 cherokee_buffer_add_long10(buf, result);
477 break;
480 case domainGetID: {
481 cherokee_buffer_add_ulong10(buf, virDomainGetID(dom));
482 break;
484 case domainGetMaxMemory: {
485 cherokee_buffer_add_ulong10(buf, virDomainGetMaxMemory (dom));
486 break;
488 case domainGetMaxVcpus: {
489 cherokee_buffer_add_long10(buf, virDomainGetMaxVcpus (dom));
490 break;
492 case domainGetName: {
493 const char *name = virDomainGetName (dom);
494 cherokee_buffer_add(buf, name, strlen(name));
495 break;
497 case domainGetUUID: {
498 unsigned char uuid[VIR_UUID_BUFLEN];
499 if (virDomainGetUUID(dom, uuid) == 0) {
500 cherokee_buffer_add_str(buf, uuid);
501 } else {
502 conn->error_code = http_internal_error;
503 return ret_error;
505 break;
507 case domainGetUUIDString: {
508 unsigned char uuid[VIR_UUID_STRING_BUFLEN];
509 if (virDomainGetUUIDString(dom, uuid) == 0) {
510 cherokee_buffer_add_str(buf, uuid);
511 } else {
512 conn->error_code = http_internal_error;
513 return ret_error;
515 break;
518 case domainCreate: {
519 cherokee_buffer_add_long10(buf, virDomainCreate (dom));
520 break;
523 case domainDestroy: {
524 cherokee_buffer_add_long10(buf, virDomainDestroy (dom));
525 break;
528 case domainReboot: {
529 cherokee_buffer_add_long10(buf, virDomainReboot (dom, 0));
530 break;
533 case domainShutdown: {
534 cherokee_buffer_add_long10(buf, virDomainShutdown (dom));
535 break;
539 case domainGetOSType: {
540 char *ostype = virDomainGetOSType(dom);
541 cherokee_buffer_add(buf, ostype, strlen(ostype));
542 free(ostype);
543 break;
548 return ret_ok;
551 static ret_t
552 virt_virt_new(cherokee_handler_virt_t *hdl, virConnectPtr virConn)
554 cherokee_buffer_t *buf = &HDL_AVAHI(hdl)->buffer;
555 cherokee_connection_t *conn = HANDLER_CONN(hdl);
556 cherokee_buffer_t xml = CHEROKEE_BUF_INIT;
557 virDomainPtr result;
558 virStoragePoolPtr pool;
559 virStorageVolPtr volume;
561 switch (hdl->action) {
562 case storageVolCloneXML_args: {
563 void *temp, *name;
564 const char *source;
565 ret_t ret;
566 ret = cherokee_connection_parse_args (conn);
567 if (unlikely(ret < ret_ok))
568 return ret;
570 ret = cherokee_avl_get_ptr (conn->arguments, "pool", &temp);
571 if (unlikely(ret < ret_ok))
572 return ret;
574 pool = virStoragePoolLookupByName(virConn, temp);
576 if (pool == NULL)
577 return ret_error;
579 ret = cherokee_avl_get_ptr (conn->arguments, "volume", &temp);
580 if (unlikely(ret < ret_ok))
581 return ret;
583 volume = virStorageVolLookupByName(pool, temp);
585 if (volume == NULL)
586 return ret_error;
588 source = virStorageVolGetKey(volume);
590 ret = cherokee_avl_get_ptr (conn->arguments, "name", &name);
591 if (unlikely(ret < ret_ok))
592 return ret;
594 ret = cherokee_avl_get_ptr (conn->arguments, "type", &temp);
595 if (unlikely(ret < ret_ok))
596 return ret;
598 enum { UNKNOWN, RAW, QCOW } storage;
599 storage clone = UNKNOWN;
601 if (strcmp("raw", temp) == 0)
602 clone = RAW;
603 else if (strcmp("qcow", temp) == 0)
604 clone = QCOW;
606 if (clone != UNKNOWN) {
607 pid_t pid;
608 if ((pid = fork()) < 0) {
609 ret = ret_error;
610 } else {
611 if (pid == 0) {
612 cherokee_buffer_t busy = CHEROKEE_BUF_INIT;
613 cherokee_buffer_t *dest;
614 cherokee_buffer_va (&busy, "/mnt/netapp/users/%s/%s.%s.busy", hdl->user.buf, name, temp);
615 cherokee_buffer_duy (&busy, &dest);
616 cherokee_buffer_drop_ending (dest, 5);
618 switch (clone) {
619 case RAW:
620 execlp("/usr/bin/cp", name, busy.buf);
621 break;
623 case QCOW:
624 execlp("/usr/sbin/qcow-create", "0", busy.buf, name);
625 break;
627 link(busy.buf, dest.buf);
628 unlink(busy.buf);
630 cherokee_buffer_mrproper(&busy);
631 cherokee_buffer_mrproper(dest);
633 exit(0);
638 cherokee_buffer_add_str(buf, "BUSY");
640 break;
643 case storageVolCreateXML_args: {
644 void *temp, *type, *name, *unit;
645 char *pool_path;
646 unsigned long int capacity, allocation;
647 ret_t ret;
648 ret = cherokee_connection_parse_args (conn);
649 if (unlikely(ret < ret_ok))
650 return ret;
652 ret = cherokee_avl_get_ptr (conn->arguments, "pool", &temp);
653 if (unlikely(ret < ret_ok))
654 return ret;
656 pool = virStoragePoolLookupByName(virConn, temp);
658 if (pool == NULL)
659 return ret_error;
661 pool_path = "/mnt/netapp/users";
663 pool_path = virStoragePoolGetPath(pool);
665 if (!pool_path) {
666 virStoragePoolFree(pool);
667 return ret_error;
672 ret = cherokee_avl_get_ptr (conn->arguments, "name", &name);
673 if (unlikely(ret < ret_ok))
674 return ret;
676 ret = cherokee_avl_get_ptr (conn->arguments, "type", &type);
677 if (unlikely(ret < ret_ok))
678 return ret;
680 ret = cherokee_avl_get_ptr (conn->arguments, "unit", &unit);
681 if (unlikely(ret < ret_ok))
682 return ret;
684 ret = cherokee_avl_get_ptr (conn->arguments, "allocation", &temp);
685 if (unlikely(ret < ret_ok))
686 return ret;
688 allocation = strtoul(temp, NULL, 10);
689 /* TODO: errno check */
691 ret = cherokee_avl_get_ptr (conn->arguments, "capacity", &temp);
692 if (unlikely(ret < ret_ok))
693 return ret;
695 capacity = strtoul(temp, NULL, 10);
696 /* TODO: errno check */
698 cherokee_buffer_add_va (&xml, VIRT_STORAGE_XML, type, name, allocation, unit, capacity, pool_path, hdl->user.buf, name, hdl->user.buf, name);
699 // free(pool_path);
700 break;
703 case domainDefineXML_args: {
704 ret_t ret;
705 unsigned int i;
706 unsigned long vcpu = 0, interface = 0, memory = 0;
708 cherokee_buffer_t xml_interfaces = CHEROKEE_BUF_INIT;
709 void *temp = NULL;
711 ret = cherokee_connection_parse_args (conn);
712 if (unlikely(ret < ret_ok))
713 return ret;
715 ret = cherokee_avl_get_ptr (conn->arguments, "vcpu", &temp);
716 if (ret == ret_ok)
717 vcpu = strtoul(temp, (char **) NULL, 10);
719 if (ret != ret_ok || errno == ERANGE || vcpu == 0) {
720 conn->error_code = http_internal_error;
721 return ret_error;
725 ret = cherokee_avl_get_ptr (conn->arguments, "memory", &temp);
726 if (ret == ret_ok)
727 memory = strtoul(temp, (char **) NULL, 10);
729 if (ret != ret_ok || errno == ERANGE || memory == 0) {
730 conn->error_code = http_internal_error;
731 return ret_error;
735 ret = cherokee_avl_get_ptr (conn->arguments, "interface", &temp);
736 if (ret == ret_ok)
737 interface = strtoul(temp, (char **) NULL, 10);
739 if (ret != ret_ok || errno == ERANGE) {
740 conn->error_code = http_internal_error;
741 return ret_error;
744 cherokee_buffer_t domu = CHEROKEE_BUF_INIT;
745 cherokee_buffer_add_va (&domu, "%s_%s", hdl->user.buf, hdl->vm.buf);
747 for (i = 0; i < interface; i++) {
748 char *mac = NULL;
749 char *ip = NULL;
750 getNewMac("dv28", domu.buf, &mac, &ip);
751 cherokee_buffer_add_va (&xml_interfaces, VIRT_INTERFACE_XML, mac, ip);
754 cherokee_buffer_mrproper(&domu);
756 cherokee_buffer_add_va (&xml, VIRT_DOMAIN_XML,
757 hdl->user.buf, hdl->vm.buf, memory, vcpu, xml_interfaces.buf);
759 cherokee_buffer_mrproper(&xml_interfaces);
760 break;
763 case storageVolCreateXML:
764 case domainDefineXML: {
765 off_t postl;
766 cherokee_post_get_len (&conn->post, &postl);
767 cherokee_post_walk_read (&conn->post, &xml, (cuint_t) postl);
768 break;
772 switch (hdl->action) {
773 case domainDefineXML_args:
774 case domainDefineXML: {
775 result = virDomainDefineXML(virConn, (const char *) xml.buf);
776 // printf("%s\n", xml.buf);
777 cherokee_buffer_mrproper(&xml);
779 if (result == NULL) {
780 /* TODO: vrij maken eventuele uitgegeven macs! */
781 conn->error_code = http_internal_error;
782 return ret_error;
785 char *output = virDomainGetXMLDesc(result, 0);
786 /* TODO: store the domain on DISK! */
787 FILE *fd = fopen("/mnt/netapp/users/%s/%s.xml", "w");
788 fwrite(output, strlen(output), sizeof(char), fd);
789 fclose(fd);
790 cherokee_buffer_add(buf, output, strlen(output));
791 free(output);
792 break;
795 case storageVolCreateXML_args:
796 case storageVolCreateXML: {
797 printf("%s", &xml.buf);
799 virStorageVolPtr vol = virStorageVolCreateXML(pool, (const char *) &xml.buf, 0);
800 virStoragePoolFree(pool);
801 cherokee_buffer_mrproper(&xml);
803 if (vol == NULL)
804 return ret_error;
806 cherokee_buffer_add_long10(buf, 0);
807 break;
811 return ret_ok;
815 static ret_t
816 virt_virt_do(cherokee_handler_virt_t *hdl, cherokee_buffer_t *domu, cherokee_buffer_t *uri)
818 cherokee_connection_t *conn = HANDLER_CONN(hdl);
820 ret_t ret = ret_error;
821 virConnectPtr virConn = NULL;
824 if (HDL_VIRT_PROPS(hdl)->read_only == FALSE)
825 virConn = virConnectOpen (uri->buf);
827 if (!virConn && !(virConn = virConnectOpenReadOnly (uri->buf))) {
828 conn->error_code = http_service_unavailable;
829 return ret_error;
832 switch (hdl->action) {
833 case domainDefineXML_args:
834 case domainDefineXML: {
835 ret = virt_virt_new(hdl, virConn);
836 break;
839 default: {
840 virDomainPtr dom;
841 if ((dom = virDomainLookupByName(virConn, domu->buf)) == NULL) {
842 conn->error_code = http_not_found;
843 } else {
844 ret = virt_virt_function(hdl, dom);
845 virDomainFree(dom);
850 virConnectClose(virConn);
851 return ret;
854 static ret_t
855 virt_while (cherokee_buffer_t *key, void *value, void *param) {
856 cherokee_handler_virt_t * hdl = param;
857 // cherokee_buffer_t uri = CHEROKEE_BUF_INIT;
858 // cherokee_buffer_add_va (&uri, "xen://%s/", ((cherokee_buffer_t *) value)->buf);
859 // virt_virt_do((cherokee_handler_virt_t *) param, key, &uri);
860 // cherokee_buffer_mrproper(&uri);
862 cherokee_buffer_add_va (&hdl->buffer, "<domain><name>%s</name></domain>", key->buf);
864 return ret_ok;
867 static ret_t
868 virt_while_user (cherokee_buffer_t *key, void *value, void *param) {
869 cherokee_handler_virt_t *hdl = param;
870 if (key->len > hdl->user.len && key->buf[hdl->user.len] == '_' && strncmp(key->buf, hdl->user.buf, hdl->user.len) == 0)
871 return virt_while (key, value, param);
873 return ret_ok;
876 static ret_t
877 virt_build_page (cherokee_handler_virt_t *hdl)
879 ret_t ret;
880 cherokee_connection_t *conn = HANDLER_CONN(hdl);
881 cherokee_buffer_t uri = CHEROKEE_BUF_INIT;
882 cherokee_buffer_t domu = CHEROKEE_BUF_INIT;
884 if (hdl->action > xml && HDL_VIRT_PROPS(hdl)->xsl.len > 0)
885 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);
888 /* First, block the event loop */
889 avahi_threaded_poll_lock(HDL_AVAHI_PROPS(hdl)->threaded_poll);
890 switch (hdl->action) {
891 case showall:
892 cherokee_buffer_add_str (&hdl->buffer, "<domains>");
893 cherokee_avl_while(&HDL_AVAHI_PROPS(hdl)->entries, virt_while, hdl, NULL, NULL);
894 cherokee_buffer_add_str (&hdl->buffer, "</domains>");
895 ret = ret_ok;
896 break;
898 case showuservms: {
899 void *param[3];
900 cherokee_buffer_add_str (&hdl->buffer, "<domains>");
901 cherokee_avl_while(&HDL_AVAHI_PROPS(hdl)->entries, virt_while_user, hdl, NULL, NULL);
902 cherokee_buffer_add_str (&hdl->buffer, "</domains>");
903 ret = ret_ok;
904 break;
907 default: {
908 cherokee_buffer_t *hostname = NULL;
909 cherokee_buffer_add_va (&domu, "%s_%s", hdl->user.buf, hdl->vm.buf);
910 ret = cherokee_avl_get_ptr(&HDL_AVAHI_PROPS(hdl)->entries, domu.buf, (void **) &hostname);
912 if (ret == ret_not_found) {
913 virDomainPtr virDom;
914 virConnectPtr virConn;
915 cherokee_buffer_add_va (&uri, "xen://%s/", HDL_VIRT_PROPS(hdl)->virt); // TODO: change!
917 if (HDL_VIRT_PROPS(hdl)->read_only == FALSE)
918 virConn = virConnectOpen (uri.buf);
920 if (!virConn && !(virConn = virConnectOpenReadOnly (uri.buf))) {
921 conn->error_code = http_service_unavailable;
922 return ret_error;
925 if ((virDom = virDomainLookupByName(virConn, domu.buf)) == NULL) {
926 if (hdl->action != domainDefineXML_args && hdl->action != domainDefineXML) {
927 hdl->action = nothing;
928 conn->error_code = http_not_found;
929 ret = ret_error;
930 } else {
931 ret = ret_ok;
933 } else {
934 virDomainFree(virDom);
935 ret = ret_ok;
937 virConnectClose (virConn);
938 } else if (ret == ret_ok) {
939 cherokee_buffer_add_va (&uri, "xen://%s/", hostname->buf);
940 printf("%s\n", uri.buf);
941 } else {
942 hdl->action = http_internal_error;
943 hdl->action = nothing;
944 ret = ret_error;
949 /* Finally, unblock the event loop */
950 avahi_threaded_poll_unlock(HDL_AVAHI_PROPS(hdl)->threaded_poll);
952 if (ret == ret_ok && hdl->action != showall && hdl->action != showuservms) {
953 ret = virt_virt_do(hdl, &domu, &uri);
956 cherokee_buffer_mrproper(&domu);
957 cherokee_buffer_mrproper(&uri);
960 return ret;
964 ret_t
965 cherokee_handler_virt_new (cherokee_handler_t **hdl, cherokee_connection_t *cnt, cherokee_module_props_t *props)
967 ret_t ret;
968 CHEROKEE_NEW_STRUCT (n, handler_virt);
970 /* Init the base class
973 cherokee_handler_init_base(HANDLER(n), cnt, HANDLER_PROPS(props), PLUGIN_INFO_HANDLER_PTR(virt));
975 MODULE(n)->init = (handler_func_init_t) cherokee_handler_virt_init;
976 MODULE(n)->free = (module_func_free_t) cherokee_handler_virt_free;
977 HANDLER(n)->step = (handler_func_step_t) cherokee_handler_virt_step;
978 HANDLER(n)->add_headers = (handler_func_add_headers_t) cherokee_handler_virt_add_headers;
980 HANDLER(n)->support = hsupport_length | hsupport_range;
982 ret = cherokee_buffer_init (&n->buffer);
983 if (unlikely(ret != ret_ok))
984 return ret;
986 ret = cherokee_buffer_ensure_size (&n->buffer, 4*1024);
987 if (unlikely(ret != ret_ok))
988 return ret;
990 *hdl = HANDLER(n);
992 return ret_ok;