Mercurial > hg > Applications > virsh-wrapper
annotate newvm.py @ 21:13646e64d98b
fix indent
author | taiki <taiki@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 05 Apr 2014 04:15:57 +0900 |
parents | 07ef2fc111a2 |
children | daa7279b0d78 |
rev | line source |
---|---|
3 | 1 #!/usr/bin/python |
2 # Used to create a vm from template | |
3 # By Curu Wong contact: prinbra(at)gmail(dot)com | |
4 import sys,os | |
5 from optparse import OptionParser | |
6 from virtinst.util import * | |
7 if sys.version_info < (2,5): | |
8 import lxml.etree as ET | |
9 else: | |
10 import xml.etree.ElementTree as ET | |
11 | |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
12 # vm_name should be |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
13 # students/e10/e105730/01 |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
14 # teachers/kono/01 |
10 | 15 # bad name returns 1 |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
16 def ie_check_name(name): |
20 | 17 m=re.match('^students/e(\d\d)/e(\d\d)5\d\d\d/0[1-4]$',name) |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
18 if m is not None: |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
19 if m.group(1)==m.group(2): |
7 | 20 return 0 |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
21 else: |
7 | 22 return 1 |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
23 elif re.match('^teachers/[-a-z0-9]+/0[1-4]$',name): |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
24 return 0 |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
25 else: |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
26 return 1 |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
27 |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
28 # make necessary sub directory |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
29 # /etc/libvirt/qemu/teachers |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
30 # /var/log/libvirt/qemu/teachers |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
31 # /var/run/libvirt/qemu/teachers |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
32 |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
33 def ie_mkdir1(name): |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
34 if not os.path.exists(name): |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
35 os.makedirs(name); |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
36 |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
37 def ie_mkdir(name): |
20 | 38 m=re.match('^(students/e\d\d/e\d\d5\d\d\d)/0[1-4]$',name) |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
39 if m is None: |
7 | 40 m=re.match('^(teachers/[-a-z0-9]+)/0[1-4]$',name) |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
41 if m is not None: |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
42 dir=m.group(1) |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
43 ie_mkdir1('/media/fcs/'+dir) |
19 | 44 ie_mkdir1('/usr/local/etc/libvirt/qemu/'+dir) |
45 ie_mkdir1('/usr/local/var/log/libvirt/qemu/'+dir) | |
46 ie_mkdir1('/usr/local/var/run/libvirt/qemu/'+dir) | |
47 ie_mkdir1('/usr/local/var/lib/libvirt/qemu/'+dir) | |
9 | 48 os.system("/bin/chown "+os.getlogin()+" /media/fcs/"+dir) |
19 | 49 |
3 | 50 parser = OptionParser(); |
51 parser.add_option("-n", "--name", dest="name", | |
52 help="VM name"); | |
53 parser.add_option("-c", "--config", dest="config", | |
54 help="Template VM XML config file"); | |
55 | |
56 (options, args) = parser.parse_args(); | |
57 | |
58 if not options.name or not options.config: | |
59 print "Usage %s -n name -c template_xml" % sys.argv[0] | |
60 sys.exit(1) | |
61 | |
62 config = ET.parse(options.config) | |
63 vm_name = options.name | |
19 | 64 print(options.config) |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
65 if ie_check_name(vm_name): |
7 | 66 print "Bad vmname %s. Try students/e11/e115711/01 or teachers/kono/02" % vm_name |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
67 sys.exit(1) |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
68 ie_mkdir(vm_name) |
3 | 69 name = config.find('name') |
70 name.text = vm_name | |
71 uuid = config.find('uuid') | |
72 uuid.text = uuidToString(randomUUID()) | |
73 mac = config.find('devices/interface/mac') | |
74 mac.attrib['address'] = randomMAC(type='qemu') | |
75 disk = config.find('devices/disk/source') | |
76 disk_old = disk.attrib['file'] | |
77 disk_path = os.path.dirname(disk_old) | |
78 disk_ext = os.path.splitext(disk_old)[1] | |
79 disk_image = disk_path + '/' + vm_name + disk_ext | |
80 disk.attrib['file'] = disk_image | |
81 | |
82 if os.path.exists(vm_name + '.xml'): | |
83 print "File %s.xml exists, abort" % vm_name | |
84 sys.exit(1) | |
19 | 85 config.write('/usr/local/etc/libvirt/qemu/' + vm_name + '.xml') |
3 | 86 print "Created vm config file %s.xml" % vm_name |
87 print "Use disk image %s, you must create it from the template disk: %s" % (disk_image, disk_old) | |
88 print "Done!" |