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

File refactor #39345

Merged
merged 4 commits into from May 10, 2018
Merged

File refactor #39345

merged 4 commits into from May 10, 2018

Conversation

abadger
Copy link
Contributor

@abadger abadger commented Apr 26, 2018

SUMMARY

Refactoring file so that it is easier to:

  • fix bugs. Right now, fixing a bug for one use case has unintended consequences for other uses
  • unittest permutations. There are many cornercases with file. Testing it with integration tests becomes really time consuming. It would be much better to be able to test this using pytest. But to do that we need to splt functionality apart into testable chunks.

A few changes may also spill over to other file handling modules like copy which depend on the file module to perform their work sometimes.

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

primarily, lib/ansible/modules/files/file.py

ANSIBLE VERSION
devel
ADDITIONAL INFORMATION

This has been okayed for inclusion in Ansible-2.6 as it is required to sanely bugfix the file module.

@ansibot ansibot added WIP This issue/PR is a work in progress. Nevertheless it was shared for getting input from peers. bug This issue/PR relates to a bug. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Apr 26, 2018
@ansibot
Copy link
Contributor

ansibot commented Apr 26, 2018

The test ansible-test sanity --test pep8 [explain] failed with 1 error:

lib/ansible/module_utils/basic.py:232:45: E261 at least two spaces before inline comment

click here for bot help

@jborean93 jborean93 removed the needs_triage Needs a first human triage before being processed. label Apr 26, 2018
@abadger abadger force-pushed the file-refactor branch 2 times, most recently from 91ed5fd to e65beb1 Compare May 4, 2018 04:32
Some file_common_args are not common to all file modules.  Divide the
args into categories for a future refator of this into a separate
file_argspec.
* Remove use of six.b as Python-2.6+ have byte literals.
* Make AnsibleModule a global object so we'll have access to it in all
  the functions we're going to break this up into.
* Rework the parameters so things that are in file_common_args are used
  from file_common_args or the reason for deviation is documented.
* Remove validate as a parameter: this should be taken care of by
  removing it from params before the copy and template action plugin
  invoke file.
* Rename diff_peek to _diff_peek as it is an internal parameter.
* add module_name execute_module call to assemble so that it is more greppable
@abadger abadger force-pushed the file-refactor branch 2 times, most recently from cc17bac to 1d8cb9d Compare May 7, 2018 22:27
@abadger abadger changed the title [WIP] File refactor File refactor May 8, 2018
@abadger abadger removed the WIP This issue/PR is a work in progress. Nevertheless it was shared for getting input from peers. label May 8, 2018
@abadger
Copy link
Contributor Author

abadger commented May 8, 2018

This is ready for review from @mattclay (as the 2.6 RM). This mostly moves the logic around without changing too much. A few refactors get their start here but will be completed in future PRs.

@abadger abadger mentioned this pull request May 8, 2018
@ansibot ansibot added the core_review In order to be merged, this PR must follow the core review workflow. label May 8, 2018
raise ParameterError(results={"path": params["path"],
"msg": "recurse option requires state to be 'directory'"})

return params
Copy link
Member

Choose a reason for hiding this comment

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

Since this function mutates the input params why return it? Did you intend to copy it before modifying it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Conceptually this is additions to the argspec so it should be mutating the params and not returning the params. I added the return since it didn't hurt anything and the calling code immediately does the equivalent of params = module.params I can remove the return and change the caller if you want.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please remove the return and update the caller.

Copy link
Member

@mattclay mattclay left a comment

Choose a reason for hiding this comment

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

Aside from the aforementioned change request, everything else looks OK.

* Separate the logic for each state into separate functions
* Start the process of separating out initialization (pre-processing of
  parameters that cannot be done via arg spec) from the logic to
  implement each state.
* Start the process of raising exceptions for errors and returning
  result values from each state implementing function   Goal is for all
  fail_json's to be consolidated into exception handlers at the toplevel
  and for there to be only one exit_json() at the toplevel.
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels May 8, 2018
argument_spec=dict(
state=dict(choices=['file', 'directory', 'link', 'hard', 'touch', 'absent'], default=None),
path=dict(aliases=['dest', 'name'], required=True, type='path'),
original_basename=dict(required=False), # Internal use only, for recursive ops
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be _original_basename to match _diff_peek?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

original_basename is used for more than one module in several action_plugins. I'll have to look at each place to see if they should all be made private or if certain ones call file while others are calling (for instance) copy.

@@ -216,25 +216,32 @@
_ANSIBLE_ARGS = None

FILE_COMMON_ARGUMENTS = dict(
src=dict(),
# These are things we want. About setting metadata (mode, ownership, permissions in general) on
Copy link
Contributor

Choose a reason for hiding this comment

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

Wonder if it's worth giving some details of how to know if something should be in here or not, ie what is "common"?
May help reduce confusion in the future and things like backup, force being added back in

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. I can do that when we actually break basic.py apart.


# not taken by the file module, but other action plugins call the file module so this ignores
# them for now. In the future, the caller should take care of removing these from the module
# arugments before calling the file module.
Copy link
Contributor

Choose a reason for hiding this comment

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

arugments -> arguments

Copy link
Contributor Author

Choose a reason for hiding this comment

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

k. I'll fix that in a followup PR

@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels May 10, 2018
@abadger abadger merged commit 4e2876b into ansible:devel May 10, 2018
@abadger abadger deleted the file-refactor branch May 10, 2018 21:48
@ansible ansible locked and limited conversation to collaborators May 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue/PR relates to a bug. core_review In order to be merged, this PR must follow the core review workflow. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants