天天看點

salt spec檔案淺析

上一篇使用salt.src.rpm檔案生成salt rpm包,src.rpm内實際上就是.tar.gz源碼、patch檔案、.spec檔案組成。生成rpm包除了源碼外,還要懂得編寫spec檔案,spec檔案一般包括軟體包基礎資訊、源碼包解壓、安裝路徑等。

[root@localhost rpmbuild]# vim SPECS/salt.spec  # 來看下salt spec檔案
%if ! (0%{?rhel} >= 6 || 0%{?fedora} > 12)
%global with_python26 1  
%define pybasever 2.6
%define __python_ver 26
%define __python %{_bindir}/python%{?pybasever}
%endif
%global include_tests 0
%{!?python_sitelib: %global python_sitelib %(%{__python} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")}
%{!?python_sitearch: %global python_sitearch %(%{__python} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")}
%{!?pythonpath: %global pythonpath %(%{__python} -c "import os, sys; print(os.pathsep.join(x for x in sys.path if x))")}

%define _salttesting SaltTesting
%define _salttesting_ver 2014.8.5      

分析:

要看懂上面那些内容,我們需要先看幾種結構。

1、%if 0%(?rhel)   

在spec檔案中0為假,非0為真

如果%{rhel}被定義了,則 %{?rhel} 傳回 %{rhel},不然%{?rhel}視為未定義。

是以,當%{rhel}被定義過的話, 0%{?rhel}就等于0%{rhel} ,執行這個if block;

反之,0%{?rhel}等于0,不執行這個if block。

2、?:結構

%{?變量:動作1}

此條件與前面的%if有點不同,其隻判斷變量是否定義,定義了就為真,否則就為假,即使變量定義為0,也為真,并運作後面的語句。

還需要了解一些系統内置的宏,如下:

%_prefix  /usr
%_exec_prefix  %{_prefix}
%_bindir  %{_exec_prefix}/bin
%_sbindir  %{_exec_prefix}/sbin
%_libexecdir  %{_exec_prefix}/libexec
%_datadir  %{_prefix}/share
%_sysconfdir  %{_prefix}/etc
%_sharedstatedir  %{_prefix}/com
%_localstatedir  %{_prefix}/var
%_libdir  %{_exec_prefix}/lib
%_includedir  %{_prefix}/include
%_oldincludedir  /usr/include
%_infodir  %{_prefix}/info
%_mandir  %{_prefix}/man      

至于%global和%define的差別,根據rpm.org的wiki上面是這樣描述的:

%define ...     define a macro
%undefine ...   undefine a macro 
%global ...     define a macro whose body is available in global context # 看這意思,應該是global定義的宏全局都生效      

更多的預設宏定義在/usr/lib/rpm/macros*檔案,作業系統不同,可能位置有所不一緻。

再來看下面這段描述資訊

Name: salt                                # 軟體包名稱,可用%{name}引用
Version: 2014.7.1                         # 軟體包版本号,可用%{version}引用
Release: 1%{?dist}                        # 釋出序列号,表示第幾次打包,可用%{release}引用
Summary: A parallel remote execution system    # 軟體包的内容概要
Group:   System Environment/Daemons            # 軟體分組
License: ASL 2.0                               # 軟體授權方式
URL:                      # 軟體首頁 
Source0: http://pypi.python.org/packages/source/s/%{name}/%{name}-%{version}.tar.gz   # 源碼包,可以有多個,如Source0、Source1等,可用%{source0}、%{source1}引用 
Source1: https://pypi.python.org/packages/source/S/%{_salttesting}/%{_salttesting}-%{_salttesting_ver}.tar.gz
Source2: %{name}-master
Source3: %{name}-syndic
Source4: %{name}-minion
Source5: %{name}-api
Source6: %{name}-master.service
Source7: %{name}-syndic.service
Source8: %{name}-minion.service
Source9: %{name}-api.service
Source10: README.fedora
Source11: logrotate.salt
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)  # 安裝或編譯時使用的"虛拟目錄",可用%{buildroot}引用,%{_tmppath}根據/usr/lib/rpm/macros查閱結果預設是在/var/tmp
BuildArch: noarch                         #  編譯的cpu架構

%ifarch %{ix86} x86_64                    # 如果cpu是x86_64架構,%{ix86}預設宏定義可查閱/usr/lib/rpm/macros       
Requires: dmidecode                       # 該rpm包依賴的軟體包名稱,可以使用>=、=、<=某一特定版本号,">="兩邊需空格隔開
%endif       

%if ((0%{?rhel} >= 6 || 0%{?fedora} > 12) && 0%{?include_tests})
BuildRequires: m2crypto                    # 編譯時依賴的軟體包名稱
BuildRequires: python-crypto
BuildRequires: python-jinja2
BuildRequires: python-msgpack
BuildRequires: python-pip
BuildRequires: python-zmq
BuildRequires: PyYAML
BuildRequires: python-requests
BuildRequires: python-unittest2
# this BR causes windows tests to happen
# clearly, that's not desired
# https://github.com/saltstack/salt/issues/3749
BuildRequires: python-mock
BuildRequires: git
BuildRequires: python-libcloud
%endif

%if 0%{?systemd_preun:1}                            
Requires(post): systemd-units                 # PreReq、Requires(pre)、Requires(post)、Requires(preun)、Requires(postun)、BuildRequires等都是針對不同階段的依賴指定
Requires(preun): systemd-units
Requires(postun): systemd-units
%endif

%description                                  # 軟體包的詳細說明
Salt is a distributed remote execution system used to execute commands and
query data. It was developed in order to bring the best solutions found in
the world of remote execution together and make them better, faster and more
malleable. Salt accomplishes this via its ability to handle larger loads of
information, and not just dozens, but hundreds or even thousands of individual
servers, handle them quickly and through a simple and manageable interface.


%package master                               # 對subpackage的描述
Summary: Management component for salt, a parallel remote execution system
Group:   System Environment/Daemons
Requires: %{name} = %{version}-%{release}
%if (0%{?rhel} >= 7 || 0%{?fedora} >= 15)
Requires: systemd-python
%endif

%description master
The Salt master is the central server to which all minions connect
關于子包的包名,來看一個例子:
Name: salt
%package master
這個子包的包名就是salt-master

%prep                                       # 預處理腳本
%setup -c                                   # 源碼包解壓
%setup -T -D -a 1  
%setup 常用參數選項參考這裡:
-c: Create Directory (and change to it) Before Unpacking
-D: Do Not Delete Directory Before Unpacking Sources
-T: Do Not Perform Default Archive Unpacking
-b <n>: Unpack The nth Sources Before Changing Directory
-a <n>: Unpack The nth Sources After Changing Directory

%build                                      # 開始建構rpm包

%install                                    # 軟體安裝到對應的目錄下,主要為了後面的%file做鋪墊的
rm -rf %{buildroot}
cd $RPM_BUILD_DIR/%{name}-%{version}/%{name}-%{version}
%{__python} setup.py install -O1 --root %{buildroot}

install -d -m 0755 %{buildroot}%{_var}/cache/salt            # 可以使用系統指令install安裝,
install -d -m 0755 %{buildroot}%{_sysconfdir}/salt
install -d -m 0755 %{buildroot}%{_sysconfdir}/salt/cloud.conf.d
install -d -m 0755 %{buildroot}%{_sysconfdir}/salt/cloud.deploy.d
install -d -m 0755 %{buildroot}%{_sysconfdir}/salt/cloud.maps.d
install -d -m 0755 %{buildroot}%{_sysconfdir}/salt/cloud.profiles.d
install -d -m 0755 %{buildroot}%{_sysconfdir}/salt/cloud.providers.d

%clean                                    # 清理臨時檔案
rm -rf %{buildroot}

%check                                    # 檢查包是否工作正常,很多packages不執行這個階段
%files                                    # 定義rpm package包含哪些檔案或目錄
%defattr(-,root,root,-)                   # 指定包裝檔案的屬性,分别是(mode,owner,group),-表示預設值,對文本檔案是0644,可執行檔案是0755
%doc $RPM_BUILD_DIR/%{name}-%{version}/%{name}-%{version}/LICENSE
%{python_sitelib}/%{name}/*            
#%{python_sitelib}/%{name}-%{version}-py?.?.egg-info
%{python_sitelib}/%{name}-*-py?.?.egg-info
%{_sysconfdir}/logrotate.d/salt
%{_var}/cache/salt
%doc $RPM_BUILD_DIR/%{name}-%{version}/%{name}-%{version}/README.fedora

%exclude                                  # 列出不想打包到rpm中的檔案


%preun master                             
  if [ $1 -eq 0 ] ; then
      /sbin/service salt-master stop >/dev/null 2>&1
      /sbin/chkconfig --del salt-master
  fi

%pre                                # rpm安裝前執行的腳本
%post                               # rpm安裝後執行的腳本
%preun                              # rpm解除安裝前執行的腳本
%postun                             # rpm解除安裝後執行的腳本

%changelog                                # 記錄變更日志      

參考連結