Red Hat Enterprise Linux

Efficient Package Management With RPM & DNF

Introduction

In this article you will learn how to take your python program and make it easy to download with your red hat pacakge manager. With the help of rpm (RedHat package manager) and dnf (also a package manager) we will achieve this task.
So let’s get started.

Creating Python Program

For this demonstration, our python program will be a backup script that uses a .conf file to get the files to backup, saves them inside a backup directory with timestamp,  and generated failure file for logging.

First we need to create a function, that will create a log file for errors. Then we need to create our backup directory with the current timestamp. For that we are using os and datatime libreries to help. After that, we need to copy the files from the conf file to our backup directory.
To achieve that we use the command:

shutil.copy

Installing and configuring RPM

Downloading requirements

Let’s start by downloading the relevant packages
First let’s update and upgrade our system
Run the commands:

sudo dnf update && upgrade

Next, let’s download the appropiate packages: rpmdevtools and rpmlint

Run the commands:

sudo dnf install rpmdevtools rpmlint

Now we need to set up a file tree to be able to build RPM packages.
Run the following command:

rpmdev-setuptree

To see the installed tree you can run the command: tree ~/rpmbuild

Now we need to create a directory that will hold our files. 
In our example we have python and conf files.

Run the following command:
mkdir ds-backup-0.0.1
mv ds-backup.py ds-backup-0.0.1
mv ds-backup.conf ds-backup-0.0.1
Now we need to convert our folder to tar for our next steps.
Run the following command:
tar --create --file ds-backup-0.0.1.tar.gz ds-backup-0.0.1
All we have left for this step is to move the file into the SOURCES directory.
Run the following command:
mv ds-backup-0.0.1.tar.gz ~/rpmbuild/SOURCES
RPM package is defined by a .spec file that we need to configure with our configurations according to our use case.
Run this command to create a .spec fileand move to SPECS:
rpmdev-newspec ds-backup-0.0.1
mv ds-backup-0.0.1.spec ~/rpmbuild/SPECS
 
To see your current work you can run:
tree ~/rpmbuild
Now we need to edit our .spec file to match our goal.
Here is an example of the file:
To find errors run:
rpmlint ~/rpmbuild/SPECS/ds-backup-0.0.1.spec

 

There may be some errors and warnings along the way, there’s no need for a %build section

Building the package (rpmbuild)

To create the binary .rpm package:
rpmbuild -bb ~/rpmbuild/SPECS/ds-backup-0.0.1.spec

 

After the build process is finished, you can see the whole tree by running:
tree ~/rpmbuild/

Installing createrepo and configuring repo

This package will help us to create a repo for our package and make it easier to run.
Run this command:
sudo dnf install createrepo
First we need to create a directory, then we need to run the createrepo command.
Run this commands:
mkdir my-rpm-package
cd my-rpm-package
. createrepo
To let us be able to run the package we need to config a special file ending in .repo in /etc/yum.repos.d
Run this command:
vi ds-backup.repo

Installing the RPM package

Congratulations!!!
If you came this far without too many bugs and errors (hopefully), you are ready for the final step: Downloading our custom RPM package.
Run the command:
sudo dnf install ds-backup

Summary

So in this article we learned how to create a backup script in python, package it using rpm, and make it available to download.
I hope you enjoyed this article, I wish as less bugs as possible and happy coding 🙂

Red Hat
Red Hat

חברת אוקטופוס היא שותף בכיר של Red Hat בישראל ומציעים רישוי לכלל מוצרי Red Hat במחירים תחרותיים וכן את שירותי ייעוץ המומחים שלנו מאחוריהם אלפי שעות בפרויקטים המורכבים ביותר במשק.

צור קשר