ZooKeeper从源码到RPM包制作过程详解

Posted Erik_ly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZooKeeper从源码到RPM包制作过程详解相关的知识,希望对你有一定的参考价值。

本文以 ZooKeeper 为例详细介绍了一种通用的从源码到 RPM 包的详细制作过程。

简介

在使用 Ambari 安装或集成大数据组件时, CentOS 系统下,需要将组件格式制作成 rpm 格式,这里总结一种通用简便的从大数据组件源码制作 rpm 格式文件的方法:将源码编译成tar包,然后再将tar包打成 rpm

这里以HDP版本的 ZooKeeper 为例介绍这种方法,其实 ZooKeeper 是可以直接从源码制作成rpm包的,这里为了介绍通用的做法,采用先将 ZooKeeper 的源码编译成 tar.gz 包,然后再制作成 rpm的方式。

源码编译

下载源码

Release HDP-2.6.4.91-3-tag: Modify HDP-CHANGES.txt to include ZOOKEEPER-2146 · hortonworks/zookeeper-release
https://github.com/hortonworks/zookeeper-release/releases/tag/HDP-2.6.4.91-3-tag

解压

tar -zxvf zookeeper-release-HDP-2.6.4.91-3-tag.tar.gz

制作 tar 包

yum install ant -y
yum install cppunit-devel -y
yum install libtool

ant 打包

cd zookeeper-release-HDP-2.6.5.148-3-tag/
ant tar

查看 tar 包

打包后的tar.gz位于zookeeper下的build目录

准备环境

所用命名未特殊说明的情况下均为使用root用户执行

安装 rpm-build 包

yum install rpm-build

安装 rpmdevtools

yum install rpmdevtools

创建工作空间

rpmdev-setuptree

制作 rpm 包

编辑 spec 文件

制作 rpm 包需要用到一个 spec 格式的文件,这里使用zookeeper.spec 文件的内容为:

Name: 		zookeeper
Version: 	3.4.6
# Avoid some silly dependencies and allow
# symlink in the init.d folder
AutoReqProv: 	no
Release: 	0
Summary: 	Zookeeper is a highly reliable distributed coordination service.
License: 	Apache 2.0
Group: 		Applications/Internet
Source0: 	zookeeper-3.4.6.tar.gz
Prefix:		/opt
BuildArch: 	noarch
BuildRoot: 	%_tmppath/%name-root
Provides: 	zookeeper
Requires(post): /sbin/chkconfig, /sbin/service
Requires(preun): /sbin/chkconfig, /sbin/service
%description
ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them ,which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed.

%prep

%build

%install
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/opt/zookeeper
mkdir -p $RPM_BUILD_ROOT/etc/init.d
mkdir -p %_localstatedir/lib/zookeeper
tar xfz $RPM_SOURCE_DIR/zookeeper-%version.tar.gz
cp -r $RPM_BUILD_DIR/zookeeper-%version/* $RPM_BUILD_ROOT/opt/zookeeper

#cp /opt/zookeeper/bin/zkEnv.sh %_sysconfdir/init.d/zkEnv.sh
cp $RPM_BUILD_ROOT/opt/zookeeper/conf/zoo_sample.cfg $RPM_BUILD_ROOT/opt/zookeeper/conf/zoo.cfg

#%pre
#%preun
#/sbin/service zookeeper stop > /dev/null 2>&1
#/sbin/chkconfig --del zookeeper

#%clean
rm -rf $RPM_BUILD_ROOT

%post
cp /opt/zookeeper/bin/zkEnv.sh %_sysconfdir/init.d/zkEnv.sh
ln -sf /opt/zookeeper/bin/zkServer.sh %_sysconfdir/init.d/zookeeper
/sbin/chkconfig zookeeper on
#/sbin/service zookeeper start

%files
%defattr(-,root,root,-)
/opt/zookeeper

%changelog
* Tue Aug 19 2014 - daniel.beneyto at abiquo dot com
- Initial release.

放置文件

zookeeper.spec文件放到 /root/rpmbuild/SPECS 文件夹中,将编译好的zookeeper-3.4.6.tar.gz放在 /root/rpmbuild/SOURCES 文件夹中

执行打 rpm 包命令

cd /root/rpmbuild/SPECS
rpmbuild -ba zookeeper.spec

查看 rpm 包

打包好的rpm包在/root/rpmbuild/RPMS文件夹中

安装测试

现在将制作出来的 rpm 包安装测试。

安装

rpm -Uvh zookeeper-3.4.6-0.noarch.rpm

启动

cd /opt/zookeeper/bin/
./zkServer.sh start

查看状态

至此,说明制作的 rpm 包是可用的。

spec 文件解析

这个方法的关键在于先后步骤及 spec 文件,现在来看下 spec 文件的几点注意事项


Version中不能出现“-”等字符
Source0需要与SOURCES中的文件名一致


创建打包过程中文件存放位置,解压并复制文件


复制zookeeper配置文件,也可不在此设置,启动之前手动配置


创建软连接

注意事项

实际上使用"下载源码"中的链接下载的源码包在编译成 tar 包的过程中是会报错的,为了不打乱步骤介绍,特在这里说明。
在执行

rpmbuild -ba zookeeper.spec

后,会出现如下错误:

可以看出是源码中的加注释时格式出错了,解决办法为修改源码后重新打包

cd /root/rpmbuild/SOURCES
mv zookeeper-3.4.6.tar.gz zookeeper-3.4.6.tar.gz.bak
tar -xvf zookeeper-3.4.6.tar.gz
cd zookeeper-3.4.6/

修改文件/root/rpmbuild/SOURCES/zookeeper-3.4.6/contrib/zkpython/src/python/zk.py/root/rpmbuild/SOURCES/zookeeper-3.4.6/src/contrib/zkpython/src/python/zk.py两个文件,


将两个zk.py文件中去掉如下图红框中的一行代码

重新打成 tar 包

cd /root/rpmbuild/SOURCES/
tar zcvf zookeeper-2.6.4.tar.gz zookeeper-2.6.4

这时重新打包rpm
首先清空BUILD、BUILDROOT、RPMS、SRPMS文件夹,重新执行:

cd /root/rpmbuild/SPECS
rpmbuild -ba zookeeper.spec

参考资料

  • RPM打包原理、示例、详解及备查 - 刘康的专栏 - CSDN博客
    https://blog.csdn.net/get_set/article/details/53453320

  • [ZOOKEEPER-2777] There is a typo in zk.py which prevents from using/compiling it. - ASF JIRA
    https://issues.apache.org/jira/browse/ZOOKEEPER-2777

  • CentOS7环境下zookeeper-release-HDP-2.6.5.148-3-tag编译打RPM包 - 汤昕的博客 - CSDN博客
    https://blog.csdn.net/Happy_Sunshine_Boy/article/details/90479508

注:本文首发于个人博客网站:http://www.erik-ly.com/2019/12/29/ZooKeeper-rpm/

以上是关于ZooKeeper从源码到RPM包制作过程详解的主要内容,如果未能解决你的问题,请参考以下文章

ZooKeeper从源码到RPM包制作过程详解

RPM包制作之Spec文件详解

linux 基础入门 软件安装 rpmyum与源码安装详解

万字详解Linux下安装软件的方式(yum配置+rpm+源码安装)

Apache ZooKeeper 服务启动源码解释

Linux中centos中httpd源码安装过程详解