summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2013-05-13 10:00:20 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2013-05-13 10:00:20 +0000
commit3cb8c3a4c1c6853f51a3c5cad411673d42d8f6b8 (patch)
tree1c40cc932c088ec2c45236d84064518929c042e6
parent9898c112dfced5bd5fe3a56aab64a7a0163ebe16 (diff)
parent1b2f2013bb85769352e82cfd55a3179fe16ccce2 (diff)
downloadmorph-3cb8c3a4c1c6853f51a3c5cad411673d42d8f6b8.tar.gz
Merge branch 'baserock/richardmaw/kvm-write-allow-autostart' of git://git.baserock.org/baserock/baserock/morph
Reviewed-by: Jonathan Maw on IRC.
-rwxr-xr-xmorphlib/exts/kvm.write19
-rwxr-xr-xmorphlib/exts/virtualbox-ssh.write9
-rwxr-xr-xmorphlib/writeexts.py12
3 files changed, 29 insertions, 11 deletions
diff --git a/morphlib/exts/kvm.write b/morphlib/exts/kvm.write
index 630f6ae7..e2f7435c 100755
--- a/morphlib/exts/kvm.write
+++ b/morphlib/exts/kvm.write
@@ -56,6 +56,7 @@ class KvmPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
temp_root, location = args
ssh_host, vm_name, vm_path = self.parse_location(location)
+ autostart = self.parse_autostart()
fd, raw_disk = tempfile.mkstemp()
os.close(fd)
@@ -63,7 +64,7 @@ class KvmPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
try:
self.transfer(raw_disk, ssh_host, vm_path)
- self.create_libvirt_guest(ssh_host, vm_name, vm_path)
+ self.create_libvirt_guest(ssh_host, vm_name, vm_path, autostart)
except BaseException:
sys.stderr.write('Error deploying to libvirt')
os.remove(raw_disk)
@@ -95,7 +96,7 @@ class KvmPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
with open(raw_disk, 'rb') as f:
cliapp.runcmd(['rsync', '-zS', raw_disk, target])
- def create_libvirt_guest(self, ssh_host, vm_name, vm_path):
+ def create_libvirt_guest(self, ssh_host, vm_name, vm_path, autostart):
'''Create the libvirt virtual machine.'''
self.status(msg='Creating libvirt/kvm virtual machine')
@@ -107,13 +108,13 @@ class KvmPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
ram_mebibytes = str(self.get_ram_size() / (1024**2))
- cliapp.runcmd(
- ['ssh', ssh_host,
- 'virt-install', '--connect qemu:///system', '--import',
- '--name', vm_name, '--vnc', '--noreboot',
- '--ram=%s' % ram_mebibytes,
- '--disk path=%s,bus=ide' % vm_path] +
- attach_opts)
+ cmdline = ['ssh', ssh_host,
+ 'virt-install', '--connect qemu:///system', '--import',
+ '--name', vm_name, '--vnc', '--ram=%s' % ram_mebibytes,
+ '--disk path=%s,bus=ide' % vm_path] + attach_opts
+ if not autostart:
+ cmdline += '--noreboot'
+ cliapp.runcmd(cmdline)
KvmPlusSshWriteExtension().run()
diff --git a/morphlib/exts/virtualbox-ssh.write b/morphlib/exts/virtualbox-ssh.write
index 37f56524..cb17b69b 100755
--- a/morphlib/exts/virtualbox-ssh.write
+++ b/morphlib/exts/virtualbox-ssh.write
@@ -62,6 +62,7 @@ class VirtualBoxPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
temp_root, location = args
ssh_host, vm_name, vdi_path = self.parse_location(location)
+ autostart = self.parse_autostart()
fd, raw_disk = tempfile.mkstemp()
os.close(fd)
@@ -70,7 +71,8 @@ class VirtualBoxPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
try:
self.transfer_and_convert_to_vdi(
raw_disk, ssh_host, vdi_path)
- self.create_virtualbox_guest(ssh_host, vm_name, vdi_path)
+ self.create_virtualbox_guest(ssh_host, vm_name, vdi_path,
+ autostart)
except BaseException:
sys.stderr.write('Error deploying to VirtualBox')
os.remove(raw_disk)
@@ -105,7 +107,7 @@ class VirtualBoxPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
str(os.path.getsize(raw_disk))],
stdin=f)
- def create_virtualbox_guest(self, ssh_host, vm_name, vdi_path):
+ def create_virtualbox_guest(self, ssh_host, vm_name, vdi_path, autostart):
'''Create the VirtualBox virtual machine.'''
self.status(msg='Create VirtualBox virtual machine')
@@ -134,6 +136,9 @@ class VirtualBoxPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
'--medium', disk]
commands.append(cmd)
+ if autostart:
+ commands.append(['startvm', vm_name])
+
for command in commands:
argv = ['ssh', ssh_host, 'VBoxManage'] + command
cliapp.runcmd(argv)
diff --git a/morphlib/writeexts.py b/morphlib/writeexts.py
index 48847d56..aea08085 100755
--- a/morphlib/writeexts.py
+++ b/morphlib/writeexts.py
@@ -251,3 +251,15 @@ class WriteExtension(cliapp.Application):
value = 'no'
return value == 'yes'
+
+ def parse_autostart(self):
+ '''Parse $AUTOSTART to determine if VMs should be started.'''
+
+ autostart = os.environ.get('AUTOSTART', 'no')
+ if autostart == 'no':
+ return False
+ elif autostart == 'yes':
+ return True
+ else:
+ raise cliapp.AppException('Unexpected value for AUTOSTART: %s' %
+ autostart)