[PYTHON] rpm packaging

rpm packaging

This is Qiita's first post. This article is a reminder about rpm packaging! I wrote it with the feeling that I should be able to understand the big framework. I will summarize the details in another article! (I'm going ...) If you make a mistake, please rush in.

What is rpm packaging in the first place?

rpm->> Software package management system developed by Red Hat Packaging->> When developing an application (?) Etc., put together what you need so that it can be used in any environment. Is it like this? Well, rpm packaging makes it easy to distribute your creations and benefit from being easy to use in any environment. I have to try this! So let's see what kind of structure it has!

Directory structure

[Directory ①]

rpmtest
  ├──BUILD
  ├──BUILDROOT
  ├──Makefile
  ├──README.md
  ├──RPMS
  ├──SOURCES
  ├──SPECS/
        ∟test.spec
  ├──SRPMS
  ├──hello-test-0.9/
     ├ Makefile
        ∟hello.sh

Let's assume that this directory structure is complete this time. When topdir is rpmtest, only Makefile README.md SOURCES SPECS hello-test-0.9 is created by yourself. Other directories are automatically created by executing a certain command.

Therefore, after all, the directory structure that you prepare first is as follows,

[Directory ②]

rpmtest
  ├──Makefile
  ├──README.md
  ├──SOURCES
  ├──SPECS/
        ∟test.spec
  ├──hello-test-0.9/
     ├ Makefile
        ∟hello.sh

By executing a certain command after creating this structure, it will look like [Directory ②]. (Insistent,)

Let's take a look at the completed [Directory ①] in order from the top. --BUILD-> Working directory to use when creating rpm packages --BUILDROOT-> Directory that will be the root for virtual installation of applications --Makefile-> You can use the make command in the top directory (rpmtest in this case) to make things easier. --README.md-> Description of what you want to package --RPS-> Directory where the completed rpm package is located --SOURCES-> The directory to put the source code to be included in the rpm package (in this case, the hello-test-0.9 directory is what you want to package, but instead of putting the source code in SOURCES from the beginning, hello-test when executing the command -0.9 I want to put a collection of directories, so I will leave the contents of SOURCES empty at first) --SPECS-> rpm Directory for placing spec files used to create packages (how to write spec files will be described later) --SRPMS-> Directory where the completed srpm file will be placed --Hello-test-0.9 Makefile-> spec file Required in% install section when starting --hello.sh of hello-test-0.9-> Source code you want to package this time

It's hard to see, but it looks like this. (I want to fix it here in an easy-to-understand manner)

Write code for rpm packaging

Let's finally get into the main subject! As a premise, what I want to package this time is hello-world-0.9 / hello.sh. The contents are as follows

#!usr/bin/env bash

echo "hello world!"

When you run it, it says hello world !, which is extremely simple, isn't it? so! In the first place, the goal this time is to execute the command make rpm and you can package this shell script! !! Wow! !! That's right! (now,,,) There is no need to dare to package such a simple thing that comes out as hello world !, but when the thing you want to package has a more complicated structure, just one command of make rpm When the packaging is complete, it's too god! !! That's why. Yes, let's get back to the story. When I run the command ./hello.sh in rpmtest / hello-world-0.9, it says that I don't have permission, but this is fine. The genius spec file will do something about it later. I will explain hello-world-0.9 / Makefile later.

Next, let's take a look at SPEC / test.spec, which plays a leading role in rpm packaging. This spec file is a genius and it's amazing! The spec file this time is as follows. [test.spec]

Summary: hellocommand
Name: hello-world
Version: 0.9
Release: 1
License: proprietary
Source0: hello-world-%{version}.tar.gz

%description
hello world

%prep
rm -rf %{buildroot}

%setup

%build

%install
echo %{buildroot}
make install BUILDROOT=%{buildroot} BINDIR=%{_bindir}
 
%clean
rm -rf %{buildroot}

%files
%attr(0755,root,root) %{_bindir}/hello

%changelog
* DAY YOUR NAME <YOUR MAIL ADDRESS> - 0.9-1
- Initial release

I made it as simple as possible. Let's move on to the explanation.

By the way, the spec file has the following structure.

Let's see how test.spec works for the time being. <%prep> Ready to package. It is troublesome if the previous version of BUILD ROOT remains, so delete it with rm just in case. <%setup> (1) Delete the Name-Version directory (considering the possibility that the source code of the previous version remains) (2) Unzip the file specified by Source0 with gzip and extract it with tar (a Directory of Name-Version is also created). ③ Move to the Name-Version directory ④ Change permissions (The problem that appears when you execute the above command without permission is solved here !!) *** [[Then where did the specified file in ② come from! !! ]] *** Good question! The Makefile (rpmtest / Makefile) in the top directory solves the question here! !! There are some parts that I haven't used yet, but I'll write them all for now. [Makefile]

STR    = .
LIB    = /var/lib

.PHONY: deploy
deploy:
    tar zcf hello-world-0.9.tar.gz hello-world-0.9
    mv hello-world-0.9.tar.gz SOURCES

.PHONY: rpm
rpm:
    make deploy
    rpmbuild -ba --define="_topdir ${PWD}" SPEC/test.spec

.PHONY: clean
clean:
    rm -rf RPMS SRPMS BUILD BUILDROOT SOURCES/hello-world*

At the moment, the deploy part is relevant. In this Makefile, when make rpm is executed, make deploy is executed and then rpmbuild is executed (spec file runs). The previous *** question *** how deploy solves it! So what exactly is done with make deploy? ❶ Tar the directory hello-world-0.9 ❷ Move the hardened tar file to the SOURCES directory These two tasks are being done. so! !! This has solved the previous question. When I run rpmbuild (even if I try to run the spec file), before that, make deploy is run and the tarred hello-world-0.9 is moved to SOURCES! <%build> Fill in the process to build the package. The starting point for this section is to move to the Name-Version (hello-world-0.9) expanded by% setup earlier. <%install> As with% build, the starting point for this section is to move to the Name-Version (hello-world-0.9) expanded by% setup. Well, you have found make here as well. Introducing Makefile again. This time Makefile is rpmtest / hello-world-0.9 / Makefile. [Makefile]

BUILDROOT    = .
BINDIR       = /bin

.PHONY: install
install:
    install -d -m 0755 ${BUILDROOT}${BINDIR}/
    install -m 0755 hello.sh ${BUILDROOT}${BINDIR}/hello

This install is a virtual install. Many of you may have thought that the explanation of BUILD ROOT that appeared at the beginning did not make sense. The BUILD ROOT here is likened to the actual root. Since it is a variable, it is versatile and easy to use. (I want to rewrite it a little more clearly here)

Come on! !! Now pip install the files in RPMS and you're all set! !! Yatta Thank you for reading though it is long. There are still a lot of rough parts, so I will rewrite it soon ...

Recommended Posts

rpm packaging
yum and rpm