Commit 6dec59be by François C.

Merge branch 'couchdb-init' into 'dev_principale'

Dev: simplification de l'environment de dev pour l'initialisation de la base CouchDB

See merge request !18
parents 2c4b1bed 0de98d5c
Pipeline #980 passed with stage
in 21 seconds
{
"views": {
"by_fp": {
"map": "function(doc){emit(doc.fingerprint);}"
},
"by_completed": {
"map": "function(doc){emit(doc.completed);}"
},
"by_odoo_id": {
"map": "function(doc){emit(doc.odoo_id);}"
}
}
}
{
"views": {
"by_type": {
"map": "function(doc){emit(doc.type);}"
}
}
}
#!/usr/bin/env sh
sleep 3
curl -X PUT http://admin:123abc@couchdb:5984/coops
curl -X PUT http://admin:123abc@couchdb:5984/inventory
curl -X PUT http://admin:123abc@couchdb:5984/envelop
curl -X PUT http://admin:123abc@couchdb:5984/shopping_carts
curl -X PUT http://admin:123abc@couchdb:5984/coops/_design/index \
-d @couchdb-coops-init.json
curl -X PUT http://admin:123abc@couchdb:5984/envelop/_design/index \
-d @couchdb-envelop-init.json
...@@ -27,17 +27,6 @@ services: ...@@ -27,17 +27,6 @@ services:
volumes: volumes:
- "couchdb-data:/opt/couchdb/data" - "couchdb-data:/opt/couchdb/data"
initializer:
image: curlimages/curl
deploy:
restart_policy:
condition: on-failure
depends_on:
- couchdb
command: ["sh", "-c", "cd /tmp && ./couchdb-setup.sh"]
volumes:
- "./:/tmp"
database: database:
image: "postgres:10" image: "postgres:10"
env_file: .env env_file: .env
......
FROM python:3-slim FROM python:3-slim
# Add french locale
RUN apt update && \
apt install -y --no-install-recommends locales && \
rm -rf /var/lib/apt/lists/* && \
sed -i '/^#.* fr_FR.UTF-8 /s/^#//' /etc/locale.gen && \
locale-gen
# Virtual env: # Virtual env:
ENV VIRTUAL_ENV=/opt/venv ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV RUN python3 -m venv $VIRTUAL_ENV
......
[couchdb] [couchdb]
single_node=true single_node=true
users_db_security_editable = true
[httpd] [httpd]
enable_cors = true enable_cors = true
[cors] [cors]
credentials = true credentials = true
origins = http://127.0.0.1:8080 origins = *
[replicator] [replicator]
db = _replicator db = _replicator
......
#! /bin/sh #!/usr/bin/env bash
port=34001 port=34001
ip=127.0.0.1 ip=127.0.0.1
if [ ! -z "$1" ]
then if [ -n "$1" ]
then
ip=$1 ip=$1
fi fi
if [ -n "$2" ]
if [ ! -z "$2" ] then
then
port=$2 port=$2
fi fi
current_path=$(pwd) current_path=$(pwd)
export PYTHONPATH="$current_path:$current_path/lib:$PYTHONPATH" export PYTHONPATH="$current_path:$current_path/lib:$PYTHONPATH"
echo yes | django-admin collectstatic --settings=outils.settings export DJANGO_SETTINGS_MODULE=outils.settings
django-admin runserver $ip:$port --settings=outils.settings # Collect static files
echo yes | django-admin collectstatic
# Make sure couchdb databases exist
python manage.py couchdb
# Run server
django-admin runserver "$ip:$port"
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
import couchdb
class Command(BaseCommand):
help = 'Initialize needed couchDB databases'
def handle(self, *args, **options):
if 'admin' not in settings.COUCHDB:
raise CommandError('''
Veuillez définir les accès d'admin à CouchDB dans votre fichier settings_secret.py
Vérifiez le fichier settings_secret_example.py pour un exemple.''')
url = settings.COUCHDB['admin']['url'] % (settings.COUCHDB['admin']['user'], settings.COUCHDB['admin']['password'])
dbnames = settings.COUCHDB['dbs']
couchserver = couchdb.Server(url)
for dbname in dbnames.values():
if dbname not in couchserver:
self.stdout.write(self.style.WARNING("Missing database %s" % dbname))
db = couchserver.create(dbname)
self.stdout.write(self.style.SUCCESS("✓ created db"))
if dbname == 'coops':
self.createCoopsViews(db)
elif dbname == 'envelop':
self.createEnvelopViews(db)
# db.security
self.createPublicAccess(db)
def createCoopsViews(self, dbConn):
byFpMapFunction = '''function(doc) {
emit(doc.fingerprint);
}'''
byCompletedMapFunction = '''function(doc) {
emit(doc.completed);
}'''
byOdooMapFunction = '''function(doc) {
emit(doc.odoo_id);
}'''
views = {
"by_fp": {
"map": byFpMapFunction
},
"by_completed": {
"map": byCompletedMapFunction
},
"by_odoo_id": {
"map": byOdooMapFunction
},
}
self.createView(dbConn, "index", views)
def createEnvelopViews(self, dbConn):
byTypeMapFunction = '''function(doc) {
emit(doc.type);
}'''
views = {
"by_type": {
"map": byTypeMapFunction
}
}
self.createView(dbConn, "index", views)
def createView(self, dbConn, designDoc, views):
self.stdout.write(self.style.SUCCESS("✓ created view %s" % designDoc))
data = {
"_id": "_design/%s" % designDoc,
"views": views,
"language": "javascript",
"options": {"partitioned": False }
}
dbConn.save(data)
def createPublicAccess(self, dbConn):
self.stdout.write(self.style.SUCCESS("✓ created security rule"))
security_doc = dbConn.resource.get_json("_security")[2]
dbConn.resource.put("_security", {})
...@@ -77,6 +77,9 @@ class OdooAPI: ...@@ -77,6 +77,9 @@ class OdooAPI:
class CouchDB: class CouchDB:
"""Class to handle interactions with CouchDB""" """Class to handle interactions with CouchDB"""
if 'private_url' in settings.COUCHDB:
url = settings.COUCHDB['private_url']
else:
url = settings.COUCHDB['url'] url = settings.COUCHDB['url']
dbs = settings.COUCHDB['dbs'] dbs = settings.COUCHDB['dbs']
db = None db = None
......
...@@ -8,7 +8,13 @@ ODOO = { ...@@ -8,7 +8,13 @@ ODOO = {
} }
COUCHDB = { COUCHDB = {
'private_url': 'http://couchdb:5984',
'url': 'http://127.0.0.1:5984', 'url': 'http://127.0.0.1:5984',
'admin': {
'url': 'http://%s:%s@couchdb:5984',
'user': 'admin',
'password': '123abc',
},
'dbs': { 'dbs': {
'member': 'coops', 'member': 'coops',
'inventory': 'inventory', 'inventory': 'inventory',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment