A method tutorial for deploying golang code online with rpm packaging

  • 2020-06-12 09:17:30
  • OfStack

Preface:

After testing and verification for a period of time, we decided to use RPM for the deployment of Golang service. There are mainly several deployment methods for our group, Python directly USES virtualenv + py code to go online, Lua is directly packaged, Golang is directly packaged after compiling and generating base 2, and then combined configuration file is directly packaged. But since much of the Golang code we developed was base components, many nodes needed to be installed and deployed. At this point, the self-compilation and packaging approach to RPM is quite conceivable, and it's not as good as reusing a mature rpm-based online system within the company.

Without further ado, let's take a look at the details.

rpm packaging Usage:


#  The installation RPM Rely on 
yum install rpmdevtools
# generate RPM directory 
cd ~
rpmdev-setuptree

Below is the basic organizational structure of rpm.


 ├ ─ ─  BUILD
 ├ ─ ─  BUILDROOT
 ├ ─ ─  RPMS
 ├ ─ ─  SOURCES
 ├ ─ ─  SPECS
 └ ─ ─  SRPMS

The simplest way to package rpm is to edit the SPECS configuration file directly, compile the go code into base 2 in %install logic, and then cp in, and you're done. I won't go into detail about the advanced usage of rpm for lack of time. Those interested can see the configuration of SPECS for themselves.


# Brief Introduction of software package 
Summary: build transcoding
# Name of the package 
Name: transcoding
# The major version number of the package 
Version: 0.0.1
# The second version number of the package 
Release: 5
# Source code package, default will be mentioned above SOURCES Look in the directory 
Source0: %{name}-%{version}.tar.gz
# License agreement 
License: GPL
# Classification of software 
Group: Development/Tools
# The content of the package is introduced 
%description
 Video transcoding cluster 
# Represents the pre-action field, the following command will be in the source code BUILD Former executive 
%prep
#BUILD Field to compile the source code by directly calling the automatic build tool in the source directory 
%build
#file
# Install the field 
%install
# 2 Base execution file 
mkdir -p ${RPM_BUILD_ROOT}/usr/bin/
cp -f /devops/app/go/src/transcoding/engine_bin ${RPM_BUILD_ROOT}/usr/bin/transcoding_engine_bin
cp -f /devops/app/go/src/transcoding/rest_bin ${RPM_BUILD_ROOT}/usr/bin/transcoding_rest_bin
#  The configuration file 
mkdir -p ${RPM_BUILD_ROOT}/etc/transcoding
cp -f /devops/app/go/src/transcoding/etc/online.config.ini ${RPM_BUILD_ROOT}/etc/transcoding/config.ini
#  Control script 
mkdir -p ${RPM_BUILD_ROOT}/etc/init.d/
cp -f /devops/app/go/src/transcoding/bin/init.sh ${RPM_BUILD_ROOT}/etc/init.d/transcoding.sh
# Call the installation execution script in the source code 
# File description fields, declarations that are redundant or missing are likely to go wrong 
%files
%defattr(-,root,root)
/usr/bin/transcoding_engine_bin
/usr/bin/transcoding_rest_bin
/etc/init.d/transcoding.sh
%dir
/etc/transcoding

When you're done editing the SPECS file, you're ready to run rpmbuild Generate the rpm package.


rpmbuild -bb transcoding.spec

After compiling, let's take a look at the rpm structure. Yes, one more rpm package...


 ├ ─ ─  BUILD
 ├ ─ ─  BUILDROOT
 ├ ─ ─  RPMS
 │   └ ─ ─  x86_64
 │   └ ─ ─  transcoding-0.0.1-5.x86_64.rpm
 ├ ─ ─  SOURCES
 ├ ─ ─  SPECS
 │   └ ─ ─  transcoding.spec
 └ ─ ─  SRPMS

All that remains is to upload the rpm package we generated to our private yum repo source. The rest of the process you can go through your standard online process.

Two things to note here:

1. rpm specs Update of version number, if not updated will cause yum update Invalid... .

2. If the profile contains the db account password, do not add it to rpm and consider it private yum repo It's not safe...

conclusion


Related articles: