Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
55694db7
Commit
55694db7
authored
Jul 09, 2012
by
Dave Hatton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
switch to hashlib.md5 or md5 instead of OS md5 commands
parent
921bf3da
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
68 additions
and
28 deletions
+68
-28
lib/ansible/runner/__init__.py
+5
-4
lib/ansible/utils.py
+14
-6
library/assemble
+11
-8
library/copy
+19
-5
library/setup
+19
-5
No files found.
lib/ansible/runner/__init__.py
View file @
55694db7
...
...
@@ -438,7 +438,7 @@ class Runner(object):
source
=
utils
.
template
(
source
,
inject
,
self
.
setup_cache
)
source
=
utils
.
path_dwim
(
self
.
basedir
,
source
)
local_md5
=
utils
.
local_
md5
(
source
)
local_md5
=
utils
.
md5
(
source
)
if
local_md5
is
None
:
result
=
dict
(
failed
=
True
,
msg
=
"could not find src=
%
s"
%
source
)
return
ReturnData
(
host
=
conn
.
host
,
result
=
result
)
...
...
@@ -495,7 +495,7 @@ class Runner(object):
dest
=
dest
.
replace
(
"//"
,
"/"
)
# 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
)
if
remote_md5
==
'0'
:
...
...
@@ -508,7 +508,7 @@ class Runner(object):
# fetch the file and check for changes
conn
.
fetch_file
(
source
,
dest
)
new_md5
=
utils
.
local_
md5
(
dest
)
new_md5
=
utils
.
md5
(
dest
)
if
new_md5
!=
remote_md5
:
result
=
dict
(
failed
=
True
,
msg
=
"md5 mismatch"
,
md5sum
=
new_md5
)
return
ReturnData
(
host
=
conn
.
host
,
result
=
result
)
...
...
@@ -746,7 +746,8 @@ class Runner(object):
test
=
"[[ -r
%
s ]]"
%
path
md5s
=
[
"(
%
s && /usr/bin/md5sum
%
s 2>/dev/null)"
%
(
test
,
path
),
"(
%
s && /sbin/md5sum -q
%
s 2>/dev/null)"
%
(
test
,
path
)
"(
%
s && /sbin/md5sum -q
%
s 2>/dev/null)"
%
(
test
,
path
),
"(
%
s && /usr/bin/digest -a md5 -v
%
s 2>/dev/null)"
%
(
test
,
path
)
]
cmd
=
" || "
.
join
(
md5s
)
cmd
=
"
%
s || (echo
\"
0
%
s
\"
)"
%
(
cmd
,
path
)
...
...
lib/ansible/utils.py
View file @
55694db7
...
...
@@ -26,11 +26,19 @@ import jinja2
import
yaml
import
optparse
from
operator
import
methodcaller
try
:
import
json
except
ImportError
:
import
simplejson
as
json
try
:
import
hashlib
HAVE_HASHLIB
=
True
except
ImportError
:
import
md5
HAVE_HASHLIB
=
False
from
ansible
import
errors
import
ansible.constants
as
C
...
...
@@ -312,14 +320,14 @@ def parse_kv(args):
options
[
k
]
=
v
return
options
def
local_md5
(
file
):
''' compute local md5sum, return None if file is not present '''
cmd
=
"/usr/bin/md5sum
%
s 2> /dev/null || /sbin/md5 -q
%
s"
%
(
file
,
file
)
if
not
os
.
path
.
exists
(
file
):
def
md5
(
filename
):
''' compute md5sum, return None if file is not present '''
if
not
os
.
path
.
exists
(
filename
):
return
None
if
HAVE_HASHLIB
:
return
hashlib
.
md5
(
file
(
filename
)
.
read
())
.
hexdigest
()
else
:
c
=
os
.
popen
(
cmd
)
return
c
.
read
()
.
split
()[
0
]
return
md5
.
new
(
file
(
filename
)
.
read
())
.
hexdigest
()
####################################################################
...
...
library/assemble
View file @
55694db7
...
...
@@ -28,6 +28,7 @@ import shlex
import
shutil
import
syslog
import
tempfile
try
:
import
hashlib
HAVE_HASHLIB
=
True
...
...
@@ -64,12 +65,14 @@ def write_temp_file(data):
os
.
close
(
fd
)
return
path
def
file_digest
(
path
):
if
HAVE_HASHLIB
:
digest
=
hashlib
.
md5
(
file
(
path
)
.
read
())
.
hexdigest
()
else
:
digest
=
md5
.
new
(
file
(
path
)
.
read
())
.
hexdigest
()
return
digest
def
md5
(
filename
):
''' compute md5sum, return None if file is not present '''
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
()
# ===========================================
...
...
@@ -111,10 +114,10 @@ if not os.path.isdir(src):
fail_json
(
msg
=
"Source (
%
s) is not a directory"
%
src
)
path
=
write_temp_file
(
assemble_from_fragments
(
src
))
pathmd5
=
file_digest
(
path
)
pathmd5
=
md5
(
path
)
if
os
.
path
.
exists
(
dest
):
destmd5
=
file_digest
(
dest
)
destmd5
=
md5
(
dest
)
if
pathmd5
!=
destmd5
:
shutil
.
copy
(
path
,
dest
)
...
...
library/copy
View file @
55694db7
...
...
@@ -24,6 +24,13 @@ import shlex
import
shutil
import
syslog
try
:
import
hashlib
HAVE_HASHLIB
=
True
except
ImportError
:
import
md5
HAVE_HASHLIB
=
False
# ===========================================
# convert arguments of form a=b c=d
# to a dictionary
...
...
@@ -38,9 +45,16 @@ def exit_kv(rc=0, **kwargs):
print
dump_kv
(
kwargs
)
sys
.
exit
(
rc
)
def
md5_sum
(
f
):
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
]
return
md5sum
def
md5
(
filename
):
''' compute md5sum, return None if file is not present '''
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
:
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
))
if
not
os
.
access
(
src
,
os
.
R_OK
):
exit_kv
(
rc
=
1
,
failed
=
1
,
msg
=
"Source
%
s not readable"
%
(
src
))
md5sum_src
=
md5
_sum
(
src
)
md5sum_src
=
md5
(
src
)
md5sum_dest
=
None
# 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
))
if
not
os
.
access
(
dest
,
os
.
R_OK
):
exit_kv
(
rc
=
1
,
failed
=
1
,
msg
=
"Destination
%
s not readable"
%
(
dest
))
md5sum_dest
=
md5
_sum
(
dest
)
md5sum_dest
=
md5
(
dest
)
else
:
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
)))
...
...
library/setup
View file @
55694db7
...
...
@@ -34,6 +34,13 @@ import traceback
import
syslog
try
:
import
hashlib
HAVE_HASHLIB
=
True
except
ImportError
:
import
md5
HAVE_HASHLIB
=
False
try
:
import
selinux
HAVE_SELINUX
=
True
except
ImportError
:
...
...
@@ -311,9 +318,16 @@ def ansible_facts():
get_service_facts
(
facts
)
return
facts
def
md5_sum
(
f
):
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
]
return
md5sum
def
md5
(
filename
):
''' compute md5sum, return None if file is not present '''
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
...
...
@@ -351,7 +365,7 @@ md5sum = None
if
not
os
.
path
.
exists
(
ansible_file
):
changed
=
True
else
:
md5sum
=
md5
_sum
(
ansible_file
)
md5sum
=
md5
(
ansible_file
)
# Get some basic facts in case facter or ohai are not installed
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
.
close
()
md5sum2
=
md5
_sum
(
ansible_file
)
md5sum2
=
md5
(
ansible_file
)
if
md5sum
!=
md5sum2
:
changed
=
True
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment