如何从 bash 脚本和 systemd 服务创建 debian 软件包?

如何从 bash 脚本和 systemd 服务创建 debian 软件包?

如何从 bash 脚本和 systemd 服务构建 debian 软件包?

.deb安装成功后,systemd 服务将通过启动/停止来控制脚本以供使用。从网络搜索中,有一些简单的示例可以将单个文件(python、shell、ruby ...脚本)转换为.deb.

答案1

这是一个最小的源包,它将安装 shell 脚本和相关服务。

树如下:

minpackage
├── debian
│   ├── changelog
│   ├── control
│   ├── install
│   ├── minpackage.service
│   ├── rules
│   └── source
│       └── format
└── script

script是你的脚本,权限为755;debian/minpackage.service是你的服务。

debian/changelog需要看起来像

minpackage (1.0) unstable; urgency=medium                     
                                                                
  * Initial release.                                            
                                                                
 -- GAD3R <[email protected]>  Tue, 05 Jan 2021 21:08:35 +0100                                                            

debian/control应包含

Source: minpackage                         
Section: admin                             
Priority: optional                         
Maintainer: GAD3R <[email protected]>
Build-Depends: debhelper-compat (= 13)     
Standards-Version: 4.5.1                   
Rules-Requires-Root: no                    
                                           
Package: minpackage                        
Architecture: all                          
Depends: ${misc:Depends}                   
Description: My super package              

debian/rules应包含

#!/usr/bin/make -f  
                    
%:                  
        dh $@       

Tab之前有一个真实的dh)。

其余文件可以按如下方式创建:

mkdir -p debian/source
echo "3.0 (native)" > debian/source/format
echo script usr/bin > debian/install

要构建包,请运行

dpkg-buildpackage -uc -us

minpackage目录中。

这将minpackage_1.0_all.deb在父目录中创建。它还将为您处理 systemd 维护者脚本,因此该服务将在安装软件包时自动启用,并支持 Debian 中可用的各种覆盖机制。

答案2

假设您想要部署myservice.service并且myscript想要一个像这样的树结构:

$ tree
.
├── DEBIAN
│   ├── control
│   ├── postinst
│   ├── postrm
│   └── prerm
├── lib
│   └── systemd
│       └── system
│           └── myservice.service
└── usr
    └── bin
        └── myscript

./DEBIAN/应包含控制文件用于元数据和部署的维护者脚本。

这是一个基本的控制文件:

Package: mypackage
Version: 0.1
Architecture: all
Maintainer: GAD3R <[email protected]>
Description: This is my package!

dh_installsystemd(1)通常用于将默认代码添加到维护者脚本中以管理服务。你的可能看起来像这样:

后置:

#!/bin/sh
set -e
# Automatically added by dh_installsystemd/13.2
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
        # This will only remove masks created by d-s-h on package removal.
        deb-systemd-helper unmask 'myservice.service' >/dev/null || true

        # was-enabled defaults to true, so new installations run enable.
        if deb-systemd-helper --quiet was-enabled 'myservice.service'; then
                # Enables the unit on first installation, creates new
                # symlinks on upgrades if the unit file has changed.
                deb-systemd-helper enable 'myservice.service' >/dev/null || true
        else
                # Update the statefile to add new symlinks (if any), which need to be
                # cleaned up on purge. Also remove old symlinks.
                deb-systemd-helper update-state 'myservice.service' >/dev/null || true
        fi
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.2
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
        if [ -d /run/systemd/system ]; then
                systemctl --system daemon-reload >/dev/null || true
                if [ -n "$2" ]; then
                        _dh_action=restart
                else
                        _dh_action=start
                fi
                deb-systemd-invoke $_dh_action 'myservice.service' >/dev/null || true
        fi
fi
# End automatically added section

后期:

#!/bin/sh
set -e
# Automatically added by dh_installsystemd/13.2
if [ -d /run/systemd/system ]; then
        systemctl --system daemon-reload >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.2
if [ "$1" = "remove" ]; then
        if [ -x "/usr/bin/deb-systemd-helper" ]; then
                deb-systemd-helper mask 'myservice.service' >/dev/null || true
        fi
fi

if [ "$1" = "purge" ]; then
        if [ -x "/usr/bin/deb-systemd-helper" ]; then
                deb-systemd-helper purge 'myservice.service' >/dev/null || true
                deb-systemd-helper unmask 'myservice.service' >/dev/null || true
        fi
fi
# End automatically added section

预审:

#!/bin/sh
set -e
# Automatically added by dh_installsystemd/13.2
if [ -d /run/systemd/system ] && [ "$1" = remove ]; then
        deb-systemd-invoke stop 'myservice.service' >/dev/null || true
fi
# End automatically added section

然后将它们放在一起:

dpkg-deb -b . mypackage.deb

相关内容