diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-04-01 14:49:41 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-04-01 14:49:41 +0300 |
commit | 95434881e706e29d04afe2d535f4ea14a83ea0f4 (patch) | |
tree | 76ea578f26f0bacdd2966df888214ddd8c3e2382 | |
parent | 786c1c7f5d00dd4bd9541f87358005df066dc7b7 (diff) | |
download | vmdb2-95434881e706e29d04afe2d535f4ea14a83ea0f4.tar.gz |
Add vmdb.runcmd function, make everything use that
This gives us a since place where commands are run, good for logging
and progress updating and such.
-rw-r--r-- | vmdb/__init__.py | 1 | ||||
-rw-r--r-- | vmdb/plugins/chroot_plugin.py | 5 | ||||
-rw-r--r-- | vmdb/plugins/debootstrap_plugin.py | 2 | ||||
-rw-r--r-- | vmdb/plugins/kernel_plugin.py | 2 | ||||
-rw-r--r-- | vmdb/plugins/mkfs_plugin.py | 2 | ||||
-rw-r--r-- | vmdb/plugins/mkimg_plugin.py | 2 | ||||
-rw-r--r-- | vmdb/plugins/mount_plugin.py | 4 | ||||
-rw-r--r-- | vmdb/plugins/partition_plugin.py | 12 | ||||
-rw-r--r-- | vmdb/runcmd.py | 44 | ||||
-rw-r--r-- | without-tests | 1 |
10 files changed, 61 insertions, 14 deletions
diff --git a/vmdb/__init__.py b/vmdb/__init__.py index b55fcdc..e0d06b2 100644 --- a/vmdb/__init__.py +++ b/vmdb/__init__.py @@ -24,4 +24,5 @@ from .step_list import ( NoMatchingRunner, StepError, ) +from .runcmd import runcmd from .app import Vmdb2 diff --git a/vmdb/plugins/chroot_plugin.py b/vmdb/plugins/chroot_plugin.py index f49af59..a1a3a51 100644 --- a/vmdb/plugins/chroot_plugin.py +++ b/vmdb/plugins/chroot_plugin.py @@ -30,7 +30,6 @@ class ChrootPlugin(cliapp.Plugin): def enable(self): self.app.step_runners.add(ChrootStepRunner()) - def enable(self): self.app.step_runners.add(ShellStepRunner()) @@ -47,7 +46,7 @@ class ChrootStepRunner(vmdb.StepRunnerInterface): sys.stdout.write( 'chroot {} to {}\n'.format(mount_point, ' '.join(shell.split('\n')))) - cliapp.runcmd( + vmdb.runcmd( ['chroot', mount_point, 'sh', '-c', shell], stdout=None, stderr=None) @@ -65,6 +64,6 @@ class ShellStepRunner(vmdb.StepRunnerInterface): 'run shell {}\n'.format(' '.join(shell.split('\n')))) env = dict(os.environ) env['ROOT'] = state.mounts[fs_tag] - cliapp.runcmd( + vmdb.runcmd( ['sh', '-c', shell], stdout=None, stderr=None, env=env) diff --git a/vmdb/plugins/debootstrap_plugin.py b/vmdb/plugins/debootstrap_plugin.py index 2d0f988..5a1100b 100644 --- a/vmdb/plugins/debootstrap_plugin.py +++ b/vmdb/plugins/debootstrap_plugin.py @@ -45,4 +45,4 @@ class DebootstrapStepRunner(vmdb.StepRunnerInterface): raise Exception('missing arg for debootstrap step') sys.stdout.write( 'Debootstrap {} {} {}\n'.format(suite, target, mirror)) - cliapp.runcmd(['debootstrap', suite, target, mirror], stdout=None, stderr=None) + vmdb.runcmd(['debootstrap', suite, target, mirror], stdout=None, stderr=None) diff --git a/vmdb/plugins/kernel_plugin.py b/vmdb/plugins/kernel_plugin.py index 0a46d21..249a3b0 100644 --- a/vmdb/plugins/kernel_plugin.py +++ b/vmdb/plugins/kernel_plugin.py @@ -43,7 +43,7 @@ class KernelStepRunner(vmdb.StepRunnerInterface): sys.stdout.write( 'Install {} to filesystem at {} ({})\n'.format( package, mount_point, fstag)) - cliapp.runcmd( + vmdb.runcmd( ['chroot', mount_point, 'apt', '-y', 'install', package], stdout=None, stderr=None) diff --git a/vmdb/plugins/mkfs_plugin.py b/vmdb/plugins/mkfs_plugin.py index 5cd3891..770d258 100644 --- a/vmdb/plugins/mkfs_plugin.py +++ b/vmdb/plugins/mkfs_plugin.py @@ -42,4 +42,4 @@ class MkfsStepRunner(vmdb.StepRunnerInterface): device = state.parts[part_tag] sys.stdout.write( 'Creating {} filesystem on {}\n'.format(fstype, device)) - cliapp.runcmd(['/sbin/mkfs', '-t', fstype, device]) + vmdb.runcmd(['/sbin/mkfs', '-t', fstype, device]) diff --git a/vmdb/plugins/mkimg_plugin.py b/vmdb/plugins/mkimg_plugin.py index 6b7ba9e..27e6262 100644 --- a/vmdb/plugins/mkimg_plugin.py +++ b/vmdb/plugins/mkimg_plugin.py @@ -45,5 +45,5 @@ class MkimgStepRunner(vmdb.StepRunnerInterface): size = step['size'] sys.stdout.write( 'Creating image file {} (size {})\n'.format(filename, size)) - cliapp.runcmd( + vmdb.runcmd( ['qemu-img', 'create', '-f', 'raw', filename, size]) diff --git a/vmdb/plugins/mount_plugin.py b/vmdb/plugins/mount_plugin.py index 3344be9..62698cf 100644 --- a/vmdb/plugins/mount_plugin.py +++ b/vmdb/plugins/mount_plugin.py @@ -52,7 +52,7 @@ class MountStepRunner(vmdb.StepRunnerInterface): sys.stdout.write( 'Mounting {} ({}) on {}\n'.format(device, fs_tag, mount_point)) - cliapp.runcmd(['mount', device, mount_point]) + vmdb.runcmd(['mount', device, mount_point]) state.mounts[fs_tag] = mount_point def teardown(self, step, settings, state): @@ -63,5 +63,5 @@ class MountStepRunner(vmdb.StepRunnerInterface): sys.stdout.write( 'Unmounting {} ({}) from {}\n'.format(mount_point, fs_tag, device)) - cliapp.runcmd(['umount', mount_point]) + vmdb.runcmd(['umount', mount_point]) os.rmdir(mount_point) diff --git a/vmdb/plugins/partition_plugin.py b/vmdb/plugins/partition_plugin.py index f64806c..a76fd39 100644 --- a/vmdb/plugins/partition_plugin.py +++ b/vmdb/plugins/partition_plugin.py @@ -43,7 +43,7 @@ class MklabelStepRunner(vmdb.StepRunnerInterface): device = step['device'] sys.stdout.write( 'Creating partition table ({}) on {}\n'.format(label_type, device)) - cliapp.runcmd(['parted', device, 'mklabel', label_type], stderr=None) + vmdb.runcmd(['parted', device, 'mklabel', label_type]) state.parts = {} @@ -62,16 +62,18 @@ class MkpartStepRunner(vmdb.StepRunnerInterface): sys.stdout.write( 'Creating partition ({}) on {} ({} to {})\n'.format( part_type, device, start, end)) - cliapp.runcmd(['parted', '-s', device, 'mkpart', part_type, start, end]) + vmdb.runcmd(['parted', '-s', device, 'mkpart', part_type, start, end]) - cliapp.runcmd(['kpartx', '-d', device]) - output = cliapp.runcmd(['kpartx', '-asv', device]) + vmdb.runcmd(['kpartx', '-d', device]) + output = vmdb.runcmd(['kpartx', '-asv', device]) + device_file = None for line in output.splitlines(): words = line.split() if words[0] == 'add': name = words[2] device_file = '/dev/mapper/{}'.format(name) + assert device_file is not None parts = getattr(state, 'parts', {}) parts[part_tag] = device_file state.parts = parts @@ -80,4 +82,4 @@ class MkpartStepRunner(vmdb.StepRunnerInterface): device = step['device'] sys.stdout.write( 'Undoing loopback devices for partitions on {}\n'.format(device)) - cliapp.runcmd(['kpartx', '-d', device]) + vmdb.runcmd(['kpartx', '-d', device]) diff --git a/vmdb/runcmd.py b/vmdb/runcmd.py new file mode 100644 index 0000000..9d8cc87 --- /dev/null +++ b/vmdb/runcmd.py @@ -0,0 +1,44 @@ +# Copyright 2017 Lars Wirzenius +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +# =*= License: GPL-3+ =*= + + +import logging +import sys + +import cliapp + + +def runcmd(argv, *argvs, **kwargs): + kwargs['stdout_callback'] = _log_stdout + kwargs['stderr_callback'] = _log_stderr + return cliapp.runcmd(argv, *argvs, **kwargs) + + +def _log_stdout(data): + logging.debug('STDOUT: %r', data) + sys.stdout.write(data) + if not data.endswith('\n'): + sys.stdout.write('\n') + return data + + +def _log_stderr(data): + logging.debug('STDERR: %r', data) + sys.stderr.write(data) + if not data.endswith('\n'): + sys.stderr.write('\n') + return data diff --git a/without-tests b/without-tests index 73892b7..fae93e0 100644 --- a/without-tests +++ b/without-tests @@ -1,6 +1,7 @@ yarns/lib.py vmdb/__init__.py vmdb/app.py +vmdb/runcmd.py vmdb/state.py vmdb/version.py vmdb/plugins/chroot_plugin.py |