Exemplo n.º 1
0
def test_migrate_config(tljh_dir):
    CONFIG_FILE = config.CONFIG_FILE
    CONFIG_DIR = config.CONFIG_DIR
    OLD_CONFIG_FILE = os.path.join(tljh_dir, "config.yaml")
    OLD_CONFIG_D = os.path.join(tljh_dir, "jupyterhub_config.d")
    CONFIG_D = os.path.join(config.CONFIG_DIR, "jupyterhub_config.d")
    old_config_py = os.path.join(OLD_CONFIG_D, "upgrade.py")
    new_config_py = os.path.join(CONFIG_D, "upgrade.py")

    # initial condition: nothing exists
    assert not os.path.exists(CONFIG_FILE)
    assert not os.path.exists(OLD_CONFIG_FILE)
    assert os.path.isdir(CONFIG_DIR)

    # run migration with old config and no new config
    upgraded_config = "old: config\n"
    with open(OLD_CONFIG_FILE, "w") as f:
        f.write(upgraded_config)
    os.makedirs(OLD_CONFIG_D, exist_ok=True)
    with open(old_config_py, "w") as f:
        f.write("c.JupyterHub.log_level = 10")

    migrator.migrate_config_files()
    assert os.path.exists(CONFIG_FILE)
    assert not os.path.exists(OLD_CONFIG_FILE)
    with open(CONFIG_FILE) as f:
        assert f.read() == upgraded_config
    assert os.path.exists(new_config_py)
    assert not os.path.exists(OLD_CONFIG_D)

    # run again, this time with both old and new config
    duplicate_config = "dupe: config\n"
    with open(OLD_CONFIG_FILE, "w") as f:
        f.write(duplicate_config)
    migrator.migrate_config_files()
    assert os.path.exists(CONFIG_FILE)
    assert not os.path.exists(OLD_CONFIG_FILE)
    # didn't clobber config:
    with open(CONFIG_FILE) as f:
        assert f.read() == upgraded_config

    # preserved old config
    backup_config = CONFIG_FILE + f".old.{date.today().isoformat()}"
    assert os.path.exists(backup_config)
    with open(backup_config) as f:
        assert f.read() == duplicate_config
Exemplo n.º 2
0
def ensure_config_yaml(plugin_manager):
    """
    Ensure we have a config.yaml present
    """
    # ensure config dir exists and is private
    for path in [CONFIG_DIR, os.path.join(CONFIG_DIR, 'jupyterhub_config.d')]:
        os.makedirs(path, mode=0o700, exist_ok=True)

    migrator.migrate_config_files()

    if os.path.exists(CONFIG_FILE):
        with open(CONFIG_FILE, 'r') as f:
            config = yaml.load(f)
    else:
        config = {}

    hook = plugin_manager.hook
    hook.tljh_config_post_install(config=config)

    with open(CONFIG_FILE, 'w+') as f:
        yaml.dump(config, f)