📜 ⬆️ ⬇️

A universal way to create packages for various distributions of GNU Linux

Various distributions provide their utilities for building and installing programs.
For example, in Debian / GNU, Linux is debuild and dpkg. On Red Hat Linux, rpmbuild and rpm.
Not infrequently we have to collect packages on our own.
Installing programs through make, bypassing the package management system in distributions is a bad form.
In cases where we have the source code, and the author of the program has taken care of creating a Makefile, you can use checkinstall .
But it also happens that the author of the java application does not post anything except the jar file. Or we want to create some kind of package on our knees, and put them in the repository, so that we can install / update it on a variety of servers with different distributions in the future. This will help us a utility called fpm .
What is its dignity? It out of the box allows us to build packages for various distributions and even operating systems.

Now supported:


Installing the required packages for working with fpm on Debian GNU / Linux:
# apt-get install ruby rubygems rpm dpkg-dev # gem install fpm 

For example, let's build the logstash package, for Debian / GNU Linux and Red Hat Linux.
Create an assembly file in which we describe dependencies, configuration files, and meta-information about the package:
 $ mkdir logstash && cd logstash && touch build.sh && chmod +x build.sh && vim build.sh 

Paste:
 #!/bin/bash JAR_URL="https://download.elasticsearch.org/logstash/logstash/logstash-1.2.2-flatjar.jar" JAR_FILE="usr/share/logstash/logstash.jar" DESCRIPTION="Logstash Open Source Log Management" EMAIL="admin@logstash.net" URL='http://www.logstash.net/' VERSION="1.2.2" NAME="logstash" #    if [ ! -d "usr" ]; then mkdir -p {etc,usr/share/logstash,var/logstash} fi #       logstash if [ ! -f "$JAR_FILE" ]; then wget "$JAR_URL" -O "$JAR_FILE" fi cd .. function build() { fpm -n $NAME -v $VERSION -a all -C logstash -m "<$EMAIL>" \ --pre-install logstash/preinstall \ --description "$DESCRIPTION" \ --url "$URL" -t "$1" -d "$2" \ --config-files etc/logstash/syslog.conf \ -s dir etc usr var } #      build "deb" "default-jre" build "rpm" "java-1.6.0-openjdk-devel" 


Create a logstash configuration file:
 $ mkdir -p etc/logstash && vim etc/logstash/syslog.conf 

insert:
etc / logstash / syslog.conf
 input { file { type => "syslog_file" exclude => [ "logstash.log*" ] path => [ "/var/log/messages", "/var/log/syslog", "/var/log/*.log" ] } } filter { grok { type => "syslog_relay" pattern => [ "^<[1-9]\d{0,2}>%{SPACE}%{GREEDYDATA:message_remainder}" ] add_tag => "got_syslog_pri" add_field => [ "syslog_raw_message", "%{@message}" ] } syslog_pri { type => "syslog_relay" tags => [ "got_syslog_pri" ] } mutate { type => "syslog_relay" tags => [ "got_syslog_pri" ] replace => [ "@message", "%{message_remainder}" ] remove => [ "message_remainder" ] remove_tag => "got_syslog_pri" } grok { type => "syslog_relay" pattern => [ "^%{SYSLOGTIMESTAMP:syslog_timestamp}%{SPACE}%{GREEDYDATA:message_remainder}" ] add_tag => "got_syslog_timestamp" add_field => [ "received_at", "%{@timestamp}" ] } date { type => "syslog_relay" tags => [ "got_syslog_timestamp" ] # season to taste for your own syslog format(s) match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss", "ISO8601" ] } mutate { type => "syslog_relay" tags => [ "got_syslog_timestamp" ] replace => [ "@message", "%{message_remainder}" ] remove => [ "message_remainder" ] remove_tag => "got_syslog_timestamp" } grok { type => "syslog_relay" pattern => [ "^%{SYSLOGHOST:syslog_hostname}%{SPACE}%{GREEDYDATA:message_remainder}" ] add_tag => "got_syslog_host" add_field => [ "logstash_source", "%{@source_host}" ] } mutate { type => "syslog_relay" tags => [ "got_syslog_host" ] replace => [ "@source_host", "%{syslog_hostname}" ] replace => [ "@message", "%{message_remainder}" ] remove => [ "message_remainder" ] remove_tag => "got_syslog_host" } grok { type => "syslog_relay" pattern => [ "^%{SYSLOGPROG:syslog_prog}:%{SPACE}%{GREEDYDATA:message_remainder}" ] add_tag => "got_syslog_prog" } mutate { type => "syslog_relay" tags => [ "got_syslog_prog" ] replace => [ "@message", "%{message_remainder}" ] remove => [ "message_remainder" ] remove_tag => "got_syslog_prog" } dns { type => 'syslog_relay' reverse => [ "@source_host", "@source_host" ] action => "replace" } mutate { type => "syslog_relay" tags => [ "syslog" ] remove => [ "syslog_hostname", "syslog_timestamp" ] } } 


')
Next, create a script that should be executed before installing the package:
 $ cat > preinstall << END #!/bin/bash useradd -g adm -r -m -d /usr/share/logstash -s /bin/false logstash || exit 0 END 

Now it’s enough to start building the packages:
 # ./build.sh 

At the output, we get rpm and deb packages that are installed without problems in Debian or Red Hat.

Install the package on the current machine and check:
 # dpkg -i ../logstash*.deb && apt-get install -f # java -jar /usr/share/logstash/logstash.jar agent -f /etc/logstash --log /var/log/logstash.log 


Conclusion


Just a few teams can build a package for various distributions and OS in a couple of minutes.
Thanks to the author for such a wonderful utility. I hope that someone will find it as useful as I am.

Source: https://habr.com/ru/post/200774/


All Articles