This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate Userdata script to set hostname of instances based on template
i.e. If we set hostname_pattern to "{instance_id}.my_project" then we drop a script in via userdata that [cloud-init](http://cloudinit.readthedocs.org/) will instruct later phases of cloud-init to set the hostname. So that sudo can resolve the hostname and and we don't get warnings/delays we also want to be able to instruct cloud_config to manage /etc/hosts - which we do via letting the user pass in a chunk of YAML for [cloud_config](http://cloudinit.readthedocs.org/en/latest/topics/examples.html) There are other types of userdata we could add to the Userdata "Mime Multipart archive" but we don't need them right now so don't expose them from the config YAML
- Loading branch information
Showing
7 changed files
with
287 additions
and
10 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import gzip | ||
|
||
from StringIO import StringIO | ||
from contextlib import closing | ||
from email import encoders | ||
from email.mime.base import MIMEBase | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
|
||
|
||
STARTS_WITH_MAPPINGS = { | ||
'#include': 'text/x-include-url', | ||
'#include-once': 'text/x-include-once-url', | ||
'#!': 'text/x-shellscript', | ||
'#cloud-config': 'text/cloud-config', | ||
'#cloud-config-archive': 'text/cloud-config-archive', | ||
'#upstart-job': 'text/upstart-job', | ||
'#part-handler': 'text/part-handler', | ||
'#cloud-boothook': 'text/cloud-boothook' | ||
} | ||
|
||
|
||
def try_decode(data): | ||
try: | ||
return (True, data.decode()) | ||
except UnicodeDecodeError: | ||
return (False, data) | ||
|
||
|
||
def get_type(content, deftype): | ||
rtype = deftype | ||
|
||
(can_be_decoded, content) = try_decode(content) | ||
|
||
if can_be_decoded: | ||
# slist is sorted longest first | ||
slist = sorted(list(STARTS_WITH_MAPPINGS.keys()), key=lambda e: 0 - len(e)) | ||
for sstr in slist: | ||
if content.startswith(sstr): | ||
rtype = STARTS_WITH_MAPPINGS[sstr] | ||
break | ||
else: | ||
rtype = 'application/octet-stream' | ||
|
||
return(rtype) | ||
|
||
|
||
def pack(parts, opts={}): | ||
outer = MIMEMultipart() | ||
|
||
for arg in parts: | ||
if isinstance(arg, basestring): | ||
arg = {'content': arg} | ||
|
||
if 'mime_type' in arg: | ||
mtype = arg['mime_type'] | ||
else: | ||
mtype = get_type(arg['content'], opts.get('deftype', "text/plain")) | ||
|
||
maintype, subtype = mtype.split('/', 1) | ||
if maintype == 'text': | ||
# Note: we should handle calculating the charset | ||
msg = MIMEText(arg['content'], _subtype=subtype) | ||
else: | ||
msg = MIMEBase(maintype, subtype) | ||
msg.set_payload(arg['content']) | ||
# Encode the payload using Base64 | ||
encoders.encode_base64(msg) | ||
|
||
outer.attach(msg) | ||
|
||
with closing(StringIO()) as buff: | ||
if opts.get('compress', False): | ||
gfile = gzip.GzipFile(fileobj=buff) | ||
gfile.write(outer.as_string().encode()) | ||
gfile.close() | ||
else: | ||
buff.write(outer.as_string().encode()) | ||
|
||
return buff.getvalue() |
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.