-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add "install hotplug" module (SC-476) #1069
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
6 commits
Select commit
Hold shift + click to select a range
aa19e1f
Add "install hotplug" module
TheRealFalcon 51b6a7d
Added
TheRealFalcon 437129f
review comments
TheRealFalcon 5248cc3
pylint finding my stupid typos
TheRealFalcon 1af35c7
review comments
TheRealFalcon 9c7d117
PER_ALWAYS->PER_INSTANCE
TheRealFalcon 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,136 @@ | ||
| # This file is part of cloud-init. See LICENSE file for license information. | ||
| """Install hotplug udev rules if supported and enabled""" | ||
| import os | ||
| from textwrap import dedent | ||
|
|
||
| from cloudinit import util | ||
| from cloudinit import subp | ||
| from cloudinit import stages | ||
| from cloudinit.config.schema import get_schema_doc, validate_cloudconfig_schema | ||
| from cloudinit.distros import ALL_DISTROS | ||
| from cloudinit.event import EventType, EventScope | ||
| from cloudinit.settings import PER_INSTANCE | ||
|
|
||
|
|
||
| frequency = PER_INSTANCE | ||
| distros = [ALL_DISTROS] | ||
|
|
||
| schema = { | ||
| "id": "cc_install_hotplug", | ||
| "name": "Install Hotplug", | ||
| "title": "Install hotplug if supported and enabled", | ||
| "description": dedent("""\ | ||
| This module will install the udev rules to enable hotplug if | ||
| supported by the datasource and enabled in the userdata. The udev | ||
| rules will be installed as | ||
| ``/etc/udev/rules.d/10-cloud-init-hook-hotplug.rules``. | ||
|
|
||
| When hotplug is enabled, newly added network devices will be added | ||
| to the system by cloud-init. After udev detects the event, | ||
| cloud-init will referesh the instance metadata from the datasource, | ||
| detect the device in the updated metadata, then apply the updated | ||
| network configuration. | ||
|
|
||
| Currently supported datasources: Openstack, EC2 | ||
| """), | ||
| "distros": distros, | ||
| "examples": [ | ||
| dedent("""\ | ||
| # Enable hotplug of network devices | ||
| updates: | ||
| network: | ||
| when: ["hotplug"] | ||
| """), | ||
| dedent("""\ | ||
| # Enable network hotplug alongside boot event | ||
| updates: | ||
| network: | ||
| when: ["boot", "hotplug"] | ||
| """), | ||
| ], | ||
| "frequency": frequency, | ||
| "type": "object", | ||
| "properties": { | ||
| "updates": { | ||
| "type": "object", | ||
| "additionalProperties": False, | ||
| "properties": { | ||
| "network": { | ||
| "type": "object", | ||
| "required": ["when"], | ||
| "additionalProperties": False, | ||
| "properties": { | ||
| "when": { | ||
| "type": "array", | ||
| "additionalProperties": False, | ||
| "items": { | ||
| "type": "string", | ||
| "additionalProperties": False, | ||
| "enum": [ | ||
| "boot-new-instance", | ||
| "boot-legacy", | ||
| "boot", | ||
| "hotplug", | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| __doc__ = get_schema_doc(schema) | ||
|
|
||
|
|
||
| HOTPLUG_UDEV_PATH = "/etc/udev/rules.d/10-cloud-init-hook-hotplug.rules" | ||
| HOTPLUG_UDEV_RULES = """\ | ||
| # Installed by cloud-init due to network hotplug userdata | ||
| ACTION!="add|remove", GOTO="cloudinit_end" | ||
| LABEL="cloudinit_hook" | ||
| SUBSYSTEM=="net", RUN+="/usr/lib/cloud-init/hook-hotplug" | ||
| LABEL="cloudinit_end" | ||
| """ | ||
|
|
||
|
|
||
| def handle(_name, cfg, cloud, log, _args): | ||
| validate_cloudconfig_schema(cfg, schema) | ||
| network_hotplug_enabled = ( | ||
| 'updates' in cfg and | ||
| 'network' in cfg['updates'] and | ||
| 'when' in cfg['updates']['network'] and | ||
| 'hotplug' in cfg['updates']['network']['when'] | ||
| ) | ||
|
blackboxsw marked this conversation as resolved.
|
||
| hotplug_supported = EventType.HOTPLUG in ( | ||
| cloud.datasource.get_supported_events( | ||
| [EventType.HOTPLUG]).get(EventScope.NETWORK, set()) | ||
| ) | ||
| hotplug_enabled = stages.update_event_enabled( | ||
| datasource=cloud.datasource, | ||
| cfg=cfg, | ||
| event_source_type=EventType.HOTPLUG, | ||
| scope=EventScope.NETWORK, | ||
| ) | ||
| if not (hotplug_supported and hotplug_enabled): | ||
| if os.path.exists(HOTPLUG_UDEV_PATH): | ||
| log.debug("Uninstalling hotplug, not enabled") | ||
| util.del_file(HOTPLUG_UDEV_PATH) | ||
| subp.subp(["udevadm", "control", "--reload-rules"]) | ||
| elif network_hotplug_enabled: | ||
| log.warning( | ||
| "Hotplug is unsupported by current datasource. " | ||
| "Udev rules will NOT be installed." | ||
| ) | ||
| else: | ||
| log.debug("Skipping hotplug install, not enabled") | ||
| return | ||
| if not subp.which("udevadm"): | ||
| log.debug("Skipping hotplug install, udevadm not found") | ||
| return | ||
|
|
||
| util.write_file( | ||
| filename=HOTPLUG_UDEV_PATH, | ||
| content=HOTPLUG_UDEV_RULES, | ||
| ) | ||
| subp.subp(["udevadm", "control", "--reload-rules"]) | ||
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 |
|---|---|---|
|
|
@@ -165,6 +165,7 @@ cloud_final_modules: | |
| - scripts-user | ||
| - ssh-authkey-fingerprints | ||
| - keys-to-console | ||
| - install-hotplug | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Glad you are ordering this last. Avoid thrashing or collisions during initial network bringup |
||
| - phone-home | ||
| - final-message | ||
| - power-state-change | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -5,4 +5,4 @@ pytest-cov | |
|
|
||
| # Only really needed on older versions of python | ||
| setuptools | ||
| jsonschema==3.2.0 | ||
| jsonschema | ||
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
Oops, something went wrong.
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.