How to check If Path Exists Using Fabric2.x
Asked Answered
N

4

8

I am using Fabric2 version and I don't see It has exist method in it to check if folder path has existed in the remote server. Please let me know how can I achieve this in Fabric 2 http://docs.fabfile.org/en/stable/.

I have seen a similar question Check If Path Exists Using Fabric, But this is for fabric 1.x version

Nuts answered 23/11, 2018 at 8:50 Comment(0)
H
7

You can execute the test command remotely with the -d option to test if the file exist and is a directory while passing the warn parameter to the run method so the execution doesn't stop in case of a non-zero exit status code. Then the value failed on the result will be True in case that the folder doesn't exist and False otherwise.

folder = '/path/to/folder'
if c.run('test -d {}'.format(folder), warn=True).failed:
    # Folder doesn't exist
    c.run('mkdir {}'.format(folder))
Hoffert answered 5/12, 2018 at 19:57 Comment(2)
please elaborate your solution at least in a brief way!Thapsus
Please add some comments about if partNuts
N
1

exists method from fabric.contrib.files was moved to patchwork.files with a small signature change, so you can use it like this:

from fabric2 import Connection
from patchwork.files import exists

conn = Connection('host')
if exists(conn, SOME_REMOTE_DIR):
   do_something()
Northamptonshire answered 30/1, 2019 at 12:3 Comment(2)
The problem with this solution is that currently only works with connections, but not with Serial neither Threading groups: File "/Users/alejandro.lorente/.python_venvs/test/lib/python3.10/site-packages/patchwork/util.py", line 114, in inner return f(*args, **kwargs) File "/Users/alejandro.lorente/.python_venvs/test/lib/python3.10/site-packages/patchwork/files.py", line 31, in exists return runner(cmd, hide=True, warn=True).ok AttributeError: 'GroupResult' object has no attribute 'ok'Tanjatanjore
patchwork cannot be used with a modern `fabric2´ 3.x version sadly. github.com/fabric/patchwork/pull/46Limitative
A
1

The below code is to check the existence of the file (-f), just change to '-d' to check the existence of a directory.

from fabric import Connection
c = Connection(host="host")
if c.run('test -f /opt/mydata/myfile', warn=True).failed:
   do.thing()

You can find it in the Fabric 2 documentation below:
https://docs.fabfile.org/en/2.5/getting-started.html?highlight=failed#bringing-it-all-together

Aharon answered 17/6, 2020 at 3:10 Comment(0)
S
-1

Hi That's not so difficult, you have to use traditional python code to check if a path already exists.

from pathlib import Path
from fabric import Connection as connection, task
import os


@task
def deploy(ctx):
    parent_deploy_dir = '/var/www'
    deploy_dir ='/var/www/my_folder'
    host = 'REMOTE_HOST'
    user = 'USER'
    with connection(host=host, user=user) as c:
                with c.cd(parent_deploy_dir):
                if not os.path.isdir(Path(deploy_dir)):
                    c.run('mkdir -p ' + deploy_dir)
Sepulchral answered 23/11, 2018 at 13:36 Comment(1)
No, this checks the path on the host, not on the remote server.Limitative

© 2022 - 2024 — McMap. All rights reserved.