This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from ministryofjustice/hostname-template
Generate Userdata script to set hostname of instances based on format string
- Loading branch information
Showing
7 changed files
with
336 additions
and
12 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.