-
Notifications
You must be signed in to change notification settings - Fork 1.1k
first commit for first cloud-init PR #745
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
Closed
beantaxi
wants to merge
23
commits into
canonical:master
from
beantaxi:shell-script-handlers-by-freq
Closed
Changes from all commits
Commits
Show all changes
23 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| FROM python:3-slim | ||
| RUN apt-get update -y | ||
| RUN apt-get install -y gcc git | ||
| COPY ./requirements.txt /src/ | ||
| COPY ./test-requirements.txt /src/ | ||
| COPY ./integration-requirements.txt /src/ | ||
| WORKDIR /src | ||
| RUN pip3 install --upgrade pip | ||
| RUN pip3 install --requirement requirements.txt | ||
| RUN pip3 install --requirement test-requirements.txt | ||
| RUN pip3 install --requirement integration-requirements.txt | ||
| RUN pip3 install flake8 pylint | ||
| VOLUME /src | ||
| ENTRYPOINT ["python", "-m"] | ||
|
|
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,7 @@ | ||
| #! /usr/bin/env sh | ||
|
|
||
| # Use this script to build the docker image. | ||
| # Not too fancy, but it does ensure the image gets a predictable name, | ||
| # for use by other scripts. | ||
|
|
||
| docker build --tag cloudinit-tester . |
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,20 @@ | ||
| #! /usr/bin/env sh | ||
|
|
||
| # Use this script to run 3 useful invocations of cloudinit-tester-run.sh | ||
| # It's easier to imagine a fancier script that asks Y/n before each test, | ||
| # etc etc, but at that point it might make sense to switch to python | ||
|
|
||
| # Flake8 | ||
| printf 'Running flake8 ...' | ||
| ./cloudinit-tester-run.sh flake8 --verbose cloudinit tests/unittests | ||
| read -p 'flake8 done (press any key to continue ...)' | ||
|
|
||
| # pylint | ||
| printf 'Running pylint (this could take a while) ...' | ||
| ./cloudinit-tester-run.sh pylint cloudinit tests/unittests | ||
| read -p '(press any key to continue ...)' | ||
|
|
||
| # pytest | ||
| printf 'Running pytest ...' | ||
| ./cloudinit-tester-run.sh pytest tests/unittests | ||
| read -p '(press any key to continue ...)' |
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,8 @@ | ||
| # !/usr/bin/env sh | ||
|
|
||
| # Use this script to run various commands inside a cloudinit-tester container. | ||
| # The cloudinit-tester container has an ENTRYPOINT of 'python -m', so use the | ||
| # script like this: | ||
| # | ||
| # ./cloud-init-tester.sh pylint cloudinit # This runs python -m pylint cloudinit | ||
| docker run --rm --name cloudinit-tester --volume $PWD:/src cloudinit-tester "$@" |
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,10 @@ | ||
| #! /usr/bin/env sh | ||
|
|
||
| # Use this script to startup a cloudinit-tester and then shell into it. | ||
| # This is potentially useful if you want to run a large number of commands, | ||
| # or play around. | ||
|
|
||
| # It also supports cmdline args at the end, to allow for running arbitrary commands | ||
| # in case the default entrypoint of `python -m` is not suitable. | ||
|
|
||
| docker run --rm --name cloudinit-tester --volume $PWD:/src -it --entrypoint /bin/bash cloudinit-tester "$@" |
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,57 @@ | ||
| import os | ||
| from cloudinit import log | ||
| from cloudinit import util | ||
| from cloudinit.handlers import Handler | ||
| from cloudinit.settings import PER_ALWAYS, PER_INSTANCE, PER_ONCE | ||
| LOG = log.getLogger(__name__) | ||
|
|
||
| # cloutinit/settings.py defines PER_*** frequency constants. It makes sense to | ||
| # use them here, instead 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. | ||
| pathMap = { | ||
| PER_ALWAYS: 'per-boot', | ||
| PER_INSTANCE: 'per-instance', | ||
| PER_ONCE: 'per-once' | ||
| } | ||
|
|
||
|
|
||
| def get_script_folder_by_frequency(freq, scripts_dir): | ||
| """Return the frequency-specific subfolder for a given frequency constant | ||
| and parent folder.""" | ||
| freqPath = pathMap[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.""" | ||
| prefixes = ["text/x-shellscript-per-boot", | ||
| "text/x-shellscript-per-instance", | ||
| "text/x-shellscript-per-once"] | ||
|
|
||
| def __init__(self, freq, paths, **_kwargs): | ||
| Handler.__init__(self, freq) | ||
| 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: | ||
| LOG.debug("script_path=%s", script_path) | ||
| filename = os.path.basename(script_path) | ||
| filename = util.clean_filename(filename) | ||
| write_script_by_frequency(script_path, payload, self.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,36 @@ | ||
|
|
||
| """Integration tests for various handlers.""" | ||
|
|
||
| from io import StringIO | ||
| from types import SimpleNamespace | ||
|
|
||
| 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 /var/tmp/test_per_freq_{} | ||
| """ | ||
|
|
||
| PER_BOOT_FILE = StringIO(PER_FREQ_TEMPLATE.format('boot')) | ||
| PER_INSTANCE_FILE = StringIO(PER_FREQ_TEMPLATE.format('instance')) | ||
| PER_ONCE_FILE = StringIO(PER_FREQ_TEMPLATE.format('once')) | ||
|
|
||
| args = SimpleNamespace( | ||
| debug=False, | ||
| list_types=False, | ||
| files=[ | ||
| (PER_BOOT_FILE, 'boot.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 = create_mime_message(args) | ||
|
|
||
| @pytest.mark.user_data(USER_DATA) | ||
| def test_per_freq(client: IntegrationInstance): | ||
| scripts = client.execute('find /var/lib/cloud/scripts') | ||
| print(scripts) |
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 |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ Aman306 | |
| andrewbogott | ||
| 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.