背景

最近来了个新需求需要对arm平台私有化部署,目前服务只适配x86_64,其中"zookeepercli"服务是在github上找的且只适配x86_64架构,导致未部署Zookeeper的服务节点无法通过命令行方式去请求Zookeeper

解决思路

平台是通过ansible实现自动部署,如果采用脚本或二进制包的形式,需要变更yaml文件,再创建一个新的tag,这种方式过程较为复杂,且交付时间长。于是想到了编写脚本打成二进制文件,再制作rpm包同步到私有yum仓库,这样只需要重新拉取自制的yum仓库,即可快速解决问题;

脚本案例

#!/usr/bin/python
# -*- coding:utf-8  -*-
####################################
#                                  #
#     get zk node information      #
#                                  #
####################################

import sys
from kazoo.client import KazooClient

class ObjZk:
    def __init__(self, request_comm, zk_server, request_type, request_path):
        self.timeout = 30
        self.request_host = zk_server
        self.request_path = request_path
        self.zk = self._init_zk()

    def _init_zk(self):
        return KazooClient(hosts=self.request_host, timeout=self.timeout)

    def _Close(self):

        self.zk.stop()
        self.zk.close()

    def ConnZk(self):
        if not self.zk.connected:
            self.zk.start()
        try:
            zk_response = self.zk.get(self.request_path)[0]
        except Exception as e:
            zk_response = "127.0.0.1:2181"
        finally:
            print (zk_response)
        self._Close()


if __name__ == '__main__':
    if len(sys.argv[1:]) == 5:
        request_comm = sys.argv[1]
        zk_server = sys.argv[2]
        request_type = sys.argv[3]
        request_method = sys.argv[4]
        request_path = sys.argv[5]
        if request_method == "get":
            ObjZk(request_comm, zk_server, request_type, request_path).ConnZk()
        else:
            print ("只能执行get方法")
    else:
        print ("传参异常")
脚本仅做案例

脚本打成二进制文件

[root@vm2 zookeeper]# pyinstaller -F zookeepercli.py 
35 INFO: PyInstaller: 3.6
35 INFO: Python: 3.6.8
36 INFO: Platform: Linux-3.10.0-1160.31.1.el7.x86_64-x86_64-with-centos-7.5.1804-Core
36 INFO: wrote /opt/app/scripts/zookeeper/zookeepercli.spec
........
6712 INFO: checking EXE
6712 INFO: Building EXE because EXE-00.toc is non existent
6712 INFO: Building EXE from EXE-00.toc
6712 INFO: Appending archive to ELF section in EXE /opt/app/scripts/zookeeper/dist/zookeepercli
6725 INFO: Building EXE from EXE-00.toc completed successfully.

-F: 打成一个文件,将依赖文件都整合在这个文件中 -D: 打成一个目录,目录下包含依赖文件

下载RPM打包依赖

  • 安装依赖
yum install rpm-build rpmdevtools

rpm-build提供打包工具 rpmdevtools提供打包环境

  • 创建打包环境
rpmdev-setuptree
默认在当前用户家目录下生成rpmbuild目录
[wangtianci@obs-kingstorage-bjarm-node4 rpmbuild]$ cat SPECS/zookeepercli.spec 
Name:  zookeepercli
Version: 1.0.0
Release: 1%{?dist}
Summary: 1
Group: Developments/Tools
License: GPL
URL: http://tcandyj.top:4014/
Source0: zookeepercli
Requires: rpm

%description
get zk node information

%install

mkdir -pvm 755 %{buildroot}/usr/bin
cp zookeepercli %{buildroot}/usr/bin

%files
%defattr (-,root,root)
/usr/bin/zookeepercli

%changelog
  • 制作RPM包
[root@vm2 rpmbuild]# rpmbuild -bb SPECS/zookeepercli.spec 
Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.NZEhC6
+ umask 022
+ cd /root/rpmbuild/BUILD
........
Checking for unpackaged file(s): /usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/zookeepercli-1.0.0-1.el7.centos.x86_64
Wrote: /root/rpmbuild/RPMS/x86_64/zookeepercli-1.0.0-1.el7.centos.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.XpsJar
+ umask 022
+ cd /root/rpmbuild/BUILD
+ /usr/bin/rm -rf /root/rpmbuild/BUILDROOT/zookeepercli-1.0.0-1.el7.centos.x86_64
+ exit 0

[root@vm2 ~]# tree rpmbuild/
rpmbuild/
├── BUILD
│   └── zookeepercli
├── BUILDROOT
├── RPMS
│   └── x86_64
│       └── zookeepercli-1.0.0-1.el7.centos.x86_64.rpm
├── SOURCES
├── SPECS
│   └── zookeepercli.spec
└── SRPMS

测试yum安装

[root@vm2 ~]# yum install /root/rpmbuild/RPMS/x86_64/zookeepercli-1.0.0-1.el7.centos.x86_64.rpm
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Examining /root/rpmbuild/RPMS/x86_64/zookeepercli-1.0.0-1.el7.centos.x86_64.rpm: zookeepercli-1.0.0-1.el7.centos.x86_64
Marking /root/rpmbuild/RPMS/x86_64/zookeepercli-1.0.0-1.el7.centos.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package zookeepercli.x86_64 0:1.0.0-1.el7.centos will be installed
--> Finished Dependency Resolution
Running transaction
  Installing : zookeepercli-1.0.0-1.el7.centos.x86_64                                                                                                                                                           1/1 
  Verifying  : zookeepercli-1.0.0-1.el7.centos.x86_64                                                                                                                                                           1/1 
Installed:
  zookeepercli.x86_64 0:1.0.0-1.el7.centos                                                                                                                                                                          
Complete!
[root@vm2 ~]# rpm -qf /usr/bin/zookeepercli 
zookeepercli-1.0.0-1.el7.centos.x86_64
[root@vm2 ~]# zookeepercli 
传参异常

Copyright & TianCiwang 2021 all right reserved,powered by Gitbook修改时间: 2021-12-15 18:17:17

results matching ""

    No results matching ""

    results matching ""

      No results matching ""