summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Thursfield <sam.thursfield@codethink.co.uk>2014-04-15 12:08:28 +0300
committerSam Thursfield <sam.thursfield@codethink.co.uk>2014-04-15 12:08:28 +0300
commita2aa9dfdc193316ca45d3d69afc423140cec544a (patch)
tree1c4d520589ec1c98922c31159375ab6bae797c9d
parent78b46a60a5d482f0c302c83b200157cdb25730ca (diff)
parent4e0b47cc3035ef4691b6e902d27fac74c4a51edd (diff)
downloadmorph-a2aa9dfdc193316ca45d3d69afc423140cec544a.tar.gz
Merge branch 'baserock/sam/deploy-kvm-check'
Reviewed-By: Lars Wirzenius <lars.wirzenius@codethink.co.uk>
-rwxr-xr-xmorphlib/exts/kvm.check47
-rwxr-xr-xmorphlib/exts/kvm.write9
-rwxr-xr-xmorphlib/exts/nfsboot.check7
-rw-r--r--morphlib/writeexts.py9
4 files changed, 60 insertions, 12 deletions
diff --git a/morphlib/exts/kvm.check b/morphlib/exts/kvm.check
index be7c51c2..957d0893 100755
--- a/morphlib/exts/kvm.check
+++ b/morphlib/exts/kvm.check
@@ -17,11 +17,16 @@
'''Preparatory checks for Morph 'kvm' write extension'''
import cliapp
+import re
+import urlparse
import morphlib.writeexts
class KvmPlusSshCheckExtension(morphlib.writeexts.WriteExtension):
+
+ location_pattern = '^/(?P<guest>[^/]+)(?P<path>/.+)$'
+
def process_args(self, args):
if len(args) != 1:
raise cliapp.AppException('Wrong number of command line args')
@@ -32,4 +37,46 @@ class KvmPlusSshCheckExtension(morphlib.writeexts.WriteExtension):
'Use the `ssh-rsync` write extension to deploy upgrades to an '
'existing remote system.')
+ location = args[0]
+ ssh_host, vm_name, vm_path = self.check_and_parse_location(location)
+
+ self.check_ssh_connectivity(ssh_host)
+ self.check_no_existing_libvirt_vm(ssh_host, vm_name)
+ self.check_extra_disks_exist(ssh_host, self.parse_attach_disks())
+
+ def check_and_parse_location(self, location):
+ '''Check and parse the location argument to get relevant data.'''
+
+ x = urlparse.urlparse(location)
+
+ if x.scheme != 'kvm+ssh':
+ raise cliapp.AppException(
+ 'URL schema must be kvm+ssh in %s' % location)
+
+ m = re.match(self.location_pattern, x.path)
+ if not m:
+ raise cliapp.AppException('Cannot parse location %s' % location)
+
+ return x.netloc, m.group('guest'), m.group('path')
+
+ def check_no_existing_libvirt_vm(self, ssh_host, vm_name):
+ try:
+ cliapp.ssh_runcmd(ssh_host,
+ ['virsh', '--connect', 'qemu:///system', 'domstate', vm_name])
+ except cliapp.AppException as e:
+ pass
+ else:
+ raise cliapp.AppException(
+ 'Host %s already has a VM named %s. You can use the ssh-rsync '
+ 'write extension to deploy upgrades to existing machines.' %
+ (ssh_host, vm_name))
+
+ def check_extra_disks_exist(self, ssh_host, filename_list):
+ for filename in filename_list:
+ try:
+ cliapp.ssh_runcmd(ssh_host, ['ls', filename])
+ except cliapp.AppException as e:
+ raise cliapp.AppException('Did not find file %s on host %s' %
+ (filename, ssh_host))
+
KvmPlusSshCheckExtension().run()
diff --git a/morphlib/exts/kvm.write b/morphlib/exts/kvm.write
index 94560972..94a55daa 100755
--- a/morphlib/exts/kvm.write
+++ b/morphlib/exts/kvm.write
@@ -50,6 +50,8 @@ class KvmPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
'''
+ location_pattern = '^/(?P<guest>[^/]+)(?P<path>/.+)$'
+
def process_args(self, args):
if len(args) != 2:
raise cliapp.AppException('Wrong number of command line args')
@@ -79,14 +81,9 @@ class KvmPlusSshWriteExtension(morphlib.writeexts.WriteExtension):
def parse_location(self, location):
'''Parse the location argument to get relevant data.'''
-
+
x = urlparse.urlparse(location)
- if x.scheme != 'kvm+ssh':
- raise cliapp.AppException(
- 'URL schema must be vbox+ssh in %s' % location)
m = re.match('^/(?P<guest>[^/]+)(?P<path>/.+)$', x.path)
- if not m:
- raise cliapp.AppException('Cannot parse location %s' % location)
return x.netloc, m.group('guest'), m.group('path')
def transfer(self, raw_disk, ssh_host, vm_path):
diff --git a/morphlib/exts/nfsboot.check b/morphlib/exts/nfsboot.check
index f84f187f..806e560a 100755
--- a/morphlib/exts/nfsboot.check
+++ b/morphlib/exts/nfsboot.check
@@ -56,12 +56,7 @@ class NFSBootCheckExtension(morphlib.writeexts.WriteExtension):
version_label, location))
def test_good_server(self, server):
- # Can be ssh'ed into
- try:
- cliapp.ssh_runcmd('root@%s' % server, ['true'])
- except cliapp.AppException:
- raise cliapp.AppException('You are unable to ssh into server %s'
- % server)
+ self.check_ssh_connectivity(server)
# Is an NFS server
try:
diff --git a/morphlib/writeexts.py b/morphlib/writeexts.py
index bff21e8d..3f9c33d5 100644
--- a/morphlib/writeexts.py
+++ b/morphlib/writeexts.py
@@ -15,6 +15,7 @@
import cliapp
+import logging
import os
import re
import shutil
@@ -417,3 +418,11 @@ class WriteExtension(cliapp.Application):
else:
raise cliapp.AppException('Unexpected value for %s: %s' %
(variable, value))
+
+ def check_ssh_connectivity(self, ssh_host):
+ try:
+ cliapp.ssh_runcmd(ssh_host, ['true'])
+ except cliapp.AppException as e:
+ logging.error("Error checking SSH connectivity: %s", str(e))
+ raise cliapp.AppException(
+ 'Unable to SSH to %s: %s' % (ssh_host, e))