3.6. Managing Services in Software Collections

When packaging your Software Collection, ensure that users can directly manage any services (daemons) provided by the Software Collection or one of the associated applications with the system default tools, like service or chkconfig on Red Hat Enterprise Linux 5 and 6, or systemctl on Red Hat Enterprise Linux 7.
For Red Hat Enterprise Linux 5 and 6 Software Collections, make sure to adjust the %install section of the spec file as follows to avoid possible name conflicts with the system versions of the services that are part of the Software Collection:
%install
install -p -c -m 644 %{SOURCE2} $RPM_BUILD_ROOT%{?scl:%_root_sysconfdir}%{!?scl:%_sysconfdir}/rc.d/init.d/%{?scl_prefix}service_name
Replace service_name with the actual name of the service.
For Red Hat Enterprise Linux 7 Software Collections, adjust the %install section of the spec file as follows:
%install
install -p -c -m 644 %{SOURCE2} $RPM_BUILD_ROOT%{_unitdir}/%{?scl_prefix}service_name.service
With this configuration in place, you can then refer to the version of the service included in the Software Collection as follows:
%{?scl_prefix}service_name
Keep in mind that no environment variables are propagated from the user's environment to a SysV init script (or a systemd service file on Red Hat Enterprise Linux 7). This is expected and ensures that services are always started in a clean environment. However, this requires you to properly set up a Software Collection environment for processes that are to be run by the SysV init scripts (or systemd service files).

3.6.1. Configuring an Environment for Services

It is recommended to make the Software Collection you want to enable for services configurable. The directions in this section show how to make a Software Collection named software_collection configurable.

Procedure 3.2. Configuring an environment for services on Red Hat Enterprise Linux 5 and 6

  1. Create a configuration file in /opt/provider/software_collection/service-environment with the following content:
    [SCLNAME]_SCLS_ENABLED="software_collection"
    Replace SCLNAME with a unique identifier for your Software Collection, for instance, your Software Collection's name written in capital letters.
    Replace software_collection with the name of your Software Collection as defined by the %scl_name macro.
  2. Add the following line at the beginning of the SysV init script:
    source /opt/provider/software_collection/service-environment
  3. In the SysV init script, determine commands that run binaries located in the /opt/provider/ file system hierarchy. Prefix these commands with scl enable $[SCLNAME]_SCLS_ENABLED, similarly to when you run a command in the Software Collection environment.
    For example, replace the following line:
    /usr/bin/daemon_binary --argument-1 --argument-2
    with:
    scl enable $[SCLNAME]_SCLS_ENABLED -- /usr/bin/daemon_binary --argument-1 --argument-2
  4. Some commands, like su or runuser, also clear environment variables. Thus, if these commands are used in the SysV init script, enable your Software Collection again after running these commands.
    For instance, replace the following line:
    su - user_name -c '/usr/bin/daemon_binary --argument-1 --argument-2'
    with:
    su - user_name -c '\
      source /opt/provider/software_collection/service-environment \
      scl enable $SCLNAME_SCLS_ENABLED -- /usr/bin/daemon_binary --argument-1 --argument-2'

Procedure 3.3. Configuring an environment for services on Red Hat Enterprise Linux 7

  1. Create a configuration file in /opt/provider/software_collection/service-environment with the following content:
    [SCLNAME]_SCLS_ENABLED="software_collection"
    Replace SCLNAME with a unique identifier for your Software Collection, for instance, your Software Collection's name written in capital letters.
    Replace software_collection with the name of your Software Collection as defined by the %scl_name macro.
  2. Add the following line in the systemd service file to load the configuration file:
    EnvironmentFile=/opt/provider/software_collection/service-environment
  3. In the systemd service file, prefix all commands specified in ExecStartPre, ExecStart, and similar directives with scl enable $[SCLNAME]_SCLS_ENABLED, similarly to when you run a command in the Software Collection environment:
    ExecStartPre=/usr/bin/scl enable $[SCLNAME]_SCLS_ENABLED -- /opt/provider/software_collection/root/usr/bin/daemon_helper_binary --argument-1 --argument-2
    ExecStart=/usr/bin/scl enable $[SCLNAME]_SCLS_ENABLED -- /opt/provider/software_collection/root/usr/bin/daemon_binary --argument-1 --argument-2