def ensure_template(filename, destination, context=None, template_dir=None, use_sudo=False, backup=True, mirror_local_mode=False, mode=None, fabric_managed_format=FABRIC_MANAGED_DEFAULT_FORMAT): run_fn = run_or_sudo(use_sudo) # Normalize destination to be an actual filename, due to using StringIO with settings(hide('everything'), warn_only=True): if run_fn('test -d %s' % _expand_path(destination)).succeeded: sep = "" if destination.endswith('/') else "/" destination += sep + os.path.basename(filename) context = modify_context(filename, destination, context=context, fabric_managed_format=fabric_managed_format) text = render_as_text(filename, context=context, template_dir=template_dir) local_sum = hashlib.sha256(text).hexdigest() remote_sum = file.calc_sha256sum(destination, use_sudo=use_sudo) if remote_sum == local_sum: return False # Use mode kwarg to implement mirror_local_mode, again due to using # StringIO if mirror_local_mode and mode is None: mode = os.stat(filename).st_mode # To prevent put() from trying to do this # logic itself mirror_local_mode = False # Back up original file if backup and exists(destination): run_fn("cp %s{,.bak}" % _expand_path(destination)) # Upload the file. put(local_path=StringIO(text), remote_path=destination, use_sudo=use_sudo, mirror_local_mode=mirror_local_mode, mode=mode) return True
def ensure_template(filename, destination, context=None, template_dir=None, use_sudo=False, backup=True, mirror_local_mode=False, mode=None, fabric_managed_format=FABRIC_MANAGED_DEFAULT_FORMAT): run_fn = run_or_sudo(use_sudo) # Normalize destination to be an actual filename, due to using StringIO with settings(hide('everything'), warn_only=True): if run_fn('test -d %s' % _expand_path(destination)).succeeded: sep = "" if destination.endswith('/') else "/" destination += sep + os.path.basename(filename) context = modify_context(filename, destination, context=context, fabric_managed_format=fabric_managed_format) text = render_as_text(filename, context=context, template_dir=template_dir) local_sum = hashlib.sha256(text).hexdigest() remote_sum = file.calc_sha256sum(destination, use_sudo=use_sudo) if remote_sum == local_sum: return False # Use mode kwarg to implement mirror_local_mode, again due to using # StringIO if mirror_local_mode and mode is None: mode = os.stat(filename).st_mode # To prevent put() from trying to do this # logic itself mirror_local_mode = False # Back up original file if backup and exists(destination): run_fn("cp %s{,.bak}" % _expand_path(destination)) # Upload the file. put( local_path=StringIO(text), remote_path=destination, use_sudo=use_sudo, mirror_local_mode=mirror_local_mode, mode=mode ) return True
def install(name_or_path, use_sudo=False): run_fn = run_or_sudo(use_sudo) run_fn('yum install -y "%s"' % name_or_path)
def download(url, dest, use_sudo=False): run_fn = run_or_sudo(use_sudo) run_fn('curl -kL -o %s %s' % (dest, url))
def calc_sha256sum(path, use_sudo=False): if not files.exists(path, use_sudo): return None run_fn = run_or_sudo(use_sudo) output = run_fn("/usr/bin/sha256sum %s" % path) return output.stdout.split(' ', 1)[0]