Skip to content
This repository has been archived by the owner on May 19, 2022. It is now read-only.

Packaging a service #100

Closed
TotalKrill opened this issue Jul 3, 2019 · 10 comments
Closed

Packaging a service #100

TotalKrill opened this issue Jul 3, 2019 · 10 comments

Comments

@TotalKrill
Copy link

I want to package a binary that is being used as a service. I have my package.service located in the project folder under scripts/package.service.

Is there any example of how I would go about in cargo deb to get the software service file included, installed and enabled?

@kornelski
Copy link
Collaborator

You can. You will need to add assets = [ to Cargo.toml listing files you want to package. See the second example in the readme.

@TotalKrill
Copy link
Author

TotalKrill commented Jul 4, 2019

Yeah, I have added it to assets and i can see that it gets into the package, however, I think that when creating deb packages otherwise, they do some automagic that enables services automatically somehow. When I add the service unit file with the assets = [, I will manually have to enable the service.

But some other threads I found highlighted some problems in doing stuff like this using postinst, etc that depended on systemctl. So before I started jumping down this rabbit-hole, i was thinking I could ask here.

This issue describes what I want.
debian service

From this one, I tried to adding the maintainers-script = "folder" tag, and put the service file in there in the hopes it would get included in the package creation, and in a similar way enable the service on installation. (it didnt)

If i have to start creating postinst and preinst scripts, an example would be very helpful so that I could know what to expect.

@kornelski
Copy link
Collaborator

kornelski commented Jul 4, 2019

There's nothing magic in deb for services. The services are "automatically" enabled by putting .service file in a directory read by systemd, and you do that by specifying paths in assets = [].

@TotalKrill
Copy link
Author

So if i wanted to enable on installation it I would have to use the postinst scripts, gotcha

@kornelski
Copy link
Collaborator

No? I think mere existence of a .service file in /etc is enough.

@TotalKrill
Copy link
Author

TotalKrill commented Jul 8, 2019

Yeah, I tried that. It is recognized by systemd so that I can run systemctl enable myservice. But it is not enabled and/or started by itself, I need to run commands for that

@fredszaq
Copy link
Contributor

fredszaq commented Jul 17, 2019

Hi!

I usually do that using the postinst, postrm and prerm maintainer scripts of the debian package

Here are the files I use (click to expand) :

debian/postinst

#!/bin/sh

# This will only remove masks created by d-s-h on package removal.
deb-systemd-helper unmask snips-dialogue.service > /dev/null || true

# was-enabled defaults to true, so new installations run enable.
if deb-systemd-helper --quiet was-enabled snips-dialogue.service
then
	# Enables the unit on first installation, creates new
	# symlinks on upgrades if the unit file has changed.
	deb-systemd-helper enable my-service.service > /dev/null || true
	deb-systemd-invoke start my-service
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 my-service.service > /dev/null || true
fi

debian/postrm

#!/bin/sh

# In case this system is running systemd, we make systemd reload the unit files
# to pick up changes.
if [ -d /run/systemd/system ] ; then
	systemctl --system daemon-reload >/dev/null || true
fi

if [ "$1" = "remove" ]; then
	if [ -x "/usr/bin/deb-systemd-helper" ]; then
		deb-systemd-helper mask my-service.service >/dev/null
	fi
fi

debian/prerm

#!/bin/sh

deb-systemd-invoke stop my-service

Put them in a directory named debian and the root of you crate (I also put my service file in this folder). you then need to register the maintainer scripts and the service file for cargo-deb to pick them up:

[package.metadata.deb]
#...
maintainer-scripts="debian"
assets = [
    ["debian/my-service.service", "/lib/systemd/system/my-service.service", "644"], 
    #...
]

@Kixunil
Copy link

Kixunil commented Sep 22, 2019

I didn't need to do anything extra when packaging something with .service file. Debian trigger system handles it. I guess there's a problem with the service file?

@ximon18 ximon18 mentioned this issue Jun 22, 2020
@ximon18
Copy link
Contributor

ximon18 commented Jul 30, 2020

When packaging using Debian packaging tools such as debhelper the dh_installsystemd command can be used to cause systemd unit activation related autoscript fragments to be inserted into maintainer scripts in place of the #DEBHELPHER# token. Such logic doesn't exist in cargo deb, you have to provide the maintainer scripts and the correct logic inside them yourself. However with my PR #135 cargo deb can detect the unit files and augment/generate the maintainer scripts for you just like dh_installsystemd does.

@kornelski
Copy link
Collaborator

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants