Exemplo n.º 1
0
def get_ssh_keys():
    keys = re.split(r'\s*,\s*', config.get_value(KEY_SSH_KEYS))
    for i in range(0, len(keys)):
        key = keys[i]
        if key[0] == '$':
            var_name = key[1:]
            key_file = KEY_FILE_NAME_PATTERN % var_name
            if not os.path.isfile(key_file):
                key_value = os.environ.get(var_name)
                if key_value:
                    marker_begin = '-----BEGIN RSA PRIVATE KEY-----'
                    marker_end = '-----END RSA PRIVATE KEY-----'
                    key_value = key_value.replace(marker_begin, '')
                    key_value = key_value.replace(marker_end, '')
                    key_value = key_value.replace(' ', '\n')
                    if marker_begin not in key_value:
                        key_value = ('%s\n' % marker_begin) + key_value
                    if marker_end not in key_value:
                        key_value += ('\n%s' % marker_end)
                    key_value = key_value.replace('\n\n', '\n')
                    save_file(key_file, key_value)
                    run('chmod 600 %s' % key_file)
                else:
                    LOG.warning(
                        'Unable to read SSH key from environment variable: %s'
                        % var_name)
            keys[i] = key_file
    return keys
Exemplo n.º 2
0
def get_config(force_load=False, config_file_only=False):
    global last_config_load_time, CACHED_CONFIG
    if TEST_CONFIG:
        return TEST_CONFIG

    CONFIG_LOCK.acquire()
    try:
        time_now = now()
        if (time_now - last_config_load_time) > CONFIG_CACHE_DURATION:
            force_load = True
        if CACHED_CONFIG and not force_load:
            return CACHED_CONFIG

        app_config = common.load_json_file(CONFIG_FILE_LOCATION)
        if not app_config:
            app_config = SystemConfiguration()
            common.save_file(CONFIG_FILE_LOCATION, app_config.to_json())
        else:
            app_config = SystemConfiguration.from_json(app_config)

        # load additional configs from DB
        if not config_file_only:
            configs = database.configs_fetch_all()
            for config in configs:
                if config['resource']:
                    app_config.get(config['section']).set(config['resource'], config['config'])
                elif config['section'] == SECTION_GLOBAL:
                    app_config.set(config['section'], config['config'])
            CACHED_CONFIG = app_config
            last_config_load_time = now()
    finally:
        CONFIG_LOCK.release()

    return app_config
Exemplo n.º 3
0
def get_config(force_load=False, config_file_only=False):
    global last_config_load_time, CACHED_CONFIG
    if TEST_CONFIG:
        return TEST_CONFIG

    CONFIG_LOCK.acquire()
    try:
        time_now = now()
        if (time_now - last_config_load_time) > CONFIG_CACHE_DURATION:
            force_load = True
        if CACHED_CONFIG and not force_load:
            return CACHED_CONFIG

        app_config = common.load_json_file(CONFIG_FILE_LOCATION)
        if not app_config:
            app_config = SystemConfiguration()
            common.save_file(CONFIG_FILE_LOCATION, app_config.to_json())
        else:
            app_config = SystemConfiguration.from_json(app_config)

        # load additional configs from DB
        if not config_file_only:
            configs = database.configs_fetch_all()
            for config in configs:
                if config['resource']:
                    app_config.get(config['section']).set(config['resource'], config['config'])
                elif config['section'] == SECTION_GLOBAL:
                    app_config.set(config['section'], config['config'])
            CACHED_CONFIG = app_config
            last_config_load_time = now()
    finally:
        CONFIG_LOCK.release()

    return app_config
Exemplo n.º 4
0
def init_aws_cli():
    home = os.path.expanduser("~")
    folder = '%s/.aws' % home
    if not os.path.exists(folder):
        os.makedirs(folder)
    file_config = '%s/.aws/config' % home
    if not os.path.isfile(file_config):
        common.save_file(file_config, "[default]\nregion = us-east-1")
    file_creds = '%s/.aws/credentials' % home
    if not os.path.isfile(file_creds):
        common.save_file(file_creds, "[default]\naws_access_key_id = testAccessKeyId\n" +
            "aws_secret_access_key = testSecretKey")
Exemplo n.º 5
0
def write(config, section=SECTION_GLOBAL, resource=None):
    app_config = get_config(force_load=True)
    if resource:
        target_config = app_config.get(section)
        old_config = target_config.get(resource)
        config = target_config.set(resource, config)
    else:
        old_config = app_config.get(section)
        config = app_config.set(section, config)
    # first save new config, then notify listeners
    common.save_file(CONFIG_FILE_LOCATION, app_config.to_json())
    notify_listeners(old_config, config, section=section, resource=resource)
    return config
Exemplo n.º 6
0
def init_aws_cli():
    home = os.path.expanduser("~")
    folder = '%s/.aws' % home
    if not os.path.exists(folder):
        os.makedirs(folder)
    file_config = '%s/.aws/config' % home
    if not os.path.isfile(file_config):
        common.save_file(file_config, "[default]\nregion = us-east-1")
    file_creds = '%s/.aws/credentials' % home
    if not os.path.isfile(file_creds):
        common.save_file(
            file_creds, "[default]\naws_access_key_id = testAccessKeyId\n" +
            "aws_secret_access_key = testSecretKey")
Exemplo n.º 7
0
def get_config(force_load=False):
    global TEST_CONFIG, last_config_load_time
    if TEST_CONFIG:
        return TEST_CONFIG
    time_now = now()
    if (time_now - last_config_load_time) > CONFIG_CACHE_DURATION:
        force_load = True
    app_config = common.load_json_file(CONFIG_FILE_LOCATION)
    last_config_load_time = time_now
    if not app_config:
        app_config = SystemConfiguration()
        common.save_file(CONFIG_FILE_LOCATION, app_config.to_json())
    else:
        app_config = SystemConfiguration.from_json(app_config)
    return app_config
Exemplo n.º 8
0
def write(config, section=SECTION_GLOBAL, resource=None):
    app_config = get_config(force_load=True)
    if section == SECTION_GLOBAL:
        new_app_config = SystemConfiguration()
        config = new_app_config.set(section, config)
        # save global config as file
        common.save_file(CONFIG_FILE_LOCATION, new_app_config.to_json())

    if resource:
        target_config = app_config.get(section)
        old_config = target_config.get(resource)
        config = target_config.set(resource, config)
    else:
        old_config = app_config.get(section)
        config = app_config.set(section, config)

    # save config to database
    config_json = config.to_json()
    database.config_save(section=section, resource=resource, config=config_json)
    # notify listeners
    notify_listeners(old_config, config, section=section, resource=resource)
    return config
Exemplo n.º 9
0
def write(config, section=SECTION_GLOBAL, resource=None):
    app_config = get_config(force_load=True)
    if section == SECTION_GLOBAL:
        new_app_config = SystemConfiguration()
        config = new_app_config.set(section, config)
        # save global config as file
        common.save_file(CONFIG_FILE_LOCATION, new_app_config.to_json())

    if resource:
        target_config = app_config.get(section)
        old_config = target_config.get(resource)
        config = target_config.set(resource, config)
    else:
        old_config = app_config.get(section)
        config = app_config.set(section, config)

    # save config to database
    config_json = config.to_json()
    database.config_save(section=section, resource=resource, config=config_json)
    # notify listeners
    notify_listeners(old_config, config, section=section, resource=resource)
    return config