MCPcopy Create free account
hub / github.com/canonical/cloud-init / _do_include

Method _do_include

cloudinit/user_data.py:205–272  ·  view source on GitHub ↗
(self, content, append_msg)

Source from the content-addressed store, hash-verified

203 self._attach_launch_index(msg)
204
205 def _do_include(self, content, append_msg):
206 # Include a list of urls, one per line
207 # also support '#include <url here>'
208 # or #include-once '<url here>'
209 include_once_on = False
210 for line in content.splitlines():
211 lc_line = line.lower()
212 if lc_line.startswith("#include-once"):
213 line = line[len("#include-once") :].lstrip()
214 # Every following include will now
215 # not be refetched.... but will be
216 # re-read from a local urlcache (if it worked)
217 include_once_on = True
218 elif lc_line.startswith("#include"):
219 line = line[len("#include") :].lstrip()
220 # Disable the include once if it was on
221 # if it wasn't, then this has no effect.
222 include_once_on = False
223 if line.startswith("#"):
224 continue
225 include_url = line.strip()
226 if not include_url:
227 continue
228
229 include_once_fn = None
230 content = None
231 if include_once_on:
232 include_once_fn = self._get_include_once_filename(include_url)
233 if include_once_on and os.path.isfile(include_once_fn):
234 content = util.load_text_file(include_once_fn)
235 else:
236 try:
237 resp = read_file_or_url(
238 include_url,
239 timeout=5,
240 retries=10,
241 ssl_details=self.ssl_details,
242 )
243 if include_once_on and resp.ok():
244 util.write_file(
245 include_once_fn, resp.contents, mode=0o600
246 )
247 if resp.ok():
248 content = resp.contents
249 else:
250 error_message = (
251 "Fetching from {} resulted in"
252 " a invalid http code of {}".format(
253 include_url, resp.code
254 )
255 )
256 _handle_error(error_message)
257 except UrlError as urle:
258 message = str(urle)
259 # Older versions of requests.exceptions.HTTPError may not
260 # include the errant url. Append it for clarity in logs.
261 if include_url not in message:
262 message += " for url: {0}".format(include_url)

Callers 1

_process_msgMethod · 0.95

Calls 7

_process_msgMethod · 0.95
read_file_or_urlFunction · 0.90
convert_stringFunction · 0.85
formatMethod · 0.80
_handle_errorFunction · 0.70
okMethod · 0.45

Tested by

no test coverage detected