Search code examples
pythonrecursionflaskansiblealembic

Flask migrate command using Ansible throws a maximum recursion depth exceeded error


My flask app uses alembic for managing database migrations. While trying to deploy the app using Ansible, it runs into a Runtime Error with maximum recursion depth exceeded when the Ansible playbook encounters the command python manage.py db upgrade. Error message:

fatal: [host.ip]: FAILED! => {"changed": true, "cmd": "/home/deploy/venvs/app/bin/python 
/var/www/app/manage.py db upgrade", "delta": "0:00:02.854134",
"end": "2016-04-11 15:59:32.656916", "failed": true, "rc": 1,
"start": "2016-04-11 15:59:29.802782",
"stderr": "INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
...
...
File \"/home/deploy/venvs/app/local/lib/python2.7/site-packages/sqlalchemy/
sql/elements.py\", line 3292, in _get_table
return self.__dict__['table']
RuntimeError: maximum recursion depth exceeded in cmp", 
"stdout": "", "stdout_lines": [], "warnings": []}

When I run the command locally, I don't get any error, nor do I when actually SSHing into the remote machine and run the same command. My app structure is:

my_app
|--my_app
|--migrations
   |--versions
   |--alembic.ini
   |--env.py
   |--README
   |--script.py.mako
|--playbooks
|--tests
|--manage.py
|--settings.py
|--wsgi.py

Sample Ansible playbook deploy.yml is as follows:

- name: install dependencies into a new virtualenv
    pip: requirements={{ app_dir }}/requirements.txt
    virtualenv={{ venv_dir }}
- name: create .env file in base directory of the app from template
    template: src=env.j2
              dest={{ app_dir }}/.env
- name: sync the database tables between Flask and PostgreSQL
    shell: ". {{ app_dir }}/.env; {{ venv_python }} {{ app_dir }}/manage.py db upgrade"

The vars file for variable assignment is as follows:

wsgi_env_vars: {
                APP_ENV: 'prod',
               }

with the corresponding env.j2 template file as:

#!/bin/bash
{% for k, v in wsgi_env_vars.iteritems() %}
export {{ k }}="{{ v }}"
{% endfor %}

Tried different approaches to resolve the error, including changing the default shell from sh to bash in ansible, but did not help in any manner. Can anyone help to figure out the mistake?


Solution

  • Figured out here at Github. Just change the ansible command to cd into the directory and then run the migration command.