mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-08-24 15:36:27 +00:00
use log_check_call everywhere
This commit is contained in:
parent
0ed091ca1c
commit
a26c83fc93
5 changed files with 40 additions and 44 deletions
|
@ -1,8 +1,18 @@
|
||||||
|
|
||||||
|
|
||||||
def log_command(command, logger):
|
def log_check_call(command, logger):
|
||||||
|
status = log_call(command, logger)
|
||||||
|
if status != 0:
|
||||||
|
from subprocess import CalledProcessError
|
||||||
|
msg = ('Command \'{command}\' returned non-zero exit status '
|
||||||
|
'{status}'.format(command=command, status=status))
|
||||||
|
raise CalledProcessError(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def log_call(command, logger):
|
||||||
import subprocess
|
import subprocess
|
||||||
import select
|
import select
|
||||||
|
|
||||||
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
while True:
|
while True:
|
||||||
reads = [process.stdout.fileno(), process.stderr.fileno()]
|
reads = [process.stdout.fileno(), process.stderr.fileno()]
|
||||||
|
@ -17,7 +27,6 @@ def log_command(command, logger):
|
||||||
if line != '':
|
if line != '':
|
||||||
logger.error(line.strip())
|
logger.error(line.strip())
|
||||||
if process.poll() is not None:
|
if process.poll() is not None:
|
||||||
break
|
|
||||||
return process.returncode
|
return process.returncode
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
from base import Task
|
from base import Task
|
||||||
from common import phases
|
from common import phases
|
||||||
from common.exceptions import TaskError
|
from common.tools import log_check_call
|
||||||
from common.tools import log_command
|
|
||||||
import logging
|
import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -31,8 +30,7 @@ class MakeTarball(Task):
|
||||||
info.tarball = os.path.join(info.manifest.bootstrapper['tarball_dir'], tarball_filename)
|
info.tarball = os.path.join(info.manifest.bootstrapper['tarball_dir'], tarball_filename)
|
||||||
|
|
||||||
command = executable + options + ['--make-tarball=' + info.tarball] + arguments
|
command = executable + options + ['--make-tarball=' + info.tarball] + arguments
|
||||||
if log_command(command, log) != 0:
|
log_check_call(command, log)
|
||||||
raise TaskError('Unable to create bootstrap tarball')
|
|
||||||
|
|
||||||
|
|
||||||
class Bootstrap(Task):
|
class Bootstrap(Task):
|
||||||
|
@ -46,5 +44,4 @@ class Bootstrap(Task):
|
||||||
options.extend(['--unpack-tarball=' + info.tarball])
|
options.extend(['--unpack-tarball=' + info.tarball])
|
||||||
|
|
||||||
command = executable + options + arguments
|
command = executable + options + arguments
|
||||||
if log_command(command, log) != 0:
|
log_check_call(command, log)
|
||||||
raise TaskError('Unable to bootstrap')
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
from base import Task
|
from base import Task
|
||||||
from common import phases
|
from common import phases
|
||||||
from common.exceptions import TaskError
|
from common.tools import log_check_call
|
||||||
from common.tools import log_command
|
|
||||||
import os.path
|
import os.path
|
||||||
import logging
|
import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
@ -18,8 +17,9 @@ class GenerateLocale(Task):
|
||||||
charmap=info.manifest.system['charmap'])
|
charmap=info.manifest.system['charmap'])
|
||||||
search = '# ' + locale_str
|
search = '# ' + locale_str
|
||||||
sed_i(locale_gen, search, locale_str)
|
sed_i(locale_gen, search, locale_str)
|
||||||
if log_command(['chroot', info.root, 'dpkg-reconfigure', '--priority=critical', 'locales'], log) != 0:
|
|
||||||
raise TaskError('Failed to regenerate locales')
|
command = ['chroot', info.root, 'dpkg-reconfigure', '--priority=critical', 'locales']
|
||||||
|
log_check_call(command, log)
|
||||||
|
|
||||||
|
|
||||||
class SetTimezone(Task):
|
class SetTimezone(Task):
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
from base import Task
|
from base import Task
|
||||||
from common import phases
|
from common import phases
|
||||||
from common.exceptions import TaskError
|
from common.exceptions import TaskError
|
||||||
from common.tools import log_command
|
from common.tools import log_check_call
|
||||||
from bootstrap import Bootstrap
|
from bootstrap import Bootstrap
|
||||||
import subprocess
|
|
||||||
import os
|
|
||||||
import logging
|
import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -16,8 +14,7 @@ class FormatVolume(Task):
|
||||||
def run(self, info):
|
def run(self, info):
|
||||||
dev_path = info.bootstrap_device['path']
|
dev_path = info.bootstrap_device['path']
|
||||||
mkfs = '/sbin/mkfs.{fs}'.format(fs=info.manifest.volume['filesystem'])
|
mkfs = '/sbin/mkfs.{fs}'.format(fs=info.manifest.volume['filesystem'])
|
||||||
with open(os.devnull, 'w') as dev_null:
|
log_check_call([mkfs, dev_path], log)
|
||||||
subprocess.check_call([mkfs, dev_path], stdout=dev_null, stderr=dev_null)
|
|
||||||
|
|
||||||
|
|
||||||
class TuneVolumeFS(Task):
|
class TuneVolumeFS(Task):
|
||||||
|
@ -28,8 +25,8 @@ class TuneVolumeFS(Task):
|
||||||
def run(self, info):
|
def run(self, info):
|
||||||
dev_path = info.bootstrap_device['path']
|
dev_path = info.bootstrap_device['path']
|
||||||
# Disable the time based filesystem check
|
# Disable the time based filesystem check
|
||||||
with open(os.devnull, 'w') as dev_null:
|
command = ['/sbin/tune2fs', '-i', '0', dev_path]
|
||||||
subprocess.check_call(['/sbin/tune2fs', '-i', '0', dev_path], stdout=dev_null, stderr=dev_null)
|
log_check_call(command, log)
|
||||||
|
|
||||||
|
|
||||||
class AddXFSProgs(Task):
|
class AddXFSProgs(Task):
|
||||||
|
@ -46,6 +43,7 @@ class CreateMountDir(Task):
|
||||||
phase = phases.volume_mounting
|
phase = phases.volume_mounting
|
||||||
|
|
||||||
def run(self, info):
|
def run(self, info):
|
||||||
|
import os
|
||||||
mount_dir = info.manifest.bootstrapper['mount_dir']
|
mount_dir = info.manifest.bootstrapper['mount_dir']
|
||||||
info.root = '{mount_dir}/{vol_id}'.format(mount_dir=mount_dir, vol_id=info.volume.id)
|
info.root = '{mount_dir}/{vol_id}'.format(mount_dir=mount_dir, vol_id=info.volume.id)
|
||||||
# Works recursively, fails if last part exists, which is exaclty what we want.
|
# Works recursively, fails if last part exists, which is exaclty what we want.
|
||||||
|
@ -64,9 +62,8 @@ class MountVolume(Task):
|
||||||
msg = 'Something is already mounted at {root}'.format(root=info.root)
|
msg = 'Something is already mounted at {root}'.format(root=info.root)
|
||||||
raise TaskError(msg)
|
raise TaskError(msg)
|
||||||
|
|
||||||
dev_path = info.bootstrap_device['path']
|
command = ['mount', info.bootstrap_device['path'], info.root]
|
||||||
with open(os.devnull, 'w') as dev_null:
|
log_check_call(command, log)
|
||||||
subprocess.check_call(['mount', dev_path, info.root], stdout=dev_null, stderr=dev_null)
|
|
||||||
|
|
||||||
|
|
||||||
class MountSpecials(Task):
|
class MountSpecials(Task):
|
||||||
|
@ -75,14 +72,10 @@ class MountSpecials(Task):
|
||||||
after = [Bootstrap]
|
after = [Bootstrap]
|
||||||
|
|
||||||
def run(self, info):
|
def run(self, info):
|
||||||
if log_command(['mount', '--bind', '/dev', '{root}/dev'.format(root=info.root)], log) != 0:
|
log_check_call(['mount', '--bind', '/dev', '{root}/dev'.format(root=info.root)], log)
|
||||||
raise TaskError('Failed to bind /dev to {root}/dev'.format(root=info.root))
|
log_check_call(['chroot', info.root, 'mount', '-t', 'proc', 'none', '/proc'], log)
|
||||||
if log_command(['chroot', info.root, 'mount', '-t', 'proc', 'none', '/proc'], log) != 0:
|
log_check_call(['chroot', info.root, 'mount', '-t', 'sysfs', 'none', '/sys'], log)
|
||||||
raise TaskError('Failed to mount /proc')
|
log_check_call(['chroot', info.root, 'mount', '-t', 'devpts', 'none', '/dev/pts'], log)
|
||||||
if log_command(['chroot', info.root, 'mount', '-t', 'sysfs', 'none', '/sys'], log) != 0:
|
|
||||||
raise TaskError('Failed to mount /sys')
|
|
||||||
if log_command(['chroot', info.root, 'mount', '-t', 'devpts', 'none', '/dev/pts'], log) != 0:
|
|
||||||
raise TaskError('Failed to mount /dev/pts')
|
|
||||||
|
|
||||||
|
|
||||||
class UnmountSpecials(Task):
|
class UnmountSpecials(Task):
|
||||||
|
@ -90,14 +83,10 @@ class UnmountSpecials(Task):
|
||||||
phase = phases.volume_unmounting
|
phase = phases.volume_unmounting
|
||||||
|
|
||||||
def run(self, info):
|
def run(self, info):
|
||||||
if log_command(['chroot', info.root, 'umount', '/dev/pts'], log) != 0:
|
log_check_call(['chroot', info.root, 'umount', '/dev/pts'], log)
|
||||||
raise TaskError('Failed to unmount /dev/pts')
|
log_check_call(['chroot', info.root, 'umount', '/sys'], log)
|
||||||
if log_command(['chroot', info.root, 'umount', '/sys'], log) != 0:
|
log_check_call(['chroot', info.root, 'umount', '/proc'], log)
|
||||||
raise TaskError('Failed to unmount /sys')
|
log_check_call(['umount', '{root}/dev'.format(root=info.root)], log)
|
||||||
if log_command(['chroot', info.root, 'umount', '/proc'], log) != 0:
|
|
||||||
raise TaskError('Failed to unmount /proc')
|
|
||||||
if log_command(['umount', '{root}/dev'.format(root=info.root)], log) != 0:
|
|
||||||
raise TaskError('Failed to unmount /dev from {root}/dev'.format(root=info.root))
|
|
||||||
|
|
||||||
|
|
||||||
class UnmountVolume(Task):
|
class UnmountVolume(Task):
|
||||||
|
@ -106,8 +95,7 @@ class UnmountVolume(Task):
|
||||||
after = [UnmountSpecials]
|
after = [UnmountSpecials]
|
||||||
|
|
||||||
def run(self, info):
|
def run(self, info):
|
||||||
with open(os.devnull, 'w') as dev_null:
|
log_check_call(['umount', info.root], log)
|
||||||
subprocess.check_call(['umount', info.root], stdout=dev_null, stderr=dev_null)
|
|
||||||
|
|
||||||
|
|
||||||
class DeleteMountDir(Task):
|
class DeleteMountDir(Task):
|
||||||
|
@ -116,5 +104,6 @@ class DeleteMountDir(Task):
|
||||||
after = [UnmountVolume]
|
after = [UnmountVolume]
|
||||||
|
|
||||||
def run(self, info):
|
def run(self, info):
|
||||||
|
import os
|
||||||
os.rmdir(info.root)
|
os.rmdir(info.root)
|
||||||
del info.root
|
del info.root
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
from base import Task
|
from base import Task
|
||||||
from common import phases
|
from common import phases
|
||||||
from common.exceptions import TaskError
|
from common.exceptions import TaskError
|
||||||
|
from common.tools import log_check_call
|
||||||
import packages
|
import packages
|
||||||
|
import logging
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class CheckPackages(Task):
|
class CheckPackages(Task):
|
||||||
|
@ -11,11 +14,9 @@ class CheckPackages(Task):
|
||||||
|
|
||||||
def run(self, info):
|
def run(self, info):
|
||||||
import subprocess
|
import subprocess
|
||||||
from os import devnull
|
|
||||||
for package in info.host_packages:
|
for package in info.host_packages:
|
||||||
try:
|
try:
|
||||||
with open(devnull, 'w') as dev_null:
|
log_check_call(['/usr/bin/dpkg', '-s', package], log)
|
||||||
subprocess.check_call(['/usr/bin/dpkg', '-s', package], stdout=dev_null, stderr=dev_null)
|
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
msg = "The package ``{0}\'\' is not installed".format(package)
|
msg = "The package ``{0}\'\' is not installed".format(package)
|
||||||
raise TaskError(msg)
|
raise TaskError(msg)
|
||||||
|
|
Loading…
Add table
Reference in a new issue