shell (four) topics for automated deployment of essential skills - custom RPM package

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wzj_110/article/details/100545635

Description : The purpose of writing this article was originally compiled mysql because time is too long a time is too time-consuming, suddenly want to be able to compile a transplant in other places too!

Three ways to install the software under a review

(1) compile and install the software , the advantages can be customized based installation directory, open the on-demand function, the disadvantage is the need to find and test out the appropriate compiler parameters , MySQL software such as compilers takes too long.
(2) yum (Red Hat series) to install the software , the advantage of fully automated installation, do not need to worry about dependence, the disadvantage is poor autonomy, software functionality, storage location have been fixed, and easy to change.
(3) compile the source code , made according to their needs custom RPM package , yum repository set up the network, yum install. The disadvantage is the poor versatility RPM package only applies to the company's environment. Also most people do not customize the RPM package. This is in large Internet companies automate the operation and maintenance of the necessary skills

The concept rpm

rpmbuild packaging tools ( control ); auxiliary 1; Auxiliary 2

detail

# 下载证书-->可以通过firefox的形式下载,更换源-->为com而不是org

gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/

Drawback: rpmbuild relatively obscure; about 7.5

Package management infrastructure

操作系统 	格式 	工具
Debian 	.deb 	apt, apt-cache, apt-get, dpkg
Ubuntu 	.deb 	apt, apt-cache, apt-get, dpkg
CentOS 	.rpm 	yum
Fedora 	.rpm 	dnf
FreeBSD 	Ports, .txz 	make, pkg

reference

Two    rpmbuild production

     The concept : rpmbuild to create software binaries and source code package . A "package" includes archive and to install and uninstall metadata file archiving. The metadata includes helper scripts, file attributes, and related descriptive information.

    Package Two: binary packages for the package has been binary file was compiled ; source code package, the package used to build the information source code and binary packages require.

Description: learning, copy or imitate!

Preliminaries : the old and new versions of the instructions

        In older versions of CentOS in, rpmbuild tool default working directory is / usr / src / redhat, because rights reasons, can not make a general user identity rpm package, only to switch to the root, to be able to make.

        In the new version of CentOS, you can create a new directory as the working directory rpmbuild rpmbuild tool in the general user's home directory. And in the new version, the publisher suggested that in order to prevent the system library files or other damage, do not use as root to make rpm package

       RPM (Redhat Package Manager) is used for Redhat , CentOS, Fedora and other Linux distributions (distribution) of the common package manager. Because it allows to distribute the compiled software, so users only use a single command to install the software.

Contrast : nginx source installation

#!/bin/bash
# (0)创建目录
mkdir -p /usr/local/lnmp/nginx
# (1)解压缩
tar zxvf nginx-1.15.8.tar.gz
# (2)一定要进入目录编译
cd nginx-1.15.8/
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module
# 关于安装的模块-->后续介绍常用的!
# (3)安装依赖库
# 例如gzip模块需要zlib库,rewrite模块需要pcre库,ssl功能需要openssl库等
yum install gcc openssl-devel pcre-devel zlib-devel -y
# https的需要(openssl)、正则的需要(pcre)-->需要什么样的功能在编译的时候就编译进去!
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_modu
# 编译的时候需不需要指定user和group为nginx!
make && make install
# (4)进入配置目录,修改主配置文件
cd /usr/local/lnmp/nginx/
# nobody改成nginx用户启动
# vim conf/nginx.conf
# (5)创建该用户
useradd -s /sbin/nologin nginx
id nginx
# (6)构建软连接,方便直接运行某些命令-->软连接很重要-->后期的升级和内容的迁移等!
ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/bin/

Principle (0) rpmbuild of

       RPM packages when the need to compile the source code, also need the compiled configuration file , binary file commands like something on into the installed like the right place, but also on the RPM packages tested according to need, these need to there is a " workspace ."

# spec文件中引用的一些宏变量主要定义在/usr/lib/rpm/macros -->全局-->定义全局的宏代码

# 自定义,在用户当前的家目录中-->工作目录

touch  ~/.rpmmacros

Core : Production steps

The most critical areas, to have a clear path to the virtual position, and macro definitions

(1) install the necessary tools

See : httpd

Description : build environment and rpmbuild tool

# 不同版本的可能有所差别-->本文是基于RHEL7.5

yum install make gcc rpm-build

# RPM打包使用的是rpmbuild命令,这个命令来自rpm-build包,这个是必装的

(2) Create a working directory to make rpm

rpmbuildCommand uses a standardized set of " workspace "

Mode 1 : manual

Core : To create a working directory to make rpm, you need to create a directory structure under the home directory of the general user identity

rpmbuild directory , as well as six of the directory catalog, including: BUILD, BUILDROOT, RPMS, SOURCES

mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}

# 结构如下

[root@random ~]# tree rpmbuild/
rpmbuild/
├── BUILD
├── BUILDROOT
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS

Embodiment 2 : The automatically generated tool

rpmdevtools

#(1)安装软件

yum install rpmdevtools -y

# rpmdevtools-8.3-5.el7.noarch 软件包

#(2)制作rpmbuild的工作目录

rpmdev-setuptree

执行此条命令,会在"用户家目录下"生成以6个目录

# 二者还是有差别的-->BUILDROOT

Description of the directory structure

Understand : the meaning of a few directories!

" Macro code " of this column can be used on behalf of that corresponding directory files SPEC, similar to the macro programming language or global variables

~/rpmbuildThis folder is also a macro code, called%_topdir

SPEC File : it can be understood as organized attack tool, playbook (Ansible script)

# 写 spec 相当于一种脚本编程

(3) Packaging Process (a bit like a pipeline, several sub-processes)

1. First, the source code needs to put in% _sourcedir; source code are placed
2. Then, compiled , the compilation process is done in the% _builddir, it is necessary to first copy the source code into the directory below, generally , the source code is a compressed format, then decompression over to;
3. third step, a "mounted," here somewhat similar pre-assembled package, the package should contain the content (such as binary files, configuration files, , man documents, etc.) copied to the% _buildrootdir , and in accordance with the directory structure after assembly of the actual installation, such as binary commands may be placed under / usr / bin, then also press at% _buildrootdir according to the same directory structure is placed ;
4 then, you need to configure some necessary work, such as in the actual before you install , after installation of cleanup, as well as before unloading the work to be done friends, etc., which also are configured in the SPEC file to tell the command rpmbuild ;
5. there are further optional operations, check that the software is running;
5. Finally , the resulting RPM package placed% _rpmdir, Source package Set to the next% _srpmdir ( alternative )

These steps are the SPEC configuration file, specifically the various stages

具体操作

rpmbuild所需的

# 1、利用rpmbuild打包,需要两类文件:1、源码,源码以tar归档进行压缩的源码包,以及一些.patch文件,存放于目录./SOURCES下;
# 2、.spec文件,定义了打包的动作,以及依赖,是打包的最主要类容。

第一步:把源码文件放置到 SOURCES 目录下,以nginx为例子(比较简单)

[kiosk@nginx ~]# mv nginx-1.15.8.tar.gz rpmbuild/SOURCES/

# nginx文件也复制到该目录下

第二步:并在SPECS目录下添加配置好的nginx.spec文件

[kiosk@nginx SPECS]$ pwd
/home/kiosk/rpmbuild/SPECS
# 说明:自动有相应的内容生成
[kiosk@nginx SPECS]$ vim nginx.spec

# 不同版本的不一样,但是都是在/usr/share/doc/路径下

less /usr/share/doc/rpm-4.11.3/GROUPS

spec文件的说明

Name:           nginx                         # 软件包名称,后面可使用%{name}引用 
Version:        1.15.8                        # 软件的版本号,与源码包一致
Release:        1%{?dist}                     # 发布序列号.后面可使用%{release}引用
Summary:        nginx generate rpm            # 软件包的内容概要,简要描述信息
                                              # 定义了安装或者编译时的虚拟目录
Group:          Development/Tools             # 软件分组-->less /usr/share/doc/rpm-4.11.3/GROUPS
License:        GPL                           # 软件授权方式,一定带上(最好是对方源码包的 License ) BSD,GPL,GPLv2
URL:            http://nginx.org/download/nginx-1.15.8.tar.gz  # #软件的主页  
Source0:        %{name}-%{version}.tar.gz     # source 主要是引用一下自己定义好的脚本,配置文件之类的内容,#源代码包  
BuildRequires:                                # 编译依赖(只有root可以安装,事先安装)
Requires:                                     # 运行依赖

#  root事先安装这些软件,才能用普通用户编译--->yum install gcc openssl-devel pcre-devel zlib-devel -y

BuildRoot的理解

# BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot 

# %{_tmppath}=/var/tmp

# 这是 make install 时使用的“虚拟”根目录也称为“构建根”目录

# 一般会将软件安装在/var/tmp/软件名称-版本号-发布序列号-buildroot/usr/ 

# 下的bin/ 下(可执行文件)、share/ 下(数据、资源文件)、lib/ 下(共享库文件)

spec的其它

剩余

%description                    # 用来定义描述软件的详细说明
This is a test for nginx.spec
                             
%prep                           # 定义脚本预处理的一些操作,如解包打补丁等
%setup -q   %{name}-%{version}  # 将tar.gz包解压出来;打开软件包,加选项对软件包进行解压处理 去我们的SOURCE目录去找到源码包并解压,这个宏的作用静默模式解压并 cd

%build                          # 开始编译源码构建包,相当于configure以及make部分
%configure                      # 编译制作阶段,这一节主要用于编译源码,这个不是关键字,而是rpm定义的标准宏命令。意思是执行源代码的configure配置
make %{?_smp_mflags}            # 并行-->优化

%install
make install DESTDIR=%{buildroot}

%files
%doc

%changelog

注意:使用./configure代替了%configure参数,解决了Nginx编译参数--host编译选项找不到的问题

遇到的问题1(必须)

解决Nginx源代码封包--host选项未知问题

遇到的问题2(非必须)

rpmbuild报error: Installed (but unpackaged) file(s) found的解决办法

vim /usr/lib/rpm/macros

383 %_unpackaged_files_terminate_build      0

# 把1改为0只警告,非必须!

# root执行--->setfacl -m u:kiosk:rwx /usr/local

遇到的问题3(可以忽略)

343 #%_source_payload       w9.gzdio
344 #%_binary_payload       w9.gzdio

# 在centos6或RHEL6下进行编译的RHEL5或centos5的rpm包

需求1:建立二进制包

-bb      build binary package only from <specfile> # 只建立二进制包

rpmbuild -bb nginx.spec

总的文档

[root@nginx SPECS]# cat nginx.spec 
Name:		nginx
Version:	1.15.8
Release:	1%{?dist}
Summary:	nginx generate rpm

Group:		Development/Tools
License:	GPL
URL:		http://nginx.org/download/nginx-1.15.8.tar.gz
Source0:	%{name}-%{version}.tar.gz
BuildRoot:      %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX) # 创建目录
BuildRequires:	gcc make pcre zlib openssl              # 最好root先安装
Requires:	openssl-devel pcre-devel zlib-devel     # 最好root先安装

%description
This is a test for nginx.spec

%prep
%setup -q %{name}-%{version} 

%build
mkdir -p /usr/local/lnmp/nginx
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module
make %{?_smp_mflags}

%install
#make install DESTDIR=%{buildroot}
make install

%files
%doc                # 该标准定义了文件应该被放置在系统的哪个位置!

%changelog

生成的二进制文件

[root@nginx rpmbuild]# tree RPMS/
RPMS/
└── x86_64
    ├── nginx-1.15.8-1.el7.x86_64.rpm
    └── nginx-debuginfo-1.15.8-1.el7.x86_64.rpm

1 directory, 2 files

测试出现的问题

[root@server1 ~]# rpm -ql nginx      # 没有生成对应的配置文件
(contains no files)

# 后续讲解

nginx-1.15.3

rpmbuild制作mysql-5.7.17的rpm包

Guess you like

Origin blog.csdn.net/wzj_110/article/details/100545635