[libvirt] [sandbox PATCH v2 1/2] Use drop-in configuration file instead of creating a custom file

This permit to no longer track the source, to use a custom file in /etc without conflict. This change require a newer version of systemd ( > 198 ) --- bin/virt-sandbox-service | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/bin/virt-sandbox-service b/bin/virt-sandbox-service index 2096be1..3cecff8 100755 --- a/bin/virt-sandbox-service +++ b/bin/virt-sandbox-service @@ -625,12 +625,16 @@ WantedBy=%(TARGET)s self.add_bind_mount(source, d) def create_container_unit(self, src, dest, unit): - fd = open(dest + "/" + unit, "w") - fd.write(""".include %s + dropin_dir = "%s/%s.d" % (dest, unit) + if not os.path.exists(dropin_dir): + os.mkdir(dropin_dir) + + fd = open(dropin_dir + "/virt-sandbox.conf", "w") + fd.write("""; file placed here by virt-sandbox-service [Service] PrivateTmp=false PrivateNetwork=false -""" % src ) +""" ) fd.close() def gen_content(self): -- 1.8.2.1

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. One could take the following file in /etc/systemd/system/nginx_lxc@.service : [Unit] Description=Test of a specific nginx running in lxc After=syslog.target network.target remote-fs.target nss-lookup.target [Service] PIDFile=/run/nginx.%i.pid ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.%i.conf ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.%i.conf Type=forking ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID [Install] WantedBy=multi-user.target Then create a container like this: # virt-sandbox-service create -u nginx_lxc@ test.example.org --package nginx And then we will have nginx running in a container, using the specific config file /etc/nginx/nginx.test.example.org.conf --- bin/virt-sandbox-service | 14 ++++++++++++++ bin/virt-sandbox-service-create.pod | 1 + 2 files changed, 15 insertions(+) diff --git a/bin/virt-sandbox-service b/bin/virt-sandbox-service index 3cecff8..942f788 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,7 +628,15 @@ 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): + if is_template_unit(unit): + shutil.copy(src, dest + "/" + unit) + unit = self.get_expanded_unit_template(unit) + os.symlink(src, dest + "/" + unit) + dropin_dir = "%s/%s.d" % (dest, unit) if not os.path.exists(dropin_dir): os.mkdir(dropin_dir) @@ -681,6 +693,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" diff --git a/bin/virt-sandbox-service-create.pod b/bin/virt-sandbox-service-create.pod index ee8cffc..942c919 100644 --- a/bin/virt-sandbox-service-create.pod +++ b/bin/virt-sandbox-service-create.pod @@ -37,6 +37,7 @@ supported currently). =item B<-u UNIT_FILE>, B<--unitfile UNIT_FILE> Name of the systemd unit file to be to run within the Systemd Container. Can be repeated if multiple unit files are required within the sandbox. Cannot be specified if you are using a COMMAND. +If the unit file end with @, this will be considered as a template, and a instancied systemd unit will be created, using the name of the container as a instance identifier. =item B<-C>, B<--copy> -- 1.8.2.1

On Fri, May 03, 2013 at 10:01:23PM +0200, Michael Scherer wrote:
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.
One could take the following file in /etc/systemd/system/nginx_lxc@.service :
[Unit] Description=Test of a specific nginx running in lxc After=syslog.target network.target remote-fs.target nss-lookup.target
[Service] PIDFile=/run/nginx.%i.pid ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.%i.conf ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.%i.conf Type=forking ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID
[Install] WantedBy=multi-user.target
Then create a container like this:
# virt-sandbox-service create -u nginx_lxc@ test.example.org --package nginx
And then we will have nginx running in a container, using the specific config file /etc/nginx/nginx.test.example.org.conf --- bin/virt-sandbox-service | 14 ++++++++++++++ bin/virt-sandbox-service-create.pod | 1 + 2 files changed, 15 insertions(+)
ACK, I've applied these patches now Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
participants (2)
-
Daniel P. Berrange
-
Michael Scherer