summaryrefslogtreecommitdiff
path: root/cloudinit/cmd
diff options
context:
space:
mode:
authorChris Lalos <chris.lalos@gmail.com>2022-02-10 15:49:38 -0600
committerGitHub <noreply@github.com>2022-02-10 15:49:38 -0600
commit600b870b399feb9e072748e07ea223556261fbe7 (patch)
tree14fec0163a37a6d8dbdf96b993ccbb3c59e204a2 /cloudinit/cmd
parentcbe840ac7247c9bf79cf63100b7f85ef38758763 (diff)
downloadvyos-cloud-init-600b870b399feb9e072748e07ea223556261fbe7.tar.gz
vyos-cloud-init-600b870b399feb9e072748e07ea223556261fbe7.zip
Shell script handlers by freq (#1166)
Handlers for per-boot/per-instance/per-once multipart MIME Add handlers for adding scripts to userdata that can be run at various frequencies. Scripts of type x-shellscript-per-boot, x-shellscript-per-instance, or x-shellscript-per-once can be added to a multipart MIME userdata message as part of instance userdata. These scripts will then be added to the appropriate per-boot, per-instance, or per-once directory in /var/lib/cloud/scripts/ during processing of userdata.
Diffstat (limited to 'cloudinit/cmd')
-rwxr-xr-xcloudinit/cmd/devel/make_mime.py51
1 files changed, 29 insertions, 22 deletions
diff --git a/cloudinit/cmd/devel/make_mime.py b/cloudinit/cmd/devel/make_mime.py
index a7493c74..c7671a93 100755
--- a/cloudinit/cmd/devel/make_mime.py
+++ b/cloudinit/cmd/devel/make_mime.py
@@ -20,6 +20,28 @@ EPILOG = (
)
+def create_mime_message(files):
+ sub_messages = []
+ errors = []
+ for i, (fh, filename, format_type) in enumerate(files):
+ contents = fh.read()
+ sub_message = MIMEText(contents, format_type, sys.getdefaultencoding())
+ sub_message.add_header(
+ "Content-Disposition", 'attachment; filename="%s"' % (filename)
+ )
+ content_type = sub_message.get_content_type().lower()
+ if content_type not in get_content_types():
+ msg = (
+ "content type %r for attachment %s " "may be incorrect!"
+ ) % (content_type, i + 1)
+ errors.append(msg)
+ sub_messages.append(sub_message)
+ combined_message = MIMEMultipart()
+ for msg in sub_messages:
+ combined_message.attach(msg)
+ return (combined_message, errors)
+
+
def file_content_type(text):
"""Return file content type by reading the first line of the input."""
try:
@@ -97,29 +119,14 @@ def handle_args(name, args):
print("\n".join(get_content_types(strip_prefix=True)))
return 0
- sub_messages = []
- errors = []
- for i, (fh, filename, format_type) in enumerate(args.files):
- contents = fh.read()
- sub_message = MIMEText(contents, format_type, sys.getdefaultencoding())
- sub_message.add_header(
- "Content-Disposition", 'attachment; filename="%s"' % (filename)
- )
- content_type = sub_message.get_content_type().lower()
- if content_type not in get_content_types():
- level = "WARNING" if args.force else "ERROR"
- msg = (
- level + ": content type %r for attachment %s may be incorrect!"
- ) % (content_type, i + 1)
- sys.stderr.write(msg + "\n")
- errors.append(msg)
- sub_messages.append(sub_message)
- if len(errors) and not args.force:
+ combined_message, errors = create_mime_message(args.files)
+ if errors:
+ level = "WARNING" if args.force else "ERROR"
+ for error in errors:
+ sys.stderr.write(f"{level}: {error}\n")
sys.stderr.write("Invalid content-types, override with --force\n")
- return 1
- combined_message = MIMEMultipart()
- for msg in sub_messages:
- combined_message.attach(msg)
+ if not args.force:
+ return 1
print(combined_message)
return 0