如何运行,用脚本从RPM内的互动? [英] How to run and interact with a script from within an RPM?

查看:854
本文介绍了如何运行,用脚本从RPM内的互动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立这需要运行bash脚本作为root RPM。

%安装规范文件的节是:

 安装%CP%{} SOURCE1%{}源2%{_ tmppath} /%{name}的 - %{}版本 - %{}发布
CD%{_ tmppath} /%{name}的 - %{}版本 - %{}发布
CHMOD U + X%{}的InstallScript
须藤./%{installscript}

其中,%{InstallScript中} 是运行为使用sudo根的脚本。

的rpmbuild 执行%{}的InstallScript并创建RPM(没有问题)。

但是,的,当我安装RPM:

  $ sudo的RPM -Uvh $ rpmpath
preparing ... ########################################### [100%]
   1:TTY帽########################################### [100 %]
$

%{InstallScript中} 脚本不会执行。

我试图通过脚本调用移动到%后更改规范文件节:

 安装%CP%{} SOURCE1%{}源2%{_ tmppath} /%{name}的 - %{}版本 - %{}发布
CD%{_ tmppath} /%{name}的 - %{}版本 - %{}发布%后CHMOD U + X%{}的InstallScript
须藤./%{installscript}

%后似乎并没有做任何事情。

我怎么能包安装时会执行一个脚本的RPM?

结果

编辑1:

回顾下有益的意见后,这里有一个%后节,实际上被RPM安装过程中执行的规范文件。结果
不过,此脚本%{InstallScript中} 不与用户交互(因为它从shell中运行时),但似乎接受了所有默认值,无需用户交互。< BR>
我应该怎么改,这样脚本将与命令用户进行交互?

  $猫〜/的rpmbuild / SPECS / demo.spec

#%_topdir和%_tmppath在〜/ .rpmmacros定义%定义名称TTY帽
%定义5.2版本
%确定发行1
%定义的buildroot%{_ tmppath} /%{name}的 - %{}版本 - %{}发布%定义tarversion TTY的5.2.0-00-70270
%定义tar文件%{} tarversion压缩.tar
%定义的InstallScript tty.install.sh名称:%(名称)
版本:%{}版本
版本:%{}发布
BuildArch:,就是noarch
摘要:布拉
许可:专有
源1:%{}的InstallScript
源2:TTY-5.2.0-00-70270.tar
preFIX中:/ opt / Intellinx / TTYCapture
buildroot的:%{_ builddir} /%{name}的-root%描述
示范RPM%preP%建立%安装CP%{} SOURCE1%{}源2%{_ tmppath} /%{name}的 - %{}版本 - %{}发布
CD%{_ tmppath} /%{name}的 - %{}版本 - %{}发布%清洁
[!$ {} RPM_BUILD_ROOT =/]和放大器;&安培;室射频$ {} RPM_BUILD_ROOT%后
回声&GT;&GT;&GT;内部信息&LT;&LT;&LT;
CHMOD U + X%{}的InstallScript
./%{installscript}%文件%定义TMP /
%{} TMP /%{} tar文件
%{} TMP /%{}的InstallScript


  $的rpmbuild -bb -v〜/的rpmbuild / SPECS / demo.spec
执行(%preP):/ bin / sh的-e /home/ronbarak/RPMBUILD/tmp/rpm-tmp.oEOM10
+ 022的umask
+ CD /家庭/ ronbarak /的rpmbuild / BUILD
+ LANG = C
+出口LANG
未设置+ DISPLAY
+ 0退出
执行(%版本):/ bin / sh的-e /home/ronbarak/RPMBUILD/tmp/rpm-tmp.qQFuTA
+ 022的umask
+ CD /家庭/ ronbarak /的rpmbuild / BUILD
+ LANG = C
+出口LANG
未设置+ DISPLAY
+ 0退出
执行(%安装):/ bin / sh的-e /home/ronbarak/RPMBUILD/tmp/rpm-tmp.8rTMLa
+ 022的umask
+ CD /家庭/ ronbarak /的rpmbuild / BUILD
+'['/home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1!='/']'
+ RM -rf /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1
++目录名/home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1
+的mkdir -p /家庭/ ronbarak /的rpmbuild / tmp目录
+的mkdir /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1
+ LANG = C
+出口LANG
未设置+ DISPLAY
+ CP /home/ronbarak/RPMBUILD/SOURCES/tty.install.sh /home/ronbarak/RPMBUILD/SOURCES/tty-5.2.0-00-70270.tar /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2 -1
+ CD /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1
+在/ usr / lib / rpm的/检查的buildroot
+在/ usr / lib / rpm的/红帽/ BRP-COM preSS
+在/ usr / lib / rpm的/红帽/ BRP-条的/ usr / bin中/条
+在/ usr / lib / rpm的/红帽/ BRP-带静电归档的/ usr / bin中/条
+在/ usr / lib / rpm的/红帽/ BRP-条注释音符的/ usr / bin中/条的/ usr / bin中/ objdump的
+在/ usr / lib / rpm的/ BRP-中的python-bytecompile
+在/ usr / lib / rpm的/红帽/ BRP-蟒蛇,固网
+在/ usr / lib / rpm的/红帽/ BRP-java的重新包装,罐
处理文件:TTY-CAP-5.2-1.noarch
需要(插值):/ bin / sh的
需要(rpmlib):rpmlib(比较pressedFileNames)LT = 3.0.4-1 rpmlib(FileDigests)LT = 4.6.0-1 rpmlib(PayloadFilesHave preFIX)LT = 4.0-1
需要(岗位):/ bin / sh的
检查包装的文件(S):在/ usr / lib / rpm的/签文件/home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1
写道:/home/ronbarak/RPMBUILD/RPMS/noarch/tty-cap-5.2-1.noarch.rpm
执行(%干净):/ bin / sh的-e /home/ronbarak/RPMBUILD/tmp/rpm-tmp.Yag9bm
+ 022的umask
+ CD /家庭/ ronbarak /的rpmbuild / BUILD
+'['/home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1!='/']'
+ RM -rf /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1
+ 0退出


  $ sudo的RPM -Uvh /home/ronbarak/RPMBUILD/RPMS/noarch/tty-cap-5.2-1.noarch.rpm
preparing ... ########################################### [100%]
   1:TTY帽########################################### [100 %]
&GT;&GT;&GT;内部岗位&LT;&LT;&LT;######开始TTY捕获安装######
指定安装目录的完整路径[/选择/ Intellinx / TTYCapture]:
的/ opt / Intellinx / TTYCapture已经存在。继续安装? (Y / N)[Y]
解压压缩包TTY-5 *。* ...请注明TTY传感器所有者用户[ronbarak]:
TTY传感器将在集团ronbarak'是由用户ronbarak访问指定传感器的工作目录的完整路径[/选择/ Intellinx / TTYCapture /工作]:指定传感器监听端口[1024-65000] [8888]你想注册的传感器服务? (Y / N)[N]
服务器gxttySensorService没有注册为守护执行默认用户的shell(Y / N)[Y]TTY捕获已成功安装。



解决方案

您在这里混的几个概念。所以,让让一步。

%$ P $页部分,你应该解开你的%{SOURCE0} ,如果任何应用补丁。这通常做

 %设置-q

然而,如果你愿意,你可以手动提取它。有关这个宏的详细信息请参见 HTTP://www.rpm。组织/ MAX-RPM / S1-RPM-内-macros.html

%建立部分你平时源代码编译到二进制文件。可能为空,如果您使用跨preTED语言或您的焦油包含已编译的二进制文件。

%安装部分,你应该将文件复制到%{buildroot的} 并创建有结构,将土地在包中。例如。 %{buildroot的}的/ etc / yourconfig %{buildroot的}的/ usr / bin中/ yourcommand 等,您可以运行有你想要的,但请记住,它只是在建造时间运行任何脚本。即只有你的机器(或编译系统)。这是为了创建这是自动生成的文件(例如,从源头code库的文档)。

然后,你必须是用户机在安装包后,运行区段%后。和所有的文件都安装在最终路径。不buildroot的。在开始的时候,你都改为 / ,所以你需要在该用户的计算机上指定的完整路径。

所以你的情况应该大概是这样的:

 安装%
MKDIR -p%{buildroot的}%{_} BINDIR
CP -a%{}的InstallScript%{buildroot的}%{_ BINDIR} /
CHMOD A + X%{buildroot的}%{_ BINDIR} /%{}的InstallScript%文件
%{_ BINDIR} /%{}的InstallScript%后
%{_} BINDIR%{}的InstallScript

Sever的笔记:


  1. %后部分根下执行,因此不需要须藤。


  2. 运行交互式脚本强烈反对。 RPM被设计成非交互式,每个实用围绕承担包安装(例如PackageKit的,太空行走等)过程中没有互动。所以越早再后来你会得到一些孔潘。它是更安全地说用户运行手动安装后的一些命令(或使用Ansible或木偶其自动化)。


I'm building an RPM which needs to run a bash script as root.

The %install stanza of the spec file is:

%install

cp %{SOURCE1} %{SOURCE2} %{_tmppath}/%{name}-%{version}-%{release}
cd %{_tmppath}/%{name}-%{version}-%{release}
chmod u+x %{installscript}
sudo ./%{installscript}

Where %{installscript} is the script that runs as root with sudo.

rpmbuild executes %{installscript} and creates the RPM (without problems).

However, when I install the RPM:

$ sudo rpm -Uvh $rpmpath
Preparing...                ########################################### [100%]
   1:tty-cap ########################################### [100%]
$

The %{installscript} script is not executed.

I tried to change the spec file by moving the script invocation to a %post stanza:

%install

cp %{SOURCE1} %{SOURCE2} %{_tmppath}/%{name}-%{version}-%{release}
cd %{_tmppath}/%{name}-%{version}-%{release}

%post

chmod u+x %{installscript}
sudo ./%{installscript}

But the %post doesn't seem to do anything.

How can I pack an RPM that will execute a script when installed?

Edit 1:

After reviewing the helpful comments below, here's a spec file with a %post stanza that actually gets executed during the RPM installation.
However, the script %{installscript} does not interact with the user (as it does when run from the shell), but seems to accept all its defaults without user interaction.
What should I change so that the script will interact with the rpm command user?

$ cat ~/RPMBUILD/SPECS/demo.spec
#
# %_topdir and %_tmppath are defined in  ~/.rpmmacros

%define name tty-cap
%define version 5.2
%define release 1
%define buildroot %{_tmppath}/%{name}-%{version}-%{release}

%define tarversion tty-5.2.0-00-70270
%define tarfile %{tarversion}.tar
%define installscript tty.install.sh

Name:           %{name}
Version:        %{version}
Release:        %{release}
BuildArch:      noarch
Summary:        Bla
License:        Proprietary
Source1:        %{installscript}
Source2:        tty-5.2.0-00-70270.tar
Prefix:         /opt/Intellinx/TTYCapture
BuildRoot:      %{_builddir}/%{name}-root

%description
Demonstration RPM

%prep

%build

%install

cp %{SOURCE1} %{SOURCE2} %{_tmppath}/%{name}-%{version}-%{release}
cd %{_tmppath}/%{name}-%{version}-%{release}

%clean
[ ${RPM_BUILD_ROOT} != "/" ] && rm -rf ${RPM_BUILD_ROOT}

%post
echo ">>> Inside post <<<"
chmod u+x %{installscript}
./%{installscript}

%files

%define tmp /
%{tmp}/%{tarfile}
%{tmp}/%{installscript}


$ rpmbuild -v -bb  ~/RPMBUILD/SPECS/demo.spec
Executing(%prep): /bin/sh -e /home/ronbarak/RPMBUILD/tmp/rpm-tmp.oEOM10
+ umask 022
+ cd /home/ronbarak/RPMBUILD/BUILD
+ LANG=C
+ export LANG
+ unset DISPLAY
+ exit 0
Executing(%build): /bin/sh -e /home/ronbarak/RPMBUILD/tmp/rpm-tmp.qQFuTA
+ umask 022
+ cd /home/ronbarak/RPMBUILD/BUILD
+ LANG=C
+ export LANG
+ unset DISPLAY
+ exit 0
Executing(%install): /bin/sh -e /home/ronbarak/RPMBUILD/tmp/rpm-tmp.8rTMLa
+ umask 022
+ cd /home/ronbarak/RPMBUILD/BUILD
+ '[' /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1 '!=' / ']'
+ rm -rf /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1
++ dirname /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1
+ mkdir -p /home/ronbarak/RPMBUILD/tmp
+ mkdir /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1
+ LANG=C
+ export LANG
+ unset DISPLAY
+ cp /home/ronbarak/RPMBUILD/SOURCES/tty.install.sh /home/ronbarak/RPMBUILD/SOURCES/tty-5.2.0-00-70270.tar /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1
+ cd /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1
+ /usr/lib/rpm/check-buildroot
+ /usr/lib/rpm/redhat/brp-compress
+ /usr/lib/rpm/redhat/brp-strip /usr/bin/strip
+ /usr/lib/rpm/redhat/brp-strip-static-archive /usr/bin/strip
+ /usr/lib/rpm/redhat/brp-strip-comment-note /usr/bin/strip /usr/bin/objdump
+ /usr/lib/rpm/brp-python-bytecompile
+ /usr/lib/rpm/redhat/brp-python-hardlink
+ /usr/lib/rpm/redhat/brp-java-repack-jars
Processing files: tty-cap-5.2-1.noarch
Requires(interp): /bin/sh
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Requires(post): /bin/sh
Checking for unpackaged file(s): /usr/lib/rpm/check-files /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1
Wrote: /home/ronbarak/RPMBUILD/RPMS/noarch/tty-cap-5.2-1.noarch.rpm
Executing(%clean): /bin/sh -e /home/ronbarak/RPMBUILD/tmp/rpm-tmp.Yag9bm
+ umask 022
+ cd /home/ronbarak/RPMBUILD/BUILD
+ '[' /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1 '!=' / ']'
+ rm -rf /home/ronbarak/RPMBUILD/tmp/tty-cap-5.2-1
+ exit 0


$ sudo rpm -Uvh /home/ronbarak/RPMBUILD/RPMS/noarch/tty-cap-5.2-1.noarch.rpm
Preparing...                ########################################### [100%]
   1:tty-cap                ########################################### [100%]
>>> Inside post <<<

###### Starting tty capturing installation ######


Specify installation directory full path [/opt/Intellinx/TTYCapture]:
/opt/Intellinx/TTYCapture already exists. continue installation? (y/n) [y]
Extracting archive tty-5*.* ...

Please specify TTY sensor owner user [ronbarak]:
TTY sensor will be accessible by user 'ronbarak' in group 'ronbarak'

Specify sensor working directory full path [/opt/Intellinx/TTYCapture/work]:

Specify sensor listening port [1024-65000] [8888]:

Do you want to register the sensor service? (y/n) [n]
Server gxttySensorService is not register as a daemon

Executing default user shell (y/n) [y]

TTY capturing has been installed successfully.


解决方案

You are mixing several concepts here. So let make step back.

In %prep section you should unpack your %{SOURCE0} and apply patches if any. This usually do

%setup -q

However if you want you can extract it manually. For more info about this macro see http://www.rpm.org/max-rpm/s1-rpm-inside-macros.html

In %build section you usually compile source into binaries. Likely empty if you use interpreted language or your tar contains already compiled binaries.

In %install section you should copy the files into %{buildroot} and create there structure which will land in package. E.g. %{buildroot}/etc/yourconfig, %{buildroot}/usr/bin/yourcommand etc. You can run there any script you want, but keep in mind that it is run only in build time. I.e. only on your machine (or build system). This is intended for creating files which are automatically generated (e.g. documentation of libraries from source code).

Then you have section %post which is run on user machine after the package was installed. And all files are installed in final path. Not in buildroot. At the beginning you are changed to / so you need to specify full path on that user machine.

So in your case it should be probably look like:

%install
mkdir -p %{buildroot}%{_bindir}
cp -a %{installscript} %{buildroot}%{_bindir}/
chmod a+x %{buildroot}%{_bindir}/%{installscript}

%files
%{_bindir}/%{installscript}

%post
%{_bindir}%{installscript}

Sever notes:

  1. %post section is executed under root, so sudo is not needed.

  2. Running interactive script is strongly discouraged. RPM was designed as non-interactive and every utility around assume no interaction during package installation (e.g. PackageKit, Spacewalk etc.). So sooner then later you will get some compains. It is much safer to say user to run some command after installation manually (or automate it using Ansible or Puppet).

这篇关于如何运行,用脚本从RPM内的互动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆