summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRadostin Stoyanov <rstoyanov1@gmail.com>2017-07-03 08:27:25 +0100
committerCédric Bosdonnat <cbosdonnat@suse.com>2017-07-04 09:06:23 +0200
commit970b4247bd9a39d36ade980967371e4d1bc3f893 (patch)
treed29a27d21fd1a4312557cc125a164db52f4c8dfb
parent65ef323df230b770c56217367e9c019cffcb0a7d (diff)
downloadvirt-bootstrap.git-970b4247bd9a39d36ade980967371e4d1bc3f893.tar.gz
virt-bootstrap.git-970b4247bd9a39d36ade980967371e4d1bc3f893.tar.xz
virt-bootstrap.git-970b4247bd9a39d36ade980967371e4d1bc3f893.zip
bootstrap: Use explicit arguments
Specify explicitly the arguments of bootstrap method. This change allows to easily bootstrap a container from another python application when the module virtBootstrap is imported. Example: import virtBootstrap virtBootstrap.bootstrap(uri="docker://fedora", dest="/tmp/foo")
-rw-r--r--src/virtBootstrap/sources.py23
-rwxr-xr-xsrc/virtBootstrap/virt_bootstrap.py59
2 files changed, 48 insertions, 34 deletions
diff --git a/src/virtBootstrap/sources.py b/src/virtBootstrap/sources.py
index 617adc6..ab1091a 100644
--- a/src/virtBootstrap/sources.py
+++ b/src/virtBootstrap/sources.py
@@ -211,9 +211,9 @@ class FileSource(object):
"""
Extract root filesystem from file.
"""
- def __init__(self, url, args):
- self.path = url.path
- self.output_format = args.format
+ def __init__(self, **kwargs):
+ self.path = kwargs['uri'].path
+ self.output_format = kwargs['fmt']
def unpack(self, dest):
"""
@@ -249,7 +249,7 @@ class DockerSource(object):
Extract files from Docker image
"""
- def __init__(self, url, args):
+ def __init__(self, **kwargs):
"""
Bootstrap root filesystem from Docker registry
@@ -261,13 +261,14 @@ class DockerSource(object):
@param no_cache: Whether to store downloaded images or not
"""
- self.registry = url.netloc
- self.image = url.path
- self.username = args.username
- self.password = args.password
- self.output_format = args.format
- self.insecure = args.not_secure
- self.no_cache = args.no_cache
+ self.registry = kwargs['uri'].netloc
+ self.image = kwargs['uri'].path
+ self.username = kwargs['username']
+ self.password = kwargs['password']
+ self.output_format = kwargs['fmt']
+ self.insecure = kwargs['not_secure']
+ self.no_cache = kwargs['no_cache']
+
if not self.registry and self.image.startswith('/'):
self.image = self.image[1:]
elif self.image and not self.image.startswith('/'):
diff --git a/src/virtBootstrap/virt_bootstrap.py b/src/virtBootstrap/virt_bootstrap.py
index fc3603d..460d0e9 100755
--- a/src/virtBootstrap/virt_bootstrap.py
+++ b/src/virtBootstrap/virt_bootstrap.py
@@ -56,22 +56,16 @@ except IOError:
logger = logging.getLogger(__name__)
-def get_source(args):
+def get_source(source_type):
"""
Get object which match the source type
"""
- url = urlparse(args.uri)
- scheme = url.scheme
-
- if scheme == "":
- scheme = 'file'
-
try:
- class_name = "%sSource" % scheme.capitalize()
+ class_name = "%sSource" % source_type.capitalize()
clazz = getattr(sources, class_name)
- return clazz(url, args)
+ return clazz
except Exception:
- raise Exception("Invalid image URI scheme: '%s'" % url.scheme)
+ raise Exception("Invalid image URL scheme: '%s'" % source_type)
def set_root_password(rootfs, password):
@@ -86,26 +80,38 @@ def set_root_password(rootfs, password):
raise CalledProcessError(chpasswd.returncode, cmd=args, output=None)
-def bootstrap(args):
+# pylint: disable=too-many-arguments
+def bootstrap(uri, dest,
+ fmt='dir',
+ username=None,
+ password=None,
+ root_password=None,
+ not_secure=False,
+ no_cache=False):
"""
Get source object and call unpack method
"""
+ uri = urlparse(uri)
+ source = get_source(uri.scheme or 'file')
- source = get_source(args)
- if not os.path.exists(args.dest):
- os.makedirs(args.dest)
- elif not os.path.isdir(args.dest): # Show error if not directory
- logger.error("Destination path '%s' is not directory.", args.dest)
+ if not os.path.exists(dest):
+ os.makedirs(dest)
+ elif not os.path.isdir(dest): # Show error if not directory
+ logger.error("Destination path '%s' is not directory.", dest)
sys.exit(1)
- elif not os.access(args.dest, os.W_OK): # Check write permissions
- logger.error("No write permissions on destination path '%s'",
- args.dest)
+ elif not os.access(dest, os.W_OK): # Check write permissions
+ logger.error("No write permissions on destination path '%s'", dest)
sys.exit(1)
- source.unpack(args.dest)
+ source(uri=uri,
+ fmt=fmt,
+ username=username,
+ password=password,
+ not_secure=not_secure,
+ no_cache=no_cache).unpack(dest)
- if args.root_password is not None:
- set_root_password(args.dest, args.root_password)
+ if root_password is not None:
+ set_root_password(dest, root_password)
def set_logging_conf(loglevel=None):
@@ -176,7 +182,14 @@ def main():
set_logging_conf(args.loglevel)
# do the job here!
- bootstrap(args)
+ bootstrap(uri=args.uri,
+ dest=args.dest,
+ fmt=args.format,
+ username=args.username,
+ password=args.password,
+ root_password=args.root_password,
+ not_secure=args.not_secure,
+ no_cache=args.no_cache)
sys.exit(0)
except KeyboardInterrupt: