Mercurial > hg > Applications > virsh-wrapper
changeset 22:daa7279b0d78
fix indent
author | taiki |
---|---|
date | Sat, 12 Apr 2014 13:28:06 -1000 |
parents | 13646e64d98b |
children | 66a88f51993f |
files | ie-virsh.c newvm.py |
diffstat | 2 files changed, 48 insertions(+), 38 deletions(-) [+] |
line wrap: on
line diff
--- a/ie-virsh.c Sat Apr 05 04:15:57 2014 +0900 +++ b/ie-virsh.c Sat Apr 12 13:28:06 2014 -1000 @@ -32,6 +32,7 @@ #define define_command "define" #define undefine_command "undefine" #define dumpxml_command "dumpxml" +#define console_command "console" static char bad_name[] = "Bad vmname. Try students/e11/e115711/01 or teachers/kono/02\n"; @@ -46,7 +47,7 @@ /* Define global variables */ - VMLISTPTR +VMLISTPTR get_vmlist(regex_t *list_pattern) { VMLISTPTR list = NEW(VMLIST); @@ -64,7 +65,7 @@ return list; } - void +void print_vmlist(VMLISTPTR list) { for(;list && list->name[0]; list=list->next) { @@ -72,7 +73,7 @@ } } - int +int check_vmlist_name(VMLISTPTR list, char *arg) { for(;list && list->name[0]; list=list->next) { @@ -81,7 +82,7 @@ return 0; } - int +int check_name(const char *p) { if (!p) return 1; @@ -98,10 +99,10 @@ return 0; } - void +void usage() { - printf("Usage: COMMAND [define|list|start|destroy|xmldump|undefine] [vm-name]\n"); + printf("Usage: COMMAND [define|list|start|destroy|xmldump|undefine|console] [vm-name]\n"); printf(" vm-name should be students/e11/e115711/01 or teachers/kono/02\n"); } @@ -185,26 +186,30 @@ * the corresponding script, if invoked. */ - if ( argv[1]==0 || strncmp(argv[1], "list", 4) == 0 ) { + if (argv[1]==0 || strncmp(argv[1], "list", 4) == 0 ) { print_vmlist(vmlist); - } else if ( strncmp(argv[1], "start", 5) == 0 ) { + } else if (strncmp(argv[1], start_command, 5) == 0) { if (execl(command, command, start_command, argv[2], NULL) < 0) { perror("Execl:"); } - } else if ( strncmp(argv[1], "destroy", 4) == 0 ) { + } else if ( strncmp(argv[1], stop_command, 4) == 0 ) { if (execl(command, command, stop_command, argv[2], NULL) < 0) { perror("Execl:"); } - } else if ( strncmp(argv[1], "dumpxml", 7) == 0 ) { + } else if ( strncmp(argv[1], dumpxml_command, 7) == 0 ) { if (execl(command, command, dumpxml_command, argv[2], NULL) < 0) { perror("Execl:"); } - } else if ( strncmp(argv[1], "define", 6) == 0 ) { + } else if ( strncmp(argv[1], console_command, 8) == 0 ) { + if (execl(command, command, console_command, argv[2], NULL) < 0) { + perror("Execl:"); + } + } else if (strncmp(argv[1], define_command, 6) == 0 ) { chdir("/usr/local/etc/libvirt/qemu"); if (execl(command, command, define_command, name_xml, NULL) < 0) { perror("Execl:"); } - } else if ( strncmp(argv[1], "undefine", 8) == 0 ) { + } else if ( strncmp(argv[1], undefine_command, 8) == 0 ) { chdir("/usr/local/etc/libvirt/qemu"); if (execl(command, command, undefine_command, argv[2], NULL) < 0) { perror("Execl:");
--- a/newvm.py Sat Apr 05 04:15:57 2014 +0900 +++ b/newvm.py Sat Apr 12 13:28:06 2014 -1000 @@ -5,25 +5,27 @@ from optparse import OptionParser from virtinst.util import * if sys.version_info < (2,5): - import lxml.etree as ET + import lxml.etree as ET else: - import xml.etree.ElementTree as ET - + import xml.etree.ElementTree as ET + # vm_name should be # students/e10/e105730/01 # teachers/kono/01 # bad name returns 1 def ie_check_name(name): - m=re.match('^students/e(\d\d)/e(\d\d)5\d\d\d/0[1-4]$',name) - if m is not None: - if m.group(1)==m.group(2): - return 0 - else: - return 1 - elif re.match('^teachers/[-a-z0-9]+/0[1-4]$',name): - return 0 - else: - return 1 + m=re.match('^students/e(\d\d)/e(\d\d)5\d\d\d/0[1-4]$',name) + if m is not None: + if m.group(1)==m.group(2): + return 0 + else: + return 1 + elif re.match('^teachers/[-a-z0-9]+/0[1-4]$',name): + return 0 + elif re.match('^test/[-a-z0-9]+/0[1-4]$',name): + return 0 + else: + return 1 # make necessary sub directory # /etc/libvirt/qemu/teachers @@ -31,14 +33,16 @@ # /var/run/libvirt/qemu/teachers def ie_mkdir1(name): - if not os.path.exists(name): + if not os.path.exists(name): os.makedirs(name); def ie_mkdir(name): - m=re.match('^(students/e\d\d/e\d\d5\d\d\d)/0[1-4]$',name) - if m is None: - m=re.match('^(teachers/[-a-z0-9]+)/0[1-4]$',name) - if m is not None: + m=re.match('^(students/e\d\d/e\d\d5\d\d\d)/0[1-4]$',name) + if m is None: + m=re.match('^(teachers/[-a-z0-9]+)/0[1-4]$',name) + if m is None: + m=re.match('^(test/[-a-z0-9]+)/0[1-4]$',name) + if m is not None: dir=m.group(1) ie_mkdir1('/media/fcs/'+dir) ie_mkdir1('/usr/local/etc/libvirt/qemu/'+dir) @@ -52,19 +56,20 @@ help="VM name"); parser.add_option("-c", "--config", dest="config", help="Template VM XML config file"); - + (options, args) = parser.parse_args(); - + if not options.name or not options.config: - print "Usage %s -n name -c template_xml" % sys.argv[0] - sys.exit(1) - + print "Usage %s -n name -c template_xml" % sys.argv[0] + sys.exit(1) + config = ET.parse(options.config) vm_name = options.name print(options.config) if ie_check_name(vm_name): print "Bad vmname %s. Try students/e11/e115711/01 or teachers/kono/02" % vm_name sys.exit(1) +print "== ie-mkdir == " ie_mkdir(vm_name) name = config.find('name') name.text = vm_name @@ -78,10 +83,10 @@ disk_ext = os.path.splitext(disk_old)[1] disk_image = disk_path + '/' + vm_name + disk_ext disk.attrib['file'] = disk_image - + if os.path.exists(vm_name + '.xml'): - print "File %s.xml exists, abort" % vm_name - sys.exit(1) + print "File %s.xml exists, abort" % vm_name + sys.exit(1) config.write('/usr/local/etc/libvirt/qemu/' + vm_name + '.xml') print "Created vm config file %s.xml" % vm_name print "Use disk image %s, you must create it from the template disk: %s" % (disk_image, disk_old)