use constants for startswith in handlers. add strip_prefix_suffix.
Just cleans up some repeated strings into module constants + a util function to do the boothook prefix cleanup (before writing).
This commit is contained in:
commit
2b64d9264c
@ -29,6 +29,7 @@ from cloudinit import util
|
|||||||
from cloudinit.settings import (PER_ALWAYS)
|
from cloudinit.settings import (PER_ALWAYS)
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
BOOTHOOK_PREFIX = "#cloud-boothook"
|
||||||
|
|
||||||
|
|
||||||
class BootHookPartHandler(handlers.Handler):
|
class BootHookPartHandler(handlers.Handler):
|
||||||
@ -41,19 +42,15 @@ class BootHookPartHandler(handlers.Handler):
|
|||||||
|
|
||||||
def list_types(self):
|
def list_types(self):
|
||||||
return [
|
return [
|
||||||
handlers.type_from_starts_with("#cloud-boothook"),
|
handlers.type_from_starts_with(BOOTHOOK_PREFIX),
|
||||||
]
|
]
|
||||||
|
|
||||||
def _write_part(self, payload, filename):
|
def _write_part(self, payload, filename):
|
||||||
filename = util.clean_filename(filename)
|
filename = util.clean_filename(filename)
|
||||||
payload = util.dos2unix(payload)
|
|
||||||
prefix = "#cloud-boothook"
|
|
||||||
start = 0
|
|
||||||
if payload.startswith(prefix):
|
|
||||||
start = len(prefix) + 1
|
|
||||||
filepath = os.path.join(self.boothook_dir, filename)
|
filepath = os.path.join(self.boothook_dir, filename)
|
||||||
contents = payload[start:]
|
contents = util.strip_prefix_suffix(util.dos2unix(payload),
|
||||||
util.write_file(filepath, contents, 0700)
|
prefix=BOOTHOOK_PREFIX)
|
||||||
|
util.write_file(filepath, contents.lstrip(), 0700)
|
||||||
return filepath
|
return filepath
|
||||||
|
|
||||||
def handle_part(self, _data, ctype, filename, # pylint: disable=W0221
|
def handle_part(self, _data, ctype, filename, # pylint: disable=W0221
|
||||||
|
@ -49,6 +49,7 @@ MERGE_HEADER = 'Merge-Type'
|
|||||||
#
|
#
|
||||||
# This gets loaded into yaml with final result {'a': 22}
|
# This gets loaded into yaml with final result {'a': 22}
|
||||||
DEF_MERGERS = mergers.string_extract_mergers('dict(replace)+list()+str()')
|
DEF_MERGERS = mergers.string_extract_mergers('dict(replace)+list()+str()')
|
||||||
|
CLOUD_PREFIX = "#cloud-config"
|
||||||
|
|
||||||
|
|
||||||
class CloudConfigPartHandler(handlers.Handler):
|
class CloudConfigPartHandler(handlers.Handler):
|
||||||
@ -60,7 +61,7 @@ class CloudConfigPartHandler(handlers.Handler):
|
|||||||
|
|
||||||
def list_types(self):
|
def list_types(self):
|
||||||
return [
|
return [
|
||||||
handlers.type_from_starts_with("#cloud-config"),
|
handlers.type_from_starts_with(CLOUD_PREFIX),
|
||||||
]
|
]
|
||||||
|
|
||||||
def _write_cloud_config(self):
|
def _write_cloud_config(self):
|
||||||
@ -78,7 +79,7 @@ class CloudConfigPartHandler(handlers.Handler):
|
|||||||
if self.cloud_buf is not None:
|
if self.cloud_buf is not None:
|
||||||
# Something was actually gathered....
|
# Something was actually gathered....
|
||||||
lines = [
|
lines = [
|
||||||
"#cloud-config",
|
CLOUD_PREFIX,
|
||||||
'',
|
'',
|
||||||
]
|
]
|
||||||
lines.extend(file_lines)
|
lines.extend(file_lines)
|
||||||
|
@ -29,6 +29,7 @@ from cloudinit import util
|
|||||||
from cloudinit.settings import (PER_ALWAYS)
|
from cloudinit.settings import (PER_ALWAYS)
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
SHELL_PREFIX = "#!"
|
||||||
|
|
||||||
|
|
||||||
class ShellScriptPartHandler(handlers.Handler):
|
class ShellScriptPartHandler(handlers.Handler):
|
||||||
@ -38,7 +39,7 @@ class ShellScriptPartHandler(handlers.Handler):
|
|||||||
|
|
||||||
def list_types(self):
|
def list_types(self):
|
||||||
return [
|
return [
|
||||||
handlers.type_from_starts_with("#!"),
|
handlers.type_from_starts_with(SHELL_PREFIX),
|
||||||
]
|
]
|
||||||
|
|
||||||
def handle_part(self, _data, ctype, filename, # pylint: disable=W0221
|
def handle_part(self, _data, ctype, filename, # pylint: disable=W0221
|
||||||
|
@ -31,6 +31,7 @@ from cloudinit import util
|
|||||||
from cloudinit.settings import (PER_INSTANCE)
|
from cloudinit.settings import (PER_INSTANCE)
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
UPSTART_PREFIX = "#upstart-job"
|
||||||
|
|
||||||
|
|
||||||
class UpstartJobPartHandler(handlers.Handler):
|
class UpstartJobPartHandler(handlers.Handler):
|
||||||
@ -40,7 +41,7 @@ class UpstartJobPartHandler(handlers.Handler):
|
|||||||
|
|
||||||
def list_types(self):
|
def list_types(self):
|
||||||
return [
|
return [
|
||||||
handlers.type_from_starts_with("#upstart-job"),
|
handlers.type_from_starts_with(UPSTART_PREFIX),
|
||||||
]
|
]
|
||||||
|
|
||||||
def handle_part(self, _data, ctype, filename, # pylint: disable=W0221
|
def handle_part(self, _data, ctype, filename, # pylint: disable=W0221
|
||||||
|
@ -1530,6 +1530,14 @@ def shellify(cmdlist, add_header=True):
|
|||||||
return content
|
return content
|
||||||
|
|
||||||
|
|
||||||
|
def strip_prefix_suffix(line, prefix=None, suffix=None):
|
||||||
|
if prefix and line.startswith(prefix):
|
||||||
|
line = line[len(prefix):]
|
||||||
|
if suffix and line.endswith(suffix):
|
||||||
|
line = line[:-len(suffix)]
|
||||||
|
return line
|
||||||
|
|
||||||
|
|
||||||
def is_container():
|
def is_container():
|
||||||
"""
|
"""
|
||||||
Checks to see if this code running in a container of some sort
|
Checks to see if this code running in a container of some sort
|
||||||
|
Loading…
x
Reference in New Issue
Block a user