def test_read_local_file(tmpdir, monkeypatch): fabfile = tmpdir.join("fabfile.py") monkeypatch.setitem(env, "real_fabfile", str(fabfile)) temp_file = tmpdir.join("file.txt") temp_file.write("text") assert read_local_file(str(temp_file)) == "text" non_existent_file = tmpdir.join("non_existent_file") with abort(r'\[Errno 2\] No such file or directory:.*'): read_local_file(str(non_existent_file)) assert read_local_file(str(non_existent_file), False) is None
def add_user_ssh_authorized_keys(username, ssh_authorized_keys_filename): """add records to user ~/.ssh/authorized_keys file Args: username: user name ssh_authorized_keys_filename: name of local file with ssh authorized keys to add """ home_directory = get_user_home_directory(username) ssh_directory = os.path.join(home_directory, ".ssh") create_directory(ssh_directory) chown(ssh_directory, username, username) chmod(ssh_directory, 0700) authorized_keys = os.path.join(ssh_directory, "authorized_keys") create_file(authorized_keys) chown(authorized_keys, username, username) chmod(authorized_keys, 0600) keys = read_local_file(ssh_authorized_keys_filename) for key in keys.split("\n"): key = key.strip() if not key: continue edit_file(authorized_keys, append_line(key, insert_empty_line_before=True))
def edit_local_file(local_filename, *editors): """Edit local file text editor. Apply all editors from list ``editors`` to text of **local** file ``local_filename``. ``editors`` is any combination of editors: :func:`~insert_line`, :func:`~delete_line`, :func:`~replace_line` and so on. .. seealso:: :func:`~edit_file` Args: local_filename: Name of **local** file to edit, if relative it will be retrieved from directory ``files`` alongside with ``env.real_fabfile``. editors: List of editors to apply for text of file ``local_filename``. Returns: True if file is changed, else False. Raises: :class:`~exceptions.SystemExit`: When error occurred. """ old_text = read_local_file(local_filename) changed, new_text = _apply_editors(old_text, *editors) if changed: _atomic_write_local_file(local_filename, new_text) return changed
config_filename = os.path.join(dirname, argument_config_filename) else: config_filename = argument_config_filename if not os.path.isfile(config_filename): if argument_config_filename is None: return else: abort('read_config: config \'%s\' not exists' % config_filename) else: debug_print('fabrix: using config \'%s\'' % config_filename) try: with open(config_filename) as config_file: config = yaml.load(config_file) except yaml.parser.ParserError, ex: abort('read_config: error parsing config \'%s\':\n\n%s' % (config_filename, ex)) config_text = read_local_file(config_filename) config_yaml = yaml.dump(config) debug_print('config_text:', config_text, 'config_yaml:', config_yaml, 'config:', config) if 'hosts' in config and 'roles' in config: abort('read_config: hosts and roles can\'t be simultaneously defined in config') if 'hosts' not in config and 'roles' not in config: abort('read_config: hosts or roles must be defined in config') hosts = list() if 'hosts' in config: if not isinstance(config['hosts'], list): abort('read_config: hosts must be list type') if not config['hosts']: abort('read_config: hosts must not be empty') hosts_set = set() for host in config['hosts']: if host is None: