Check if there is a way to use fabric

I am running this code to check if this directory exists on the remote machine or not, but this code checks the directory on the local machine. How do I check a directory on a remote computer?

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!"

      

+4


source to share


3 answers


You can use the function files.exists

.

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

      



And name it execute

.

def main():
    execute(check_exists, '/path/to/file/on/remote')

      

+12


source


why not just keep it simply stupid

like:



from fabric.contrib.files import exists

def foo():
    if exists('/path/to/remote/file', use_sudo=True):
      # do something...

      

+5


source


While the accepted answer is valid for factory ver 1, for those that fall into this thread looking for the same thing but for factory 2:

exists

the method from has fabric.contrib.files

been moved to patchwork.files

with a slight change in signature, 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()

      

0


source







All Articles