From 10b74fbc4f0079d632eef4214604c8d2796f6ced Mon Sep 17 00:00:00 2001 From: scragg Date: Mon, 30 Sep 2013 09:34:16 -0500 Subject: [PATCH] default repo --- reimagine/__init__.py | 26 ++++++++++++++++++++------ reimagine/models.py | 8 +++----- reimagine/util.py | 6 ++++++ srv/salt/supervisor/supervisord.conf | 5 +++-- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/reimagine/__init__.py b/reimagine/__init__.py index 84f5920..10a6c1f 100644 --- a/reimagine/__init__.py +++ b/reimagine/__init__.py @@ -2,20 +2,21 @@ import config import redis import logging import rethinkdb as rdb +import os from flask import Flask, request, render_template, url_for, redirect from flask.ext.bcrypt import Bcrypt from flask.ext.login import LoginManager from flask.ext.assets import Environment from session import RedisSessionInterface from gittle import Gittle -from os import sep, path from wiki import Wiki +from util import mkdir_safe app = Flask(__name__) app.config.update(config.flask) app.debug = (config.ENV is not 'PROD') app.secret_key = config.secret_key -app.static_path = sep + 'static' +app.static_path = os.sep + 'static' app.session_interface = RedisSessionInterface() bcrypt = Bcrypt(app) @@ -29,13 +30,26 @@ assets.directory = app.static_folder cache = redis.StrictRedis(host=config.cache['host'], port=config.cache['port']) -conn = rdb.connect(config.db['host'], config.db['port']) +conn = rdb.connect(config.db['host'], config.db['port'], db=config.db['dbname']) + +if not config.db['dbname'] in rdb.db_list().run(conn) and config.ENV is not 'PROD': + # Create default db and repo + print "Creating DB %s" % config.db['dbname'] + rdb.db_create(config.db['dbname']).run(conn) + for tbl in ['sites', 'users', 'pages']: + rdb.table_create(tbl).run(conn) + +repo_dir = config.repos['dir'] + "/" + config.repos['main'] + +repo = Gittle(repo_dir) +mkdir_safe(repo_dir) + +if not repo.has_index(): + repo.init(repo_dir) from models import Site -site = Site.get_by_name(".") - -w = Wiki(site.get('repo')) +w = Wiki(repo_dir) def redirect_url(): diff --git a/reimagine/models.py b/reimagine/models.py index 6870486..c404b6b 100644 --- a/reimagine/models.py +++ b/reimagine/models.py @@ -1,4 +1,5 @@ -from reimagine import rdb, conn +import rethinkdb as rdb +from reimagine import conn def get_one(cur): @@ -7,9 +8,7 @@ def get_one(cur): class BaseModel(): - __db__ = None __table__ = None - _rdb = rdb _conn = conn def __init__(self): @@ -17,14 +16,13 @@ class BaseModel(): @classmethod def filter(cls, f, limit=None): - q = cls._rdb.db(cls.__db__).table(cls.__table__).filter(f) + q = rdb.table(cls.__table__).filter(f) if limit: q.limit(int(limit)) return q.run(cls._conn) class Site(BaseModel): - __db__ = 'lastdb' __table__ = 'sites' @classmethod diff --git a/reimagine/util.py b/reimagine/util.py index d5cf2ad..49916d7 100644 --- a/reimagine/util.py +++ b/reimagine/util.py @@ -2,6 +2,12 @@ import re import os +def mkdir_safe(path): + if path and not(os.path.exists(path)): + os.makedirs(path) + return path + + def extract_path(file_path): if not file_path: return None diff --git a/srv/salt/supervisor/supervisord.conf b/srv/salt/supervisor/supervisord.conf index 1c99d29..a017d39 100644 --- a/srv/salt/supervisor/supervisord.conf +++ b/srv/salt/supervisor/supervisord.conf @@ -18,7 +18,7 @@ nodaemon = false minfds = 1024 minprocs = 200 umask = 022 -user = deploy +user = root identifier = supervisor directory = /tmp nocleanup = true @@ -29,4 +29,5 @@ strip_ansi = false command=/usr/bin/python /vagrant/app.py [program:rethinkdb] -command=/usr/bin/rethinkdb --config-file /etc/rethinkdb/rdb0.conf \ No newline at end of file +command=/usr/bin/rethinkdb --config-file /etc/rethinkdb/rdb0.conf +user=root \ No newline at end of file