RPM 规范中模板化 .service/.conf 文件的推荐

Recommendation For Templated .service/.conf File in RPM Spec

我们已经从 .spec 文件创建了 RPM,其中包括为 upstart 和 systemd(因此,.conf 和 .service)文件安装服务。但是 .conf 和 .service 文件包含服务文件的硬连线路径。

myservice.service:

[Unit]
Description=My Service

[Service]
WorkingDirectory=/opt/myproduct
ExecStart=/opt/myproduct/myservice /opt/myproduct/myservicearg
Restart=always

[Install]
WantedBy=multi-user.target

myservice.conf:

description "My Service"

respawn
respawn limit 15 5

start on (stopped rc and runlevel [2345])
stop on runlevel [06]

chdir /opt/myproduct

exec /opt/myproduct/myservice /opt/myproduct/myservicearg

安装路径可能会改变,但蛮力搜索和替换似乎是石器时代。

我将 Ansible 与 .j2 (Jinja2) 模板文件一起使用,这似乎是为 binary/script 路径使用变量的好方法。使用它们可能看起来像这样:

myservice.service.j2:

[Unit]
Description=My Service

[Service]
WorkingDirectory={{ myproductpath }}
ExecStart={{ myproductpath }}/myservice {{ myproductpath }}/myservicearg
Restart=always

[Install]
WantedBy=multi-user.target

myservice.conf.j2:

description "My Service"

respawn
respawn limit 15 5

start on (stopped rc and runlevel [2345])
stop on runlevel [06]

chdir {{ myproductpath }}

exec {{ myproductpath }}/myservice {{ myproductpath }}/myservicearg

但我找不到任何证据表明这是构建 RPM 的常用方法。在 RPM 中是否有推荐的方法来模板化这些 .conf 和 .service 文件,在 RPM 构建期间或在安装期间填写?

没有。 Rpm 没有任何这样的模板工具。大多数开发人员更喜欢经典 sed:

%build
....
sed -i 's/{{ myproductpath }}/\/real\/path/g' myservice.conf.j2
mv myservice.conf.j2 myservice.conf

或者您可以 BuildRequires: ansible 让 ansible 扩展它。但对于这项工作来说,这是一个相当沉重的工具。