Fix git for py3
Comparing to the output of run_command() needs to use native strings Also fix imports: We were relying on them coming from the import of basic. A few (like yaml) weren't imported at all.
This commit is contained in:
parent
5446e6639f
commit
fad760dc7a
1 changed files with 30 additions and 24 deletions
|
@ -21,7 +21,7 @@
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
module: git
|
module: git
|
||||||
author:
|
author:
|
||||||
- "Ansible Core Team"
|
- "Ansible Core Team"
|
||||||
- "Michael DeHaan"
|
- "Michael DeHaan"
|
||||||
version_added: "0.0.1"
|
version_added: "0.0.1"
|
||||||
|
@ -46,8 +46,8 @@ options:
|
||||||
default: "HEAD"
|
default: "HEAD"
|
||||||
description:
|
description:
|
||||||
- What version of the repository to check out. This can be the
|
- What version of the repository to check out. This can be the
|
||||||
the literal string C(HEAD), a branch name, a tag name.
|
the literal string C(HEAD), a branch name, a tag name.
|
||||||
It can also be a I(SHA-1) hash, in which case C(refspec) needs
|
It can also be a I(SHA-1) hash, in which case C(refspec) needs
|
||||||
to be specified if the given revision is not already available.
|
to be specified if the given revision is not already available.
|
||||||
accept_hostkey:
|
accept_hostkey:
|
||||||
required: false
|
required: false
|
||||||
|
@ -55,8 +55,8 @@ options:
|
||||||
choices: [ "yes", "no" ]
|
choices: [ "yes", "no" ]
|
||||||
version_added: "1.5"
|
version_added: "1.5"
|
||||||
description:
|
description:
|
||||||
- if C(yes), adds the hostkey for the repo url if not already
|
- if C(yes), adds the hostkey for the repo url if not already
|
||||||
added. If ssh_opts contains "-o StrictHostKeyChecking=no",
|
added. If ssh_opts contains "-o StrictHostKeyChecking=no",
|
||||||
this parameter is ignored.
|
this parameter is ignored.
|
||||||
ssh_opts:
|
ssh_opts:
|
||||||
required: false
|
required: false
|
||||||
|
@ -186,9 +186,9 @@ requirements:
|
||||||
|
|
||||||
notes:
|
notes:
|
||||||
- "If the task seems to be hanging, first verify remote host is in C(known_hosts).
|
- "If the task seems to be hanging, first verify remote host is in C(known_hosts).
|
||||||
SSH will prompt user to authorize the first contact with a remote host. To avoid this prompt,
|
SSH will prompt user to authorize the first contact with a remote host. To avoid this prompt,
|
||||||
one solution is to use the option accept_hostkey. Another solution is to
|
one solution is to use the option accept_hostkey. Another solution is to
|
||||||
add the remote host public key in C(/etc/ssh/ssh_known_hosts) before calling
|
add the remote host public key in C(/etc/ssh/ssh_known_hosts) before calling
|
||||||
the git module, with the following command: ssh-keyscan -H remote_host.com >> /etc/ssh/ssh_known_hosts."
|
the git module, with the following command: ssh-keyscan -H remote_host.com >> /etc/ssh/ssh_known_hosts."
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
@ -214,12 +214,18 @@ EXAMPLES = '''
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import shlex
|
||||||
|
import stat
|
||||||
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
from distutils.version import LooseVersion
|
from distutils.version import LooseVersion
|
||||||
|
|
||||||
|
from ansible.module_utils.basic import AnsibleModule, get_module_path
|
||||||
|
from ansible.module_utils.known_hosts import add_git_host_key
|
||||||
from ansible.module_utils.six import string_types
|
from ansible.module_utils.six import string_types
|
||||||
from ansible.module_utils._text import to_bytes, to_native
|
from ansible.module_utils._text import to_bytes, to_native
|
||||||
|
|
||||||
|
|
||||||
def head_splitter(headfile, remote, module=None, fail_on_error=False):
|
def head_splitter(headfile, remote, module=None, fail_on_error=False):
|
||||||
'''Extract the head reference'''
|
'''Extract the head reference'''
|
||||||
# https://github.com/ansible/ansible-modules-core/pull/907
|
# https://github.com/ansible/ansible-modules-core/pull/907
|
||||||
|
@ -253,13 +259,13 @@ def unfrackgitpath(path):
|
||||||
|
|
||||||
def get_submodule_update_params(module, git_path, cwd):
|
def get_submodule_update_params(module, git_path, cwd):
|
||||||
|
|
||||||
#or: git submodule [--quiet] update [--init] [-N|--no-fetch]
|
#or: git submodule [--quiet] update [--init] [-N|--no-fetch]
|
||||||
#[-f|--force] [--rebase] [--reference <repository>] [--merge]
|
#[-f|--force] [--rebase] [--reference <repository>] [--merge]
|
||||||
#[--recursive] [--] [<path>...]
|
#[--recursive] [--] [<path>...]
|
||||||
|
|
||||||
params = []
|
params = []
|
||||||
|
|
||||||
# run a bad submodule command to get valid params
|
# run a bad submodule command to get valid params
|
||||||
cmd = "%s submodule update --help" % (git_path)
|
cmd = "%s submodule update --help" % (git_path)
|
||||||
rc, stdout, stderr = module.run_command(cmd, cwd=cwd)
|
rc, stdout, stderr = module.run_command(cmd, cwd=cwd)
|
||||||
lines = stderr.split('\n')
|
lines = stderr.split('\n')
|
||||||
|
@ -272,7 +278,7 @@ def get_submodule_update_params(module, git_path, cwd):
|
||||||
update_line = update_line.replace(']','')
|
update_line = update_line.replace(']','')
|
||||||
update_line = update_line.replace('|',' ')
|
update_line = update_line.replace('|',' ')
|
||||||
parts = shlex.split(update_line)
|
parts = shlex.split(update_line)
|
||||||
for part in parts:
|
for part in parts:
|
||||||
if part.startswith('--'):
|
if part.startswith('--'):
|
||||||
part = part.replace('--', '')
|
part = part.replace('--', '')
|
||||||
params.append(part)
|
params.append(part)
|
||||||
|
@ -320,7 +326,7 @@ def set_git_ssh(ssh_wrapper, key_file, ssh_opts):
|
||||||
del os.environ["GIT_KEY"]
|
del os.environ["GIT_KEY"]
|
||||||
|
|
||||||
if key_file:
|
if key_file:
|
||||||
os.environ["GIT_KEY"] = key_file
|
os.environ["GIT_KEY"] = key_file
|
||||||
|
|
||||||
if os.environ.get("GIT_SSH_OPTS"):
|
if os.environ.get("GIT_SSH_OPTS"):
|
||||||
del os.environ["GIT_SSH_OPTS"]
|
del os.environ["GIT_SSH_OPTS"]
|
||||||
|
@ -420,7 +426,7 @@ def reset(git_path, module, dest):
|
||||||
|
|
||||||
def get_diff(module, git_path, dest, repo, remote, depth, bare, before, after):
|
def get_diff(module, git_path, dest, repo, remote, depth, bare, before, after):
|
||||||
''' Return the difference between 2 versions '''
|
''' Return the difference between 2 versions '''
|
||||||
if before == None:
|
if before is None:
|
||||||
return { 'prepared': '>> Newly checked out %s' % after }
|
return { 'prepared': '>> Newly checked out %s' % after }
|
||||||
elif before != after:
|
elif before != after:
|
||||||
# Ensure we have the object we are referring to during git diff !
|
# Ensure we have the object we are referring to during git diff !
|
||||||
|
@ -469,7 +475,7 @@ def get_remote_head(git_path, module, dest, version, remote, bare):
|
||||||
out = to_native(out)
|
out = to_native(out)
|
||||||
|
|
||||||
if tag:
|
if tag:
|
||||||
# Find the dereferenced tag if this is an annotated tag.
|
# Find the dereferenced tag if this is an annotated tag.
|
||||||
for tag in out.split('\n'):
|
for tag in out.split('\n'):
|
||||||
if tag.endswith(version + '^{}'):
|
if tag.endswith(version + '^{}'):
|
||||||
out = tag
|
out = tag
|
||||||
|
@ -483,7 +489,7 @@ def get_remote_head(git_path, module, dest, version, remote, bare):
|
||||||
def is_remote_tag(git_path, module, dest, remote, version):
|
def is_remote_tag(git_path, module, dest, remote, version):
|
||||||
cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version)
|
cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version)
|
||||||
(rc, out, err) = module.run_command(cmd, check_rc=True, cwd=dest)
|
(rc, out, err) = module.run_command(cmd, check_rc=True, cwd=dest)
|
||||||
if to_bytes(version, errors='surrogate_or_strict') in out:
|
if to_native(version, errors='surrogate_or_strict') in out:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
@ -550,6 +556,10 @@ def get_head_branch(git_path, module, dest, remote, bare=False):
|
||||||
# Check if the .git is a file. If it is a file, it means that we are in a submodule structure.
|
# Check if the .git is a file. If it is a file, it means that we are in a submodule structure.
|
||||||
if os.path.isfile(repo_path):
|
if os.path.isfile(repo_path):
|
||||||
try:
|
try:
|
||||||
|
### FIXME: This introduces another dep that we don't want. We
|
||||||
|
# probably need to take a look at the format of this file and do
|
||||||
|
# our own parsing.
|
||||||
|
import yaml
|
||||||
gitdir = yaml.safe_load(open(repo_path)).get('gitdir')
|
gitdir = yaml.safe_load(open(repo_path)).get('gitdir')
|
||||||
# There is a possibility the .git file to have an absolute path.
|
# There is a possibility the .git file to have an absolute path.
|
||||||
if os.path.isabs(gitdir):
|
if os.path.isabs(gitdir):
|
||||||
|
@ -602,7 +612,6 @@ def fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec, g
|
||||||
fetch_str = 'download remote objects and refs'
|
fetch_str = 'download remote objects and refs'
|
||||||
fetch_cmd = [git_path, 'fetch']
|
fetch_cmd = [git_path, 'fetch']
|
||||||
|
|
||||||
|
|
||||||
refspecs = []
|
refspecs = []
|
||||||
if depth:
|
if depth:
|
||||||
# try to find the minimal set of refs we need to fetch to get a
|
# try to find the minimal set of refs we need to fetch to get a
|
||||||
|
@ -674,7 +683,7 @@ def submodules_fetch(git_path, module, remote, track_submodules, dest):
|
||||||
if line.strip().startswith('url'):
|
if line.strip().startswith('url'):
|
||||||
repo = line.split('=', 1)[1].strip()
|
repo = line.split('=', 1)[1].strip()
|
||||||
if module.params['ssh_opts'] is not None:
|
if module.params['ssh_opts'] is not None:
|
||||||
if not "-o StrictHostKeyChecking=no" in module.params['ssh_opts']:
|
if "-o StrictHostKeyChecking=no" not in module.params['ssh_opts']:
|
||||||
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
|
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
|
||||||
else:
|
else:
|
||||||
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
|
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
|
||||||
|
@ -859,7 +868,7 @@ def main():
|
||||||
result = dict( warnings=list() )
|
result = dict( warnings=list() )
|
||||||
|
|
||||||
# evaluate and set the umask before doing anything else
|
# evaluate and set the umask before doing anything else
|
||||||
if umask != None:
|
if umask is not None:
|
||||||
if not isinstance(umask, string_types):
|
if not isinstance(umask, string_types):
|
||||||
module.fail_json(msg="umask must be defined as a quoted octal integer")
|
module.fail_json(msg="umask must be defined as a quoted octal integer")
|
||||||
try:
|
try:
|
||||||
|
@ -897,9 +906,9 @@ def main():
|
||||||
set_git_ssh(ssh_wrapper, key_file, ssh_opts)
|
set_git_ssh(ssh_wrapper, key_file, ssh_opts)
|
||||||
module.add_cleanup_file(path=ssh_wrapper)
|
module.add_cleanup_file(path=ssh_wrapper)
|
||||||
|
|
||||||
# add the git repo's hostkey
|
# add the git repo's hostkey
|
||||||
if module.params['ssh_opts'] is not None:
|
if module.params['ssh_opts'] is not None:
|
||||||
if not "-o StrictHostKeyChecking=no" in module.params['ssh_opts']:
|
if "-o StrictHostKeyChecking=no" not in module.params['ssh_opts']:
|
||||||
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
|
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
|
||||||
else:
|
else:
|
||||||
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
|
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
|
||||||
|
@ -1020,9 +1029,6 @@ def main():
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
# import module snippets
|
|
||||||
from ansible.module_utils.basic import *
|
|
||||||
from ansible.module_utils.known_hosts import *
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue