Skip to content

Commit 0338987

Browse files
authored
update celery services handling (#726)
Signed-off-by: Marcus Furlong <furlongm@gmail.com>
1 parent 65e8851 commit 0338987

9 files changed

Lines changed: 121 additions & 30 deletions

debian/python3-patchman.install

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/dh-exec
22
etc/patchman/apache.conf.example => etc/apache2/conf-available/patchman.conf
33
etc/patchman/local_settings.py etc/patchman
4-
etc/systemd/system/patchman-celery.service => lib/systemd/system/patchman-celery.service
4+
etc/systemd/system/patchman-celery-worker.service => lib/systemd/system/patchman-celery-worker@.service
5+
etc/systemd/system/patchman-celery-beat.service => lib/systemd/system/patchman-celery-beat.service

debian/python3-patchman.postinst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,37 @@ if [ "$1" = "configure" ] ; then
2525
chown -R www-data:www-data /var/lib/patchman
2626
adduser --system --group patchman-celery
2727
usermod -a -G www-data patchman-celery
28+
chown root:patchman-celery /etc/patchman/celery.conf
29+
chmod 640 /etc/patchman/celery.conf
2830
chmod g+w /var/lib/patchman /var/lib/patchman/db /var/lib/patchman/db/patchman.db
2931

32+
WORKER_COUNT=1
33+
if [ -f /etc/patchman/celery.conf ]; then
34+
. /etc/patchman/celery.conf
35+
WORKER_COUNT=${CELERY_WORKER_COUNT:-1}
36+
fi
37+
38+
if [ -d /run/systemd/system ]; then
39+
systemctl daemon-reload >/dev/null || true
40+
for i in $(seq 1 "${WORKER_COUNT}"); do
41+
deb-systemd-helper enable "patchman-celery-worker@$i.service" >/dev/null || true
42+
deb-systemd-invoke start "patchman-celery-worker@$i.service" >/dev/null || true
43+
done
44+
45+
active_instances=$(systemctl list-units --type=service --state=active "patchman-celery-worker@*" --no-legend | awk '{print $1}')
46+
47+
for service in $active_instances; do
48+
inst_num=$(echo "$service" | cut -d'@' -f2 | cut -d'.' -f1)
49+
if [ "$inst_num" -gt "${WORKER_COUNT}" ]; then
50+
deb-systemd-invoke stop "$service" >/dev/null || true
51+
deb-systemd-helper disable "$service" >/dev/null || true
52+
fi
53+
done
54+
55+
deb-systemd-helper enable "patchman-celery-beat.service" >/dev/null || true
56+
deb-systemd-invoke start "patchman-celery-beat.service" >/dev/null || true
57+
fi
58+
3059
echo
3160
echo "Remember to run 'patchman-manage createsuperuser' to create a user."
3261
echo

etc/patchman/celery.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
REDIS_HOST=127.0.0.1
22
REDIS_PORT=6379
3+
CELERY_POOL_TYPE=solo
4+
CELERY_WORKER_COUNT=1
5+
CELERY_CONCURRENCY=1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[Unit]
2+
Description=Patchman Celery Beat Scheduler Service
3+
Requires=network-online.target
4+
After=network-online.target
5+
6+
[Service]
7+
Type=simple
8+
User=patchman-celery
9+
Group=patchman-celery
10+
EnvironmentFile=/etc/patchman/celery.conf
11+
ExecStart=/usr/bin/celery \
12+
--broker redis://${REDIS_HOST:-127.0.0.1}:${REDIS_PORT:-6379}/0 \
13+
--app patchman \
14+
beat \
15+
--loglevel info \
16+
--scheduler django_celery_beat.schedulers:DatabaseScheduler
17+
18+
[Install]
19+
WantedBy=multi-user.target
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[Unit]
2+
Description=Patchman Celery Worker Service %i
3+
Requires=network-online.target
4+
After=network-online.target
5+
6+
[Service]
7+
Type=simple
8+
User=patchman-celery
9+
Group=patchman-celery
10+
EnvironmentFile=/etc/patchman/celery.conf
11+
ExecStart=/usr/bin/celery \
12+
--broker redis://${REDIS_HOST:-127.0.0.1}:${REDIS_PORT:-6379}/0 \
13+
--app patchman \
14+
worker \
15+
--task-events \
16+
--pool ${CELERY_POOL_TYPE:-solo} \
17+
--concurrency ${CELERY_CONCURRENCY:-1} \
18+
--hostname patchman-celery-worker%i@%%h
19+
20+
[Install]
21+
WantedBy=multi-user.target

etc/systemd/system/patchman-celery.service

Lines changed: 0 additions & 14 deletions
This file was deleted.

patchman-client.spec

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Source: %{expand:%%(pwd)}
1010
BuildArch: noarch
1111
Requires: curl which coreutils util-linux gawk
1212

13-
%define binary_payload w9.gzdio
13+
%define _binary_payload w9.gzdio
1414

1515
%description
1616
patchman-client provides a client that uploads reports to a patchman server
@@ -20,14 +20,15 @@ find . -mindepth 1 -delete
2020
cp -af %{SOURCEURL0}/. .
2121

2222
%install
23-
mkdir -p %{buildroot}/usr/sbin
24-
mkdir -p %{buildroot}/etc/patchman
25-
cp ./client/%{name} %{buildroot}/usr/sbin
26-
cp ./client/%{name}.conf %{buildroot}/etc/patchman
23+
mkdir -p %{buildroot}%{_sbindir}
24+
mkdir -p %{buildroot}%{_sysconfdir}/patchman
25+
install -m 755 client/%{name} %{buildroot}%{_sbindir}/%{name}
26+
install -m 644 client/%{name}.conf %{buildroot}%{_sysconfdir}/patchman/%{name}.conf
2727

2828
%files
29-
%defattr(755,root,root)
30-
/usr/sbin/patchman-client
31-
%config(noreplace) /etc/patchman/patchman-client.conf
29+
%defattr(-,root,root)
30+
%{_sbindir}/patchman-client
31+
%dir %{_sysconfdir}/patchman
32+
%config(noreplace) %{_sysconfdir}/patchman/patchman-client.conf
3233

3334
%changelog

scripts/rpm-post-install.sh

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ if [ ! -e /etc/httpd/conf.d/patchman.conf ] ; then
44
cp /etc/patchman/apache.conf.example /etc/httpd/conf.d/patchman.conf
55
fi
66

7-
if ! grep /usr/lib/python3.9/site-packages /etc/httpd/conf.d/patchman.conf >/dev/null 2>&1 ; then
8-
sed -i -e "s/^\(Define patchman_pythonpath\).*/\1 \/usr\/lib\/python3.9\/site-packages/" \
7+
PYTHON_SITEPACKAGES=$(python3 -c "import site; print(site.getsitepackages()[0])")
8+
if ! grep "${PYTHON_SITEPACKAGES}" /etc/httpd/conf.d/patchman.conf >/dev/null 2>&1 ; then
9+
sed -i -e "s|^\(Define patchman_pythonpath\).*|\1 ${PYTHON_SITEPACKAGES}|" \
910
/etc/httpd/conf.d/patchman.conf
1011
fi
1112

@@ -24,15 +25,38 @@ patchman-manage makemigrations
2425
patchman-manage migrate --run-syncdb --fake-initial
2526
sqlite3 /var/lib/patchman/db/patchman.db 'PRAGMA journal_mode=WAL;'
2627

27-
chown -R apache:apache /var/lib/patchman
2828
adduser --system --group patchman-celery
2929
usermod -a -G apache patchman-celery
30-
chmod g+w /var/lib/patchman /var/lib/patchman/db /var/lib/patchman/db/patchman.db
31-
chcon --type httpd_sys_rw_content_t /var/lib/patchman/db/patchman.db
32-
semanage port -a -t http_port_t -p tcp 5672
30+
chown root:patchman-celery /etc/patchman/celery.conf
31+
chmod 640 /etc/patchman/celery.conf
32+
33+
chown -R apache:apache /var/lib/patchman
34+
semanage fcontext -a -t httpd_sys_rw_content_t "/var/lib/patchman/db(/.*)?"
35+
restorecon -Rv /var/lib/patchman/db
3336
setsebool -P httpd_can_network_memcache 1
3437
setsebool -P httpd_can_network_connect 1
3538

39+
WORKER_COUNT=1
40+
if [ -f /etc/patchman/celery.conf ]; then
41+
. /etc/patchman/celery.conf
42+
WORKER_COUNT=${CELERY_WORKER_COUNT:-1}
43+
fi
44+
45+
for i in $(seq 1 "${WORKER_COUNT}"); do
46+
systemctl enable --now "patchman-celery-worker@$i.service"
47+
done
48+
49+
active_instances=$(systemctl list-units --type=service --state=active "patchman-celery-worker@*" --no-legend | awk '{print $1}')
50+
for service in $active_instances; do
51+
inst_num=$(echo "$service" | cut -d'@' -f2 | cut -d'.' -f1)
52+
if [ "$inst_num" -gt "${WORKER_COUNT}" ]; then
53+
systemctl stop "$service"
54+
systemctl disable "$service"
55+
fi
56+
done
57+
58+
systemctl enable --now patchman-celery-beat.service
59+
3660
echo
3761
echo "Remember to run 'patchman-manage createsuperuser' to create a user."
3862
echo

setup.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with Patchman. If not, see <http://www.gnu.org/licenses/>
1818

1919
import os
20+
import sys
2021

2122
from setuptools import find_packages, setup
2223

@@ -29,8 +30,14 @@
2930
with open('requirements.txt', 'r', encoding='utf_8') as rt:
3031
install_requires = rt.read().splitlines()
3132

32-
3333
data_files = []
34+
if 'bdist_rpm' in sys.argv:
35+
data_files.append(
36+
('/usr/lib/systemd/system', [
37+
'etc/systemd/system/patchman-celery-worker@.service',
38+
'etc/systemd/system/patchman-celery-beat.service'
39+
])
40+
)
3441

3542
for dirpath, dirnames, filenames in os.walk('etc'):
3643
# Ignore dirnames that start with '.'

0 commit comments

Comments
 (0)