Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ansible] Fix docker shell plugin #1253

Merged
merged 4 commits into from
Dec 4, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions ansible/shell_plugins/docker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
from __future__ import (absolute_import, division)
__metaclass__ = type

DOCUMENTATION = '''
name: docker
plugin_type: shell
short_description: "docker shell plugin"
version_added: historical
description:
- This module allows you to execute commands directly in docker on the remote host
extends_documentation_fragment:
- shell_common
'''

import os
import re
import pipes
Expand All @@ -18,6 +29,7 @@ class ShellModule(sh):
def __init__(self, *args, **kwargs):
super(ShellModule, self).__init__(*args, **kwargs)
self.dtemps = []
self.container = None

def join_path(self, *args):
## HACK! HACK! HACK!
Expand All @@ -29,7 +41,22 @@ def join_path(self, *args):

return super(ShellModule, self).join_path(*args)

def build_module_command(self, env_string, shebang, cmd, arg_path=None, rm_tmp=None):
def remove(self, path, recurse=False):
if not self.container:
self.container = self.get_option('ansible_python_interpreter').split()[3]
Copy link
Contributor

@qiluo-msft qiluo-msft Dec 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.get_option('ansible_python_interpreter').split()[3] [](start = 29, length = 56)

This logic is not robust.
If this case not self.container happens in your test? If not, we can just fallback to base class function. #Closed

Copy link
Contributor Author

@msosyak msosyak Dec 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am right this module was made just for one thing, to execute tasks inside the container on the host. And it retrieves the container name from shebang in build_module_command which is not anything else as ansible_python_interpreter. And for this plugin, it should look like docker exec -i <container> python what is not obvious. So I just put get_option('ansible_python_interpreter') under if to minimize get_option calls.

For any case changed this to the same approach as in build_module_command.

For me, it looks like is better to define mandatory ansible_container option for this plugin, change remove and build_module_command to use it as a container name source and replace

vars:
    ansible_shell_type: docker
    ansible_python_interpreter: docker exec -i swss python

to

vars:
    ansible_shell_type: docker
    ansible_container: swss

I could do this in separate PR if you accept.
Or maybe it is the time to look to the docker connection plugin.


remove_files_on_host_cmd = super(ShellModule, self).remove(path, recurse)

cmd = remove_files_on_host_cmd + "; docker exec -i "
cmd += self.container + " "
cmd += 'rm -f '
if recurse:
cmd += '-r '
cmd += " ".join(self.dtemps)

return cmd

def build_module_command(self, env_string, shebang, cmd, arg_path=None):
# assert(self.container_name)
argv = shlex.split(shebang.replace("#!", ""))
assert(argv[0] == 'docker')
Expand All @@ -51,13 +78,7 @@ def build_module_command(self, env_string, shebang, cmd, arg_path=None, rm_tmp=N
pre = ''.join('docker exec {1} mkdir -p {0}; docker cp {0}/. {1}:{0}; '
msosyak marked this conversation as resolved.
Show resolved Hide resolved
.format(dtemp, self.container_name) for dtemp in self.dtemps)

if rm_tmp:
post = ''.join('docker exec {1} rm -rf {0}; '
.format(dtemp, self.container_name) for dtemp in self.dtemps)
else:
post = ''

return pre + super(ShellModule, self).build_module_command('', shebang_env, cmd, arg_path, rm_tmp) + '; ' + post
return pre + super(ShellModule, self).build_module_command('', shebang_env, cmd, arg_path)

def checksum(self, path, python_interp):
"""
Expand Down