使用fpm来制作rpm包

2017/2/22


一、配置fpm环境1、安装ruby和fpm使用rvm来管理rubycurl-sSLhttps://rvm.io/mpapis.asc|gpg2--import-curl-Lhttps://get.rvm.io|bash-sstable--ruby重新打开一个shell#ruby-vruby2.4.0p0(2016-12-24revision57164)[x86_64-linux]安装fpm依赖包yuminstallrpm-build-y安装fpm[root@tvm-rpm~]#geminstallfpm-V2、fpm的参数参考:https://github.com/jordansissel/fpm/wiki%fpm-s<sourcetype>-t<targettype>[options]-s源格式-t目标格式-n包名-vversion值,实际版本号--iterationrelease值,发布序列号--epochepoch值--vendor厂商--maintainer维护者--description描述--url软件主页--workdirfpm工作目录-d依赖的软件包--directories递归指定的目录标记为属于这个包-C切换到指定的目录-p输出到指定的路径--force强制覆盖文件--after-installFILE包安装后执行的脚本--before-installFILE包安装前执行的脚本--after-removeFILE包移除后执行的脚本--before-removeFILE包移除前执行的脚本--after-upgradeFILE包升级后执行的脚本--before-upgradeFILE包升级前执行的脚本-ebuilding前编辑spec文件二、示例【实例1:将python3的源码打包成rpm来安装】1、配置编译python环境所需的包[root@tvm-rpm~]#mkdir/data/{download,rpms}使用局域网的epel源:[root@tvm-rpm~]#mv/etc/yum.repos.d/*.repo/tmp/\&&wgethttp://mirrors.office.test/local-office.repo-O/etc/yum.repos.d/local-office.repo\&&yumcleanall\&&yummakecache注:如果要使用公网的epel源,可以这样操作:#rpm-Uvhhttp://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm[root@tvm-rpm~]#yum-ygroupinstall"Developmenttools"[root@tvm-rpm~]#yum-yinstallopenssl-develreadline-develbzip2-develsqlite-develzlib-develncurses-develdb4-develexpat-devel下载最新的python源码包:[root@tvm-rpm~]#wgethttps://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz-O/data/download/Python-3.4.3.tgz[root@tvm-rpm~]#cd/data/download/[root@tvm-rpmdownload]#tarxfPython-3.4.3.tgz[root@tvm-rpmdownload]#cdPython-3.4.3[root@tvm-rpmPython-3.4.3]#[root@tvm-rpmPython-3.4.3]#exportDIR_PY_SRC_INSTALL=/data/rpms/Python-3.4.3[root@tvm-rpmPython-3.4.3]#exportDIR_PY_RPM_INSTALL=/usr/local[root@tvm-rpmPython-3.4.3]#exportLDFLAGS="-Wl,-rpath=${DIR_PY_RPM_INSTALL}/lib${LDFLAGS}"[root@tvm-rpmPython-3.4.3]#./configure--prefix=${DIR_PY_RPM_INSTALL}[root@tvm-rpmPython-3.4.3]#make[root@tvm-rpmPython-3.4.3]#makeinstallDESTDIR=${DIR_PY_SRC_INSTALL}[root@tvm-rpmPython-3.4.3]#cd..我们来看一下,生成了那些目录和文件:[root@tvm-rpmPython-3.4.3]#cd/data/rpms[root@tvm-rpmPython-3.4.3]#lsPython-3.4.3/usr/local/binincludelibshare2、开始打包[root@tvm-rpmPython-3.4.3]#fpm-sdir-trpm\-npython3\-v'3.4.3'\--iteration'1.el6'\--epoch'0'\--vendor'PythonSoftwareFoundation'\--maintainer'PC'\--description'usefpmtomakeapkgforpython-3.4.3'\--url'https://www.python.org/downloads/release/python-343/'\--workdir/data/rpms\-p/data/rpms/pkgs/\-C${DIR_PY_SRC_INSTALL}\--directories=${DIR_PY_RPM_INSTALL}/lib\--directories=${DIR_PY_RPM_INSTALL}/include\-d'openssl'\-d'bzip2'\-d'zlib'\-d'expat'\-d'db4'\-d'sqlite'\-d'ncurses'\-d'readline'Createdpackage{:path=>"/data/rpms/pkgs/python3-3.4.3-1.el6.x86_64.rpm"}我们来看看包的内容:[root@tvm-rpmrpms]#rpm-qpipkgs/python3-3.4.3-1.el6.x86_64.rpmName:python3Relocations:/Version:3.4.3Vendor:PythonSoftwareFoundationRelease:1.el6BuildDate:Wed15Jul201504:55:17PMCSTInstallDate:(notinstalled)BuildHost:tvm-rpmGroup:defaultSourceRPM:python3-3.4.3-1.el6.src.rpmSize:126285890License:unknownSignature:(none)Packager:PCURL:https://www.python.org/downloads/release/python-343/Summary:usefpmtomakeapkgforpython-3.4.3Description:usefpmtomakeapkgforpython-3.4.33、安装测试1)安装前:[root@tvm-rpmrpms]#ls/usr/local/{bin,include,lib,share/man}/usr/local/bin:/usr/local/include:/usr/local/lib:/usr/local/share/man/man1:2)开始安装:[root@tvm-rpmrpms]#rpm-ivhpkgs/python3-3.4.3-1.el6.x86_64.rpmPreparing...###########################################[100%]1:python3###########################################[100%][root@tvm-rpmrpms]#rpm-qa|greppython3python3-3.4.3-1.el6.x86_643)安装后:[root@tvm-rpmrpms]#ls/usr/local/{bin,include,lib,share/man/man1}/usr/local/bin:2to32to3-3.4easy_install-3.4idle3idle3.4pip3pip3.4pydoc3pydoc3.4python3python3.4python3.4-configpython3.4mpython3.4m-configpython3-configpyvenvpyvenv-3.4/usr/local/include:python3.4m/usr/local/lib:libpython3.4m.apkgconfigpython3.4/usr/local/share/man/man1:python3.1python3.4.1执行python命令,查看版本:[root@tvm-rpmrpms]#python3-c"importsys;print(sys.version)"3.4.3(default,Jul152015,14:40:59)[GCC4.4.720120313(RedHat4.4.7-11)]4)卸载:[root@tvm-rpmrpms]#rpm-epython3-3.4.3-1.el6.x86_64[root@tvm-rpmrpms]#ls/usr/local/{bin,include,lib,share/man/man1}/usr/local/bin:/usr/local/include:/usr/local/lib:/usr/local/share/man/man1:符合预期。【实例2:打包yum源的配置文件为rpm包】[root@tvm-rpmpkgs]#cd/data/rpms/pkgs/[root@tvm-rpmpkgs]#fpm-sdir-trpm\-a'x86_64'\-n'office-repo-latest'\-v'6'\--iteration'1.el6'\--epoch'0'\--vendor'pc@office'\--maintainer'PC'\--description'providefile:[local-office.repo]forlocalusers.include:centos-base,eple,user-definerpms'\/etc/yum.repos.d/local-office.repoCreatedpackage{:path=>"office-repo-latest-6-1.el6.noarch.rpm"}[root@tvm-rpmpkgs]#rpm-qploffice-repo-latest-6-1.el6.x86_64.rpm/etc/yum.repos.d/local-office.repo[root@tvm-rpmpkgs]#rpm-qpioffice-repo-latest-6-1.el6.x86_64.rpmName:office-repo-latestRelocations:/Version:6Vendor:pc@officeRelease:1.el6BuildDate:Thu06Aug201504:53:26PMCSTInstallDate:(notinstalled)BuildHost:tvm-rpmGroup:defaultSourceRPM:office-repo-latest-6-1.el6.src.rpmSize:2392License:unknownSignature:(none)Packager:PCURL:http://example.com/no-uri-givenSummary:providefile:[local-office.repo]forlocalusers.include:centos-base,eple,user-definerpmsDescription:providefile:[local-office.repo]forlocalusers.include:centos-base,eple,user-definerpms测试1:将打包的文件拷贝到其他主机上[root@tvm-test~]#mv/etc/yum.repos.d/local-office.repo/tmp/安装:[root@tvm-test~]#rpm-ivhoffice-repo-latest-6-1.el6.x86_64.rpmPreparing...###########################################[100%]1:office-repo-latest###########################################[100%][root@tvm-test~]#diff/etc/yum.repos.d/local-office.repo/tmp/local-office.repo测试2:将打包的文件拷贝到本地的yum源来提供安装。[root@tvm-yum~]#mvoffice-repo-latest-6-1.el6.x86_64.rpm/data/yum/repo/先移除之前测试1安装的包:[root@tvm-test~]#rpm-qa|grepofficeoffice-repo-latest-6-1.el6.x86_64[root@tvm-test~]#rpm-eoffice-repo-latest-6-1.el6.x86_64安装:[root@tvm-test~]#rpm-ivhhttp://mirrors.office.test/office-repo-latest-6-1.el6.x86_64.rpmRetrievinghttp://mirrors.office.test/office-repo-latest-6-1.el6.x86_64.rpmPreparing...###########################################[100%]1:office-repo-latest###########################################[100%][root@tvm-test~]#diff/etc/yum.repos.d/local-office.repo/tmp/local-office.repo【实例3:打包最新版的monit】1)从官网下载最新的binary[root@tvm-rpmrpms]#mkdirmonit&&cdmonit[root@tvm-rpmmonit]#wgethttps://mmonit.com/monit/dist/binary/5.14/monit-5.14-linux-x64.tar.gz[root@tvm-rpmmonit]#tarzxvfmonit-5.14-linux-x64.tar.gzmonit-5.14/monit-5.14/bin/monit-5.14/bin/monitmonit-5.14/COPYINGmonit-5.14/conf/monit-5.14/conf/monitrcmonit-5.14/man/monit-5.14/man/man1/monit-5.14/man/man1/monit.1[root@tvm-rpmmonit]#cdmonit-5.14[root@tvm-rpmmonit-5.14]#llbin/conf/man/man1/bin/:total2688-rwxr-xr-x1rootroot2752045Jun918:18monitconf/:total12-rw-------1rootroot11220Jun918:18monitrcman/man1/:total132-rw-r--r--1rootroot131280Jun918:18monit.1拷贝到合适的位置:[root@tvm-rpmmonit-5.14]#cp-abin/monit/usr/bin/[root@tvm-rpmmonit-5.14]#cp-aconf/monitrc/etc/[root@tvm-rpmmonit-5.14]#cp-aman/man1/monit.1/usr/share/man/man1/增加一个控制脚本:[root@tvm-rpmmonit-5.14]#llinit.d/total4-rwxr-xr-x1rootroot1272Jun52014monit[root@tvm-rpmmonit-5.14]#cp-ainit.d/monit/etc/init.d/创建目录:[root@tvm-rpmmonit-5.14]#mkdir/etc/monit.d调整配置文件:[root@tvm-rpmmonit-5.14]#grep^[^#]/etc/monitrcsetdaemon120withstartdelay240setlogfile/var/log/monitinclude/etc/monit.d/*2)打包[root@tvm-rpmmonit-5.14]#cd/data/rpms/pkgs/[root@tvm-rpmpkgs]#fpm-sdir-trpm\-a'x86_64'\-n'monit'\-v'5.14'\--iteration'1.el6'\--epoch'0'\--vendor'pc@office'\--maintainer'PC'\--description'monit-5.14forlocalusers'\/usr/bin/monit\/etc/monitrc\/etc/monit.d\/usr/share/man/man1/monit.1\/etc/init.d/monitCreatedpackage{:path=>"monit-5.14-1.el6.x86_64.rpm"}[root@tvm-rpmpkgs]#rpm-qplmonit-5.14-1.el6.x86_64.rpm/etc/init.d/monit/etc/monit.d/etc/monitrc/usr/bin/monit/usr/share/man/man1/monit.1[root@tvm-rpmpkgs]#rpm-qpimonit-5.14-1.el6.x86_64.rpmName:monitRelocations:/Version:5.14Vendor:pc@officeRelease:1.el6BuildDate:Fri28Aug201506:00:57PMCSTInstallDate:(notinstalled)BuildHost:tvm-rpmGroup:defaultSourceRPM:monit-5.14-1.el6.src.rpmSize:2895915License:unknownSignature:(none)Packager:PCURL:http://example.com/no-uri-givenSummary:monit-5.14forlocalusersDescription:monit-5.14forlocalusers测试1:将打包的文件拷贝到其他主机上[root@tvm-test~]#rpm-Uvhmonit-5.14-1.el6.x86_64.rpmPreparing...###########################################[100%]1:monit###########################################[100%][root@tvm-test~]#servicemonitstartStartingmonit:StartingMonit5.14daemonMonitstartdelayset--pausefor240s[OK][root@tvm-test~]#chkconfigmoniton卸载:[root@tvm-test~]#rpm-emonit-5.14-1.el6.x86_64[root@tvm-test~]#ls/usr/bin/monit/etc/monitrcls:cannotaccess/usr/bin/monit:Nosuchfileordirectoryls:cannotaccess/etc/monitrc:Nosuchfileordirectory可以发现,已经被删除。测试2:更新到本地的yum源后安装[root@tvm-yumx86_64]#pwd/data/yum/repo/office/6/x86_64上传rpm包,重建repo:[root@tvm-yumx86_64]#createrepo.Spawningworker0with30pkgsWorkersFinishedGatheringworkerresultsSavingPrimarymetadataSavingfilelistsmetadataSavingothermetadataGeneratingsqliteDBsSqliteDBscomplete安装:[root@tvm-test~]#yummakecache[root@tvm-test~]#yuminstallmonitLoadedplugins:fastestmirror,securityLoadingmirrorspeedsfromcachedhostfileSettingupInstallProcessResolvingDependencies-->Runningtransactioncheck--->Packagemonit.x86_640:5.14-1.el6willbeinstalled-->FinishedDependencyResolutionDependenciesResolved===============================================================================================================================PackageArchVersionRepositorySize===============================================================================================================================Installing:monitx86_645.14-1.el6office1.2MTransactionSummary===============================================================================================================================Install1Package(s)Totaldownloadsize:1.2MInstalledsize:2.8MIsthisok[y/N]:yDownloadingPackages:monit-5.14-1.el6.x86_64.rpm|1.2MB00:00Runningrpm_check_debugRunningTransactionTestTransactionTestSucceededRunningTransactionInstalling:monit-5.14-1.el6.x86_641/1Verifying:monit-5.14-1.el6.x86_641/1Installed:monit.x86_640:5.14-1.el6Complete!【实例4:打包tengine】1、准备mkdir/opt/fpm/{src,rpms,install}-pexportDIR_SRC_MAKE_INSTALL=/opt/fpm/installexportDIR_RPM_INSTALL=/opt/tengineexportDIR_RPMS=/opt/fpm/rpmsexportDIR_FPM_SCRIPTS=/opt/fpm/scripts2、源码编译cd/opt/fpm/srcwgethttp://tengine.taobao.org/download/tengine-2.1.2.tar.gzyuminstallpcrepcre-devel-ytarzxvftengine-2.1.2.tar.gz&&cdtengine-2.1.2./configure--prefix=${DIR_RPM_INSTALL}===================================================注意以下输出,确认是否符合预期。Configurationsummary+usingsystemPCRElibrary+usingsystemOpenSSLlibrary+md5:usingOpenSSLlibrary+sha1:usingOpenSSLlibrary+usingsystemzliblibrary+jemalloclibraryisdisablednginxpathprefix:"/opt/tengine"nginxbinaryfile:"/opt/tengine/sbin/nginx"nginxconfigurationprefix:"/opt/tengine/conf"nginxconfigurationfile:"/opt/tengine/conf/nginx.conf"nginxpidfile:"/opt/tengine/logs/nginx.pid"nginxerrorlogfile:"/opt/tengine/logs/error.log"nginxhttpaccesslogfile:"/opt/tengine/logs/access.log"nginxhttpclientrequestbodytemporaryfiles:"client_body_temp"nginxdsomodulepath:"/opt/tengine/modules/"nginxhttpproxytemporaryfiles:"proxy_temp"nginxhttpfastcgitemporaryfiles:"fastcgi_temp"nginxhttpuwsgitemporaryfiles:"uwsgi_temp"nginxhttpscgitemporaryfiles:"scgi_temp"===================================================make&&makeinstallDESTDIR=${DIR_SRC_MAKE_INSTALL}3、自定义配置文件和脚本[root@tvm-rpmtengine-2.1.2]#cd/opt/fpm1)编辑脚本,用于rpm包安装和卸载时执行指令:[root@tvm-rpmfpm]#mkdirscripts[root@tvm-rpmfpm]#catscripts/after-install.sh#!/bin/bash##2017/2/22#userecho-e'\n\033[1;34mCreateusernginx:\033[0m\n'idnginx>/dev/null2>&1&&echo'User[nginx]exist.'||(useradd-s/sbin/nologin-d/var/cache/nginx-c"nginxuser"nginx;idnginx)echo-e'\n\033[1;34mConfigurationsummary:\033[0m\n'cat<<'_EOF'===================================================+usingsystemPCRElibrary+usingsystemOpenSSLlibrary+md5:usingOpenSSLlibrary+sha1:usingOpenSSLlibrary+usingsystemzliblibrary+jemalloclibraryisdisablednginxpathprefix:"/opt/tengine"nginxbinaryfile:"/opt/tengine/sbin/nginx"nginxconfigurationprefix:"/opt/tengine/conf"nginxconfigurationfile:"/opt/tengine/conf/nginx.conf"nginxpidfile:"/opt/tengine/logs/nginx.pid"nginxerrorlogfile:"/opt/tengine/logs/error.log"nginxhttpaccesslogfile:"/opt/tengine/logs/access.log"nginxhttpclientrequestbodytemporaryfiles:"client_body_temp"nginxdsomodulepath:"/opt/tengine/modules/"nginxhttpproxytemporaryfiles:"proxy_temp"nginxhttpfastcgitemporaryfiles:"fastcgi_temp"nginxhttpuwsgitemporaryfiles:"uwsgi_temp"nginxhttpscgitemporaryfiles:"scgi_temp"startupscript:/etc/init.d/tengine===================================================_EOFecho-e'\n\033[1;33mTenginehasbeensuccessfullyinstalled.\033[0m\n'[root@tvm-rpmfpm]#catscripts/before-remove.sh#!/bin/bash##2017/2/22echo-e'\n\033[1;33mInordertobackupyoudata,pleasefollowtheguide,controlitbyhand.\033[0m\n'#serviceecho-e'\n\033[1;34mServicestopped.\033[0m\n'servicetenginestop#userecho-e'\n\033[1;34m[Guide]Trytoremoveusernginx?\033[0m\n'echo'userdel-frnginx'#direcho-e'\n\033[1;34mDirectorybackuped:[/opt/tengine]->[/opt/tengine.old]\033[0m\n'mv-fv/opt/tengine/opt/tengine.old2)控制脚本[root@tvm-rpmfpm]#mkdirinstall/etc/init.d[root@tvm-rpmfpm]#viminstall/etc/init.d/tengine(略)[root@tvm-rpmfpm]#chmod+xinstall/etc/init.d/tengine4、开始打包[root@tvm-rpmfpm]#fpm-sdir-trpm\-nTengine\-v'2.1.2'\--iteration'1.el6'\--epoch'0'\--vendor'Tengine@taobao'\--maintainer'PC'\--description'TengineisbasedonNginxwhichstandsforEngine-X.'\--url'tengine.taobao.org'\-C${DIR_SRC_MAKE_INSTALL}\-p${DIR_RPMS}\-d'pcre'\--after-install${DIR_FPM_SCRIPTS}/after-install.sh\--before-remove${DIR_FPM_SCRIPTS}/before-remove.sh\-fCreatedpackage{:path=>"/opt/fpm/rpms/Tengine-2.1.2-1.el6.x86_64.rpm"}5、查看rpm包内的文件:[root@tvm-rpmfpm]#rpm-qpl/opt/fpm/rpms/Tengine-2.1.2-1.el6.x86_64.rpm[root@tvm-rpmfpm]#tree-L2.├──install->fpm打包时,将chroot到这个目录,对应安装后的文件系统中以下路径:/opt,/etc│├──etc->在nginx的安装包中拷贝/etc/init.d/nginx,改一下配置文件中的相关路径为tengine的即可,然后将文件拷贝到这里(/etc/init.d/tengine)即可。│└──opt->源码安装后的目录├──rpms│└──Tengine-2.1.2-1.el6.x86_64.rpm->rpm包保存在这里├──scripts│├──after-install.sh->安装rpm包后执行这个脚本│└──before-remove.sh->卸载rpm包前执行这个脚本└──src├──tengine-2.1.2->源码编译的工作目录└──tengine-2.1.2.tar.gz->源码7directories,4files6、拷贝rpm包到一台新的机器上测试:符合预期。ZYXW、参考1、fpm/wikihttps://github.com/jordansissel/fpm/wiki2、使用FPM创建Python的RPM包http://theo.im/blog/2014/05/16/use-fpm-to-create-python-rpm-packages/3、使用FPM快速生成RPM包https://linux.cn/article-3184-1.html