teamplayer: replace openrc-style startup with systemd units

When I switched over from openrc to systemd I was being lazy and took
advantage of the fact that /etc/local.d still gets run as usual on
Gentoo.  This commit finally does a proper(-ish) systemd startup using
(3) unit files.
This commit is contained in:
Albert Hopkins 2015-09-23 03:35:47 +00:00
parent bde2764a9c
commit 73c5fce1e0
8 changed files with 65 additions and 79 deletions

View File

@ -7,13 +7,14 @@ PGVER = 9.4
INSTALL = install
AVAHI := NO
M4_DEFS += -D TP_USER=$(TP_USER) -D TP_HOME=$(TP_HOME) -D TP_DB=$(TP_DB) -D HOSTNAME=$(HOSTNAME)
M4_DEFS += -D TP_USER=$(TP_USER) -D TP_HOME=$(TP_HOME) -D TP_DB=$(TP_DB) -D HOSTNAME=$(HOSTNAME) -D PGVER=$(PGVER)
M4C = $(M4) $(M4_DEFS)
rcdefault := /etc/runlevels/default
post_files = bash_profile settings_local.py start-teamplayer stop-teamplayer
post_files += local.start local.stop issue nginx.conf teamplayer.service
post_files = bash_profile settings_local.py teamplayer-pre.service
post_files += teamplayer-wsgi.service teamplayer-spindoctor.service
post_files += issue nginx.conf teamplayer.service
preinstall:
@ -24,7 +25,6 @@ postinstall: $(post_files) $(SCROBBLER_AUTH) urls.py
$(inroot) eselect postgresql set $(PGVER)
$(inroot) rm -rf /var/lib/postgresql/$(PGVER)/data
$(inroot) bash -c "echo y |$(EMERGE) --config postgresql:$(PGVER)"
$(inroot) systemctl enable postgresql-$(PGVER).service
ifeq ($(AVAHI),YES)
$(inroot) $(EMERGE) --noreplace $(USEPKG) net-dns/avahi
$(inroot) rm -f /etc/avahi/services/*
@ -48,27 +48,20 @@ endif
ifdef SCROBBLER_AUTH
cat $(SCROBBLER_AUTH) >> $(CHROOT)/$(TP_HOME)/project/settings_local.py
endif
mkdir -p $(CHROOT)$(TP_HOME)/bin
$(M4C) start-teamplayer > $(CHROOT)$(TP_HOME)/bin/start-teamplayer
chmod +x $(CHROOT)$(TP_HOME)/bin/start-teamplayer
$(M4C) stop-teamplayer > $(CHROOT)$(TP_HOME)/bin/stop-teamplayer
chmod +x $(CHROOT)$(TP_HOME)/bin/stop-teamplayer
$(inroot) bash -c ". $(TP_HOME)/bin/activate; cd $(TP_HOME); DJANGO_SETTINGS_MODULE=project.settings_local $(TP_HOME)/manage.py collectstatic --noinput"
$(inroot) $(INSTALL) -d -o $(TP_USER) -g $(TP_USER) $(TP_DB)
$(inroot) $(INSTALL) -d -o $(TP_USER) -g $(TP_USER) $(TP_DB)/songs
$(inroot) mkdir -p $(TP_HOME)/library
$(inroot) chown -R $(TP_USER):$(TP_USER) $(TP_HOME)
$(inroot) $(INSTALL) -d -o $(TP_USER) -g $(TP_USER) $(TP_DB)/mpd
$(inroot) $(INSTALL) -d -o $(TP_USER) -g $(TP_USER) /var/log/teamplayer
$(M4C) local.start > $(CHROOT)/etc/local.d/teamplayer.start
$(M4C) local.stop > $(CHROOT)/etc/local.d/teamplayer.stop
$(inroot) chmod +x /etc/local.d/teamplayer.start
$(inroot) chmod +x /etc/local.d/teamplayer.stop
$(M4C) teamplayer-pre.service > $(CHROOT)/etc/systemd/system/teamplayer-pre.service
$(M4C) teamplayer-wsgi.service > $(CHROOT)/etc/systemd/system/teamplayer-wsgi.service
$(M4C) teamplayer-spindoctor.service > $(CHROOT)/etc/systemd/system/teamplayer-spindoctor.service
cp issue $(CHROOT)/etc/issue
$(M4C) nginx.conf > $(CHROOT)/etc/nginx/nginx.conf
$(inroot) gpasswd -a nginx teamplayer
$(inroot) systemctl enable nginx.service
#$(inroot) ln -sf /etc/init.d/ntpd $(rcdefault)/ntpd
$(inroot) systemctl enable teamplayer-wsgi.service
$(inroot) systemctl enable teamplayer-spindoctor.service
$(inroot) $(EMERGE) --depclean --with-bdeps=n
clean:

View File

@ -1,16 +0,0 @@
#!/bin/sh
LOGFILE="/var/log/teamplayer/teamplayer.log"
if [ ! -f /etc/firstboot ] ; then
echo "Creating TeamPlayer database ..."
createuser -U postgres -D -S -R teamplayer
createdb -U postgres teamplayer
touch "$LOGFILE"
chown TP_USER:TP_USER "$LOGFILE"
fi
su -c "TP_HOME/bin/start-teamplayer" TP_USER
#tailf "$LOGFILE" > /dev/console &
touch /etc/firstboot

View File

@ -1,3 +0,0 @@
#!/bin/sh
su -c "TP_HOME/bin/stop-teamplayer" TP_USER

View File

@ -1,28 +0,0 @@
#!/bin/sh
# this should be run as the TP_USER user
PYTHON=python
PYTHONPATH="$HOME" ; export PYTHONPATH
DJANGO_SETTINGS_MODULE="project.settings_local" ; export DJANGO_SETTINGS_MODULE
cd ~
. bin/activate
if [ ! -f "/etc/firstboot" ] ; then
$PYTHON manage.py syncdb --noinput --verbosity=0
$PYTHON manage.py collectstatic --noinput --verbosity=0
fi
# start the uwsgi daemon
uwsgi --socket TP_DB/teamplayer.sock \
--chmod=660 \
-w project.wsgi \
--uid teamplayer \
--gid teamplayer \
--pidfile TP_DB/uwsgi.pid \
--daemonize2 /var/log/teamplayer/uwsgi.log \
-T
# Start the Spin Doctor
$PYTHON manage.py spindoctor --verbosity=2 $@ >> "/var/log/teamplayer/teamplayer.log" 2>&1 &
echo $! > "TP_DB/tps.pid"

View File

@ -1,15 +0,0 @@
#!/bin/sh
# this should be run as the TP_USER user
PYTHON=python
PYTHONPATH="$HOME" ; export PYTHONPATH
DJANGO_SETTINGS_MODULE="project.settings_local" ; export DJANGO_SETTINGS_MODULE
. ~/bin/activate
cd ~
$PYTHON manage.py shutdown >> "/var/log/teamplayer/teamplayer.log"
[ -f "TP_DB/tps.pid" ] && kill $(cat "TP_DB/tps.pid")
[ -f "TP_DB/uwsgi.pid" ] && kill $(cat "TP_DB/uwsgi.pid")

View File

@ -0,0 +1,15 @@
[Unit]
Description=TeamPlayer (Pre)
Documentation=https://bitbucket.org/marduk/teamplayer/
Requires=postgresql-PGVER.service
After=postgresql-PGVER.service
[Service]
Type=oneshot
User=TP_USER
Environment=PYTHONPATH=TP_HOME
Environment=DJANGO_SETTINGS_MODULE=project.settings_local
WorkingDirectory=TP_HOME
ExecStart=-/usr/bin/createuser -U postgres -D -S -R TP_USER
ExecStart=-/usr/bin/createdb -U postgres TP_USER
ExecStart=TP_HOME/bin/python manage.py migrate --noinput

View File

@ -0,0 +1,17 @@
[Unit]
Description=TeamPlayer Spin Doctor
Documentation=https://bitbucket.org/marduk/teamplayer/
Requires=teamplayer-pre.service
After=teamplayer-pre.service
After=teamplayer-wsgi.service
[Service]
User=TP_USER
Environment=PYTHONPATH=TP_HOME
Environment=DJANGO_SETTINGS_MODULE=project.settings_local
WorkingDirectory=TP_HOME
ExecStart=TP_HOME/bin/python manage.py spindoctor --verbosity=2
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,23 @@
[Unit]
Description=TeamPlayer WSGI Server
Documentation=https://bitbucket.org/marduk/teamplayer/
Requires=teamplayer-pre.service
After=teamplayer-pre.service
Wants=nginx.service
[Service]
User=teamplayer
WorkingDirectory=/opt/teamplayer
Environment=PYTHONPATH=/opt/teamplayer
Environment=DJANGO_SETTINGS_MODULE=project.settings_local
WorkingDirectory=/opt/teamplayer
ExecStart=/opt/teamplayer/bin/uwsgi --master -p 4 \
--socket=TP_DB/teamplayer.sock \
--chmod=660 -w project.wsgi --uid=teamplayer --gid=teamplayer \
--pidfile=TP_DB/teamplayer.pid
PIDFile=TP_DB/teamplayer.pid
TimeoutStopSec=10
[Install]
WantedBy=multi-user.target