@@ -41,36 +41,46 @@ class SetupCommand(AbstractInstallComman
default=None,
help='Admin password min 6 chars')
parser.add_option('--repos',
action='store',
dest='repos_location',
help='Absolute path to repositories location')
parser.add_option('--name',
dest='section_name',
help='The name of the section to set up (default: app:main)')
parser.add_option('--force-yes',
action='store_true',
dest='force_ask',
help='Force yes to every question')
parser.add_option('--force-no',
action='store_false',
help='Force no to every question')
def command(self):
config_spec = self.args[0]
section = self.options.section_name
if section is None:
if '#' in config_spec:
config_spec, section = config_spec.split('#', 1)
else:
section = 'main'
if not ':' in section:
plain_section = section
section = 'app:'+section
section = 'app:' + section
plain_section = section.split(':', 1)[0]
if not config_spec.startswith('config:'):
config_spec = 'config:' + config_spec
if plain_section != 'main':
config_spec += '#' + plain_section
config_file = config_spec[len('config:'):].split('#', 1)[0]
config_file = os.path.join(os.getcwd(), config_file)
self.logging_file_config(config_file)
conf = appconfig(config_spec, relative_to=os.getcwd())
ep_name = conf.context.entry_point_name
ep_group = conf.context.protocol
@@ -48,50 +48,57 @@ from rhodecode.model.meta import Session
log = logging.getLogger(__name__)
def notify(msg):
"""
Notification for migrations messages
ml = len(msg) + (4 * 2)
print >> sys.stdout, ('*** %s ***\n%s' % (msg, '*' * ml)).upper()
class DbManage(object):
def __init__(self, log_sql, dbconf, root, tests=False):
def __init__(self, log_sql, dbconf, root, tests=False, cli_args={}):
self.dbname = dbconf.split('/')[-1]
self.tests = tests
self.root = root
self.dburi = dbconf
self.log_sql = log_sql
self.db_exists = False
self.cli_args = cli_args
self.init_db()
global ask_ok
if self.cli_args.get('force_ask') is True:
ask_ok = lambda *args, **kwargs: True
elif self.cli_args.get('force_ask') is False:
ask_ok = lambda *args, **kwargs: False
def init_db(self):
engine = create_engine(self.dburi, echo=self.log_sql)
init_model(engine)
self.sa = Session()
def create_tables(self, override=False, defaults={}):
def create_tables(self, override=False):
Create a auth database
quiet = defaults.get('quiet')
log.info("Any existing database is going to be destroyed")
if self.tests or quiet:
if self.tests:
destroy = True
destroy = ask_ok('Are you sure to destroy old database ? [y/n]')
if not destroy:
sys.exit()
sys.exit('Nothing done')
if destroy:
Base.metadata.drop_all()
checkfirst = not override
Base.metadata.create_all(checkfirst=checkfirst)
log.info('Created tables for %s' % self.dbname)
def set_db_version(self):
ver = DbMigrateVersion()
ver.version = __dbversion__
ver.repository_id = 'rhodecode_db_migrations'
ver.repository_path = 'versions'
@@ -319,29 +326,30 @@ class DbManage(object):
Fixes rhodecode settings adds ga_code key for google analytics
hgsettings3 = RhodeCodeSetting('ga_code', '')
try:
self.sa.add(hgsettings3)
self.sa.commit()
except:
self.sa.rollback()
raise
def admin_prompt(self, second=False, defaults={}):
def admin_prompt(self, second=False):
if not self.tests:
import getpass
# defaults
defaults = self.cli_args
username = defaults.get('username')
password = defaults.get('password')
email = defaults.get('email')
def get_password():
password = getpass.getpass('Specify admin password '
'(min 6 chars):')
confirm = getpass.getpass('Confirm password:')
if password != confirm:
log.error('passwords mismatch')
return False
@@ -498,25 +506,26 @@ class DbManage(object):
return
u2p = UserToPerm.query()\
.filter(UserToPerm.user == default_user).all()
fixed = False
if len(u2p) != len(User.DEFAULT_PERMISSIONS):
for p in u2p:
Session().delete(p)
fixed = True
self.populate_default_permissions()
return fixed
def config_prompt(self, test_repo_path='', retries=3, defaults={}):
def config_prompt(self, test_repo_path='', retries=3):
_path = defaults.get('repos_location')
if retries == 3:
log.info('Setting up repositories config')
if _path is not None:
path = _path
elif not self.tests and not test_repo_path:
path = raw_input(
'Enter a valid absolute path to store repositories. '
'All repositories in that path will be added automatically:'
)
psql -U postgres -h localhost -c 'drop database if exists rhodecode;'
psql -U postgres -h localhost -c 'create database rhodecode;'
paster setup-rhodecode rc.ini -q --user=marcink --password=qweqwe --email=marcin@python-blog.com --repos=/home/marcink/repos
paster setup-rhodecode rc.ini --force-yes --user=marcink --password=qweqwe --email=marcin@python-blog.com --repos=/home/marcink/repos
API_KEY=`psql -R " " -A -U postgres -h localhost -c "select api_key from users where admin=TRUE" -d rhodecode | awk '{print $2}'`
echo "run those after running server"
paster serve rc.ini --pid-file=rc.pid --daemon
sleep 3
rhodecode-api --apikey=$API_KEY --apihost=http://127.0.0.1:5001 create_user username:demo1 password:qweqwe email:demo1@rhodecode.org
rhodecode-api --apikey=$API_KEY --apihost=http://127.0.0.1:5001 create_user username:demo2 password:qweqwe email:demo2@rhodecode.org
rhodecode-api --apikey=$API_KEY --apihost=http://127.0.0.1:5001 create_user username:demo3 password:qweqwe email:demo3@rhodecode.org
rhodecode-api --apikey=$API_KEY --apihost=http://127.0.0.1:5001 create_users_group group_name:demo12
rhodecode-api --apikey=$API_KEY --apihost=http://127.0.0.1:5001 add_user_to_users_group usersgroupid:demo12 userid:demo1
rhodecode-api --apikey=$API_KEY --apihost=http://127.0.0.1:5001 add_user_to_users_group usersgroupid:demo12 userid:demo2
echo "killing server"
kill `cat rc.pid`
@@ -28,24 +28,24 @@ import logging
from rhodecode.config.environment import load_environment
from rhodecode.lib.db_manage import DbManage
from rhodecode.model.meta import Session
def setup_app(command, conf, vars):
"""Place any commands to setup rhodecode here"""
dbconf = conf['sqlalchemy.db1.url']
dbmanage = DbManage(log_sql=True, dbconf=dbconf, root=conf['here'],
tests=False)
dbmanage.create_tables(override=True, defaults=command.options.__dict__)
tests=False, cli_args=command.options.__dict__)
dbmanage.create_tables(override=True)
dbmanage.set_db_version()
opts = dbmanage.config_prompt(None, defaults=command.options.__dict__)
opts = dbmanage.config_prompt(None)
dbmanage.create_settings(opts)
dbmanage.create_default_user()
dbmanage.admin_prompt(defaults=command.options.__dict__)
dbmanage.admin_prompt()
dbmanage.create_permissions()
dbmanage.populate_default_permissions()
Session.commit()
Session().commit()
load_environment(conf.global_conf, conf.local_conf, initial=True)
dbmanage.finish()
Status change: