Archiv von discourse.mafiasi.de vom Saturday September 21, 2019.

Python Virtualenv and systemd for Graphite

Nils

Some days ago i wanted to run Graphite on our new server. So I created a new user and a virtualenv for it and installed Graphite into it. The server is a Debian Jessie so I wanted to run Graphite with systemd. In the official documentation is a systemd example, but unfortunately this only works without a virtualenv.

So I went searching in the Internet for a solution. As far as I found out there was only one working solution in the net, and one not working. The working solution suggested to encapsulate the source command together with the run command in a bash script.

I wasn't happy with this, because it take away the possibilities of socket activation. So I tried some things.

The first thing tried was to execute the activate.sh script with the ExecStartPre= hook. This does not work because the script tries to set the environment witch ends after the hook.

So I took a look into the activate.sh and found out it only sets two environment variables (and define a new shell prompt and the deactivate function). So I tried to set the environment variables with systemds Environment directive, and now it works.

my graphite-web.service file:

[Unit]
Description=graphite-web
Requires=graphite-web.socket
After=network.target

[Service]
PIDFile=/opt/graphite/graphite-web.pid
User=graphite
Group=graphite
WorkingDirectory=/opt/graphite/webapp/graphite/
Environment=VIRTUAL_ENV="/home/graphite/.py-env"
Environment=PATH="$VIRTUAL_ENV/bin:$PATH"
ExecStart=/home/graphite/.py-env/bin/gunicorn --pid /opt/graphite/graphite-web.pid -w 2 wsgi:application
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

The graphite-web.socket file:

[Unit]
Description=graphite-web socket

[Socket]
ListenStream=/run/graphite-web/socket
ListenStream=127.0.0.1:8980
#ListenStream=[::]:8000

[Install]
WantedBy=sockets.target

the carbon-cache.service file:

[Unit]
Description=carbon cahce The cache vor graphite
Requires=
After=network.target

[Service]
PIDFile=/opt/graphite/carbon-cache.pid
User=graphite
Group=graphite
WorkingDirectory=/opt/graphite/bin
Environment=VIRTUAL_ENV="/home/graphite/.py-env"
Environment=PATH="$VIRTUAL_ENV/bin:$PATH"
ExecStart=/opt/graphite/bin/carbon-cache.py --pidfile /opt/graphite/carbon-cache.pid start
ExecReload=/bin/kill -USR1 $MAINPID
ExecStop=/opt/graphite/bin/carbon-cache.py --pidfile /opt/graphite/carbon-cache.pid stop
PrivateTmp=true
Type=forking

[Install]
WantedBy=multi-user.target