diff options
author | Milo Casagrande <milo.casagrande@linaro.org> | 2014-10-22 15:57:52 +0200 |
---|---|---|
committer | Milo Casagrande <milo.casagrande@linaro.org> | 2014-10-22 15:57:52 +0200 |
commit | 879f02f86456bab6b018db8f14da798083037994 (patch) | |
tree | 417da59f36db6f714aad4478344788e1b3bc4ff7 /app/handlers/dbindexes.py | |
parent | 065aa60d12d9d96dffb4e80287f35754ed25413b (diff) |
server: Add mongodb parameters.
* Make sure we can define the mongodb parameters in some way.
They should be available from the CLI or from the config file.
* Refactor dbindexes out from utils and into the handlers.
Change-Id: I7f8da28cdc15fe56fed77c847c228c71244086f4
Diffstat (limited to 'app/handlers/dbindexes.py')
-rw-r--r-- | app/handlers/dbindexes.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/app/handlers/dbindexes.py b/app/handlers/dbindexes.py new file mode 100644 index 0000000..8e87487 --- /dev/null +++ b/app/handlers/dbindexes.py @@ -0,0 +1,84 @@ +# Copyright (C) 2014 Linaro Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +"""Make sure indexes are created at startup.""" + +from pymongo import ( + ASCENDING, + DESCENDING, +) + +from models import ( + CREATED_KEY, + DB_NAME, + STATUS_KEY +) +from models import ( + BOOT_COLLECTION, + DEFCONFIG_COLLECTION, + JOB_COLLECTION, +) + + +def ensure_indexes(client, db_options): + """Ensure that mongodb indexes exists, if not create them. + + This should be called at server startup. + + :param client: The mongodb client used to access the database. + :param db_options: The mongodb database connection parameters. + :type db_options: dict + """ + db_user = db_options["dbuser"] + db_pwd = db_options["dbpassword"] + + database = client[DB_NAME] + if all([db_user, db_pwd]): + database.authenticate(db_user, password=db_pwd) + + _ensure_job_indexes(database) + _ensure_boot_indexes(database) + _ensure_defconfig_indexes(database) + + +def _ensure_job_indexes(database): + """Ensure indexes exists for the 'job' collection. + + :param database: The database connection. + """ + database[JOB_COLLECTION].ensure_index( + [(CREATED_KEY, DESCENDING)], background=True + ) + + +def _ensure_boot_indexes(database): + """Ensure indexes exists for the 'boot' collection. + + :param database: The database connection. + """ + database[BOOT_COLLECTION].ensure_index( + [(CREATED_KEY, DESCENDING)], background=True + ) + + +def _ensure_defconfig_indexes(database): + """Ensure indexes exists for the 'defconfig' collection. + + :param database: The database connection. + """ + collection = database[DEFCONFIG_COLLECTION] + + collection.ensure_index([(CREATED_KEY, DESCENDING)], background=True) + collection.ensure_index([(STATUS_KEY, ASCENDING)], background=True) |