Simple sever库打包、升级流程
打包环境准备
打包需要的内容:源码、spec文件、rpmbuild工具
打包环境需要确保以下事项:
- 要打包的库文件执行正常(所引入的库均有安装)
- 安装pyinstaller,安装方式:pip install pyinstaller
- 安装rpmbuild,安装方式:yum install -y rpm-build
打包步骤
- 使用pyinstaller,将py文件打包成可执行文件:pyinstaller -F simple_server.py
提示“completed successfully”完成之后,在当前目录的dist下生成对应的可执行文件simple_server
- 将要打包的源文件simple_server、simple_server.py、server.csr、server.pem、server.crt、server.key、upgrade.sh、cpHTTPServer.py、favicon.ico等放到/root/rpmbuild/SOURCES目录
- vim /root/rpmbuild/SPECS/simple_server.spec
%define _prefix /usr/local/simple_server
Name: simple_server
Version: 1.0 //大版本号
Release: 6 //小版本号
Summary:Run in Linux only.
Group: System
License: GPL
URL:''
Source0: simple_server
Source1: server.key
Source2: server.csr
Source3: server.crt
Source4: server.pem
Source5: favicon.ico
Source6: ReadMe.txt
Source7: wsMeasuredCheck.htm
Source8: simple_server.py
Source9: upgrade.sh
Source10: cpHTTPServer.py //Source0-Source10为源文件信息
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
%description
test
%install //安装时的操作
#mkdir -p %{buildroot}/%{_sysconfdir}/init.d
#cp %{SOURCE1} %{buildroot}/%{_sysconfdir}/init.d/simple_server
mkdir -p %{buildroot}/%{_usr}/local/simple_server
mkdir -p %{buildroot}/%{_usr}/local/simple_server/upgrade
mkdir -p %{buildroot}/%{_usr}/local/simple_server/logs
cp %{SOURCE0} %{buildroot}/%{_usr}/local/simple_server
cp %{SOURCE1} %{buildroot}/%{_usr}/local/simple_server
cp %{SOURCE2} %{buildroot}/%{_usr}/local/simple_server
cp %{SOURCE3} %{buildroot}/%{_usr}/local/simple_server
cp %{SOURCE4} %{buildroot}/%{_usr}/local/simple_server
cp %{SOURCE5} %{buildroot}/%{_usr}/local/simple_server
cp %{SOURCE6} %{buildroot}/%{_usr}/local/simple_server
cp %{SOURCE7} %{buildroot}/%{_usr}/local/simple_server
cp %{SOURCE8} %{buildroot}/%{_usr}/local/simple_server
cp %{SOURCE9} %{buildroot}/%{_usr}/local/simple_server/upgrade
cp %{SOURCE10} %{buildroot}/%{_usr}/local/simple_server
exit 0
%clean //清理临时文件操作
rm -rf %{buildroot}
%files
%defattr (-,root,root)
%{_usr}/local/simple_server/simple_server
%{_usr}/local/simple_server/simple_server.py
%{_usr}/local/simple_server/server.key
%{_usr}/local/simple_server/server.csr
%{_usr}/local/simple_server/server.crt
%{_usr}/local/simple_server/server.pem
%{_usr}/local/simple_server/favicon.ico
%{_usr}/local/simple_server/ReadMe.txt
%{_usr}/local/simple_server/wsMeasuredCheck.htm
%{_usr}/local/simple_server/upgrade/upgrade.sh
%{_usr}/local/simple_server/cpHTTPServer.py
%pre //安装之前的操作
mkdir -p %{_usr}/local/simple_server/logs
rm -rf /usr/local/bin/simple_server
%post //安装之后的操作
chmod 777 /usr/local/simple_server/simple_server
ln -s /usr/local/simple_server/simple_server /usr/local/bin/
%preun //卸载之前的操作
- 执行打包操作:rpmbuild -bb simple_server.spec
- 打包完成后,在/root/rpmbuild/RPMS/x86_64/目录生成对应的rpm包
命令方式升级
parser.add_argument('-u', dest='upgrade', type=str, choices=['yes', 'no'], default='no', help='upgrade or not')
parser.add_argument('-url', dest='url', type=str, default="http://10.8.225.126/simple_server/simple_server.rpm",
help='http server upgrade')
库里有增加2个参数配合命令方式升级
-u : yes/no,默认no,默认不开启升级,若需升级设置为yes
-url : 取升级包的地址,默认为http://10.8.225.126/simple_server/simple_server.rpm,若新的安装包放到别处则带上新的url,如:-url http://10.8.60.136/upgrade/simple_server.rpm
升级命令:simple_server -u yes -url http://10.8.60.136/upgrade/simple_server.rpm
即可自动下载安装包自助升级,在比较多台机器需要同时升级时较方便使用,避免手动每台进行上传安装包执行-Uvh操作