-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Shell script handlers by freq #1166
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
TheRealFalcon
merged 59 commits into
canonical:main
from
beantaxi:shell-script-handlers-by-freq
Feb 10, 2022
Merged
Changes from all commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
d8488b1
first commit for first cloud-init PR
beantaxi bd88965
cleaned up log statements, in part to fix travis
beantaxi ccb2435
flake8 fixes part 2 (of 2?)
beantaxi 0e4d9de
pylint fixes
beantaxi 6523f80
Merge branch 'master' into shell-script-handlers-by-freq
beantaxi 445ba33
Merge branch 'master' into shell-script-handlers-by-freq
beantaxi 30fd096
ok lets do this
beantaxi b57f5a6
Merge remote-tracking branch 'mother/master' into shell-script-handle…
beantaxi 0d9acbe
First commit of the changes suggested/requested by smoser and raharper
beantaxi d511f7d
Change scripts_dir initialization to match shell_script.py to fix cir…
beantaxi 56ea2cc
fixed typos
beantaxi 45e1320
more fixes
beantaxi 3439b25
flake8 fixes
beantaxi 888e70b
pytest fixes
beantaxi a834b2e
pytest fixes
beantaxi 87fddb9
Changed to get_cpath() and added some comments
beantaxi f33429e
Common base class for the part handlers. Required # pylint:disable; m…
beantaxi b7f6518
using prefixes for list_types now
beantaxi 3b98863
Merge remote-tracking branch 'mother/master' into shell-script-handle…
beantaxi ab9d238
Changes suggested by TheRealFalcon
beantaxi 3251001
Fixes for flake8; prematurely pushed the last one
beantaxi d8cd589
Removed subclasses - now there's just one class with frequency-specif…
beantaxi 45cfa7c
start of integration test ... plus cloudinit-tester scripts for fun
beantaxi bee79ad
Still trying to get integration test working
chrislalos 1e58a42
Trying to get integration test working ...
chrislalos 0a1bdc8
dunno
1db9696
merge
chrislalos 400ac42
added print statements
chrislalos 0671d08
Added print() statements for debugging
chrislalos 74bf2fc
second pass at incorporating falcons changes
chrislalos b56c2f8
more logging
chrislalos 69bf964
dunno
chrislalos e2f0b9a
merge
chrislalos 09a3006
added more tests
chrislalos 9741cc0
merge
chrislalos 2d32b53
moved Docker test stuff to cloudinit/cmd/devel
chrislalos 56db4bc
Merge branch 'shell-script-handlers-by-freq' of https://github.com/be…
chrislalos 776a51d
removed Dockerfiles from this branch - they will be added to another …
chrislalos 8d8229f
merged in upstream/main
chrislalos ea2a262
passes tox -e flake8
chrislalos 325cb00
passes tox -e format
chrislalos 854a871
Incorporated changes from James Falcons suggestions on 1/11/22
chrislalos e5db3cd
final changes to pass tox -e format
chrislalos 0fb03aa
Merge remote-tracking branch 'upstream/main' into shell-script-handle…
chrislalos 8aed6f2
Fixed mistake I made in changing test_builtin_handlers that was break…
chrislalos c5f677f
Take 2: Fixed mistake I made in changing test_builtin_handlers that w…
chrislalos 7077f3d
flake8 + black
chrislalos d072fcd
one more commit; tbh this has changes I thought were committed
chrislalos e3645ac
print() removal
chrislalos 29442ec
falcojrs more significant suggestions
chrislalos e1cf569
fixed flake8 pylint and black tests
chrislalos c0fb855
slight change to test
chrislalos 4a6b25e
added trailing newline to .gitignore
chrislalos 0e8f81b
marking my int tests with @pytest.mark.ci
chrislalos bd3b7b8
renamed test file
chrislalos 383731f
changes for falconjrs comments
chrislalos 7ea8dd4
Fixed flake8 black & isort failures
chrislalos eae159d
Consolidate 3 handler classes into 1 as per falconjr
chrislalos e5f79d6
fixed test failures (missed pathMap -> path_map)
chrislalos 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
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,62 @@ | ||
| import os | ||
|
|
||
| from cloudinit import log, util | ||
| from cloudinit.handlers import Handler | ||
| from cloudinit.settings import PER_ALWAYS, PER_INSTANCE, PER_ONCE | ||
|
|
||
| LOG = log.getLogger(__name__) | ||
|
|
||
| # cloudinit/settings.py defines PER_*** frequency constants. It makes sense to | ||
| # use them here, instead of hardcodes, and map them to the 'per-***' frequency- | ||
| # specific folders in /v/l/c/scripts. It might make sense to expose this at a | ||
| # higher level or in a more general module -- eg maybe in cloudinit/settings.py | ||
| # itself -- but for now it's here. | ||
| path_map = { | ||
| PER_ALWAYS: "per-boot", | ||
| PER_INSTANCE: "per-instance", | ||
| PER_ONCE: "per-once", | ||
| } | ||
|
|
||
|
|
||
| def get_mime_type_by_frequency(freq): | ||
| mime_type = f"text/x-shellscript-{path_map[freq]}" | ||
| return mime_type | ||
|
|
||
|
|
||
| def get_script_folder_by_frequency(freq, scripts_dir): | ||
| """Return the frequency-specific subfolder for a given frequency constant | ||
| and parent folder.""" | ||
| freqPath = path_map[freq] | ||
| folder = os.path.join(scripts_dir, freqPath) | ||
| return folder | ||
|
|
||
|
|
||
| def write_script_by_frequency(script_path, payload, frequency, scripts_dir): | ||
| """Given a filename, a payload, a frequency, and a scripts folder, write | ||
| the payload to the correct frequency-specific path""" | ||
| filename = os.path.basename(script_path) | ||
| filename = util.clean_filename(filename) | ||
| folder = get_script_folder_by_frequency(frequency, scripts_dir) | ||
| path = os.path.join(folder, filename) | ||
| payload = util.dos2unix(payload) | ||
| util.write_file(path, payload, 0o700) | ||
|
|
||
|
|
||
| class ShellScriptByFreqPartHandler(Handler): | ||
| """Common base class for the frequency-specific script handlers.""" | ||
|
|
||
| def __init__(self, script_frequency, paths, **_kwargs): | ||
| Handler.__init__(self, PER_ALWAYS) | ||
| self.prefixes = [get_mime_type_by_frequency(script_frequency)] | ||
| self.script_frequency = script_frequency | ||
| self.scripts_dir = paths.get_cpath("scripts") | ||
| if "script_path" in _kwargs: | ||
| self.scripts_dir = paths.get_cpath(_kwargs["script_path"]) | ||
|
|
||
| def handle_part(self, data, ctype, script_path, payload, frequency): | ||
| if script_path is not None: | ||
| filename = os.path.basename(script_path) | ||
| filename = util.clean_filename(filename) | ||
| write_script_by_frequency( | ||
| script_path, payload, self.script_frequency, self.scripts_dir | ||
| ) |
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,48 @@ | ||
| """Integration tests for various handlers.""" | ||
|
|
||
| from io import StringIO | ||
|
|
||
| import pytest | ||
|
|
||
| from cloudinit.cmd.devel.make_mime import create_mime_message | ||
| from tests.integration_tests.instances import IntegrationInstance | ||
|
|
||
| PER_FREQ_TEMPLATE = """\ | ||
| #!/bin/bash | ||
| touch /tmp/test_per_freq_{} | ||
| """ | ||
|
|
||
| PER_ALWAYS_FILE = StringIO(PER_FREQ_TEMPLATE.format("always")) | ||
| PER_INSTANCE_FILE = StringIO(PER_FREQ_TEMPLATE.format("instance")) | ||
| PER_ONCE_FILE = StringIO(PER_FREQ_TEMPLATE.format("once")) | ||
|
|
||
| FILES = [ | ||
| (PER_ALWAYS_FILE, "always.sh", "x-shellscript-per-boot"), | ||
| (PER_INSTANCE_FILE, "instance.sh", "x-shellscript-per-instance"), | ||
| (PER_ONCE_FILE, "once.sh", "x-shellscript-per-once"), | ||
| ] | ||
|
|
||
| USER_DATA, errors = create_mime_message(FILES) | ||
|
|
||
|
|
||
| @pytest.mark.ci | ||
| @pytest.mark.user_data(USER_DATA) | ||
| def test_per_freq(client: IntegrationInstance): | ||
| # Sanity test for scripts folder | ||
| cmd = "test -d /var/lib/cloud/scripts" | ||
| assert client.execute(cmd).ok | ||
| # Test per-boot | ||
| cmd = "test -f /var/lib/cloud/scripts/per-boot/always.sh" | ||
| assert client.execute(cmd).ok | ||
| cmd = "test -f /tmp/test_per_freq_always" | ||
| assert client.execute(cmd).ok | ||
| # Test per-instance | ||
| cmd = "test -f /var/lib/cloud/scripts/per-instance/instance.sh" | ||
| assert client.execute(cmd).ok | ||
| cmd = "test -f /tmp/test_per_freq_instance" | ||
| assert client.execute(cmd).ok | ||
| # Test per-once | ||
| cmd = "test -f /var/lib/cloud/scripts/per-once/once.sh" | ||
| assert client.execute(cmd).ok | ||
| cmd = "test -f /tmp/test_per_freq_once" | ||
| assert client.execute(cmd).ok | ||
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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ andrewbogott | |
| andrewlukoshko | ||
| antonyc | ||
| aswinrajamannar | ||
| beantaxi | ||
| beezly | ||
| bipinbachhao | ||
| BirknerAlex | ||
|
|
||
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.