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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #!/usr/bin/python
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 # Used to create a vm from template
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 # By Curu Wong contact: prinbra(at)gmail(dot)com
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 import sys,os
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 from optparse import OptionParser
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 from virtinst.util import *
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 if sys.version_info < (2,5):
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 import lxml.etree as ET
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 else:
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 import xml.etree.ElementTree as ET
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
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
0740e88a783b fix pattern match
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 9
diff changeset
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
07ef2fc111a2 allow other faculties' students.
k128568
parents: 19
diff changeset
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
bff4054ed58f looks like working...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
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
bff4054ed58f looks like working...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
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
07ef2fc111a2 allow other faculties' students.
k128568
parents: 19
diff changeset
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
bff4054ed58f looks like working...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
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
7b4cdb0c9a99 unknown changed
taiki
parents: 10
diff changeset
44 ie_mkdir1('/usr/local/etc/libvirt/qemu/'+dir)
7b4cdb0c9a99 unknown changed
taiki
parents: 10
diff changeset
45 ie_mkdir1('/usr/local/var/log/libvirt/qemu/'+dir)
7b4cdb0c9a99 unknown changed
taiki
parents: 10
diff changeset
46 ie_mkdir1('/usr/local/var/run/libvirt/qemu/'+dir)
7b4cdb0c9a99 unknown changed
taiki
parents: 10
diff changeset
47 ie_mkdir1('/usr/local/var/lib/libvirt/qemu/'+dir)
9
ea51e779b381 error message
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 8
diff changeset
48 os.system("/bin/chown "+os.getlogin()+" /media/fcs/"+dir)
19
7b4cdb0c9a99 unknown changed
taiki
parents: 10
diff changeset
49
3
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 parser = OptionParser();
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 parser.add_option("-n", "--name", dest="name",
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 help="VM name");
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 parser.add_option("-c", "--config", dest="config",
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 help="Template VM XML config file");
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 (options, args) = parser.parse_args();
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 if not options.name or not options.config:
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 print "Usage %s -n name -c template_xml" % sys.argv[0]
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 sys.exit(1)
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 config = ET.parse(options.config)
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 vm_name = options.name
19
7b4cdb0c9a99 unknown changed
taiki
parents: 10
diff changeset
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
bff4054ed58f looks like working...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
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
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 name = config.find('name')
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 name.text = vm_name
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 uuid = config.find('uuid')
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 uuid.text = uuidToString(randomUUID())
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 mac = config.find('devices/interface/mac')
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 mac.attrib['address'] = randomMAC(type='qemu')
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 disk = config.find('devices/disk/source')
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 disk_old = disk.attrib['file']
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 disk_path = os.path.dirname(disk_old)
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 disk_ext = os.path.splitext(disk_old)[1]
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 disk_image = disk_path + '/' + vm_name + disk_ext
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 disk.attrib['file'] = disk_image
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 if os.path.exists(vm_name + '.xml'):
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 print "File %s.xml exists, abort" % vm_name
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 sys.exit(1)
19
7b4cdb0c9a99 unknown changed
taiki
parents: 10
diff changeset
85 config.write('/usr/local/etc/libvirt/qemu/' + vm_name + '.xml')
3
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 print "Created vm config file %s.xml" % vm_name
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 print "Use disk image %s, you must create it from the template disk: %s" % (disk_image, disk_old)
203445eb53a8 add xml generator.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 print "Done!"