def test_rsync_file(): # setup d = 'test_rsync' remote_f = 'remote\ file.txt' local_f = 'local_file.txt' directory(d) with cd(d): rm(remote_f) append(remote_f, 'Hello world') # download file src = '%s/%s' % (d, remote_f) rsync( remote_dir=src, local_dir=local_f, upload=False ) assert contains(local_f, 'Hello world', runner=local) is True # append to and upload file append(local_f, 'Hello from local', runner=local) rsync( remote_dir=src, local_dir=local_f, ) # check uploaded file with cd(d): assert contains(remote_f, 'Hello from local') is True # cleanup rm(local_f, runner=local) rmdir(d)
def all_args_in_play(self, cxn): directory( cxn, "/some/dir", user="******", group="admins", mode="0700" ) assert cxn.run.call_args_list == [ call("mkdir -p /some/dir"), call("chown user:admins /some/dir"), call("chmod 0700 /some/dir"), ]
def _setup_repo(conn: Connection, repo_dir: str, repo_name: str): directory(conn, repo_dir, group=WEBADMIN_GROUP, sudo=True) if not exists(conn, '{0}/.git'.format(repo_dir)): if not verify_access_token(): raise Exception( "Unable to access GitHub account. Run 'auth' to fix this") create_key(conn, repo_name, WEBADMIN_GROUP) add_repo_key(conn, repo_name) clone(conn, repo_name, repo_dir, skip_strict_key_checking=True)
def all_args_in_play(self, cxn): directory(cxn, "/some/dir", user="******", group="admins", mode="0700") assert cxn.run.call_args_list == [ call("mkdir -p /some/dir"), call("chown user:admins /some/dir"), call("chmod 0700 /some/dir"), ]
def new_release(conn, deploy_root, branch, in_repo_path, release_path=None): if release_path is None: release_path = join(deploy_root, DEPLOY_RELEASES_DIR, int(time.time() * 1000.0)) files.directory(conn, release_path) with conn.cd(join(deploy_root, DEPLOY_REPO_DIR)): conn.run(f"""git archive {branch} {in_repo_path} | \\ tar -x --strip-components 1 -f - -C {release_path}""") conn.run( f"git rev-list --max-count=1 {branch} > {join(release_path, 'REVISION')}" ) return release_path
def do_add_ssh_user(c, username, sshkeyfile, password=None): c.run(f'adduser {username} --disabled-password --gecos ""') do_add_user_to_group(c, username, 'sudo') if (password is not None): do_set_password(c, username, password) with open(sshkeyfile) as f: sshkey = f.read() homedir = f'/home/{username}' files.directory(c, f'hotexamples_com/.ssh', username, username, '700') authorized_key_file = f'hotexamples_com/.ssh/authorized_keys' if (not files.exists(c, authorized_key_file)): set_file(c, authorized_key_file, username, username, 600) files.append(c, authorized_key_file, sshkey) print(f'Successfully created {username} user on {c.host}')
def setup(c): """ Prepare the server for deployments """ files.directory(conn, utils.join(SALT_DEPLOY_PATH, utils.DEPLOY_REPO_DIR)) files.directory(conn, utils.join(SALT_DEPLOY_PATH, utils.DEPLOY_RELEASES_DIR)) with conn.cd(utils.join(SALT_DEPLOY_PATH, utils.DEPLOY_REPO_DIR)): if not files.exists(conn, "HEAD"): conn.run( f"git clone --mirror --depth 1 --no-single-branch {SALT_REPO} ." ) conn.run(f"git remote set-url origin {SALT_REPO}") conn.run(f"git fetch --depth 1 origin {SALT_BRANCH}")
def check_credentials(local_dir, sshpath, password, subdir=None): """ Checks whether one can download to (or upload from) the directory ``local_dir`` on the Plex_ server to the remote SSH server. :param str local_dir: the local directory, on the Plex_ server, into which to download files from the remote SSH server. :param str sshpath: the full path with username and host name for the SSH server. Format is ``'username@hostname'``. :param str password: the password to connect as the SSH server. :param str subdir: if not ``None``, the subdirectory on the remote SSH server from which to download files. :returns: if everything works, return ``'SUCCESS'``. If fails, return specific illuminating error messages. :rtype: str .. seealso:: * :py:meth:`push_credentials <howdy.core.core_rsync.push_credentials>`. * :py:meth:`get_credentials <howdy.core.core_rsync.get_credentials>`. """ try: # ## first, does local directory exist? if not os.path.isdir(os.path.abspath(local_dir)): raise ValueError("Error, %s is not a directory." % os.path.abspath(local_dir)) # ## second, can we login with username and password? uname = sshpath.split('@')[0] hostname = sshpath.split('@')[1] print(uname, hostname) # raises a ValueError if cannot do so # needs to pass in look_for_keys = False so not use id_* keys with Connection(hostname, user=uname, connect_kwargs={ 'password': password, 'look_for_keys': False }) as conn: conn.run('ls', hide=True) # errors out if not a valid connection # ## third, if subdir is None does it exist? if subdir is not None: if not exists(conn, subdir): raise ValueError("Error, %s does not exist." % subdir) # will raise an error if this is a file directory(conn, subdir) return 'SUCCESS' except Exception as e: return str(e)
def ensure_dir_exists(path, user=None, group=None, mode=None) -> None: # https://fabric-patchwork.readthedocs.io/en/latest/api/files.html#patchwork.files.directory return files.directory(global_context.conn, runner=global_context.conn.run, path=path, user=user, group=group, mode=mode)
def ensure_dir_exists(path: str, user: Optional[str] = None, group: Optional[str] = None, mode: Optional[str] = None) -> None: """ Проверить что директория существует и параметры соответствуют заданным <https://fabric-patchwork.readthedocs.io/en/latest/api/files.html#patchwork.files.directory> :param path: путь до директории :param user: владелец :param group: группа :param mode: права """ assert global_context.conn is not None, "No connection" files.directory(global_context.conn, runner=global_context.conn.run, path=path, user=user, group=group, mode=mode)
def setup_server(conn, setup_wins=False): conn.sudo('add-apt-repository universe') conn.sudo('apt-get update') _setup_node(conn) base_packages = [ 'git', 'python3-venv', 'postgresql', 'python3-psycopg2', 'nginx', 'uwsgi', 'uwsgi-plugin-python3', ] plush.fabric_commands.install_packages(conn, base_packages) if setup_wins: _setup_wins(conn) conn.sudo('mkdir -p /etc/nginx/ssl') directory(conn, '/var/www', group=WEBADMIN_GROUP, sudo=True) directory(conn, '/var/www/python', group=WEBADMIN_GROUP, sudo=True) matching_user_count = conn.run( "psql postgres -tAc \"SELECT 1 FROM pg_roles WHERE rolname='root'\"" ).stdout if '1' not in matching_user_count: conn.run('createuser -s root') directory(conn, '/var/uwsgi', user='******', group='root', mode='777', sudo=True) default_site = '/etc/nginx/sites-enabled/default' if exists(conn, default_site): conn.sudo('rm {0}'.format(default_site)) conn.sudo('/etc/init.d/nginx start')
def _update_source(conn: Connection, repo_dir: str, branch: str): directory(conn, repo_dir, group=WEBADMIN_GROUP, mode='ug+w', sudo=True) with conn.cd(repo_dir): conn.run('sudo git fetch origin') conn.run('sudo git reset --hard origin/{0}'.format(branch))
def group_may_be_given_to_change_group(self, cxn): directory(cxn, "/some/dir", user="******", group="admins") cxn.run.assert_any_call("chown user:admins /some/dir")
def mode_adds_a_chmod(self, cxn): directory(cxn, "/some/dir", mode="0700") cxn.run.assert_any_call("chmod 0700 /some/dir")
def base_case_creates_dir_with_dash_p(self, cxn): directory(cxn, "/some/dir") cxn.run.assert_called_once_with("mkdir -p /some/dir")
def base_case_creates_dir_with_dash_p(self, cxn): directory(cxn, "/some/dir") cxn.run.assert_called_once_with("mkdir -p /some/dir")
def mode_adds_a_chmod(self, cxn): directory(cxn, "/some/dir", mode="0700") cxn.run.assert_any_call("chmod 0700 /some/dir")
def group_may_be_given_to_change_group(self, cxn): directory(cxn, "/some/dir", user="******", group="admins") cxn.run.assert_any_call("chown user:admins /some/dir")
def user_sets_owner_and_group(self, cxn): directory(cxn, "/some/dir", user="******") cxn.run.assert_any_call("chown user:user /some/dir")
def user_sets_owner_and_group(self, cxn): directory(cxn, "/some/dir", user="******") cxn.run.assert_any_call("chown user:user /some/dir")
def create_directory(): """"Demo: Create a directory """ files.directory("/tmp/some_directory", user="******", group="some_group", mode="g+x")