1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge branch 'change_to_using_hashlib_or_md5' of https://github.com/davehatton/ansible into devel

Conflicts:
	lib/ansible/utils.py
This commit is contained in:
Michael DeHaan 2012-07-07 11:51:30 -04:00
commit 211f1e69c3
5 changed files with 66 additions and 34 deletions

View file

@ -438,7 +438,7 @@ class Runner(object):
source = utils.template(source, inject, self.setup_cache) source = utils.template(source, inject, self.setup_cache)
source = utils.path_dwim(self.basedir, source) source = utils.path_dwim(self.basedir, source)
local_md5 = utils.local_md5(source) local_md5 = utils.md5(source)
if local_md5 is None: if local_md5 is None:
result=dict(failed=True, msg="could not find src=%s" % source) result=dict(failed=True, msg="could not find src=%s" % source)
return ReturnData(host=conn.host, result=result) return ReturnData(host=conn.host, result=result)
@ -495,7 +495,7 @@ class Runner(object):
dest = dest.replace("//","/") dest = dest.replace("//","/")
# compare old and new md5 for support of change hooks # compare old and new md5 for support of change hooks
local_md5 = utils.local_md5(dest) local_md5 = utils.md5(dest)
remote_md5 = self._remote_md5(conn, tmp, source) remote_md5 = self._remote_md5(conn, tmp, source)
if remote_md5 == '0': if remote_md5 == '0':
@ -508,7 +508,7 @@ class Runner(object):
# fetch the file and check for changes # fetch the file and check for changes
conn.fetch_file(source, dest) conn.fetch_file(source, dest)
new_md5 = utils.local_md5(dest) new_md5 = utils.md5(dest)
if new_md5 != remote_md5: if new_md5 != remote_md5:
result = dict(failed=True, msg="md5 mismatch", md5sum=new_md5) result = dict(failed=True, msg="md5 mismatch", md5sum=new_md5)
return ReturnData(host=conn.host, result=result) return ReturnData(host=conn.host, result=result)

View file

@ -26,11 +26,19 @@ import jinja2
import yaml import yaml
import optparse import optparse
from operator import methodcaller from operator import methodcaller
try: try:
import json import json
except ImportError: except ImportError:
import simplejson as json import simplejson as json
try:
import hashlib
HAVE_HASHLIB=True
except ImportError:
import md5
HAVE_HASHLIB=False
from ansible import errors from ansible import errors
import ansible.constants as C import ansible.constants as C
@ -312,21 +320,14 @@ def parse_kv(args):
options[k]=v options[k]=v
return options return options
def local_md5(file): def md5(filename):
''' compute local md5sum, return None if file is not present ''' ''' compute md5sum, return None if file is not present '''
#was >>> cmd = "/usr/bin/md5sum %s 2> /dev/null || /sbin/md5 -q %s" % (file,file) if not os.path.exists(filename):
md5s = [
"(/usr/bin/md5sum %s 2>/dev/null)" % (file),
"(/sbin/md5sum -q %s 2>/dev/null)" % (file),
"(/usr/bin/digest -a md5 -v %s 2>/dev/null)" % (file)
]
cmd = " || ".join(md5s)
if not os.path.exists(file):
return None return None
if HAVE_HASHLIB:
return hashlib.md5(file(filename).read()).hexdigest()
else: else:
c = os.popen(cmd) return md5.new(file(filename).read()).hexdigest()
return c.read().split()[0]
#################################################################### ####################################################################

View file

@ -28,6 +28,7 @@ import shlex
import shutil import shutil
import syslog import syslog
import tempfile import tempfile
try: try:
import hashlib import hashlib
HAVE_HASHLIB=True HAVE_HASHLIB=True
@ -64,12 +65,14 @@ def write_temp_file(data):
os.close(fd) os.close(fd)
return path return path
def file_digest(path): def md5(filename):
if HAVE_HASHLIB: ''' compute md5sum, return None if file is not present '''
digest = hashlib.md5(file(path).read()).hexdigest() if not os.path.exists(filename):
else: return None
digest = md5.new(file(path).read()).hexdigest() if HAVE_HASHLIB:
return digest return hashlib.md5(file(filename).read()).hexdigest()
else:
return md5.new(file(filename).read()).hexdigest()
# =========================================== # ===========================================
@ -111,10 +114,10 @@ if not os.path.isdir(src):
fail_json(msg="Source (%s) is not a directory" % src) fail_json(msg="Source (%s) is not a directory" % src)
path = write_temp_file(assemble_from_fragments(src)) path = write_temp_file(assemble_from_fragments(src))
pathmd5 = file_digest(path) pathmd5 = md5(path)
if os.path.exists(dest): if os.path.exists(dest):
destmd5 = file_digest(dest) destmd5 = md5(dest)
if pathmd5 != destmd5: if pathmd5 != destmd5:
shutil.copy(path, dest) shutil.copy(path, dest)

View file

@ -24,6 +24,13 @@ import shlex
import shutil import shutil
import syslog import syslog
try:
import hashlib
HAVE_HASHLIB=True
except ImportError:
import md5
HAVE_HASHLIB=False
# =========================================== # ===========================================
# convert arguments of form a=b c=d # convert arguments of form a=b c=d
# to a dictionary # to a dictionary
@ -38,9 +45,16 @@ def exit_kv(rc=0, **kwargs):
print dump_kv(kwargs) print dump_kv(kwargs)
sys.exit(rc) sys.exit(rc)
def md5_sum(f): def md5(filename):
md5sum = os.popen("/usr/bin/md5sum %(file)s 2>/dev/null || /sbin/md5 -q %(file)s 2>/dev/null || /usr/bin/digest -a md5 -v %(file)s 2>/dev/null" % {"file": f}).read().split()[0] ''' compute md5sum, return None if file is not present '''
return md5sum if not os.path.exists(filename):
return None
if HAVE_HASHLIB:
return hashlib.md5(file(filename).read()).hexdigest()
else:
return md5.new(file(filename).read()).hexdigest()
# ===========================================
if len(sys.argv) == 1: if len(sys.argv) == 1:
exit_kv(rc=1, failed=1, msg="incorrect number of arguments given") exit_kv(rc=1, failed=1, msg="incorrect number of arguments given")
@ -73,7 +87,7 @@ if not os.path.exists(src):
exit_kv(rc=1, failed=1, msg="Source %s failed to transfer" % (src)) exit_kv(rc=1, failed=1, msg="Source %s failed to transfer" % (src))
if not os.access(src, os.R_OK): if not os.access(src, os.R_OK):
exit_kv(rc=1, failed=1, msg="Source %s not readable" % (src)) exit_kv(rc=1, failed=1, msg="Source %s not readable" % (src))
md5sum_src = md5_sum(src) md5sum_src = md5(src)
md5sum_dest = None md5sum_dest = None
# check if there is no dest file # check if there is no dest file
@ -83,7 +97,7 @@ if os.path.exists(dest):
exit_kv(rc=1, failed=1, msg="Destination %s not writable" % (dest)) exit_kv(rc=1, failed=1, msg="Destination %s not writable" % (dest))
if not os.access(dest, os.R_OK): if not os.access(dest, os.R_OK):
exit_kv(rc=1, failed=1, msg="Destination %s not readable" % (dest)) exit_kv(rc=1, failed=1, msg="Destination %s not readable" % (dest))
md5sum_dest = md5_sum(dest) md5sum_dest = md5(dest)
else: else:
if not os.access(os.path.dirname(dest), os.W_OK): if not os.access(os.path.dirname(dest), os.W_OK):
exit_kv(rc=1, failed=1, msg="Destination %s not writable" % (os.path.dirname(dest))) exit_kv(rc=1, failed=1, msg="Destination %s not writable" % (os.path.dirname(dest)))

View file

@ -33,6 +33,13 @@ import subprocess
import traceback import traceback
import syslog import syslog
try:
import hashlib
HAVE_HASHLIB=True
except ImportError:
import md5
HAVE_HASHLIB=False
try: try:
import selinux import selinux
HAVE_SELINUX=True HAVE_SELINUX=True
@ -311,9 +318,16 @@ def ansible_facts():
get_service_facts(facts) get_service_facts(facts)
return facts return facts
def md5_sum(f): def md5(filename):
md5sum = os.popen("/usr/bin/md5sum %(file)s 2>/dev/null || /sbin/md5 -q %(file)s 2>/dev/null || /usr/bin/digest -a md5 -v %(file)s 2>/dev/null" % {"file": f}).read().split()[0] ''' compute md5sum, return None if file is not present '''
return md5sum if not os.path.exists(filename):
return None
if HAVE_HASHLIB:
return hashlib.md5(file(filename).read()).hexdigest()
else:
return md5.new(file(filename).read()).hexdigest()
# ===========================================
# load config & template variables # load config & template variables
@ -351,7 +365,7 @@ md5sum = None
if not os.path.exists(ansible_file): if not os.path.exists(ansible_file):
changed = True changed = True
else: else:
md5sum = md5_sum(ansible_file) md5sum = md5(ansible_file)
# Get some basic facts in case facter or ohai are not installed # Get some basic facts in case facter or ohai are not installed
for (k, v) in ansible_facts().items(): for (k, v) in ansible_facts().items():
@ -400,7 +414,7 @@ reformat = json.dumps(setup_options, sort_keys=True, indent=4)
f.write(reformat) f.write(reformat)
f.close() f.close()
md5sum2 = md5_sum(ansible_file) md5sum2 = md5(ansible_file)
if md5sum != md5sum2: if md5sum != md5sum2:
changed = True changed = True