Add toggle to disable sql migrations for the offline client

We don't want to run SQL migrations systematically when instanciating an
offline client, especially if we expect that they have been run already.

This is the case for the ara_record action plugin which is not expected
to run until after there has been a playbook created.

Change-Id: I6a34f92bc13ff9fddc8de010eba159d57a6ca9a6
This commit is contained in:
David Moreau Simard 2019-06-20 16:05:39 -04:00
parent 9065642a67
commit 414783240f
No known key found for this signature in database
GPG Key ID: CBEB466764A9E621
4 changed files with 36 additions and 8 deletions

View File

@ -33,7 +33,7 @@ except ImportError as e:
class AraOfflineClient(AraHttpClient):
def __init__(self, auth=None):
def __init__(self, auth=None, run_sql_migrations=True):
self.log = logging.getLogger(__name__)
from django import setup as django_setup
@ -41,6 +41,7 @@ class AraOfflineClient(AraHttpClient):
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ara.server.settings")
if run_sql_migrations:
# Automatically create the database and run migrations (is there a better way?)
execute_from_command_line(["django", "migrate"])

View File

@ -20,7 +20,14 @@ from requests.auth import HTTPBasicAuth
from ara.clients.http import AraHttpClient
def get_client(client="offline", endpoint="http://127.0.0.1:8000", timeout=30, username=None, password=None):
def get_client(
client="offline",
endpoint="http://127.0.0.1:8000",
timeout=30,
username=None,
password=None,
run_sql_migrations=True,
):
"""
Returns a specified client configuration or one with sane defaults.
"""
@ -31,7 +38,7 @@ def get_client(client="offline", endpoint="http://127.0.0.1:8000", timeout=30, u
if client == "offline":
from ara.clients.offline import AraOfflineClient
return AraOfflineClient(auth=auth)
return AraOfflineClient(auth=auth, run_sql_migrations=run_sql_migrations)
elif client == "http":
return AraHttpClient(endpoint=endpoint, timeout=timeout, auth=auth)
else:

View File

@ -150,7 +150,12 @@ class ActionModule(ActionBase):
username = options["api_username"]
password = options["api_password"]
self.client = client_utils.get_client(
client=client, endpoint=endpoint, timeout=timeout, username=username, password=password
client=client,
endpoint=endpoint,
timeout=timeout,
username=username,
password=password,
run_sql_migrations=False,
)
def create_or_update_key(self, playbook, key, value, type):

View File

@ -13,7 +13,9 @@ ARA Offline API client
If your use case doesn't require a remote or persistent API server, the offline
client lets you query the API without needing to start an API server.
In order to use this client, you would instanciate it like this::
In order to use this client, you would instanciate it like this:
.. code-block:: python
#!/usr/bin/env python3
# Import the client
@ -22,6 +24,17 @@ In order to use this client, you would instanciate it like this::
# Instanciate the offline client
client = AraOfflineClient()
Note that, by default, instanciating an offline client will automatically run
SQL migrations.
If you expect the migrations to have already been run when you instanciate
the client, you can disable automatic SQL migrations with by specifying
``run_sql_migrations=False``:
.. code-block:: python
client = AraOfflineClient(run_sql_migrations=False)
ARA HTTP API client
~~~~~~~~~~~~~~~~~~~
@ -29,7 +42,9 @@ ARA HTTP API client
``AraOfflineClient``.
You can set your client to communicate with a remote ``ara-server`` API by
specifying an endpoint parameter::
specifying an endpoint parameter:
.. code-block:: python
#!/usr/bin/env python3
# Import the client