[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 13/16] Add InteractiveContainer support. First use case will be OpenShift.



Differentiating on which kind of container to create based off of the

--command == InteractiveContainer
--unitfile == ServiceContainer

Resorted create args to be shown aphabetically except for the --command and --unitfile which I want to come at the end.
---
 bin/virt-sandbox-service | 139 +++++++++++++++++++++++++++++++++--------------
 1 file changed, 99 insertions(+), 40 deletions(-)

diff --git a/bin/virt-sandbox-service b/bin/virt-sandbox-service
index f4d0eff..b559cf5 100755
--- a/bin/virt-sandbox-service
+++ b/bin/virt-sandbox-service
@@ -413,6 +413,45 @@ class Container:
         mount = LibvirtSandbox.ConfigMountRam.new(dest, size);
         self.config.add_mount(mount)
 
+class InteractiveContainer(Container):
+    def __init__(self, name=None, uri = "lxc:///", path = Container.DEFAULT_PATH, config=None, create=False):
+        Container.__init__(self, name, uri, path, config, create)
+
+        if create:
+            self.config = LibvirtSandbox.ConfigInteractive.new(name)
+
+    def _gen_filesystems(self):
+        Container._gen_filesystems(self)
+        self.add_bind_mount(self.dest, self.path)
+
+    def _create(self):
+        #
+        # Create an InteractiveContainer
+        #
+        Container.create(self)
+        self._gen_filesystems()
+
+        if self.image:
+            self._create_image()
+            self._umount()
+            sys.stdout.write(_("Created sandbox container image %s\n") % self.image)
+        else:
+            sys.stdout.write(_("Created sandbox container dir %s\n") % self.dest)
+        self.save_config()
+
+    def create(self):
+        try:
+            self._create()
+        except Exception, e:
+            try:
+                self._delete()
+            except Exception, e2:
+                pass
+            raise e
+
+    def set_command(self, command):
+        self.config.set_command(command)
+
 class ServiceContainer(Container):
     IGNORE_DIRS        = [ "/var/run/", "/etc/logrotate.d/", "/etc/pam.d" ]
     DEFAULT_DIRS       = [ "/etc", "/var" ]
@@ -836,19 +875,26 @@ MB = int(1000000)
 
 def delete(args):
     config = read_config(args.name)
-    container = ServiceContainer(uri=args.uri, config = config)
+    if isinstance(config, gi.repository.LibvirtSandbox.ConfigInteractive):
+        container = InteractiveContainer(uri=args.uri, config = config)
+    else:
+        container = ServiceContainer(uri=args.uri, config = config)
     container.delete()
 
 def create(args):
-    container = ServiceContainer(name = args.name, uri=args.uri, create = True)
-    container.set_copy(args.copy)
+    if args.command:
+        container = InteractiveContainer(name = args.name, uri=args.uri, create = True)
+        container.set_command(args.command.split())
+    else:
+        container = ServiceContainer(name = args.name, uri=args.uri, create = True)
+        container.set_copy(args.copy)
+        container.set_unit_file_list(args.unitfiles)
     for net in args.network:
         container.add_network(net)
     if args.security:
         container.set_security(args.security)
-    container.set_unit_file_list(args.unitfiles)
     container.set_path(args.path)
-
+    container.set_file_type(args.file_type)
     if args.imagesize:
         container.set_image(args.imagesize)
 
@@ -879,17 +925,21 @@ def sandbox_list(args):
 
 def sandbox_reload(args):
     config = read_config(args.name)
+    if isinstance(config, gi.repository.LibvirtSandbox.ConfigInteractive):
+        raise ValueError(_("Interactive Containers do not support reload"))
     container = ServiceContainer(uri = args.uri, config = config)
     container.reload(args.unitfiles)
 
 def start(args):
     os.execl("/usr/libexec/virt-sandbox-service-util", "virt-sandbox-service-util","-s", args.name)
-#    container = Container(args.name, args.uri)
+#    config = read_config(args.name)
+#    container = Container(uri = args.uri, config=config)
 #    container.start()
 
 def stop(args):
     os.execl("/usr/libexec/virt-sandbox-service-util", "virt-sandbox-service-util","-S", args.name)
-#    container = Container(args.name, args.uri)
+#    config = read_config(args.name)
+#    container = Container(uri = args.uri, config=config)
 #    container.stop()
 
 def connect(args):
@@ -898,7 +948,8 @@ Connected to %s.
 Type 'Ctrl + ]' to detach from the console.
 """ % ( args.name )
     os.execl("/usr/libexec/virt-sandbox-service-util", "virt-sandbox-service-util","-a", args.name)
-#    container = Container(args.name, args.uri)
+#    config = read_config(args.name)
+#    container = Container(uri = args.uri, config=config)
 #    container.connect()
 
 #
@@ -945,25 +996,27 @@ def clone(args):
         shutil.copytree(old_path, new_path, symlinks=True)
         sys.stdout.write(_("Created sandbox container dir %s\n") % new_path)
 
-    fd = open(container.get_unit_path())
-    recs = fd.read()
-    fd.close()
-
-    fd = open(container.get_unit_path(args.dest), "wx")
-    fd.write(recs.replace(args.source, args.dest))
-    fd.close()
+    if isinstance(config, gi.repository.LibvirtSandbox.ConfigInteractive):
+        container = InteractiveContainer(name=args.dest, uri=args.uri, create=True)
+    else:
+        fd = open(container.get_unit_path())
+        recs = fd.read()
+        fd.close()
+        fd = open(container.get_unit_path(args.dest), "wx")
+        fd.write(recs.replace(args.source, args.dest))
+        fd.close()
+        new_unit = container.get_config_path(args.dest)
+        fd = open(new_unit,"wx")
+        fd.write(newrec)
+        fd.close()
+        sys.stdout.write(_("Created unit file %s\n") % new_unit)
 
-    new_unit = container.get_config_path(args.dest)
-    fd = open(new_unit,"wx")
-    fd.write(newrec)
-    fd.close()
-    sys.stdout.write(_("Created unit file %s\n") % new_unit)
+        container = ServiceContainer(name=args.dest, uri=args.uri, create=True)
+        container.gen_machine_id()
+        container.gen_hostname()
 
-    container = ServiceContainer(name=args.dest, uri=args.uri, create=True)
     if args.security:
         container.set_security(args.security)
-    container.gen_machine_id()
-    container.gen_hostname()
     container.set_security_label()
     container.save_config()
 
@@ -1022,28 +1075,34 @@ def gen_create_args(subparser):
     parser = subparser.add_parser("create",
                                   help=_("create a sandbox container"))
 
-    parser.add_argument("-u", "--unitfile", required=True,
-                        action=CheckUnit,
-                        dest="unitfiles",
-                        help=_("Systemd Unit file to run within the sandbox container"))
-    parser.add_argument("-p", "--path", dest="path",  default=None,
-                        help=_("select path to store sandbox content.  Default: %s") % c.DEFAULT_PATH)
+    parser.add_argument("-C", "--copy", default=False,
+                        action="store_true",
+                        help=_("copy content from the hosts /etc and /var directories that will be mounted within the sandbox"))
 
+    parser.add_argument("-f", "--filetype", dest="file_type",
+                        default=c.get_file_type(),
+                        help=_("SELinux file type to assign to content within the sandbox.  Default: %s") % c.get_file_type())
+    parser.add_argument("-i", "--imagesize", dest="imagesize", default = None,
+                       action=SizeAction,
+                       help=_("create image of this many megabytes."))
+    parser.add_argument("-n", "--network", dest="network",
+                        action=SetNet, default=[],
+                        help=_("Specify the network configuration"))
+    parser.add_argument("-p", "--path", dest="path",  default=c.DEFAULT_PATH,
+                        help=_("select path to store sandbox content.  Default: %s") % c.DEFAULT_PATH)
     parser.add_argument("-s", "--security", dest="security",
                         default=default_security_opts(),
                         help=_("Specify the security model configuration for the sandbox: Defaults to dynamic"))
-    parser.add_argument("-N", "--network", dest="network",
-                        action=SetNet,
-                        help=_("Specify the network configuration"))
 
-    image = parser.add_argument_group("Create On Disk Image File")
-
-    image.add_argument("-i", "--imagesize", dest="imagesize", default = None,
-                       action=SizeAction,
-                       help=_("create image of this many megabytes."))
-    parser.add_argument("-C", "--copy", default=False,
-                        action="store_true",
-                        help=_("copy content from /etc and /var directories that will be mounted within the sandbox"))
+    ctype = parser.add_argument_group(_("Type of sandbox container to create"))
+    group = ctype.add_mutually_exclusive_group(required=True)
+    group.add_argument("-c", "--command",
+                        dest="command", default=None,
+                        help=_("Command to run within the sandbox container"))
+    group.add_argument("-u", "--unitfile",
+                        action=CheckUnit,
+                        dest="unitfiles",
+                        help=_("Systemd Unit file to run within the sandbox container"))
     requires_name(parser)
 
     parser.set_defaults(func=create)
-- 
1.8.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]