KVM SR: Factor runner templates.

Doing fast&dirty code is nice, but only if you don't maintain the code yourself.
parent aeb5df40
...@@ -64,24 +64,13 @@ class Recipe(GenericBaseRecipe): ...@@ -64,24 +64,13 @@ class Recipe(GenericBaseRecipe):
vnc_passwd=self.options['passwd'], vnc_passwd=self.options['passwd'],
default_disk_image=self.options['default-disk-image'], default_disk_image=self.options['default-disk-image'],
virtual_hard_drive_url=self.options['virtual-hard-drive-url'], virtual_hard_drive_url=self.options['virtual-hard-drive-url'],
use_tap=self.options['use-tap'],
nat_rules = self.options.get('nat-rules', ''),
) )
path_list = [] path_list = []
use_tap = self.options.get('use-tap') if not self.isTrueValue(self.options.get('use-tap')):
if self.isTrueValue(use_tap):
runner_path = self.createExecutable(
self.options['runner-path'],
self.substituteTemplate(self.getTemplateFilename('kvm_run.in'),
config))
path_list.append(runner_path)
else:
config['nat_rules'] = self.options['nat-rules']
runner_path = self.createExecutable(
self.options['runner-path'],
self.substituteTemplate(self.getTemplateFilename('kvm_run_nat.in'),
config))
path_list.append(runner_path)
# XXX This could be done using Jinja. # XXX This could be done using Jinja.
for port in self.options['nat-rules'].split(): for port in self.options['nat-rules'].split():
ipv6_port = int(port) + 10000 ipv6_port = int(port) + 10000
...@@ -101,6 +90,13 @@ class Recipe(GenericBaseRecipe): ...@@ -101,6 +90,13 @@ class Recipe(GenericBaseRecipe):
) )
path_list.append(tunnel_path) path_list.append(tunnel_path)
runner_path = self.createExecutable(
self.options['runner-path'],
self.substituteTemplate(self.getTemplateFilename('kvm_run.in'),
config))
path_list.append(runner_path)
controller_path = self.createExecutable( controller_path = self.createExecutable(
self.options['controller-path'], self.options['controller-path'],
self.substituteTemplate(self.getTemplateFilename('kvm_controller_run.in'), self.substituteTemplate(self.getTemplateFilename('kvm_controller_run.in'),
......
...@@ -42,6 +42,15 @@ default_disk_image = '%(default_disk_image)s' ...@@ -42,6 +42,15 @@ default_disk_image = '%(default_disk_image)s'
disk_path = '%(disk_path)s' disk_path = '%(disk_path)s'
virtual_hard_drive_url = '%(virtual_hard_drive_url)s'.strip() virtual_hard_drive_url = '%(virtual_hard_drive_url)s'.strip()
virtual_hard_drive_md5_url = '%(virtual_hard_drive_md5_url)s'.strip() virtual_hard_drive_md5_url = '%(virtual_hard_drive_md5_url)s'.strip()
nat_rules = '%(nat_rules)s'.strip()
use_tap = '%(use_tap)s'
# Generate NAT rules, if any
# XXX: use_tap should be a boolean
if use_tap == 'True':
qemu_network_parameter = 'user,' + ','.join('hostfwd=tcp:%(vnc_ip)s:%%s-:%%s' %% (int(port) + 10000, port) for port in nat_rules.split())
else:
qemu_network_parameter = 'tap,ifname=%(tap_interface)s,script=no,downscript=no'
# Download existing hard drive if needed at first boot # Download existing hard drive if needed at first boot
if not os.path.exists(disk_path) and virtual_hard_drive_url != '': if not os.path.exists(disk_path) and virtual_hard_drive_url != '':
...@@ -60,7 +69,7 @@ if not os.path.exists(disk_path): ...@@ -60,7 +69,7 @@ if not os.path.exists(disk_path):
kvm_argument_list = ['%(qemu_path)s', kvm_argument_list = ['%(qemu_path)s',
'-enable-kvm', '-net', 'nic,macaddr=%(mac_address)s', '-enable-kvm', '-net', 'nic,macaddr=%(mac_address)s',
'-net', 'tap,ifname=%(tap_interface)s,script=no,downscript=no', '-net', qemu_network_parameter,
'-smp', '%(smp_count)s', '-smp', '%(smp_count)s',
'-m', '%(ram_size)s', '-m', '%(ram_size)s',
'-drive', 'file=%(disk_path)s,if=%(disk_type)s', '-drive', 'file=%(disk_path)s,if=%(disk_type)s',
......
#!%(python_path)s
# BEWARE: This file is operated by slapgrid
# BEWARE: It will be overwritten automatically
# Echo client program
import os
import socket
import subprocess
import urllib
def getSocketStatus(host, port):
s = None
for res in socket.getaddrinfo(host, port,
socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error, msg:
s = None
continue
try:
s.connect(sa)
except socket.error, msg:
s.close()
s = None
continue
break
return s
# XXX: give all of this through parameter, don't use this as template
default_disk_image = '%(default_disk_image)s'
virtual_hard_drive_url = '%(virtual_hard_drive_url)s'
disk_path = '%(disk_path)s'
# Download existing hard drive if needed
if virtual_hard_drive_url != '':
urllib.urlretrieve(virtual_hard_drive_url, disk_path)
# create disk if doesn't exist
if not os.path.exists(disk_path):
subprocess.Popen(['%(qemu_img_path)s', 'create' ,'-f', 'qcow2',
disk_path, '%(disk_size)sG'])
# Generate NAT rules
nat_rules = '%(nat_rules)s'
nat_rules = ','.join('hostfwd=tcp:%(vnc_ip)s:%%s-:%%s' %% (int(port) + 10000, port) for port in nat_rules.split())
kvm_argument_list = ['%(qemu_path)s',
'-enable-kvm', '-net', 'nic,macaddr=%(mac_address)s',
'-net', 'user,%%s' %% nat_rules,
'-smp', '%(smp_count)s',
'-m', '%(ram_size)s',
'-drive', 'file=%(disk_path)s,if=%(disk_type)s',
'-vnc', '%(vnc_ip)s:1,ipv4,password',
'-boot', 'menu=on',
'-qmp', 'unix:%(socket_path)s,server',
'-pidfile', '%(pid_file_path)s',
]
# Try to connect to NBD server (and second nbd if defined)
for nbd_ip, nbd_port in (
('%(nbd_ip)s', %(nbd_port)s), ('%(nbd2_ip)s', %(nbd2_port)s)):
if nbd_ip and nbd_port:
s = getSocketStatus(nbd_ip, nbd_port)
if s is None:
# NBD is not available : launch kvm without it
print 'Warning : Nbd is not available.'
else:
# NBD is available
kvm_argument_list.extend([
'-drive',
'file=nbd:[%%s]:%%s,media=cdrom' %% (nbd_ip, nbd_port)])
# If no NBD is specified/available: use internal disk image
else:
kvm_argument_list.extend([
'-drive', 'file=%%s,media=cdrom' %% default_disk_image
])
os.execv('%(qemu_path)s', kvm_argument_list)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment