I have started using Ansible to manage the few services that I still have running around here. I want to deploy clortho to a user on the system I use for serving up movies to my Roku players . I need to copy the source, setup a venv with the dependencies (aiohttp ), and setup a systemd unit to make sure it is started at boot time.
As of Python 3.3 the core library includes support for venv , and Python 3.4 added default installation of pip so that now the only support you need on a target system is the core Python 3.4 build. Everything else can be done inside a venv.
Here’s the Ansible playbook that I am using:
--- - hosts: clortho-host tasks: - name: Test connection ping: - name: Create clortho user user: name=clortho - name: Install clortho.service copy: src=./configs/clortho/clortho.service dest=/usr/lib/systemd/system/ notify: - restart clortho - reload systemd - name: Install clortho synchronize: src=./git/clortho/ dest=/home/clortho/app/ rsync_opts=--exclude=.git notify: - restart clortho - name: Setup venv for clortho pip: requirements=/home/clortho/app/requirements.txt virtualenv=/home/clortho/.venv/ virtualenv_command=pyvenv-3.4 notify: - restart clortho - name: Enable clortho.service at boot service: name=clortho enabled=yes handlers: - name: restart clortho service: name=clortho state=restarted - name: reload systemd command: systemctl daemon-reload
This creates a clortho user with a default home directory, installs the systemd service file, and copies over the checked-out version of clortho from the host. Currently I manage deploying my code from a git repo that is a clone of the working repo. I can then check out experimental branches for deployment without disturbing other work on the same project.
The venv (this is a Python 3.4 project) is setup using pip with Python 3.4 installed to /usr/local/ on the target system. Other than the core Python 3.4 modules all dependencies are in the application specific venv in the clortho home directory. The important bit is setting the virtualenv_command to pyvenv-3.4 so that Python 3.4 is used for the setup, without that it would use the system’s default version of Python.
In order to start the service at boot time it uses a systemd service file that looks like this:
[Unit] Description=Clortho key server After=network-online.target [Service] WorkingDirectory=/home/clortho/ User=clortho ExecStart=/home/clortho/.venv/bin/python3.4 /home/clortho/app/src/clortho.py --port 9006 [Install] WantedBy=multi-user.target
This runs the service as the clortho user and starts it using the Python 3.4 venv environment. Systemd has made the management of services much easier and cleaner than the old style init systems. I was skeptical at first, but it really is a huge improvement.
With all of the above I now have a key/value server running on localhost port 9006. The goal is to use this with the Home Media Server project for the Roku so that it will remember the last played episodes and positions for the videos. Currently the HMS application writes that data to the Roku’s tmp:/ filesystem which is deleted when you exit, meaning you start each session by scrolling through a pile of episodes trying to remember the last one you watched. I’ll talk about the changes to HMS in a future post.