go語言之美迅速打rpm包實現詳解
引言
之前寫過一篇如何手操rpm包,這次寫寫go語言打包有多爽。
各組件版本
# git大版本小于2搞不了 git 2.2.1 go 1.13.6
準備
- rpmbuild命令
sudo yum install -y gcc make rpm-build redhat-rpm-config vim lrzsz
- git 2.2.1 版本 先用源安裝
yum install git -y
如果安裝完發(fā)現git版本不對就手動安裝
- 編譯好的go文件或者等待安裝的文件(不局限于go打包) 當然啦,不準備好能跑的文件搞雞毛啊。
開始
你需要一個json文件,告訴系統(tǒng)你想把哪些文件放到rpm包里, build,安裝前,安裝后做什么事情。
這里以打openstack-exporter為例(一個go程序)
{
"name": "openstack-exporter",
"version": "0.9.0",
"release": "release",
"arch": "amd64",
"summary": "RPM_SUMMARY",
"description": "RPM_SUMMARY",
"license": "Tencent.com",
"url": "http://git.code.oa.com/",
"postinst": "ci/package/rpm/postinst",
"files": [
{
"from": "./bin/LinuxAmd64/!name!",
"to": "/usr/local/bin/",
"base": "",
"type": ""
},
{
"from": "!name!.service",
"to": "/usr/lib/systemd/system/",
"base": "",
"type": ""
},
{
"from": "clouds.yaml",
"to": "/etc/openstack/",
"base": "",
"type": ""
}
]
}
核心配置含義:
- "name": "openstack-exporter" 代表你的
rpm服務名,你可以rpm -e openstack-exporter直接卸載他。 - "arch": "amd64" 內核:x86_64,也可以用其他內核。
- files 要拷貝到
rpm包中的文件,fromto從本地某個文件到包內目錄。 - "postinst": "ci/package/rpm/postinst" 安裝完執(zhí)行的腳本 腳本內容
systemctl daemon-reload
我打算把這個服務用systemctl托管起來,如果不需要刪掉就可以
service配置
這是給systemctl用的,上面也寫了拷貝后的路徑/usr/lib/systemd/system/ 配置文件取名:服務名+.service
openstack-exporter.service
[Unit] Description=openstack exporter After=network.target [Service] Environment= User=root Group=root PermissionsStartOnly=true ExecStart=/usr/local/bin/openstack-exporter default Restart=always LimitNOFILE=65535 WorkingDirectory=/ [Install] WantedBy=multi-user.target
含義一看不是今天要說的,不解釋。當然你也可以用任何守護進程來托管你的服務。
運行
我比較喜歡把好用的go包放在一個公共目錄里例如/root/go 編譯后得到的二進制當系統(tǒng)命令來用,只需要執(zhí)行
echo "export PATH=\$PATH:/root/go/bin" >> /etc/bashrc export PATH=$PATH:/root/go/bin
/root/go是默認的GOPATH,可以不管。
安裝go-bin-rpm命令
GOPATH=/root/go mkdir -p $GOPATH/src/github.com/mh-cbon/go-bin-rpm cd $GOPATH/src/github.com/mh-cbon/go-bin-rpm git clone https://github.com/mh-cbon/go-bin-rpm.git . glide install go install
打包只要一行命令
go-bin-rpm generate -f rpm_linux_amd64.json -o ./rpms/openstack-exporter-0.9.0_amd64.rpm
總結
要用這個組件來打包要準備
json文件用來描述拷貝哪些文件到實際安裝的目錄,打包前后運行哪些命令- 注冊相應守護進程需要的文件
我們還學會了新建一個目錄專門防止go語言共同包以及編譯好的go二進制文件,當作新的命令使用。
如果你把這個東西封裝到 持續(xù)集成 里,會有多爽,你懂的。
以上就是go語言之美迅速打rpm包實現詳解的詳細內容,更多關于go語言打rpm包的資料請關注腳本之家其它相關文章!
相關文章
Android開發(fā)vsts?agent支持自定義task過程詳解
這篇文章主要介紹了Android開發(fā)vsts?agent支持自定義task過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-04-04
android private libraries 中的包源代碼添加方法
這篇文章主要介紹了android private libraries 中的包源代碼添加方法,方法很簡單,看完本文即可學會,需要的朋友可以參考下2015-05-05

