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!"
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()
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')
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With