-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add module 'write-deferred-files' executed in stage 'final' #916
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
cca1dd3
Change module order: run 'users-groups' before 'write-files'
lucendio c0bd6cb
Revert "Change module order: run 'users-groups' before 'write-files'"
lucendio a8c86be
Add module to allow deferring file writing
lucendio f502cfc
Add tests for the module 'write-deferred-files'
lucendio 073785f
Remove users.files in favour of the new write_files[*].defer field
lucendio 6bfbdfc
Integrate first couple of review feedback (the simple ones)
lucendio f0b2e1a
Adhere to linter rules
lucendio f85690d
Move from functional filtering to list comprehension
lucendio 6f6042a
Make processing the input more robust
lucendio 0b062c0
Add unit test to write_files
lucendio 96a1338
Add integration test for write_files_deferred module
lucendio 3a56b39
Add author's username to CLA signers
lucendio 32ac09a
Just a test - To be reverted
lucendio db1d8da
Revert "Just a test - To be reverted"
lucendio d1e2f2b
Revert "Add integration test for write_files_deferred module"
lucendio 28b4bb3
Extend write_files integration test to verify 'defer' behaviour
lucendio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Copyright (C) 2021 Canonical Ltd. | ||
| # | ||
| # This file is part of cloud-init. See LICENSE file for license information. | ||
|
|
||
| """Defer writing certain files""" | ||
|
|
||
| from textwrap import dedent | ||
|
|
||
| from cloudinit.config.schema import validate_cloudconfig_schema | ||
| from cloudinit import util | ||
| from cloudinit.config.cc_write_files import ( | ||
| schema as write_files_schema, write_files, DEFAULT_DEFER) | ||
|
TheRealFalcon marked this conversation as resolved.
|
||
|
|
||
|
|
||
| schema = util.mergemanydict([ | ||
| { | ||
| 'id': 'cc_write_files_deferred', | ||
| 'name': 'Write Deferred Files', | ||
| 'title': dedent("""\ | ||
| write certain files, whose creation as been deferred, during | ||
| final stage | ||
| """), | ||
| 'description': dedent("""\ | ||
| This module is based on `'Write Files' <write-files>`__, and | ||
| will handle all files from the write_files list, that have been | ||
| marked as deferred and thus are not being processed by the | ||
| write-files module. | ||
|
|
||
| *Please note that his module is not exposed to the user through | ||
| its own dedicated top-level directive.* | ||
| """) | ||
| }, | ||
| write_files_schema | ||
| ]) | ||
|
|
||
| # Not exposed, because related modules should document this behaviour | ||
| __doc__ = None | ||
|
|
||
|
|
||
| def handle(name, cfg, _cloud, log, _args): | ||
| validate_cloudconfig_schema(cfg, schema) | ||
| file_list = cfg.get('write_files', []) | ||
| filtered_files = [ | ||
| f for f in file_list if util.get_cfg_option_bool(f, | ||
| 'defer', | ||
| DEFAULT_DEFER) | ||
| ] | ||
| if not filtered_files: | ||
| log.debug(("Skipping module named %s," | ||
| " no deferred file defined in configuration"), name) | ||
| return | ||
| write_files(name, filtered_files) | ||
|
|
||
|
|
||
| # vi: ts=4 expandtab | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
tests/unittests/test_handler/test_handler_write_files_deferred.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # This file is part of cloud-init. See LICENSE file for license information. | ||
|
|
||
| import tempfile | ||
| import shutil | ||
|
|
||
| from cloudinit.config.cc_write_files_deferred import (handle) | ||
| from .test_handler_write_files import (VALID_SCHEMA) | ||
| from cloudinit import log as logging | ||
| from cloudinit import util | ||
|
|
||
| from cloudinit.tests.helpers import ( | ||
| CiTestCase, FilesystemMockingTestCase, mock, skipUnlessJsonSchema) | ||
|
|
||
| LOG = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @skipUnlessJsonSchema() | ||
| @mock.patch('cloudinit.config.cc_write_files_deferred.write_files') | ||
| class TestWriteFilesDeferredSchema(CiTestCase): | ||
|
|
||
| with_logs = True | ||
|
|
||
| def test_schema_validation_warns_invalid_value(self, | ||
| m_write_files_deferred): | ||
| """If 'defer' is defined, it must be of type 'bool'.""" | ||
|
|
||
| valid_config = { | ||
| 'write_files': [ | ||
| {**VALID_SCHEMA.get('write_files')[0], 'defer': True} | ||
| ] | ||
| } | ||
|
|
||
| invalid_config = { | ||
| 'write_files': [ | ||
| {**VALID_SCHEMA.get('write_files')[0], 'defer': str('no')} | ||
| ] | ||
| } | ||
|
|
||
| cc = self.tmp_cloud('ubuntu') | ||
| handle('cc_write_files_deferred', valid_config, cc, LOG, []) | ||
| self.assertNotIn('Invalid config:', self.logs.getvalue()) | ||
| handle('cc_write_files_deferred', invalid_config, cc, LOG, []) | ||
| self.assertIn('Invalid config:', self.logs.getvalue()) | ||
| self.assertIn("defer: 'no' is not of type 'boolean'", | ||
| self.logs.getvalue()) | ||
|
|
||
|
|
||
| class TestWriteFilesDeferred(FilesystemMockingTestCase): | ||
|
|
||
| with_logs = True | ||
|
|
||
| def setUp(self): | ||
| super(TestWriteFilesDeferred, self).setUp() | ||
| self.tmp = tempfile.mkdtemp() | ||
| self.addCleanup(shutil.rmtree, self.tmp) | ||
|
|
||
| def test_filtering_deferred_files(self): | ||
| self.patchUtils(self.tmp) | ||
| expected = "hello world\n" | ||
| config = { | ||
| 'write_files': [ | ||
| { | ||
| 'path': '/tmp/deferred.file', | ||
| 'defer': True, | ||
| 'content': expected | ||
| }, | ||
| {'path': '/tmp/not_deferred.file'} | ||
| ] | ||
| } | ||
| cc = self.tmp_cloud('ubuntu') | ||
| handle('cc_write_files_deferred', config, cc, LOG, []) | ||
| self.assertEqual(util.load_file('/tmp/deferred.file'), expected) | ||
| with self.assertRaises(FileNotFoundError): | ||
| util.load_file('/tmp/not_deferred.file') | ||
|
|
||
|
|
||
| # vi: ts=4 expandtab |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ jshen28 | |
| klausenbusk | ||
| landon912 | ||
| lucasmoura | ||
| lucendio | ||
| lungj | ||
| mal | ||
| mamercad | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.