最好用的RPM打包工具-FPM


FPM打包步驟


一、我們來先安裝FPM:


# FPM是Ruby模塊

yum -y install ruby rubygems ruby-devel


# 添加騰訊的Ruby倉庫

gem sources -a http://gems.ruby-china.org/    #gem sources --add https://ruby.taobao.org/ 


# 移除原生的Ruby倉庫

gem sources --remove http://rubygems.org/


# 安裝fpm

gem install fpm    #gem install fpm -v 1.3.0 -V


#開啓yum緩存

[root@loaclhost~]#sed -i 's#keepcache=0#keepcache=1#g' /etc/yum.conf  



==============================================

FPM常用參數:



-s:指定源類型


-t:指定目標類型,即想要製作爲什麼包


-n:指定包的名字


-v:指定包的版本號


-C:指定打包的相對路徑


-d:指定依賴於哪些包


-f:第二次包時目錄下如果有同名安裝包存在,則覆蓋它


-p:輸出的安裝包的目錄,不想放在當前目錄下就需要指定


--post-install:軟件包安裝完成之後所要運行的腳本;  同--offer-install


--pre-install:軟件包安裝完成之前所要運行的腳本;   同--before-install


--post-uninstall:軟件包卸載完成之後所要運行的腳本;同--offer-remove


--pre-uninstall:軟件包卸載完成之前所要運行的腳本; 同—before-remove


小技巧:'-e' 參數支持打包之前手動修改FPM自動生成的SPEC文件,然後再打包。不過注意CentOS5和CentOS6之間的rpm包不能混用,因爲他們之間的glibc版本不同,動態庫鏈接不上。



==============================================

二、fpm打包過程:



###Installing the nginx

wget http://nginx.org/download/nginx-1.11.8.tar.gz


# 1.創建用戶

groupadd -r nginx

useradd -r -g nginx -s /bin/false -M nginx


# 2.解壓

tar zxvf nginx-1.11.8.tar.gz -C /u01


# 3.進入源碼目錄

cd /u01/nginx-1.11.8


# 4.腳本製作

創建放置服務啓動腳本的目錄:

mkdir -pv /tmp/installdir/etc/rc.d/init.d


爲nginx提供SysV init腳本:

vim /tmp/installdir/etc/rc.d/init.d/nginx

#!/bin/sh

#

# nginx - this script starts and stops the nginx daemon

#

# chkconfig:   - 85 15 

# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \

#               proxy and IMAP/POP3 proxy server

# processname: nginx

# config:      /etc/nginx/nginx.conf

# config:      /etc/sysconfig/nginx

# pidfile:     /var/run/nginx.pid

 

# Source function library.

. /etc/rc.d/init.d/functions

 

# Source networking configuration.

. /etc/sysconfig/network

 

# Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0

 

nginx="/usr/sbin/nginx"

prog=$(basename $nginx)

 

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

 

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

 

lockfile=/var/lock/subsys/nginx

 

make_dirs() {

   # make required directories

   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`

   options=`$nginx -V 2>&1 | grep 'configure arguments:'`

   for opt in $options; do

       if [ `echo $opt | grep '.*-temp-path'` ]; then

           value=`echo $opt | cut -d "=" -f 2`

           if [ ! -d "$value" ]; then

               # echo "creating" $value

               mkdir -p $value && chown -R $user $value

           fi

       fi

   done

}

 

start() {

    [ -x $nginx ] || exit 5

    [ -f $NGINX_CONF_FILE ] || exit 6

    make_dirs

    echo -n $"Starting $prog: "

    daemon $nginx -c $NGINX_CONF_FILE

    retval=$?

    echo

    [ $retval -eq 0 ] && touch $lockfile

    return $retval

}

 

stop() {

    echo -n $"Stopping $prog: "

    killproc $prog -QUIT

    retval=$?

    echo

    [ $retval -eq 0 ] && rm -f $lockfile

    return $retval

}

 

restart() {

    configtest || return $?

    stop

    sleep 1

    start

}

 

reload() {

    configtest || return $?

    echo -n $"Reloading $prog: "

    killproc $nginx -HUP

    RETVAL=$?

    echo

}

 

force_reload() {

    restart

}

 

configtest() {

  $nginx -t -c $NGINX_CONF_FILE

}

 

rh_status() {

    status $prog

}

 

rh_status_q() {

    rh_status >/dev/null 2>&1

}

 

case "$1" in

    start)

        rh_status_q && exit 0

        $1

        ;;

    stop)

        rh_status_q || exit 0

        $1

        ;;

    restart|configtest)

        $1

        ;;

    reload)

        rh_status_q || exit 7

        $1

        ;;

    force-reload)

        force_reload

        ;;

    status)

        rh_status

        ;;

    condrestart|try-restart)

        rh_status_q || exit 0

            ;;

    *)

        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

        exit 2

esac


*FPM會實現腳本功能,有自定義選項需要啓用該腳本

準備rpm安裝後及卸載後所需要運行的腳本:

mkdir -pv /tmp/installdir/tmp

vim install_after.sh

#!/bin/bash

#add user nginx

source /etc/rc.d/init.d/functions

getent group nginx > /dev/null || groupadd -r nginx

getent passwd nginx > /dev/null || useradd -r -g nginx -s /sbin/nologin nginx

exit 0

#此腳本是在nginx安裝好後可以檢測系統上是否有nginx組和用戶,如果沒有就創建。


vim remove_after.sh

#!/bin/bash

#

source /etc/rc.d/init.d/functions 

rm -rf /usr/local/nginx

rm -rf /etc/nginx

userdel nginx

exit 0

#此腳本是在卸載nginx時刪除在安裝時生成的各個目錄及創建的用戶



# 5.指定配置參數

./configure \

  --prefix=/usr \

  --sbin-path=/usr/sbin/nginx \

  --conf-path=/etc/nginx/nginx.conf \

  --error-log-path=/var/log/nginx/error.log \

  --http-log-path=/var/log/nginx/access.log \

  --pid-path=/var/run/nginx/nginx.pid  \

  --lock-path=/var/lock/nginx.lock \

  --user=nginx \

  --group=nginx \

  --with-http_ssl_module \

  --with-http_flv_module \

  --with-http_stub_status_module \

  --with-http_gzip_static_module \

  --http-client-body-temp-path=/var/tmp/nginx/client/ \

  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \

  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \

  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

  --http-scgi-temp-path=/var/tmp/nginx/scgi \

  --with-pcre


# 6.使用多核編譯

make -j24

mkdir /tmp/installdir 


# 7.指定安裝路徑

make install DESTDIR=/tmp/installdir/


# 8.生成RPM

fpm -s dir -t rpm --vendor CentOS6.5 -n nginx -v 1.11.8 --iteration 1.el6 -d 'pcre-devel,openssl-devel' \

--category 'Development/Languages' -m 'rongxinxxxx' --url 'http://www.lnredone.com' --description 'nginx server' --license 'GPL.GPLv2+,GPLv' \

--post-install /tmp/installdir/tmp/install_after.sh --post-uninstall /tmp/installdir/tmp/remove_after.sh -C /tmp/installdir/ -p /u01 -e




三、查看打包信息


查看RPM包信息

rpm -qpi nginx-1.11.8-release.x86_64.rpm 


查看rpm包中的內容:

rpm -qpl nginx-1.11.8-release.x86_64.rpm

 

查看rpm包的依賴

rpm -qpR nginx-1.11.8-release.x86_64.rpm


查看rpm執行的腳本

rpm -qp --scripts nginx-1.11.8-release.x86_64.rpm





================================================

fpm打包實例:


1.fpm打包Nginx

[root@localhost opt]# fpm -s dir -t rpm -n nginx -v 1.11.8 -d 'pcre-devel,openssl-devel' --post-install /opt/nginx_rpm.sh -f /opt/nginx/

no value for epoch is set, defaulting to nil {:level=>:warn}

no value for epoch is set, defaulting to nil {:level=>:warn}

Created package {:path=>"nginx-1.6.1-1.x86_64.rpm"}


2.fpm相對路徑打包:

[root@localhost~]# fpm -s dir -t rpm -n opt -v 1.1.1.1 -d 'gcc,gcc+' -C ../opt/   

no value for epoch is set, defaulting to nil {:level=>:warn}

no value for epoch is set, defaulting to nil {:level=>:warn}

Created package {:path=>"opt-1.1.1.1-1.x86_64.rpm"}


3.使用fpm將生成包指定到/tmp下:

[root@localhost ~]# fpm -s dir -t rpm -n ansible-v 1.1.1.1 -d 'gcc,gcc+' -f ansible -p /tmp/

no value for epoch is set, defaulting to nil {:level=>:warn}

no value for epoch is set, defaulting to nil {:level=>:warn}

Created package {:path=>"/tmp/ansible-1.1.1.1-1.x86_64.rpm"}





鏡像同步公網YUM源:

centos官方標準源:

rsync://mirrors.ustc.edu.cn/centos/6/os/x86_64/

rsync://mirrors.ustc.edu.cn/centos/6/extras/x86_64/

rsync://mirrors.ustc.edu.cn/centos/6/updates/x86_64/


epel源:

rsync://mirrors.ustc.edu.cn/epel/6/x86_64/

命令:reposync




###報錯

1.Need executable 'rpmbuild' to convert dir to rpm {:level=>:error}

解決方法:

[root@localhost]# yum install -y rpm-build


2.如果裏面有gcc make的錯誤.

解決方法:

yum install -y gcc








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章