Go 程序打成 rpm 包( go-bin-rpm 将二进制文件打包成 rpm 包)

Go 程序打成 rpm 包( go-bin-rpm 将二进制文件打包成 rpm 包)

如果使用 rpmbuild 打包,需要你编写很多配置文件,非常繁琐,一个简单的 rpm 包可能就只是执行一个二进制文件而已,完全没必要花费大量的时间去系统学习它们,于是乎,有人就编写了一个开源工具 go-bin-rpm,用它可以很方便的将二进制文件打包成 rpm 包;

1、环境准备

(1)rpmbuild 的相关依赖安装

yum install -y gcc make rpm-build redhat-rpm-config

(2)go-bin-rpm 安装,项目地址:https://github.com/mh-cbon/go-bin-rpm

基础软件包安装:

安装libffi:
$ yum install libffi-devel gettext-devel pkg-config
安装zlib:
$ yum install -y zlib zlib-devel

安装 go-bin-rpm

curl -L https://raw.githubusercontent.com/mh-cbon/latest/master/install.sh \
| GH=mh-cbon/go-bin-rpm sh -xe

验证是否安装成功

$ go-bin-rpm --version
go-bin-rpm version 1.0.0

2、配置文件编写:

(1)go 编译的二进制文件为:BSMonitorClientGo

(2)go-bin-rpm 的配置文件是 rpm.json

  • “name”: “bsmonitorclientgo” 服务名(可以使用rpm -e bsmonitorclientgo直接卸载它)
  • “arch”: “amd64” 内核:x86_64,也可以用其他内核
  • files 要拷贝到rpm包中的文件,from to 把 BSMonitorClientGo 文件拷贝到 /usr/local/bin/ 下面
  • “postinst”: “systemctl daemon-reload” 安装完要执行的脚本内容
  • “summary”: “monitoring client” 下面描述项精简出的摘要
  • “description”: “Server health status monitoring client”, 对这个服务的描述
{
  "name": "bsmonitorclientgo",
  "version": "1.0.1",
  "release": "20230316",
  "arch": "amd64",
  "summary": "monitoring client",
  "description": "Server health status monitoring client",
  "license": "sunkaiyuan.com",
  "url": "http://sunkaiyuan.com",
  "postinst": "systemctl daemon-reload",
  "files": [
    {
      "from": "BSMonitorClientGo",
      "to": "/usr/local/bin/",
      "base": "",
      "type": ""
    },
    {
      "from": "!name!.service",
      "to": "/usr/lib/systemd/system/",
      "base": "",
      "type": ""
    },
    {
      "from": "config.yaml",
      "to": "/etc/BSMonitorClient/",
      "base": "",
      "type": ""
    }
  ]
}

(3)我打算把这个服务用systemctl托管起来

  • 上面已经写了拷贝后的路径/usr/lib/systemd/system/
  • !name!.service # !name! 相当于变量,会取前面 rpm 包名称 bsmonitorclientgo(bsmonitorclientgo.service)

bsmonitorclientgo.service 文件内容如下:

[Unit]
Description=bsmonitorclientgo
After=network.target

[Service]
Environment=
User=root
Group=root
PermissionsStartOnly=true
ExecStart=/usr/local/bin/BSMonitorClientGo default
Restart=always
LimitNOFILE=65535
WorkingDirectory=/

[Install]
WantedBy=multi-user.target

4、一切准备完毕,开始编译

将 go编译的二进制文件BSMonitorClientGo,go-bin-rpm的配置文件是 rpm.json,bsmonitorclientgo.service文件,放在同一目录下:

BSMonitor
	|____BSMonitorClientGo
	|____rpm.json
	|____bsmonitorclientgo.service
	|____config.yaml		----------- golang 配置参数文件

开始编译:

$ go-bin-rpm generate -f rpm.json -o ./rpms/name-version-release.Linux版本.内核架构.rpm
$ go-bin-rpm generate -f rpm.json -o ./rpms/bsmonitorclientgo-1.0.0-20230316.el7.x86_64.rpm

对应替换

name		服务名				bsmonitorclientgo
version		版本号				1.0.0
release      release号          20230316
Linux版本    x86_64 架构         el7
内核架构	 Linux 7 (el7)       x86_64

编译生成:bsmgo-1.0.0-20230316.el7.x86_64.rpm

安装服务:rpm -ivh bsmonitorclientgo-1.0.0-20230316.el7.x86_64.rpm
运行服务:systemctl start bsmonitorclientgo
卸载服务:rpm -e bsmonitorclientgo

systemctl status bsmonitorclientgo
journalctl -u bsmonitorclientgo 		可以查找到指定服务的启动日志

systemctl start  bsmonitorclientgo
systemctl stop  bsmonitorclientgo
systemctl restart  bsmonitorclientgo

5、成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux 中,RPM(Red Hat Package Manager)是一种常见的软件管理工具,可以方便地安装、升级、卸载软件。下面是使用 RPM 打包的基本步骤: 1. 准备打包文件 将需要打包文件放在同一个目录下,并确保目录结构正确。 2. 创建 spec 文件 spec 文件RPM 打包的核心文件,它含了软件的相关信息,如软件名称、版本号、依赖关系等。可以使用文本编辑器创建 spec 文件,命名为 software.spec,其中 software 为软件名称。 3. 编写 spec 文件 spec 文件格式比较复杂,需要按照规范编写。以下是一个简单的示例: ``` Name: software Version: 1.0 Release: 1 Summary: A brief summary of the software License: GPL Group: Applications/Tools Source0: software.tar.gz BuildRoot: %{_tmppath}/%{name}-%{version}-root %description A detailed description of the software. %prep %setup -q %build ./configure make %install make install DESTDIR=%{buildroot} %clean rm -rf %{buildroot} %files %defattr(-,root,root,-) /usr/local/bin/software %doc README %changelog ``` 其中,Name、Version、Release、Summary、License、Group、Source0 等字段需要根据实际情况填写,%description、%files、%doc、%changelog 等字段可以根据需求添加或删除。 4. 执行 rpmbuild 命令 执行以下命令进行打包: ``` $ rpmbuild -bb software.spec ``` 其中,-bb 表示打包并构建二进制 RPM 。如果一切顺利,RPM 将生在 /usr/src/redhat/RPMS 目录下。 以上是 RPM 打包的基本步骤,更多细节请参考 RPM 手册。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值