[libvirt] [sandbox PATCH] allow to use a templated unit in virt-sandbox

Michael Scherer misc at zarb.org
Fri May 3 14:32:45 UTC 2013


This permit to create a templated unit inside the sandbox,
using the sandbox name as a variable and so running the same
unit with a different configuration without too much hassle.

For example, someone could have several different configuration of
website in /etc/nginx/websites.d/ and have each of them started in
a different sandbox, with a sample templated unit using the sandbox
name as a option to read the proper configuration file directly.
---
 bin/virt-sandbox-service | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/bin/virt-sandbox-service b/bin/virt-sandbox-service
index 2096be1..0d89b54 100755
--- a/bin/virt-sandbox-service
+++ b/bin/virt-sandbox-service
@@ -345,6 +345,10 @@ class GenericContainer(Container):
     def set_command(self, command):
         self.config.set_command(command)
 
+
+def is_template_unit(unit):
+    return '@' in unit
+
 class SystemdContainer(Container):
     IGNORE_DIRS        = [ "/var/run/", "/etc/logrotate.d/", "/etc/pam.d" ]
     DEFAULT_DIRS       = [ "/etc", "/var" ]
@@ -624,14 +628,22 @@ WantedBy=%(TARGET)s
                 source = "%s%s" % ( self.dest, d)
                 self.add_bind_mount(source, d)
 
+    def get_expanded_unit_template(self, unit):
+        return unit.replace('@', '@' + self.name)
+
     def create_container_unit(self, src, dest, unit):
-            fd = open(dest + "/" + unit, "w")
-            fd.write(""".include %s
+            if is_template_unit(unit):
+                expanded_unit_name = self.get_expanded_unit_template(unit)
+                os.symlink(src, dest + "/" + expanded_unit_name)
+                shutil.copy(src, dest + "/" + unit)
+            else:
+                fd = open(dest + "/" + unit, "w")
+                fd.write(""".include %s
 [Service]
 PrivateTmp=false
 PrivateNetwork=false
 """ % src )
-            fd.close()
+                fd.close()
 
     def gen_content(self):
         if self.copy:
@@ -677,6 +689,8 @@ PrivateNetwork=false
 
         for i, src in self.unit_file_list:
             self.create_container_unit(src, self.dest + unitdir, i)
+            if is_template_unit(i):
+                i = self.get_expanded_unit_template(i)
             os.symlink("../" + i, self.dest + tgtdir + "/" + i)
 
         tgtfile = unitdir + "/multi-user.target"
-- 
1.8.2.1




More information about the libvir-list mailing list