Wednesday, September 20, 2017

Create a Cisco CSM systemd Startup Service on Linux

If you are using Cisco's new linux-based Cisco Software Manager server, then you probably want to make sure there is a startup service for it. In this post, I'm going to focus on a systemd solution, not the older init system.

I'll assume that you've already installed the CSM server. For purposes of this post, I'll use /usr/local as it's location.

Getting started


If you're using a python virtual environment, then you'll need to explicitly tell the CSM server where your environment's python interpreter and gunicorn executable are. If anyone knows how to create the systemd service without having to do this, please share - because my attempts failed. If you aren't using a python virtual environment, you can skip to the next section where we create the systemd service.

 - Open /usr/local/csm/csmserver/csmserver in your editor.
 - Change the following line to include the absolute path to python in your virtual environment. Don't close the file yet.
# for Python interpreter
PYTHON="/usr/local/csm/csmserver/env/bin/python"

 - Within the same file, add a variable for gunicorn in the appropriate section. Don't close the file yet.
# for gunicorn
GUNICORN="/usr/local/csm/csmserver/env/bin/gunicorn"

 - In the 'function start_csmserver' and 'function start_secure_csmserver' sections, change each instance of 'gunicorn' to '$GUNICORN', to match our newly created variable. Then save and close the csmserver file.
$GUNICORN -w $WORKERS -b $LISTENING_IP:$PORT --timeout $TIMEOUT --log-file=- csmserver:app & $PYTHON csmdispatcher.py &

$GUNICORN -w $WORKERS -b $LISTENING_IP:$PORT --timeout $TIMEOUT --log-file=- --keyfile=$SSLKEY --certfile=$SSLCERT  csmserver:app & $PYTHON csmdispatcher.py &

Creating the systemd service


 - Create a new file in /usr/lib/systemd/system called csm.service
vi /usr/lib/systemd/system/csm.service

 - Add the following to the file. Save and close the file. Be sure to use the correct path, if different from /usr/local.
[Unit]
Description="Cisco Software Manager"

[Service]
WorkingDirectory=/usr/local/csm/csmserver
RemainAfterExit=yes
ExecStart=/usr/local/csm/csmserver/csmserver start
ExecStop=/usr/local/csm/csmserver/csmserver stop

[Install]
WantedBy=multi-user.target

 - Reload the systemd daemon and set the service to start at boot
systemctl daemon-reload
systemctl enable csm.service

 - Start the service. Be sure to verify that the service is active and that you can see the gunicorn and csmdispatcher.py processes.
systemctl start csm.service
systemctl status csm.service


Keep in mind that if you upgrade your CSM server in the future and you use a Python virtual environment, you'll need to go back in to the csmserver file and re-add the appropriate lines.

That should be all you need to create your systemd service for the Cisco Software Manager server!