-
Notifications
You must be signed in to change notification settings - Fork 167
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
Self parse user data #306
Self parse user data #306
Changes from 6 commits
795ebfd
3a19de3
46bc428
bc5676d
b3ffba5
9f0f668
0496068
e7ea56c
08f93bd
e04cf9e
63ab875
7c9143f
5cffa64
8f52dd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import copy | ||
import hashlib | ||
import logging | ||
from string import Template as StringTemplate | ||
from stacker.util import read_value_from_path | ||
|
||
from troposphere import ( | ||
Parameter, | ||
Ref, | ||
Template, | ||
Template | ||
) | ||
|
||
from ..exceptions import ( | ||
|
@@ -220,7 +222,48 @@ def resolve_variable(var_name, var_def, provided_variable, blueprint_name): | |
return value | ||
|
||
|
||
def parse_user_data(variables, raw_user_data, blueprint_name): | ||
"""Translate a userdata file to into the file contents. | ||
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. this sentence doesn't make sense |
||
|
||
It supports referencing template variables to create userdata | ||
that's supplemented with information from the data, as commonly | ||
required when creating EC2 userdata files. Automatically, encodes | ||
the data file to base64 after it is processed. | ||
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. I don't think this is true any longer, right? 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. this indentation looks off |
||
|
||
Args: | ||
raw_user_data (str): the user data with the cloud-init info | ||
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. missing some Args here. |
||
|
||
Returns: | ||
str: The parsed user data, with all the variables values and | ||
refs replaced with their resolved values. | ||
|
||
Raises: | ||
MissingVariable: Raised when a variable is in the user_data that | ||
is not given in the blueprint | ||
|
||
""" | ||
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. This appears to be over indented - I think the whole docstring block is actually. 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. This indentation is still off, should be flush with the start of the doc string. |
||
variable_values = {} | ||
|
||
for key in variables.keys(): | ||
if type(variables[key]) is CFNParameter: | ||
variable_values[key] = variables[key].to_parameter_value() | ||
else: | ||
variable_values[key] = variables[key] | ||
|
||
template = StringTemplate(raw_user_data) | ||
|
||
res = "" | ||
|
||
try: | ||
res = template.substitute(variable_values) | ||
except Exception as e: | ||
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. we should be catching the exact exception that gets raised |
||
raise MissingVariable(blueprint_name, e) | ||
|
||
return res | ||
|
||
|
||
class Blueprint(object): | ||
|
||
"""Base implementation for rendering a troposphere template. | ||
|
||
Args: | ||
|
@@ -402,6 +445,23 @@ def render_template(self): | |
version = hashlib.md5(rendered).hexdigest()[:8] | ||
return (version, rendered) | ||
|
||
def read_user_data(self, user_data_path): | ||
"""Reads and parses a user_data file. | ||
|
||
Args: | ||
user_data_path (str): | ||
path to the userdata file | ||
|
||
Returns: | ||
str: the parsed user data file | ||
|
||
""" | ||
raw_user_data = read_value_from_path(user_data_path) | ||
|
||
variables = self.get_variables() | ||
|
||
return parse_user_data(variables, raw_user_data, self.name) | ||
|
||
@property | ||
def rendered(self): | ||
if not self._rendered: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just use
import string
here, and then use Template asstring.Template
later