-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Overhaul user data formats documentation (#5551)
Fixes GH-4739
- Loading branch information
1 parent
52a8292
commit 79174b6
Showing
9 changed files
with
379 additions
and
185 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,22 +1,58 @@ | ||
#part-handler | ||
|
||
"""This is a trivial example part-handler that creates a file with the path | ||
specified in the payload. It performs no input checking or error handling. | ||
|
||
To use it, first save the file you are currently viewing into your current | ||
working directory. Then run the following: | ||
``` | ||
$ echo '/var/tmp/my_path' > part | ||
$ cloud-init devel make-mime -a part-handler.py:part-handler -a part:x-my-path --force > user-data | ||
``` | ||
|
||
This will create a mime file with the contents of 'part' and the | ||
part-handler. You can now pass 'user-data' to your cloud of choice. | ||
|
||
When run, cloud-init will have created an empty file at /var/tmp/my_path. | ||
""" | ||
|
||
import pathlib | ||
from typing import Any | ||
|
||
from cloudinit.cloud import Cloud | ||
|
||
|
||
def list_types(): | ||
# return a list of mime-types that are handled by this module | ||
return(["text/plain", "text/go-cubs-go"]) | ||
|
||
def handle_part(data, ctype, filename, payload): | ||
# data: the cloudinit object | ||
# ctype: '__begin__', '__end__', or the specific mime-type of the part | ||
# filename: the filename for the part, or dynamically generated part if | ||
# no filename is given attribute is present | ||
# payload: the content of the part (empty for begin or end) | ||
"""Return a list of mime-types that are handled by this module.""" | ||
return ["text/x-my-path"] | ||
|
||
|
||
def handle_part(data: Cloud, ctype: str, filename: str, payload: Any): | ||
"""Handle a part with the given mime-type. | ||
|
||
This function will get called multiple times. The first time is | ||
to allow any initial setup needed to handle parts. It will then get | ||
called once for each part matching the mime-type returned by `list_types`. | ||
Finally, it will get called one last time to allow for any final | ||
teardown. | ||
|
||
:data: A `Cloud` instance. This will be the same instance for each call | ||
to handle_part. | ||
:ctype: '__begin__', '__end__', or the mime-type | ||
(for this example 'text/x-my-path') of the part | ||
:filename: The filename for the part as defined in the MIME archive, | ||
or dynamically generated part if no filename is given | ||
:payload: The content of the part. This will be | ||
`None` when `ctype` is '__begin__' or '__end__'. | ||
""" | ||
if ctype == "__begin__": | ||
print("my handler is beginning") | ||
return | ||
# Any custom setup needed before handling payloads | ||
return | ||
|
||
if ctype == "__end__": | ||
print("my handler is ending") | ||
return | ||
# Any custom teardown needed after handling payloads can happen here | ||
return | ||
|
||
print(f"==== received ctype={ctype} filename={filename} ====") | ||
print(payload) | ||
print(f"==== end ctype={ctype} filename={filename}") | ||
# If we've made it here, we're dealing with a real payload, so handle | ||
# it appropriately | ||
pathlib.Path(payload.strip()).touch() |
This file contains 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.