All printing had to be reordered and now it is done from a central point.
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
-from image_creator.util import get_command, warn, progress_generator, success
+from image_creator.util import get_command
+from image_creator.util import warn, progress, success, output
from image_creator import FatalError
-from clint.textui import puts
import stat
import os
the Disk instance.
"""
- puts("Examining source media `%s'..." % self.source, False)
+ output("Examining source media `%s'..." % self.source, False)
sourcedev = self.source
mode = os.stat(self.source).st_mode
if stat.S_ISDIR(mode):
success('looks like a block device')
# Take a snapshot and return it to the user
- puts("Snapshotting media source...", False)
+ output("Snapshotting media source...", False)
size = blockdev('--getsize', sourcedev)
cowfd, cow = tempfile.mkstemp()
os.close(cowfd)
def enable(self):
"""Enable a newly created DiskDevice"""
- self.progressbar = progress_generator("Launching helper VM: ")
+ self.progressbar = progress("Launching helper VM: ")
self.progressbar.next()
eh = self.g.set_event_callback(self.progress_callback,
guestfs.EVENT_PROGRESS)
self.progressbar.send(100)
self.progressbar = None
- puts('Inspecting Operating System...', False)
+ output('Inspecting Operating System...', False)
roots = self.g.inspect_os()
if len(roots) == 0:
raise FatalError("No operating system found")
def mount(self):
"""Mount all disk partitions in a correct order."""
+
+ output("Mounting image...", False)
mps = self.g.inspect_get_mountpoints(self.root)
# Sort the keys to mount the fs in a correct order.
try:
self.g.mount(dev, mp)
except RuntimeError as msg:
- print "%s (ignored)" % msg
+ warn("%s (ignored)" % msg)
+ success("done")
def umount(self):
"""Umount all mounted filesystems."""
disk and then updating the partition table. The new disk size
(in bytes) is returned.
"""
- puts("Shrinking image (this may take a while)...", False)
- sys.stdout.flush()
+ output("Shrinking image (this may take a while)...", False)
dev = self.g.part_to_dev(self.root)
parttype = self.g.part_get_parttype(dev)
blocksize = 2 ** 22 # 4MB
size = self.size()
progress_size = (size + 2 ** 20 - 1) // 2 ** 20 # in MB
- progressbar = progress_generator("Dumping image file: ", progress_size)
+ progressbar = progress("Dumping image file: ", progress_size)
source = open(self.device, "r")
try:
from image_creator import __version__ as version
from image_creator import FatalError
from image_creator.disk import Disk
-from image_creator.util import get_command, error, progress_generator, success
-from clint.textui import puts
-
+from image_creator.util import get_command, error, success, output
+from image_creator import util
import sys
import os
import optparse
action="store_true", help="overwrite output files if they exist")
parser.add_option("--no-cleanup", dest="cleanup", default=True,
- help="don't cleanup sensitive data",
- action="store_false")
+ help="don't cleanup sensitive data", action="store_false")
parser.add_option("--no-sysprep", dest="sysprep", default=True,
- help="don't perform system preperation",
- action="store_false")
+ help="don't perform system preperation", action="store_false")
parser.add_option("--no-shrink", dest="shrink", default=True,
- help="don't shrink any partition",
- action="store_false")
+ help="don't shrink any partition", action="store_false")
parser.add_option("-o", "--outfile", type="string", dest="outfile",
default=None, action="callback", callback=check_writable_dir,
- help="dump image to FILE",
- metavar="FILE")
+ help="dump image to FILE", metavar="FILE")
+
+ parser.add_option("-s", "--silent", dest="silent", default=False,
+ help="silent mode, only output error", action="store_true")
parser.add_option("-u", "--upload", dest="upload", default=False,
- help="upload the image to pithos",
- action="store_true")
+ help="upload the image to pithos", action="store_true")
parser.add_option("-r", "--register", dest="register", default=False,
help="register the image to ~okeanos", action="store_true")
def image_creator():
- puts('snf-image-creator %s\n' % version)
options = parse_options(sys.argv[1:])
+ if options.silent:
+ util.silent = True
+
+ output('snf-image-creator %s\n' % version)
+
if os.geteuid() != 0:
raise FatalError("You must run %s as root" \
% os.path.basename(sys.argv[0]))
image_os = osclass(dev.root, dev.g)
metadata = image_os.get_metadata()
- puts()
+ output()
if options.sysprep:
image_os.sysprep()
dev.dump(options.outfile)
finally:
- puts('cleaning up...')
+ output('cleaning up...')
disk.cleanup()
return 0
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
+from image_creator.util import output
+
import re
-from clint.textui import indent, puts
def add_prefix(target):
def data_cleanup(self):
"""Cleanup sensitive data out of the OS image."""
- puts('Cleaning up sensitive data out of the OS image:')
+ output('Cleaning up sensitive data out of the OS image:')
is_cleanup = lambda x: x.startswith('data_cleanup_') and \
callable(getattr(self, x))
cnt = 0
for task in tasks:
cnt += 1
- puts(('(%d/%d)' % (cnt, size)).ljust(7), False)
+ output(('(%d/%d)' % (cnt, size)).ljust(7), False)
task()
- puts()
+ output()
def sysprep(self):
"""Prepere system for image creation."""
- puts('Preparing system for image creation:')
+ output('Preparing system for image creation:')
is_sysprep = lambda x: x.startswith('sysprep_') and \
callable(getattr(self, x))
cnt = 0
for task in tasks:
cnt += 1
- puts(('(%d/%d)' % (cnt, size)).ljust(7), False)
+ output(('(%d/%d)' % (cnt, size)).ljust(7), False)
task()
- puts()
+ output()
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
# or implied, of GRNET S.A.
from image_creator.os_type.unix import Unix
-from image_creator.util import warn
-
-from clint.textui import puts, indent
+from image_creator.util import warn, output
import re
import time
"""
if print_header:
- print 'Fixing acpid powerdown action'
+ output('Fixing acpid powerdown action')
powerbtn_action = '#!/bin/sh\n\nPATH=/sbin:/bin:/usr/bin\n' \
'shutdown -h now \"Power button pressed\"\n'
"""
if print_header:
- puts('Removing persistent network interface names')
+ output('Removing persistent network interface names')
rule_file = '/etc/udev/rules.d/70-persistent-net.rules'
if self.g.is_file(rule_file):
"""
if print_header:
- puts('Replacing fstab & grub non-persistent device appearences')
+ output('Replacing fstab & grub non-persistent device appearences')
# convert all devices in fstab to persistent
persistent_root = self._persistent_fstab()
import sys
from image_creator.os_type import OSBase
-from image_creator.util import warn
-from clint.textui import puts
+from image_creator.util import warn, output
class Unix(OSBase):
"""Remove all regular files under /var/cache"""
if print_header:
- puts('Removing files under /var/cache')
+ output('Removing files under /var/cache')
self.foreach_file('/var/cache', self.g.rm, ftype='r')
"""Remove all files under /tmp and /var/tmp"""
if print_header:
- puts('Removing files under /tmp and /var/tmp')
+ output('Removing files under /tmp and /var/tmp')
self.foreach_file('/tmp', self.g.rm_rf, maxdepth=1)
self.foreach_file('/var/tmp', self.g.rm_rf, maxdepth=1)
"""Empty all files under /var/log"""
if print_header:
- puts('Emptying all files under /var/log')
+ output('Emptying all files under /var/log')
self.foreach_file('/var/log', self.g.truncate, ftype='r')
"""Remove all files under /var/mail and /var/spool/mail"""
if print_header:
- puts('Removing files under /var/mail and /var/spool/mail')
+ output('Removing files under /var/mail and /var/spool/mail')
self.foreach_file('/var/spool/mail', self.g.rm_rf, maxdepth=1)
self.foreach_file('/var/mail', self.g.rm_rf, maxdepth=1)
homedirs = ['/root'] + self.ls('/home/')
if print_header:
- puts('Removing sensitive user data under %s' % " ".join(homedirs))
+ output('Removing sensitive user data under %s' % " ".
+ join(homedirs))
for homedir in homedirs:
for data in self.sensitive_userdata:
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
+import sys
import pbs
-from clint.textui import puts, puts_err, colored, progress
+from clint.textui import colored, progress as uiprogress
+
+silent = False
def get_command(command):
def error(msg, new_line=True):
- puts_err(colored.red("Error: %s\n" % msg), new_line)
+ nl = "\n" if new_line else ''
+ sys.stderr.write('Error: %s' % msg + nl)
def warn(msg, new_line=True):
- puts_err(colored.yellow("Warning: %s" % msg), new_line)
+ if not silent:
+ nl = "\n" if new_line else ''
+ sys.stderr.write(colored.yellow("Warning: %s" % msg) + nl)
def success(msg, new_line=True):
- puts(colored.green(msg), new_line)
+ if not silent:
+ nl = "\n" if new_line else ''
+ sys.stdout.write(colored.green(msg) + nl)
+ if not nl:
+ sys.stdout.flush()
+
+
+def output(msg="", new_line=True):
+ if not silent:
+ nl = "\n" if new_line else ''
+ sys.stdout.write(msg + nl)
+ if not nl:
+ sys.stdout.flush()
+
+
+def progress(label='', n=100):
+ PROGRESS_LENGTH = 32
+ MESSAGE_LENGTH = 32
-def progress_generator(label='', n=100):
- position = 0
- for i in progress.bar(range(n), label):
- if i < position:
- continue
- position = yield
- yield # suppress the StopIteration exception
+ def progress_generator(label, n):
+ position = 0
+ for i in uiprogress.bar(range(n), label.ljust(MESSAGE_LENGTH), \
+ PROGRESS_LENGTH, silent):
+ if i < position:
+ continue
+ position = yield
+ yield # suppress the StopIteration exception
+ return progress_generator(label, n)
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :