Révision 9003c07e
Ajouté par Jérôme Schneider il y a plus de 9 ans
mandaye/skel/manager.py | ||
---|---|---|
5 | 5 |
""" |
6 | 6 |
|
7 | 7 |
import os |
8 |
os.environ['MANDAYE_CONFIG_MODULE'] = '{project_name}.config' |
|
9 |
|
|
10 |
import base64 |
|
11 | 8 |
|
12 | 9 |
from optparse import OptionParser |
13 |
|
|
14 |
from mandaye import config |
|
15 |
from mandaye.log import logger |
|
10 |
from {project_name} import default_config |
|
16 | 11 |
|
17 | 12 |
def get_cmd_options(): |
18 |
usage = "usage: %prog --createdb|--upgradedb|--cryptpwd"
|
|
13 |
usage = "usage: %prog --config=/path/to/config.ini --createdb|--upgradedb"
|
|
19 | 14 |
parser = OptionParser(usage=usage) |
15 |
parser.add_option("--config", |
|
16 |
dest="config", |
|
17 |
type="string", |
|
18 |
help="Path of the configuration file" |
|
19 |
) |
|
20 | 20 |
parser.add_option("--createdb", |
21 | 21 |
dest="createdb", |
22 | 22 |
default=False, |
... | ... | |
29 | 29 |
action="store_true", |
30 | 30 |
help="Upgrade Mandaye database" |
31 | 31 |
) |
32 |
parser.add_option("--cryptpwd", |
|
33 |
dest="cryptpwd", |
|
34 |
default=False, |
|
35 |
action="store_true", |
|
36 |
help="Crypt external password in Mandaye's database" |
|
37 |
) |
|
38 | 32 |
(options, args) = parser.parse_args() |
39 | 33 |
return options |
40 | 34 |
|
41 |
def encrypt_pwd(pwd): |
|
42 |
from Crypto.Cipher import AES |
|
43 |
logger.debug("Encrypt password") |
|
44 |
enc_pwd = pwd |
|
45 |
if config.encrypt_secret: |
|
46 |
try: |
|
47 |
cipher = AES.new(config.encrypt_secret, AES.MODE_CFB) |
|
48 |
enc_pwd = cipher.encrypt(pwd) |
|
49 |
enc_pwd = base64.b64encode(enc_pwd) |
|
50 |
except Exception, e: |
|
51 |
if config.debug: |
|
52 |
traceback.print_exc() |
|
53 |
logger.warning('Password encrypting failed %s' % e) |
|
54 |
else: |
|
55 |
logger.warning("You must set a secret to use pwd encryption") |
|
56 |
return enc_pwd |
|
57 |
|
|
58 | 35 |
def main(): |
59 | 36 |
options = get_cmd_options() |
37 |
|
|
38 |
config_files = [default_config] |
|
39 |
if options.config: |
|
40 |
config_files.append(options.config) |
|
41 |
os.environ['MANDAYE_CONFIG_FILES'] = ' '.join(config_files) |
|
42 |
|
|
43 |
from mandaye import config |
|
44 |
from mandaye.log import logger |
|
60 | 45 |
if options.createdb or options.upgradedb: |
61 | 46 |
logger.info("Creating or upgrading database...") |
62 | 47 |
from alembic.config import Config |
63 | 48 |
from alembic import command |
64 |
from mandaye import global_config |
|
65 |
alembic_cfg = Config(global_config.alembic_cfg) |
|
66 |
alembic_cfg.set_main_option("script_location", global_config.alembic_script_path) |
|
49 |
alembic_cfg = Config(config.alembic_cfg) |
|
50 |
alembic_cfg.set_main_option("script_location", config.alembic_script_path) |
|
67 | 51 |
command.upgrade(alembic_cfg, "head") |
68 | 52 |
logger.info("Database upgraded") |
69 |
if options.cryptpwd: |
|
70 |
from mandaye.backends.default import ManagerSPUser |
|
71 |
for user in ManagerSPUser.all(): |
|
72 |
user.password = encrypt_pwd(user.password) |
|
73 |
ManagerSPUser.save() |
|
74 | 53 |
|
75 | 54 |
if __name__ == "__main__": |
76 | 55 |
main() |
Formats disponibles : Unified diff
Centralized config.py into mandaye core
All the configuration is now in ini files.
You could have 3 levels of ini files : * default mandaye core ini file * default mandaye project ini file * optional ini file loading with --config option
Closes #5495