-
Notifications
You must be signed in to change notification settings - Fork 0
/
lnscreator.py
executable file
·204 lines (170 loc) · 6.61 KB
/
lnscreator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/python3
import sys, argparse
import libvirt
import csv
import subprocess
import crypt
import os
import string
import tempfile
import shlex
import xml.etree.ElementTree as ET
import threading
#debian7 base
#root pass: lnsexam
#user: testuser
#pass: lnsexam
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
debian_interfaces = """
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address $ip
netmask $netmask
gateway $gateway
dns-nameserver $dns
"""
network_configs = dict()
network_configs['debian7'] = debian_interfaces
network_configs['ubuntu1404'] = debian_interfaces
class Machine():
hostname = None
password = None
username = None
ip = None
netmask = None
gateway = None
dns = None
system = None
disk_path = None
domain = None
interface = ""
memory = 512
net_template = None
base = None
def customize(self):
password_hash = crypt.crypt(self.password)
args = ['virt-customize']
args += ['-a', self.disk_path]
args += ['--delete', '/etc/network/interfaces']
args += ['--hostname', self.hostname]
args += ['--upload', '{}:{}'.format(self.render_interfaces_file(), "/etc/network/interfaces")]
args += ['--run-command', 'useradd -m -s /bin/bash -p {} -U -G sudo {}'.format(shlex.quote(password_hash), self.username)]
args += ['--run-command', 'dpkg-reconfigure openssh-server']
if "debian" in self.hostname:
args += ['--root-password', "password:{}".format(self.password)]
args += ['--delete', '/etc/resolv.conf']
#args += ['--install', 'sudo'] #done in base img
subprocess.check_call(args, stdout=subprocess.DEVNULL)
def install(self, libvirt_url):
args = ['virt-install']
args += ['-w', 'type=direct,source={},model=virtio'.format(self.interface)]
args += ['--name', '{}-{}'.format(self.hostname, self.ip)]
args += ['--memory', str(self.memory)]
args += ['--disk', self.disk_path]
args += ['--os-variant', 'debian7']
args += ['--cpu', 'host']
args += ['--import']
args += ['--connect', libvirt_url]
args += ['--noreboot']
args += ['--noautoconsole']
subprocess.check_call(args, stdout=subprocess.DEVNULL)
def render_interfaces_file(self):
template = string.Template(self.net_template)
output = template.substitute(ip=self.ip, netmask=self.netmask, gateway=self.gateway, dns=self.dns)
f = tempfile.NamedTemporaryFile(mode='w', delete=False)
f.write(output)
path = f.name
f.close()
return path
class Student:
name = None
surname = None
machines = None
def __repr__(self) :
return 'Name: {}'.format(self.name)
def parse_students(path, interface, base_domains):
f = open(path, 'r')
reader = csv.DictReader(f)
data = list()
for row in reader:
student = Student()
student.name = row["Name"]
student.surname = row["Surname"]
machines = list()
for domain in base_domains:
vm = Machine()
vm.username = row["Name"].lower()
vm.ip = row["IP"]
vm.netmask = row["Netmask"]
vm.dns = row["DNS"]
vm.gateway = row["Gateway"]
vm.password = row["Surname"].lower()
vm.base = domain
vm.interface = interface
machines.append(vm)
student.machines = machines
data.append(student)
return data
def create_overlay_image(base_path, overlay_path):
subprocess.check_call(['qemu-img', 'create', '-b', base_path, '-f', 'qcow2', overlay_path], stdout=subprocess.DEVNULL)
def get_disk_from_domain(domain):
root = ET.fromstring(domain.XMLDesc())
source_element = root.find(".//disk[@device='disk']/source")
path = source_element.items()[0][1]
return path
def worker(student, base_images, template, libvirt_url):
print(color.BOLD + color.UNDERLINE + "Student: {} {}".format(student.name, student.surname) + color.END)
for machine in student.machines:
machine.hostname = "{}-{}-{}".format(student.name, student.surname, machine.base)
machine.domain = machine.hostname
base_dir = os.path.split(base_images[machine.base])[0]
new_img_path = os.path.join(base_dir, "{}.qcow2".format(machine.hostname))
print(color.BOLD + "Creating image {}".format(new_img_path) + color.END)
create_overlay_image(base_images[machine.base], new_img_path)
machine.disk_path = new_img_path
machine.net_template = template
print(color.BOLD + "Customizing {}".format(machine.domain) + color.END)
machine.customize()
print(color.BOLD + "Installing {}".format(machine.domain) + color.END)
machine.install(libvirt_url)
print(color.GREEN + "Machines for student {} {} are ready".format(student.name, student.surname) + color.END)
def main(argv):
parser = argparse.ArgumentParser(description="Automatically create exam machines")
parser.add_argument("students", help="CSV file with student and machine info")
parser.add_argument("libvirt_url", help="The libVirt URL where the changes will be made")
parser.add_argument("interface", help="The host interface the domains will use")
parser.add_argument("base_domains", help="List of base domains the script will use", nargs='+')
args = parser.parse_args()
conn = libvirt.open(args.libvirt_url)
print(color.BOLD + "Connected to server: {}".format(conn.getHostname()) + color.END)
base_images = dict()
print(color.GREEN + "List of base images:")
for i in args.base_domains:
domain = conn.lookupByName(i)
path = get_disk_from_domain(domain)
base_images[i] = path
print("{} → {}".format(i,path))
print(color.END)
data = parse_students(args.students, args.interface, args.base_domains)
threads = []
for student in data:
worker(student, base_images, debian_interfaces, args.libvirt_url)
print(color.BOLD + color.UNDERLINE + "Everything went fine. Enjoy the machines" + color.END)
if __name__ == "__main__":
main(sys.argv[1:])