SoFunction
Updated on 2025-03-11

Go language beauty quickly hits rpm package implementation detailed explanation

introduction

I wrote an article beforeHow to fuck rpm bag by hand, this timegoHow cool is language packing?

Each component version

# git version is less than 2 can't do itgit 2.2.1
go 1.13.6

Prepare

  • rpmbuild command
sudo yum install -y gcc make rpm-build redhat-rpm-config vim lrzsz
  • git version 2.2.1 Install first using source
yum install git -y

If the installation is foundgitInstall manually if the version is wrong

  • Compiled go files or files waiting to be installed (not limited to go package) Of course, you are not ready to run files to make a fuss.

start

You need a json file to tell the system which files you want to put intorpmIn the bag,build, What to do before and after installation.

Here to fightopenstack-exporterAs an example (a go program)

{
  "name": "openstack-exporter",
  "version": "0.9.0",
  "release": "release",
  "arch": "amd64",
  "summary": "RPM_SUMMARY",
  "description": "RPM_SUMMARY",
  "license": "",
  "url": "/",
  "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": "",
      "to": "/etc/openstack/",
      "base": "",
      "type": ""
    }
  ]
}

Core configuration meaning:

  • "name": "openstack-exporter" represents yourrpmService name, you canrpm -e openstack-exporterUninstall him directly.
  • "arch": "amd64" kernel: x86_64, other kernels can also be used.
  • files to copyrpmFiles in the package,from toFrom a local file to a directory in the package.
  • "postinst": "ci/package/rpm/postinst" The script content after installation and execution
systemctl daemon-reload

I plan to use this servicesystemctlHosted, if you don't need to delete it

Service configuration

This is forsystemctlUsed, the copy path is also written on it/usr/lib/systemd/system/Configuration file name: service name+.service

[Unit]
Description=openstack exporter
After=

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

[Install]
WantedBy=

The meaning is not what I want to say today, so I will not explain it. Of course you can also use any daemon to host your services.

run

I prefer to use itgoThe package is placed in a public directory, for example/root/goThe binary obtained after compilation is used when the system command is used, it only needs to be executed.

echo "export PATH=\$PATH:/root/go/bin" >> /etc/bashrc
export PATH=$PATH:/root/go/bin

/root/goIt is the defaultGOPATH, you can ignore it.

Installgo-bin-rpmOrder

GOPATH=/root/go
mkdir -p $GOPATH/src//mh-cbon/go-bin-rpm
cd $GOPATH/src//mh-cbon/go-bin-rpm
git clone /mh-cbon/ .
glide install
go install

Packaging only one line of command

go-bin-rpm generate -f rpm_linux_amd64.json -o ./rpms/openstack-exporter-0.9.0_amd64.rpm

Summarize

To package this component, you need to prepare

  • jsonFiles are used to describe which files are copied to the actual installed directory, and which commands are run before and after packaging.
  • Register files required by the corresponding daemon

We also learned to create a new directory specifically to preventgoLanguage co-package and compiledgoBinary files, used as new commands.

If you encapsulate this thing into continuous integration, you know how cool it will be.

The above is the detailed explanation of the implementation of the beauty of Go language to quickly build RPM package. For more information about the RPM package of Go language, please pay attention to my other related articles!