Beispiel #1
0
def config_install(args, l, rc):
    import yaml
    import pkgutil
    import os
    from os.path import join, dirname
    import getpass
    import ambry.support
    from ambry.run import ROOT_DIR, USER_DIR, CONFIG_FILE, ACCOUNTS_FILE
    from ambry.util import AttrDict

    user = getpass.getuser()

    default_config_file = join(dirname(ambry.support.__file__),'ambry-{}.yaml'.format(args.template))

    d = AttrDict().update_yaml(default_config_file)

    user_config_dir = os.path.join(os.path.expanduser('~'), USER_DIR)

    if user == 'root': # Root user
        config_dir = ROOT_DIR
        default_root = d.library.filesystem_root

    elif os.getenv('VIRTUAL_ENV'):  # Special case for python virtual environments
        config_dir = os.path.join(os.getenv('VIRTUAL_ENV'), USER_DIR)
        default_root = os.path.join(os.getenv('VIRTUAL_ENV'), 'data')

    else: # Non-root user, outside of virtualenv
        config_dir = user_config_dir
        warn(("Installing as non-root, to '{}'\n" +
              "Run as root to install for all users.").format(config_dir))
        default_root = os.path.join(os.path.expanduser('~'), 'ambry')

    if args.root:
        default_root = args.root

    if not os.path.exists(user_config_dir):
        os.makedirs(user_config_dir)

    if not os.path.exists(config_dir):
        os.makedirs(config_dir)

    if os.path.exists(os.path.join(config_dir, CONFIG_FILE)):
        if args.force:
            prt("File output file exists, overwriting: {}".format(config_dir))

        else:
            fatal("Output file {} exists. Use  -f to overwrite".format(config_dir))

    d['library']['filesystem_root'] = default_root

    s = d.dump()

    if args.prt:
        prt(s.replace("{", "{{").replace("}", "}}"))
        return

    #Create an empty accounts file, if it does not exist
    user_accounts_file =os.path.join(user_config_dir, ACCOUNTS_FILE)

    if not os.path.exists(user_accounts_file):
        with open(user_accounts_file, 'w') as f:
            from ambry.util import random_string
            d = dict(accounts=dict(
                        password=random_string(16),
                        ambry=dict(
                            name=None, email=None
                        )
                    )
                )

            prt('Writing accounts file: {}'.format(user_accounts_file))
            f.write(yaml.dump(d, indent=4, default_flow_style=False))

    config_file = os.path.join(config_dir, CONFIG_FILE)

    with open(config_file, 'w') as f:
        prt('Writing config file: {}'.format(config_file))
        f.write(s)

    # Make the directories.

    from ..run import get_runconfig
    rc = get_runconfig(config_file)

    for name, v in iteritems(rc.filesystem):
        dr = v.format(root=rc.library.filesystem_root)

        try:

            if not os.path.exists(dr):
                prt("Making directory: {}".format(dr))
                os.makedirs(dr)
        except KeyError:
            pass