Consul - create rpm package

Posted on Sat 18 February 2017 by Pavlo Khmel

RPM package is convenient way to automate consul installation on many RHEL or CentOS servers. But consul rpm is not provided by HashiCorp.
Let's create consul RPM package for CentOS 7 with systemd support.

Install packages:

cd /root
yum -y install rpmdevtools unzip

Build folder trees:

rpmdev-setuptree
mkdir -p consul-0.7.5/etc/consul.d
mkdir -p consul-0.7.5/var/lib/consul
mkdir -p consul-0.7.5/etc/systemd/system
mkdir -p consul-0.7.5/usr/bin

Download and unarchive consul:

curl -O https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip
unzip consul_0.7.5_linux_amd64.zip
mv consul consul-0.7.5/usr/bin/

Create service file for systemd:

cat << 'EOF' > consul-0.7.5/etc/systemd/system/consul.service
[Unit]
Description=consul agent
Requires=network-online.target
After=network-online.target

[Service]
EnvironmentFile=-/etc/sysconfig/consul
Environment=GOMAXPROCS=2
Restart=on-failure
ExecStart=/usr/bin/consul agent -config-dir=/etc/consul.d -data-dir /var/lib/consul -rejoin
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGTERM

[Install]
WantedBy=multi-user.target
EOF

Archive created directory:

tar czf consul-0.7.5.tar.gz consul-0.7.5
mv consul-0.7.5.tar.gz rpmbuild/SOURCES/

Create spec file:

cat << 'EOF' > rpmbuild/SPECS/consul_0.7.5_linux_amd64.spec
%define debug_package %{nil}
Name:           consul
Version:        0.7.5
Release:        1%{?dist}
Summary:        Consul is a tool for service discovery and configuration.
Group:          System Environment/Daemons
SOURCE0:        consul-0.7.5.tar.gz
License:        MPLv2.0
URL:            https://www.consul.io
BuildRoot:      %{_tmppath}/%{name}-%{version}
%description
Consul is a tool for service discovery and configuration.
%prep
%setup
%build
%install
rm -rf "$RPM_BUILD_ROOT"
mkdir -p "$RPM_BUILD_ROOT"
cp -R * "$RPM_BUILD_ROOT"
%clean
rm -rf $RPM_BUILD_ROOT
%files
%dir /var/lib/consul
%dir /etc/consul.d
/usr/bin/consul
/etc/systemd/system/consul.service
EOF

Create RPM package:

rpmbuild -ba rpmbuild/SPECS/consul_0.7.5_linux_amd64.spec

Install:

rpm -ivh rpmbuild/RPMS/x86_64/consul-0.7.5-1.el7.centos.x86_64.rpm

Create client configuration:

cat << 'EOF' > /etc/consul.d/config.json
{
    "server": false,
    "data_dir": "/var/lib/consul",
    "log_level": "INFO",
    "enable_syslog": true,
    "start_join": ["192.168.0.1"]
}
EOF

Enable and start consul service:

systemctl enable consul
systemctl start consul
systemctl status consul