Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check If Path Exists Using Fabric

I am running this code to check whether this directory exists on remote machine or not but this code is checking for the directory on local machine. How can I verify directory on remote machine?

rom fabric.api import run, sudo, env
import os

env.hosts = ['remote_server']
env.user = 'ubuntu'
env.key_filename = '/home/ubuntu/ubuntu16-multi.pem'


def Directory_Check():
  DIR_1="/home/ubuntu/test-dir"
  if os.path.exists(DIR_1):
    print "Directory Exist!"
  else:
    print "Directory Does Not Exist!"
like image 412
Saqib Iqbal Avatar asked Jun 24 '17 12:06

Saqib Iqbal


3 Answers

Although the accepted answer is valid for fabric ver 1, for whoever hits this thread while looking for the same thing but for fabric2:

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()
like image 71
MikeL Avatar answered Oct 27 '22 13:10

MikeL


You can use the files.exists function.

def check_exists(filename):
    from fabric.contrib import files
    if files.exists(filename):
        print('%s exists!' % filename)

And call it with execute.

def main():
    execute(check_exists, '/path/to/file/on/remote')
like image 41
2ps Avatar answered Oct 27 '22 13:10

2ps


why not just keep it simply stupid as:

from fabric.contrib.files import exists

def foo():
    if exists('/path/to/remote/file', use_sudo=True):
      # do something...
like image 27
andilabs Avatar answered Oct 27 '22 14:10

andilabs