介绍

1. 支持的源类型包

  1. dir 将目录打包成所需要的类型,可以用于源码编译安装的软件包
  2. rpm 对rpm进行转换
  3. gem 对rubygem包进行转换
  4. python 将python模块打包成相应的类型

2.支持的目标类型包

  1. rpm 转换为rpm包
  2. deb 转换为deb包
  3. solaris 转换为solaris包
  4. puppet 转换为puppet模块

3.FPM参数

常用的几个参数如下,详细使用见fpm –help

  • ​-s 指定源类型​
  • ​-t 指定目标类型,即想要制作为什么包​
  • ​-n 指定包的名字​
  • ​-v 指定包的版本号​
  • ​-C 指定打包的相对路径 Change directory to here before searching forfiles​
  • ​-d 指定依赖于哪些包​
  • ​-f 第二次打包时目录下如果有同名安装包存在,则覆盖它​
  • ​-p 输出的安装包的目录,不想放在当前目录下就需要指定​
  • ​--post-install 软件包安装完成之后所要运行的脚本;同--after-install​
  • ​--pre-install 软件包安装完成之前所要运行的脚本;同--before-install​
  • ​--post-uninstall 软件包卸载完成之后所要运行的脚本;同--after-remove​
  • ​--pre-uninstall 软件包卸载完成之前所要运行的脚本;同--before-remove​
  • ​--vendor 供应商​
  • ​--description 注释信息​

执行步骤

1、fpm软件环境准备

fpm安装(需要外网依赖)
# 安装ruby模块, fpm是ruby写的,因此系统环境需要ruby,且ruby版本号大于1.8.5。
yum -y install ruby rubygems ruby-devel

# 查看当前使用的rubygems仓库
gem sources list

# 添加国内Rubygems仓库,外国的源慢,移除原生的Ruby仓库
gem sources --add https://mirrors.tuna.tsinghua.edu.cn/rubygems/ --remove https://rubygems.org/
gem sources --add https://ruby.taobao.org/ --remove http://rubygems.org/

# 安装fpm
gem install fpm

2、安装脚本内容目录结构

#以 keepalived-v1.3.5-19 为例

软件配置自定义
安装自定义软件包
# 解压源码包,自定义安装目录
cd /tmp && rpm2cpio keepalived-1.3.5-19.el7.src.rpm | cpio -div
tar zxvf keepalived-1.3.5.tar.gz
cd keepalived-1.3.5/
./configure --prefix=/usr/local/keepalived-1.3.5 --sysconf=/etc/keepalived-1.3.5 --sbindir=/usr/local/keepalived-1.3.5/sbin --bindir=/usr/local/keepalived-1.3.5/bin --with-systemdsystemunitdir=/usr/local/keepalived-1.3.5/share/ --runstatedir=/usr/local/keepalived-1.3.5/run/
make &&make install
自定义配置文件
#备份原始配置
tar zcvf /etc/keepalived-1.3.5/keepalived/samples-init.tar.gz /etc/keepalived-1.3.5/keepalived/samples
rm -rf /etc/keepalived-1.3.5/keepalived/samples
mkdir /etc/keepalived-1.3.5/keepalived/conf.d

###添加新配置
cat /etc/keepalived-1.3.5/keepalived/keepalived.conf
####################
! Configuration File for keepalived

global_defs {
#notification_email {
# acassen@firewall.loc
# failover@firewall.loc
# sysadmin@firewall.loc
#}
#notification_email_from Alexandre.Cassen@firewall.loc
#smtp_server 192.168.200.1
#smtp_connect_timeout 30
router_id softname-ip3-ip4
vrrp_garp_master_delay 2
vrrp_garp_master_repeat 1
vrrp_garp_lower_prio_delay 10
vrrp_garp_lower_prio_repeat 1
vrrp_garp_master_refresh 60
vrrp_garp_master_refresh_repeat 2
vrrp_lower_prio_no_advert false
script_user root
enable_script_security
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}

include conf.d/*.conf
cat /etc/keepalived-1.3.5/keepalived/conf.d/default.conf
####################
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.11
192.168.100.12
192.168.100.13
}
}
分发rpm自定义文件
# 创建目录
mkdir -p /data/rpm/keepalived-v1.3.5-19/{installtarget,scripts}

#并将要分发的文件的完整目录放置到 installtarget 目录中
cd /data/rpm/keepalived-v1.3.5-19/installtarget/
mkdir -p etc/rsyslog.d
mkdir -p usr/local
mkdir -p usr/lib/systemd/system
mv /etc/keepalived-1.3.5 etc/
mv /usr/local/keepalived-1.3.5 usr/local/keepalived-v1.3.5-19
mkdir -p usr/local/keepalived-v1.3.5-19/run
mkdir -p data/logs/keepalived

#调整日志输出目录
sed -i 's/KEEPALIVED_OPTIONS.*$/KEEPALIVED_OPTIONS="-D -S 0"/g' etc/keepalived-1.3.5/sysconfig/keepalived
echo "local0.* /data/logs/keepalived/keepalived.log" > etc/rsyslog.d/keepalived.conf
cd /data/rpm/keepalived-v1.3.5-19/installtarget/
#拷贝keepalived启动文件,自定义内容如下
mv usr/local/keepalived-v1.3.5-19/share/keepalived.service usr/lib/systemd/system/keepalived.service
#授权执行权限
chmod +x usr/lib/systemd/system/keepalived.service
##########################################################################
# systemctl启动文件内容
cat usr/lib/systemd/system/keepalived.service
####################
[Unit]
Description=LVS and VRRP High Availability Monitor
After=syslog.target network-online.target

[Service]
Type=forking
PIDFile=/var/run/keepalived.pid
KillMode=process
EnvironmentFile=-/etc/keepalived-1.3.5/sysconfig/keepalived
ExecStart=/usr/local/keepalived-v1.3.5-19/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target
安装后执行的脚本
# 安装后执行的脚本(这是安装完rpm包要执行的脚本)
vim /data/rpm/keepalived-v1.3.5-19/scripts/keepalived_after_install.sh
####################
#!/bin/bash

/bin/ln -snf /usr/local/keepalived-v1.3.5-19 /usr/local/keepalived
/bin/ln -snf /usr/local/keepalived-v1.3.5-19/sbin/keepalived /usr/local/sbin/keepalived
/bin/ln -snf /usr/local/keepalived-v1.3.5-19/bin/genhash /usr/local/bin/genhash
if [ -d '/etc/keepalived' ];then
/bin/ln -snf /etc/keepalived-1.3.5/keepalived /etc/keepalived/keepalived
else
/bin/ln -snf /etc/keepalived-1.3.5/keepalived /etc/keepalived
fi
systemctl daemon-reload
systemctl restart rsyslog.service
systemctl restart keepalived
systemctl disable keepalived
exit 0
卸载脚本内容
vim /data/rpm/keepalived-v1.3.5-19/scripts/keepalived_pre_remove.sh
####################
#!/bin/bash

systemctl daemon-reload
systemctl stop keepalived.service
if [ -L '/etc/keepalived/keepalived' ];then
unlink /etc/keepalived/keepalived
elif [ -L '/etc/keepalived' ];then
unlink /etc/keepalived
fi
unlink /usr/local/keepalived
unlink /usr/local/sbin/keepalived
unlink /usr/local/bin/genhash
vim /data/rpm/keepalived-v1.3.5-19/scripts/keepalived_after_remove.sh
####################
#!/bin/bash

/bin/rm -rf /usr/local/keepalived-v1.3.5-19
/bin/rm -rf /etc/keepalived-1.3.5
/bin/rm -f /usr/lib/systemd/system/keepalived.service
systemctl daemon-reload
exit 0
升级脚本内容
vim /data/rpm/keepalived-v1.3.5-19/scripts/keepalived_before_upgrade.sh

####################
#!/bin/bash

echo 'before_upgrade'
exit 0
keepalived-rpm.sh 脚本
vim /data/rpm/keepalived-v1.3.5-19/keepalived-rpm.sh
####################
#!/bin/bash

work_dir=$(dirname $(readlink -f $0))
install_dir="${work_dir}/installtarget"
scripts_dir="${work_dir}/scripts"
pkg_name=keepalived # 包名
version="1.3.5" # 版本号
commit='modified by qiangsh'
release=19 # release

pre_remove_script="${scripts_dir}/keepalived_pre_remove.sh"
after_remove_script="${scripts_dir}/keepalived_after_remove.sh"
after_install_script="${scripts_dir}/keepalived_after_install.sh"


fpm -s dir -t rpm -n $pkg_name -v $version --description "$commit" --iteration $release -C $install_dir --post-install $after_install_script --pre-uninstall $pre_remove_script --post-uninstall $after_remove_script -p $work_dir -a x86_64 --rpm-dist=el7
打包目录结构
[root@ops-rpmbuild-13-217 ~]# cd /data/rpm/keepalived-v1.3.5-19/
[root@ops-rpmbuild-13-217 keepalived-v1.3.5-19]# tree -L 6
.
|-- installtarget ##rpm 构建根目录
| |-- data ## 本层目录是要分发的目录
| | `-- logs
| | `-- keepalived
| |-- etc
| | |-- keepalived-1.3.5
| | | |-- keepalived
| | | | |-- conf.d
| | | | | `-- default.conf
| | | | |-- keepalived.conf
| | | | `-- samples-init.tar.gz
| | | `-- sysconfig
| | | `-- keepalived
| | `-- rsyslog.d
| | `-- keepalived.conf
| `-- usr
| |-- lib
| | `-- systemd
| | `-- system
| | `-- keepalived.service
| `-- local
| `-- keepalived-v1.3.5-19
| |-- bin
| | `-- genhash
| |-- run
| |-- sbin
| | `-- keepalived
| `-- share
| |-- man
| `-- snmp
|-- keepalived-rpm.sh ## rpm构建脚本
`-- scripts
|-- keepalived_after_install.sh # rpm安装完成后执行的操作
|-- keepalived_after_remove.sh # rpm卸载完成后执行的操作
|-- keepalived_before_upgrade.sh # rpm更新前执行的操作
`-- keepalived_pre_remove.sh # rpm卸载完成前执行的操作

23 directories, 13 files

3.执行构建脚本

[root@ops-rpmbuild-13-217 ~]# cd /data/rpm/keepalived-v1.3.5-19
[root@ops-rpmbuild-13-217 keepalived-v1.3.5-19]# bash keepalived-rpm.sh
Package version '1.3.5-19' includes dashes, converting to underscores {:level=>:warn}
Created package {:path=>"/data/rpm/keepalived-v1.3.5-19/keepalived-1.3.5-19.el7.x86_64.rpm"}

4.安装rpm包

yum localinstall keepalived-1.3.5-19.el7.x86_64.rpm