如何在 RPM 规范文件中创建全局变量

如何在 RPM 规范文件中创建全局变量

At RPM installation time, I want to determine if the host is UAT or PROD. Once I know, I can then set a variable (say %user, %group) that can be used anywhere in the RPM (our App uses a different Unix user:group for PROD and UAT). Right now, if I need to know if the host is PROD or UAT in both %pre and %post sections, I have to check this in each section due to variable scope - i.e. if %user is being defined in %pre, I cannot use it in %post.

The way I determine if the host is PROD or UAT is a simple regex on the hostname.

I've looked into %global macros but I'm struggling, hence a question to a wider audience.

Thank you in advance

答案1

The macros in the SPEC file are set during rpmbuild time. I.e. when you are creating the rpm package. During the build time they are expanded and cannot be altered later - e.g., during installation.

If you set macro in the SPEC file based on some content on the disk, then it will be set based on the content of the file on host where you created the RPM package.

If you want to alter your behaviour during installation time then you have to do it in each section again. E.g.

%pre
if [ `hostname` -eq 'foo' ]; then
   .....

%post
if [ `hostname` -eq 'foo' ]; then
   .....

These sections are interpreted as two separate shell scripts, and there is no way to keep variables between these two runs. I.e. when you set the shell variable in the first scriptlet, it will not propagate to the second one. The recommended way is to drop a file that you re-read in the second scriptlet. https://docs.fedoraproject.org/en-US/packaging-guidelines/Scriptlets/#_saving_state_between_scriptlets

答案2

不要认为后宏和前宏应该存在单一的全局作用域 - 最后,这些实际上是单独运行的独立脚本。

相关内容