lorax/lorax-composer/_modules/pylorax/api/compose.html
2021-04-23 14:43:15 -07:00

1147 lines
158 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>pylorax.api.compose &mdash; Lorax 19.7.43 documentation</title>
<link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../../',
VERSION: '19.7.43',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../../../_static/jquery.js"></script>
<script type="text/javascript" src="../../../_static/underscore.js"></script>
<script type="text/javascript" src="../../../_static/doctools.js"></script>
<link rel="top" title="Lorax 19.7.43 documentation" href="../../../index.html" />
<link rel="up" title="pylorax.api" href="../api.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li><a href="../../../index.html">Lorax 19.7.43 documentation</a> &raquo;</li>
<li><a href="../../index.html" >Module code</a> &raquo;</li>
<li><a href="../../pylorax.html" >pylorax</a> &raquo;</li>
<li><a href="../api.html" accesskey="U">pylorax.api</a> &raquo;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<h1>Source code for pylorax.api.compose</h1><div class="highlight"><pre>
<span class="c"># Copyright (C) 2018-2019 Red Hat, Inc.</span>
<span class="c">#</span>
<span class="c"># This program is free software; you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation; either version 2 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># This program is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="c">#</span>
<span class="sd">&quot;&quot;&quot; Setup for composing an image</span>
<span class="sd">Adding New Output Types</span>
<span class="sd">-----------------------</span>
<span class="sd">The new output type must add a kickstart template to ./share/composer/ where the</span>
<span class="sd">name of the kickstart (without the trailing .ks) matches the entry in compose_args.</span>
<span class="sd">The kickstart should not have any url or repo entries, these will be added at build</span>
<span class="sd">time. The %packages section should be the last thing, and while it can contain mandatory</span>
<span class="sd">packages required by the output type, it should not have the trailing %end because the</span>
<span class="sd">package NEVRAs will be appended to it at build time.</span>
<span class="sd">compose_args should have a name matching the kickstart, and it should set the novirt_install</span>
<span class="sd">parameters needed to generate the desired output. Other types should be set to False.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">&quot;lorax-composer&quot;</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">glob</span> <span class="kn">import</span> <span class="n">glob</span>
<span class="kn">from</span> <span class="nn">StringIO</span> <span class="kn">import</span> <span class="n">StringIO</span>
<span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">ceil</span>
<span class="kn">import</span> <span class="nn">pytoml</span> <span class="kn">as</span> <span class="nn">toml</span>
<span class="kn">import</span> <span class="nn">shutil</span>
<span class="kn">from</span> <span class="nn">uuid</span> <span class="kn">import</span> <span class="n">uuid4</span>
<span class="kn">from</span> <span class="nn">pyanaconda.simpleconfig</span> <span class="kn">import</span> <span class="n">SimpleConfigFile</span>
<span class="c"># Use pykickstart to calculate disk image size</span>
<span class="kn">from</span> <span class="nn">pykickstart.parser</span> <span class="kn">import</span> <span class="n">KickstartParser</span>
<span class="kn">from</span> <span class="nn">pykickstart.version</span> <span class="kn">import</span> <span class="n">makeVersion</span><span class="p">,</span> <span class="n">RHEL7</span>
<span class="kn">from</span> <span class="nn">pylorax.api.projects</span> <span class="kn">import</span> <span class="n">projects_depsolve</span><span class="p">,</span> <span class="n">projects_depsolve_with_size</span><span class="p">,</span> <span class="n">dep_nevra</span>
<span class="kn">from</span> <span class="nn">pylorax.api.projects</span> <span class="kn">import</span> <span class="n">ProjectsError</span>
<span class="kn">from</span> <span class="nn">pylorax.api.recipes</span> <span class="kn">import</span> <span class="n">read_recipe_and_id</span>
<span class="kn">from</span> <span class="nn">pylorax.api.timestamp</span> <span class="kn">import</span> <span class="n">TS_CREATED</span><span class="p">,</span> <span class="n">write_timestamp</span>
<span class="kn">from</span> <span class="nn">pylorax.executils</span> <span class="kn">import</span> <span class="n">runcmd_output</span>
<span class="kn">from</span> <span class="nn">pylorax.imgutils</span> <span class="kn">import</span> <span class="n">default_image_name</span>
<span class="kn">from</span> <span class="nn">pylorax.sysutils</span> <span class="kn">import</span> <span class="n">joinpaths</span>
<div class="viewcode-block" id="test_templates"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.test_templates">[docs]</a><span class="k">def</span> <span class="nf">test_templates</span><span class="p">(</span><span class="n">yb</span><span class="p">,</span> <span class="n">share_dir</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Try depsolving each of the the templates and report any errors</span>
<span class="sd"> :param yb: yum base object</span>
<span class="sd"> :type yb: YumBase</span>
<span class="sd"> :returns: List of template types and errors</span>
<span class="sd"> :rtype: List of errors</span>
<span class="sd"> Return a list of templates and errors encountered or an empty list</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">template_errors</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">compose_type</span> <span class="ow">in</span> <span class="n">compose_types</span><span class="p">(</span><span class="n">share_dir</span><span class="p">):</span>
<span class="c"># Read the kickstart template for this type</span>
<span class="n">ks_template_path</span> <span class="o">=</span> <span class="n">joinpaths</span><span class="p">(</span><span class="n">share_dir</span><span class="p">,</span> <span class="s">&quot;composer&quot;</span><span class="p">,</span> <span class="n">compose_type</span><span class="p">)</span> <span class="o">+</span> <span class="s">&quot;.ks&quot;</span>
<span class="n">ks_template</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">ks_template_path</span><span class="p">,</span> <span class="s">&quot;r&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="c"># How much space will the packages in the default template take?</span>
<span class="n">ks_version</span> <span class="o">=</span> <span class="n">makeVersion</span><span class="p">(</span><span class="n">RHEL7</span><span class="p">)</span>
<span class="n">ks</span> <span class="o">=</span> <span class="n">KickstartParser</span><span class="p">(</span><span class="n">ks_version</span><span class="p">,</span> <span class="n">errorsAreFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">missingIncludeIsFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">ks</span><span class="o">.</span><span class="n">readKickstartFromString</span><span class="p">(</span><span class="n">ks_template</span><span class="o">+</span><span class="s">&quot;</span><span class="se">\n</span><span class="si">%e</span><span class="s">nd</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
<span class="n">pkgs</span> <span class="o">=</span> <span class="p">[(</span><span class="n">name</span><span class="p">,</span> <span class="s">&quot;*&quot;</span><span class="p">)</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">packages</span><span class="o">.</span><span class="n">packageList</span><span class="p">]</span>
<span class="n">grps</span> <span class="o">=</span> <span class="p">[</span><span class="n">grp</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">grp</span> <span class="ow">in</span> <span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">packages</span><span class="o">.</span><span class="n">groupList</span><span class="p">]</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">_</span> <span class="o">=</span> <span class="n">projects_depsolve</span><span class="p">(</span><span class="n">yb</span><span class="p">,</span> <span class="n">pkgs</span><span class="p">,</span> <span class="n">grps</span><span class="p">)</span>
<span class="k">except</span> <span class="n">ProjectsError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="n">template_errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&quot;Error depsolving </span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">compose_type</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">)))</span>
<span class="k">return</span> <span class="n">template_errors</span>
</div>
<div class="viewcode-block" id="repo_to_ks"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.repo_to_ks">[docs]</a><span class="k">def</span> <span class="nf">repo_to_ks</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">url</span><span class="o">=</span><span class="s">&quot;url&quot;</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Return a kickstart line with the correct args.</span>
<span class="sd"> Set url to &quot;baseurl&quot; if it is a repo, leave it as &quot;url&quot; for the installation url.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">cmd</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
<span class="k">if</span> <span class="n">url</span> <span class="o">==</span> <span class="s">&quot;url&quot;</span><span class="p">:</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">r</span><span class="o">.</span><span class="n">urls</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">&quot;Cannot find a base url for </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">r</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="c"># url is passed to Anaconda on the cmdline with --repo, so it cannot support a mirror</span>
<span class="c"># If a mirror is setup yum will return the list of mirrors in .urls</span>
<span class="c"># So just use the first one.</span>
<span class="n">cmd</span> <span class="o">+=</span> <span class="s">&#39;--</span><span class="si">%s</span><span class="s">=&quot;</span><span class="si">%s</span><span class="s">&quot; &#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">r</span><span class="o">.</span><span class="n">urls</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">elif</span> <span class="n">r</span><span class="o">.</span><span class="n">metalink</span><span class="p">:</span>
<span class="c"># XXX Total Hack</span>
<span class="c"># RHEL7 kickstart doesn&#39;t support metalink. If the url has &#39;metalink&#39; in it, rewrite it as &#39;mirrorlist&#39;</span>
<span class="k">if</span> <span class="s">&quot;metalink&quot;</span> <span class="ow">in</span> <span class="n">r</span><span class="o">.</span><span class="n">metalink</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;RHEL7 does not support metalink, translating to mirrorlist&quot;</span><span class="p">)</span>
<span class="n">cmd</span> <span class="o">+=</span> <span class="s">&#39;--mirrorlist=&quot;</span><span class="si">%s</span><span class="s">&quot; &#39;</span> <span class="o">%</span> <span class="n">r</span><span class="o">.</span><span class="n">metalink</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&quot;metalink&quot;</span><span class="p">,</span> <span class="s">&quot;mirrorlist&quot;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;Could not convert metalink to mirrorlist. </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">r</span><span class="o">.</span><span class="n">metalink</span><span class="p">)</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">&quot;Cannot convert metalink to mirrorlist: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">r</span><span class="o">.</span><span class="n">metalink</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">r</span><span class="o">.</span><span class="n">mirrorlist</span><span class="p">:</span>
<span class="n">cmd</span> <span class="o">+=</span> <span class="s">&#39;--mirrorlist=&quot;</span><span class="si">%s</span><span class="s">&quot; &#39;</span> <span class="o">%</span> <span class="n">r</span><span class="o">.</span><span class="n">mirrorlist</span>
<span class="k">elif</span> <span class="n">r</span><span class="o">.</span><span class="n">baseurl</span><span class="p">:</span>
<span class="n">cmd</span> <span class="o">+=</span> <span class="s">&#39;--</span><span class="si">%s</span><span class="s">=&quot;</span><span class="si">%s</span><span class="s">&quot; &#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">r</span><span class="o">.</span><span class="n">baseurl</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">&quot;Repo has no baseurl or mirror&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">r</span><span class="o">.</span><span class="n">proxy</span><span class="p">:</span>
<span class="n">cmd</span> <span class="o">+=</span> <span class="s">&#39;--proxy=&quot;</span><span class="si">%s</span><span class="s">&quot; &#39;</span> <span class="o">%</span> <span class="n">r</span><span class="o">.</span><span class="n">proxy</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">r</span><span class="o">.</span><span class="n">sslverify</span><span class="p">:</span>
<span class="n">cmd</span> <span class="o">+=</span> <span class="s">&#39;--noverifyssl&#39;</span>
<span class="k">if</span> <span class="n">r</span><span class="o">.</span><span class="n">sslcacert</span><span class="p">:</span>
<span class="n">cmd</span> <span class="o">+=</span> <span class="s">&#39; --sslcacert=&quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="n">r</span><span class="o">.</span><span class="n">sslcacert</span>
<span class="k">if</span> <span class="n">r</span><span class="o">.</span><span class="n">sslclientcert</span><span class="p">:</span>
<span class="n">cmd</span> <span class="o">+=</span> <span class="s">&#39; --sslclientcert=&quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="n">r</span><span class="o">.</span><span class="n">sslclientcert</span>
<span class="k">if</span> <span class="n">r</span><span class="o">.</span><span class="n">sslclientkey</span><span class="p">:</span>
<span class="n">cmd</span> <span class="o">+=</span> <span class="s">&#39; --sslclientkey=&quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="n">r</span><span class="o">.</span><span class="n">sslclientkey</span>
<span class="k">return</span> <span class="n">cmd</span>
</div>
<div class="viewcode-block" id="bootloader_append"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.bootloader_append">[docs]</a><span class="k">def</span> <span class="nf">bootloader_append</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">kernel_append</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Insert the kernel_append string into the --append argument</span>
<span class="sd"> :param line: The bootloader ... line</span>
<span class="sd"> :type line: str</span>
<span class="sd"> :param kernel_append: The arguments to append to the --append section</span>
<span class="sd"> :type kernel_append: str</span>
<span class="sd"> Using pykickstart to process the line is the best way to make sure it</span>
<span class="sd"> is parsed correctly, and re-assembled for inclusion into the final kickstart</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">ks_version</span> <span class="o">=</span> <span class="n">makeVersion</span><span class="p">()</span>
<span class="n">ks</span> <span class="o">=</span> <span class="n">KickstartParser</span><span class="p">(</span><span class="n">ks_version</span><span class="p">,</span> <span class="n">errorsAreFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">missingIncludeIsFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">ks</span><span class="o">.</span><span class="n">readKickstartFromString</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">if</span> <span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">bootloader</span><span class="o">.</span><span class="n">appendLine</span><span class="p">:</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">bootloader</span><span class="o">.</span><span class="n">appendLine</span> <span class="o">+=</span> <span class="s">&quot; </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">kernel_append</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">bootloader</span><span class="o">.</span><span class="n">appendLine</span> <span class="o">=</span> <span class="n">kernel_append</span>
<span class="c"># Converting back to a string includes a comment, return just the bootloader line</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">bootloader</span><span class="p">)</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="get_kernel_append"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.get_kernel_append">[docs]</a><span class="k">def</span> <span class="nf">get_kernel_append</span><span class="p">(</span><span class="n">recipe</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return the customizations.kernel append value</span>
<span class="sd"> :param recipe:</span>
<span class="sd"> :type recipe: Recipe object</span>
<span class="sd"> :returns: append value or empty string</span>
<span class="sd"> :rtype: str</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="s">&quot;customizations&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span> <span class="ow">or</span> \
<span class="s">&quot;kernel&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">]</span> <span class="ow">or</span> \
<span class="s">&quot;append&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">][</span><span class="s">&quot;kernel&quot;</span><span class="p">]:</span>
<span class="k">return</span> <span class="s">&quot;&quot;</span>
<span class="k">return</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">][</span><span class="s">&quot;kernel&quot;</span><span class="p">][</span><span class="s">&quot;append&quot;</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="timezone_cmd"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.timezone_cmd">[docs]</a><span class="k">def</span> <span class="nf">timezone_cmd</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">settings</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Update the timezone line with the settings</span>
<span class="sd"> :param line: The timezone ... line</span>
<span class="sd"> :type line: str</span>
<span class="sd"> :param settings: A dict with timezone and/or ntpservers list</span>
<span class="sd"> :type settings: dict</span>
<span class="sd"> Using pykickstart to process the line is the best way to make sure it</span>
<span class="sd"> is parsed correctly, and re-assembled for inclusion into the final kickstart</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">ks_version</span> <span class="o">=</span> <span class="n">makeVersion</span><span class="p">()</span>
<span class="n">ks</span> <span class="o">=</span> <span class="n">KickstartParser</span><span class="p">(</span><span class="n">ks_version</span><span class="p">,</span> <span class="n">errorsAreFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">missingIncludeIsFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">ks</span><span class="o">.</span><span class="n">readKickstartFromString</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">if</span> <span class="s">&quot;timezone&quot;</span> <span class="ow">in</span> <span class="n">settings</span><span class="p">:</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">timezone</span><span class="o">.</span><span class="n">timezone</span> <span class="o">=</span> <span class="n">settings</span><span class="p">[</span><span class="s">&quot;timezone&quot;</span><span class="p">]</span>
<span class="k">if</span> <span class="s">&quot;ntpservers&quot;</span> <span class="ow">in</span> <span class="n">settings</span><span class="p">:</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">timezone</span><span class="o">.</span><span class="n">ntpservers</span> <span class="o">=</span> <span class="n">settings</span><span class="p">[</span><span class="s">&quot;ntpservers&quot;</span><span class="p">]</span>
<span class="c"># Converting back to a string includes a comment, return just the timezone line</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">timezone</span><span class="p">)</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="get_timezone_settings"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.get_timezone_settings">[docs]</a><span class="k">def</span> <span class="nf">get_timezone_settings</span><span class="p">(</span><span class="n">recipe</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return the customizations.timezone dict</span>
<span class="sd"> :param recipe:</span>
<span class="sd"> :type recipe: Recipe object</span>
<span class="sd"> :returns: append value or empty string</span>
<span class="sd"> :rtype: dict</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="s">&quot;customizations&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span> <span class="ow">or</span> \
<span class="s">&quot;timezone&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">]:</span>
<span class="k">return</span> <span class="p">{}</span>
<span class="k">return</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">][</span><span class="s">&quot;timezone&quot;</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="lang_cmd"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.lang_cmd">[docs]</a><span class="k">def</span> <span class="nf">lang_cmd</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">languages</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Update the lang line with the languages</span>
<span class="sd"> :param line: The lang ... line</span>
<span class="sd"> :type line: str</span>
<span class="sd"> :param settings: The list of languages</span>
<span class="sd"> :type settings: list</span>
<span class="sd"> Using pykickstart to process the line is the best way to make sure it</span>
<span class="sd"> is parsed correctly, and re-assembled for inclusion into the final kickstart</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">ks_version</span> <span class="o">=</span> <span class="n">makeVersion</span><span class="p">()</span>
<span class="n">ks</span> <span class="o">=</span> <span class="n">KickstartParser</span><span class="p">(</span><span class="n">ks_version</span><span class="p">,</span> <span class="n">errorsAreFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">missingIncludeIsFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">ks</span><span class="o">.</span><span class="n">readKickstartFromString</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">if</span> <span class="n">languages</span><span class="p">:</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">lang</span> <span class="o">=</span> <span class="n">languages</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">languages</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">addsupport</span> <span class="o">=</span> <span class="n">languages</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
<span class="c"># Converting back to a string includes a comment, return just the lang line</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">lang</span><span class="p">)</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="get_languages"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.get_languages">[docs]</a><span class="k">def</span> <span class="nf">get_languages</span><span class="p">(</span><span class="n">recipe</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return the customizations.locale.languages list</span>
<span class="sd"> :param recipe: The recipe</span>
<span class="sd"> :type recipe: Recipe object</span>
<span class="sd"> :returns: list of language strings</span>
<span class="sd"> :rtype: list</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="s">&quot;customizations&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span> <span class="ow">or</span> \
<span class="s">&quot;locale&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">]</span> <span class="ow">or</span> \
<span class="s">&quot;languages&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">][</span><span class="s">&quot;locale&quot;</span><span class="p">]:</span>
<span class="k">return</span> <span class="p">[]</span>
<span class="k">return</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">][</span><span class="s">&quot;locale&quot;</span><span class="p">][</span><span class="s">&quot;languages&quot;</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="keyboard_cmd"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.keyboard_cmd">[docs]</a><span class="k">def</span> <span class="nf">keyboard_cmd</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">layout</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Update the keyboard line with the layout</span>
<span class="sd"> :param line: The keyboard ... line</span>
<span class="sd"> :type line: str</span>
<span class="sd"> :param settings: The keyboard layout</span>
<span class="sd"> :type settings: str</span>
<span class="sd"> Using pykickstart to process the line is the best way to make sure it</span>
<span class="sd"> is parsed correctly, and re-assembled for inclusion into the final kickstart</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">ks_version</span> <span class="o">=</span> <span class="n">makeVersion</span><span class="p">()</span>
<span class="n">ks</span> <span class="o">=</span> <span class="n">KickstartParser</span><span class="p">(</span><span class="n">ks_version</span><span class="p">,</span> <span class="n">errorsAreFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">missingIncludeIsFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">ks</span><span class="o">.</span><span class="n">readKickstartFromString</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">if</span> <span class="n">layout</span><span class="p">:</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">keyboard</span><span class="o">.</span><span class="n">keyboard</span> <span class="o">=</span> <span class="n">layout</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">keyboard</span><span class="o">.</span><span class="n">vc_keymap</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">keyboard</span><span class="o">.</span><span class="n">x_layouts</span> <span class="o">=</span> <span class="p">[]</span>
<span class="c"># Converting back to a string includes a comment, return just the keyboard line</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">keyboard</span><span class="p">)</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="get_keyboard_layout"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.get_keyboard_layout">[docs]</a><span class="k">def</span> <span class="nf">get_keyboard_layout</span><span class="p">(</span><span class="n">recipe</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return the customizations.locale.keyboard list</span>
<span class="sd"> :param recipe: The recipe</span>
<span class="sd"> :type recipe: Recipe object</span>
<span class="sd"> :returns: The keyboard layout string</span>
<span class="sd"> :rtype: str</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="s">&quot;customizations&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span> <span class="ow">or</span> \
<span class="s">&quot;locale&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">]</span> <span class="ow">or</span> \
<span class="s">&quot;keyboard&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">][</span><span class="s">&quot;locale&quot;</span><span class="p">]:</span>
<span class="k">return</span> <span class="p">[]</span>
<span class="k">return</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">][</span><span class="s">&quot;locale&quot;</span><span class="p">][</span><span class="s">&quot;keyboard&quot;</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="firewall_cmd"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.firewall_cmd">[docs]</a><span class="k">def</span> <span class="nf">firewall_cmd</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">settings</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Update the firewall line with the new ports and services</span>
<span class="sd"> :param line: The firewall ... line</span>
<span class="sd"> :type line: str</span>
<span class="sd"> :param settings: A dict with the list of services and ports to enable and disable</span>
<span class="sd"> :type settings: dict</span>
<span class="sd"> Using pykickstart to process the line is the best way to make sure it</span>
<span class="sd"> is parsed correctly, and re-assembled for inclusion into the final kickstart</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">ks_version</span> <span class="o">=</span> <span class="n">makeVersion</span><span class="p">()</span>
<span class="n">ks</span> <span class="o">=</span> <span class="n">KickstartParser</span><span class="p">(</span><span class="n">ks_version</span><span class="p">,</span> <span class="n">errorsAreFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">missingIncludeIsFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">ks</span><span class="o">.</span><span class="n">readKickstartFromString</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="c"># Do not override firewall --disabled</span>
<span class="k">if</span> <span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">firewall</span><span class="o">.</span><span class="n">enabled</span> <span class="o">!=</span> <span class="bp">False</span> <span class="ow">and</span> <span class="n">settings</span><span class="p">:</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">firewall</span><span class="o">.</span><span class="n">ports</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">settings</span><span class="p">[</span><span class="s">&quot;ports&quot;</span><span class="p">]</span> <span class="o">+</span> <span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">firewall</span><span class="o">.</span><span class="n">ports</span><span class="p">))</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">firewall</span><span class="o">.</span><span class="n">services</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">settings</span><span class="p">[</span><span class="s">&quot;enabled&quot;</span><span class="p">]</span> <span class="o">+</span> <span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">firewall</span><span class="o">.</span><span class="n">services</span><span class="p">))</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">firewall</span><span class="o">.</span><span class="n">remove_services</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">settings</span><span class="p">[</span><span class="s">&quot;disabled&quot;</span><span class="p">]</span> <span class="o">+</span> <span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">firewall</span><span class="o">.</span><span class="n">remove_services</span><span class="p">))</span>
<span class="c"># Converting back to a string includes a comment, return just the keyboard line</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">firewall</span><span class="p">)</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="get_firewall_settings"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.get_firewall_settings">[docs]</a><span class="k">def</span> <span class="nf">get_firewall_settings</span><span class="p">(</span><span class="n">recipe</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return the customizations.firewall settings</span>
<span class="sd"> :param recipe: The recipe</span>
<span class="sd"> :type recipe: Recipe object</span>
<span class="sd"> :returns: A dict of settings</span>
<span class="sd"> :rtype: dict</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">settings</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;ports&quot;</span><span class="p">:</span> <span class="p">[],</span> <span class="s">&quot;enabled&quot;</span><span class="p">:</span> <span class="p">[],</span> <span class="s">&quot;disabled&quot;</span><span class="p">:</span> <span class="p">[]}</span>
<span class="k">if</span> <span class="s">&quot;customizations&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span> <span class="ow">or</span> \
<span class="s">&quot;firewall&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">]:</span>
<span class="k">return</span> <span class="n">settings</span>
<span class="n">settings</span><span class="p">[</span><span class="s">&quot;ports&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">][</span><span class="s">&quot;firewall&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;ports&quot;</span><span class="p">,</span> <span class="p">[])</span>
<span class="k">if</span> <span class="s">&quot;services&quot;</span> <span class="ow">in</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">][</span><span class="s">&quot;firewall&quot;</span><span class="p">]:</span>
<span class="n">settings</span><span class="p">[</span><span class="s">&quot;enabled&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">][</span><span class="s">&quot;firewall&quot;</span><span class="p">][</span><span class="s">&quot;services&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;enabled&quot;</span><span class="p">,</span> <span class="p">[])</span>
<span class="n">settings</span><span class="p">[</span><span class="s">&quot;disabled&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">][</span><span class="s">&quot;firewall&quot;</span><span class="p">][</span><span class="s">&quot;services&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;disabled&quot;</span><span class="p">,</span> <span class="p">[])</span>
<span class="k">return</span> <span class="n">settings</span>
</div>
<div class="viewcode-block" id="services_cmd"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.services_cmd">[docs]</a><span class="k">def</span> <span class="nf">services_cmd</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">settings</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Update the services line with additional services to enable/disable</span>
<span class="sd"> :param line: The services ... line</span>
<span class="sd"> :type line: str</span>
<span class="sd"> :param settings: A dict with the list of services to enable and disable</span>
<span class="sd"> :type settings: dict</span>
<span class="sd"> Using pykickstart to process the line is the best way to make sure it</span>
<span class="sd"> is parsed correctly, and re-assembled for inclusion into the final kickstart</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c"># Empty services and no additional settings, return an empty string</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">line</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">settings</span><span class="p">[</span><span class="s">&quot;enabled&quot;</span><span class="p">]</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">settings</span><span class="p">[</span><span class="s">&quot;disabled&quot;</span><span class="p">]:</span>
<span class="k">return</span> <span class="s">&quot;&quot;</span>
<span class="n">ks_version</span> <span class="o">=</span> <span class="n">makeVersion</span><span class="p">()</span>
<span class="n">ks</span> <span class="o">=</span> <span class="n">KickstartParser</span><span class="p">(</span><span class="n">ks_version</span><span class="p">,</span> <span class="n">errorsAreFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">missingIncludeIsFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="c"># Allow passing in a &#39;default&#39; so that the enable/disable may be applied to it, without</span>
<span class="c"># parsing it and emitting a kickstart error message</span>
<span class="k">if</span> <span class="n">line</span> <span class="o">!=</span> <span class="s">&quot;services&quot;</span><span class="p">:</span>
<span class="n">ks</span><span class="o">.</span><span class="n">readKickstartFromString</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="c"># Add to any existing services, removing any duplicates</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">services</span><span class="o">.</span><span class="n">enabled</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">settings</span><span class="p">[</span><span class="s">&quot;enabled&quot;</span><span class="p">]</span> <span class="o">+</span> <span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">services</span><span class="o">.</span><span class="n">enabled</span><span class="p">))</span>
<span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">services</span><span class="o">.</span><span class="n">disabled</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">settings</span><span class="p">[</span><span class="s">&quot;disabled&quot;</span><span class="p">]</span> <span class="o">+</span> <span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">services</span><span class="o">.</span><span class="n">disabled</span><span class="p">))</span>
<span class="c"># Converting back to a string includes a comment, return just the keyboard line</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">services</span><span class="p">)</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="get_services"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.get_services">[docs]</a><span class="k">def</span> <span class="nf">get_services</span><span class="p">(</span><span class="n">recipe</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return the customizations.services settings</span>
<span class="sd"> :param recipe: The recipe</span>
<span class="sd"> :type recipe: Recipe object</span>
<span class="sd"> :returns: A dict of settings</span>
<span class="sd"> :rtype: dict</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">settings</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;enabled&quot;</span><span class="p">:</span> <span class="p">[],</span> <span class="s">&quot;disabled&quot;</span><span class="p">:</span> <span class="p">[]}</span>
<span class="k">if</span> <span class="s">&quot;customizations&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span> <span class="ow">or</span> \
<span class="s">&quot;services&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">]:</span>
<span class="k">return</span> <span class="n">settings</span>
<span class="n">settings</span><span class="p">[</span><span class="s">&quot;enabled&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">][</span><span class="s">&quot;services&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;enabled&quot;</span><span class="p">,</span> <span class="p">[]))</span>
<span class="n">settings</span><span class="p">[</span><span class="s">&quot;disabled&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">][</span><span class="s">&quot;services&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;disabled&quot;</span><span class="p">,</span> <span class="p">[]))</span>
<span class="k">return</span> <span class="n">settings</span>
</div>
<div class="viewcode-block" id="get_default_services"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.get_default_services">[docs]</a><span class="k">def</span> <span class="nf">get_default_services</span><span class="p">(</span><span class="n">recipe</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Get the default string for services, based on recipe</span>
<span class="sd"> :param recipe: The recipe</span>
<span class="sd"> :type recipe: Recipe object</span>
<span class="sd"> :returns: string with &quot;services&quot; or &quot;&quot;</span>
<span class="sd"> :rtype: str</span>
<span class="sd"> When no services have been selected we don&#39;t need to add anything to the kickstart</span>
<span class="sd"> so return an empty string. Otherwise return &quot;services&quot; which will be updated with</span>
<span class="sd"> the settings.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">services</span> <span class="o">=</span> <span class="n">get_services</span><span class="p">(</span><span class="n">recipe</span><span class="p">)</span>
<span class="k">if</span> <span class="n">services</span><span class="p">[</span><span class="s">&quot;enabled&quot;</span><span class="p">]</span> <span class="ow">or</span> <span class="n">services</span><span class="p">[</span><span class="s">&quot;disabled&quot;</span><span class="p">]:</span>
<span class="k">return</span> <span class="s">&quot;services&quot;</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="s">&quot;&quot;</span>
</div>
<div class="viewcode-block" id="customize_ks_template"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.customize_ks_template">[docs]</a><span class="k">def</span> <span class="nf">customize_ks_template</span><span class="p">(</span><span class="n">ks_template</span><span class="p">,</span> <span class="n">recipe</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Customize the kickstart template and return it</span>
<span class="sd"> :param ks_template: The kickstart template</span>
<span class="sd"> :type ks_template: str</span>
<span class="sd"> :param recipe:</span>
<span class="sd"> :type recipe: Recipe object</span>
<span class="sd"> Apply customizations to existing template commands, or add defaults for ones that are</span>
<span class="sd"> missing and required.</span>
<span class="sd"> Apply customizations.kernel.append to the bootloader argument in the template.</span>
<span class="sd"> Add bootloader line if it is missing.</span>
<span class="sd"> Add default timezone if needed. It does NOT replace an existing timezone entry</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c"># Commands to be modified [NEW-COMMAND-FUNC, NEW-VALUE, DEFAULT, REPLACE]</span>
<span class="c"># The function is called with a kickstart command string and the value to replace</span>
<span class="c"># The value is specific to the command, and is understood by the function</span>
<span class="c"># The default is a complete kickstart command string, suitable for writing to the template</span>
<span class="c"># If REPLACE is False it will not change an existing entry only add a missing one</span>
<span class="n">commands</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;bootloader&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">bootloader_append</span><span class="p">,</span>
<span class="n">get_kernel_append</span><span class="p">(</span><span class="n">recipe</span><span class="p">),</span>
<span class="s">&#39;bootloader --location=none&#39;</span><span class="p">,</span> <span class="bp">True</span><span class="p">],</span>
<span class="s">&quot;timezone&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">timezone_cmd</span><span class="p">,</span>
<span class="n">get_timezone_settings</span><span class="p">(</span><span class="n">recipe</span><span class="p">),</span>
<span class="s">&#39;timezone UTC&#39;</span><span class="p">,</span> <span class="bp">False</span><span class="p">],</span>
<span class="s">&quot;lang&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">lang_cmd</span><span class="p">,</span>
<span class="n">get_languages</span><span class="p">(</span><span class="n">recipe</span><span class="p">),</span>
<span class="s">&#39;lang en_US.UTF-8&#39;</span><span class="p">,</span> <span class="bp">True</span><span class="p">],</span>
<span class="s">&quot;keyboard&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">keyboard_cmd</span><span class="p">,</span>
<span class="n">get_keyboard_layout</span><span class="p">(</span><span class="n">recipe</span><span class="p">),</span>
<span class="s">&#39;keyboard --xlayouts us --vckeymap us&#39;</span><span class="p">,</span> <span class="bp">True</span><span class="p">],</span>
<span class="s">&quot;firewall&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">firewall_cmd</span><span class="p">,</span>
<span class="n">get_firewall_settings</span><span class="p">(</span><span class="n">recipe</span><span class="p">),</span>
<span class="s">&#39;firewall --enabled&#39;</span><span class="p">,</span> <span class="bp">True</span><span class="p">],</span>
<span class="s">&quot;services&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">services_cmd</span><span class="p">,</span>
<span class="n">get_services</span><span class="p">(</span><span class="n">recipe</span><span class="p">),</span>
<span class="n">get_default_services</span><span class="p">(</span><span class="n">recipe</span><span class="p">),</span> <span class="bp">True</span><span class="p">]</span>
<span class="p">}</span>
<span class="n">found</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">output</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">()</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">ks_template</span><span class="o">.</span><span class="n">splitlines</span><span class="p">():</span>
<span class="k">for</span> <span class="n">cmd</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">commands</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
<span class="p">(</span><span class="n">new_command</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">default</span><span class="p">,</span> <span class="n">replace</span><span class="p">)</span> <span class="o">=</span> <span class="n">commands</span><span class="p">[</span><span class="n">cmd</span><span class="p">]</span>
<span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">cmd</span><span class="p">):</span>
<span class="n">found</span><span class="p">[</span><span class="n">cmd</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span>
<span class="k">if</span> <span class="n">value</span> <span class="ow">and</span> <span class="n">replace</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Replacing </span><span class="si">%s</span><span class="s"> with </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">cmd</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">new_command</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">value</span><span class="p">),</span> <span class="nb">file</span><span class="o">=</span><span class="n">output</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Skipping </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">cmd</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="nb">file</span><span class="o">=</span><span class="n">output</span><span class="p">)</span>
<span class="k">break</span>
<span class="k">else</span><span class="p">:</span>
<span class="c"># No matches, write the line as-is</span>
<span class="k">print</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="nb">file</span><span class="o">=</span><span class="n">output</span><span class="p">)</span>
<span class="c"># Write out defaults for the ones not found</span>
<span class="c"># These must go FIRST because the template still needs to have the packages added</span>
<span class="n">defaults</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">()</span>
<span class="k">for</span> <span class="n">cmd</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">commands</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
<span class="k">if</span> <span class="n">cmd</span> <span class="ow">in</span> <span class="n">found</span><span class="p">:</span>
<span class="k">continue</span>
<span class="p">(</span><span class="n">new_command</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">default</span><span class="p">,</span> <span class="n">_</span><span class="p">)</span> <span class="o">=</span> <span class="n">commands</span><span class="p">[</span><span class="n">cmd</span><span class="p">]</span>
<span class="k">if</span> <span class="n">value</span> <span class="ow">and</span> <span class="n">default</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Setting </span><span class="si">%s</span><span class="s"> to use </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">cmd</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">new_command</span><span class="p">(</span><span class="n">default</span><span class="p">,</span> <span class="n">value</span><span class="p">),</span> <span class="nb">file</span><span class="o">=</span><span class="n">defaults</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">default</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Setting </span><span class="si">%s</span><span class="s"> to </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">cmd</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">default</span><span class="p">,</span> <span class="nb">file</span><span class="o">=</span><span class="n">defaults</span><span class="p">)</span>
<span class="k">return</span> <span class="n">defaults</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span> <span class="o">+</span> <span class="n">output</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="write_ks_root"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.write_ks_root">[docs]</a><span class="k">def</span> <span class="nf">write_ks_root</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">user</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Write kickstart root password and sshkey entry</span>
<span class="sd"> :param f: kickstart file object</span>
<span class="sd"> :type f: open file object</span>
<span class="sd"> :param user: A blueprint user dictionary</span>
<span class="sd"> :type user: dict</span>
<span class="sd"> If the entry contains a ssh key, use sshkey to write it</span>
<span class="sd"> If it contains password, use rootpw to set it</span>
<span class="sd"> root cannot be used with the user command. So only key and password are supported</span>
<span class="sd"> for root.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c"># ssh key uses the sshkey kickstart command</span>
<span class="k">if</span> <span class="s">&quot;key&quot;</span> <span class="ow">in</span> <span class="n">user</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;sshkey --user </span><span class="si">%s</span><span class="s"> &quot;</span><span class="si">%s</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">user</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">],</span> <span class="n">user</span><span class="p">[</span><span class="s">&quot;key&quot;</span><span class="p">]))</span>
<span class="k">if</span> <span class="s">&quot;password&quot;</span> <span class="ow">in</span> <span class="n">user</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="n">user</span><span class="p">[</span><span class="s">&quot;password&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">prefix</span><span class="p">)</span> <span class="k">for</span> <span class="n">prefix</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&quot;$2b$&quot;</span><span class="p">,</span> <span class="s">&quot;$6$&quot;</span><span class="p">,</span> <span class="s">&quot;$5$&quot;</span><span class="p">]):</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Detected pre-crypted password&quot;</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;rootpw --iscrypted &quot;</span><span class="si">%s</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">user</span><span class="p">[</span><span class="s">&quot;password&quot;</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Detected plaintext password&quot;</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;rootpw --plaintext &quot;</span><span class="si">%s</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">user</span><span class="p">[</span><span class="s">&quot;password&quot;</span><span class="p">])</span>
</div>
<div class="viewcode-block" id="write_ks_user"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.write_ks_user">[docs]</a><span class="k">def</span> <span class="nf">write_ks_user</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">user</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Write kickstart user and sshkey entry</span>
<span class="sd"> :param f: kickstart file object</span>
<span class="sd"> :type f: open file object</span>
<span class="sd"> :param user: A blueprint user dictionary</span>
<span class="sd"> :type user: dict</span>
<span class="sd"> If the entry contains a ssh key, use sshkey to write it</span>
<span class="sd"> All of the user fields are optional, except name, write out a kickstart user entry</span>
<span class="sd"> with whatever options are relevant.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c"># ssh key uses the sshkey kickstart command</span>
<span class="k">if</span> <span class="s">&quot;key&quot;</span> <span class="ow">in</span> <span class="n">user</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;sshkey --user </span><span class="si">%s</span><span class="s"> &quot;</span><span class="si">%s</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">user</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">],</span> <span class="n">user</span><span class="p">[</span><span class="s">&quot;key&quot;</span><span class="p">]))</span>
<span class="c"># Write out the user kickstart command, much of it is optional</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;user --name </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">user</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">])</span>
<span class="k">if</span> <span class="s">&quot;home&quot;</span> <span class="ow">in</span> <span class="n">user</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot; --homedir </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">user</span><span class="p">[</span><span class="s">&quot;home&quot;</span><span class="p">])</span>
<span class="k">if</span> <span class="s">&quot;password&quot;</span> <span class="ow">in</span> <span class="n">user</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="n">user</span><span class="p">[</span><span class="s">&quot;password&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">prefix</span><span class="p">)</span> <span class="k">for</span> <span class="n">prefix</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&quot;$2b$&quot;</span><span class="p">,</span> <span class="s">&quot;$6$&quot;</span><span class="p">,</span> <span class="s">&quot;$5$&quot;</span><span class="p">]):</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Detected pre-crypted password&quot;</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot; --iscrypted&quot;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Detected plaintext password&quot;</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot; --plaintext&quot;</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot; --password </span><span class="se">\&quot;</span><span class="si">%s</span><span class="se">\&quot;</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">user</span><span class="p">[</span><span class="s">&quot;password&quot;</span><span class="p">])</span>
<span class="k">if</span> <span class="s">&quot;shell&quot;</span> <span class="ow">in</span> <span class="n">user</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot; --shell </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">user</span><span class="p">[</span><span class="s">&quot;shell&quot;</span><span class="p">])</span>
<span class="k">if</span> <span class="s">&quot;uid&quot;</span> <span class="ow">in</span> <span class="n">user</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot; --uid </span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="nb">int</span><span class="p">(</span><span class="n">user</span><span class="p">[</span><span class="s">&quot;uid&quot;</span><span class="p">]))</span>
<span class="k">if</span> <span class="s">&quot;gid&quot;</span> <span class="ow">in</span> <span class="n">user</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot; --gid </span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="nb">int</span><span class="p">(</span><span class="n">user</span><span class="p">[</span><span class="s">&quot;gid&quot;</span><span class="p">]))</span>
<span class="k">if</span> <span class="s">&quot;description&quot;</span> <span class="ow">in</span> <span class="n">user</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot; --gecos </span><span class="se">\&quot;</span><span class="si">%s</span><span class="se">\&quot;</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">user</span><span class="p">[</span><span class="s">&quot;description&quot;</span><span class="p">])</span>
<span class="k">if</span> <span class="s">&quot;groups&quot;</span> <span class="ow">in</span> <span class="n">user</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot; --groups </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="s">&quot;,&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">user</span><span class="p">[</span><span class="s">&quot;groups&quot;</span><span class="p">]))</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="write_ks_group"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.write_ks_group">[docs]</a><span class="k">def</span> <span class="nf">write_ks_group</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">group</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Write kickstart group entry</span>
<span class="sd"> :param f: kickstart file object</span>
<span class="sd"> :type f: open file object</span>
<span class="sd"> :param group: A blueprint group dictionary</span>
<span class="sd"> :type user: dict</span>
<span class="sd"> gid is optional</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="s">&quot;name&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">group</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">&quot;group entry requires a name&quot;</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;group --name </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">group</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">])</span>
<span class="k">if</span> <span class="s">&quot;gid&quot;</span> <span class="ow">in</span> <span class="n">group</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot; --gid </span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="nb">int</span><span class="p">(</span><span class="n">group</span><span class="p">[</span><span class="s">&quot;gid&quot;</span><span class="p">]))</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="add_customizations"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.add_customizations">[docs]</a><span class="k">def</span> <span class="nf">add_customizations</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">recipe</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Add customizations to the kickstart file</span>
<span class="sd"> :param f: kickstart file object</span>
<span class="sd"> :type f: open file object</span>
<span class="sd"> :param recipe:</span>
<span class="sd"> :type recipe: Recipe object</span>
<span class="sd"> :returns: None</span>
<span class="sd"> :raises: RuntimeError if there was a problem writing to the kickstart</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="s">&quot;customizations&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">recipe</span><span class="p">:</span>
<span class="k">return</span>
<span class="n">customizations</span> <span class="o">=</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;customizations&quot;</span><span class="p">]</span>
<span class="c"># allow customizations to be incorrectly specified as [[customizations]] instead of [customizations]</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">customizations</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
<span class="n">customizations</span> <span class="o">=</span> <span class="n">customizations</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">if</span> <span class="s">&quot;hostname&quot;</span> <span class="ow">in</span> <span class="n">customizations</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;network --hostname=</span><span class="si">%s</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">customizations</span><span class="p">[</span><span class="s">&quot;hostname&quot;</span><span class="p">])</span>
<span class="c"># TODO - remove this, should use user section to define this</span>
<span class="k">if</span> <span class="s">&quot;sshkey&quot;</span> <span class="ow">in</span> <span class="n">customizations</span><span class="p">:</span>
<span class="c"># This is a list of entries</span>
<span class="k">for</span> <span class="n">sshkey</span> <span class="ow">in</span> <span class="n">customizations</span><span class="p">[</span><span class="s">&quot;sshkey&quot;</span><span class="p">]:</span>
<span class="k">if</span> <span class="s">&quot;user&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">sshkey</span> <span class="ow">or</span> <span class="s">&quot;key&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">sshkey</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s"> is incorrect, skipping&quot;</span><span class="p">,</span> <span class="n">sshkey</span><span class="p">)</span>
<span class="k">continue</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;sshkey --user </span><span class="si">%s</span><span class="s"> &quot;</span><span class="si">%s</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">sshkey</span><span class="p">[</span><span class="s">&quot;user&quot;</span><span class="p">],</span> <span class="n">sshkey</span><span class="p">[</span><span class="s">&quot;key&quot;</span><span class="p">]))</span>
<span class="c"># Creating a user also creates a group. Make a list of the names for later</span>
<span class="n">user_groups</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">if</span> <span class="s">&quot;user&quot;</span> <span class="ow">in</span> <span class="n">customizations</span><span class="p">:</span>
<span class="c"># only name is required, everything else is optional</span>
<span class="k">for</span> <span class="n">user</span> <span class="ow">in</span> <span class="n">customizations</span><span class="p">[</span><span class="s">&quot;user&quot;</span><span class="p">]:</span>
<span class="k">if</span> <span class="s">&quot;name&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">user</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">&quot;user entry requires a name&quot;</span><span class="p">)</span>
<span class="c"># root is special, cannot use normal user command for it</span>
<span class="k">if</span> <span class="n">user</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;root&quot;</span><span class="p">:</span>
<span class="n">write_ks_root</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">user</span><span class="p">)</span>
<span class="k">continue</span>
<span class="n">write_ks_user</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">user</span><span class="p">)</span>
<span class="n">user_groups</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">user</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">])</span>
<span class="k">if</span> <span class="s">&quot;group&quot;</span> <span class="ow">in</span> <span class="n">customizations</span><span class="p">:</span>
<span class="k">for</span> <span class="n">group</span> <span class="ow">in</span> <span class="n">customizations</span><span class="p">[</span><span class="s">&quot;group&quot;</span><span class="p">]:</span>
<span class="k">if</span> <span class="n">group</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">user_groups</span><span class="p">:</span>
<span class="n">write_ks_group</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">group</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&quot;Skipping group </span><span class="si">%s</span><span class="s">, already created by user&quot;</span><span class="p">,</span> <span class="n">group</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">])</span>
</div>
<div class="viewcode-block" id="get_md_size"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.get_md_size">[docs]</a><span class="k">def</span> <span class="nf">get_md_size</span><span class="p">(</span><span class="n">yum_path</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Estimate the amount of space needed by anaconda</span>
<span class="sd"> Anaconda doesn&#39;t download the filelists or &#39;other&#39; metadata, which can add</span>
<span class="sd"> up to a significant difference, so exclude those from the calculation.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">du_output</span> <span class="o">=</span> <span class="n">runcmd_output</span><span class="p">([</span><span class="s">&quot;/usr/bin/du&quot;</span><span class="p">,</span>
<span class="s">&quot;--exclude&quot;</span><span class="p">,</span> <span class="s">&quot;*other*&quot;</span><span class="p">,</span>
<span class="s">&quot;--exclude&quot;</span><span class="p">,</span> <span class="s">&quot;*filelists*&quot;</span><span class="p">,</span>
<span class="s">&quot;-sb&quot;</span><span class="p">,</span>
<span class="n">yum_path</span><span class="p">])</span>
<span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">du_output</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">IndexError</span><span class="p">)</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;Problem calculating metadata size from &#39;</span><span class="si">%s</span><span class="s">&#39;: </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">du_output</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
<span class="k">return</span> <span class="mi">0</span>
</div>
<div class="viewcode-block" id="start_build"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.start_build">[docs]</a><span class="k">def</span> <span class="nf">start_build</span><span class="p">(</span><span class="n">cfg</span><span class="p">,</span> <span class="n">yumlock</span><span class="p">,</span> <span class="n">gitlock</span><span class="p">,</span> <span class="n">branch</span><span class="p">,</span> <span class="n">recipe_name</span><span class="p">,</span> <span class="n">compose_type</span><span class="p">,</span> <span class="n">test_mode</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Start the build</span>
<span class="sd"> :param cfg: Configuration object</span>
<span class="sd"> :type cfg: ComposerConfig</span>
<span class="sd"> :param yumlock: Lock and YumBase for depsolving</span>
<span class="sd"> :type yumlock: YumLock</span>
<span class="sd"> :param recipe: The recipe to build</span>
<span class="sd"> :type recipe: str</span>
<span class="sd"> :param compose_type: The type of output to create from the recipe</span>
<span class="sd"> :type compose_type: str</span>
<span class="sd"> :returns: Unique ID for the build that can be used to track its status</span>
<span class="sd"> :rtype: str</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">share_dir</span> <span class="o">=</span> <span class="n">cfg</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;composer&quot;</span><span class="p">,</span> <span class="s">&quot;share_dir&quot;</span><span class="p">)</span>
<span class="n">lib_dir</span> <span class="o">=</span> <span class="n">cfg</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;composer&quot;</span><span class="p">,</span> <span class="s">&quot;lib_dir&quot;</span><span class="p">)</span>
<span class="c"># Make sure compose_type is valid</span>
<span class="k">if</span> <span class="n">compose_type</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">compose_types</span><span class="p">(</span><span class="n">share_dir</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">&quot;Invalid compose type (</span><span class="si">%s</span><span class="s">), must be one of </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">compose_type</span><span class="p">,</span> <span class="n">compose_types</span><span class="p">(</span><span class="n">share_dir</span><span class="p">)))</span>
<span class="k">with</span> <span class="n">gitlock</span><span class="o">.</span><span class="n">lock</span><span class="p">:</span>
<span class="p">(</span><span class="n">commit_id</span><span class="p">,</span> <span class="n">recipe</span><span class="p">)</span> <span class="o">=</span> <span class="n">read_recipe_and_id</span><span class="p">(</span><span class="n">gitlock</span><span class="o">.</span><span class="n">repo</span><span class="p">,</span> <span class="n">branch</span><span class="p">,</span> <span class="n">recipe_name</span><span class="p">)</span>
<span class="c"># Combine modules and packages and depsolve the list</span>
<span class="n">module_nver</span> <span class="o">=</span> <span class="n">recipe</span><span class="o">.</span><span class="n">module_nver</span>
<span class="n">package_nver</span> <span class="o">=</span> <span class="n">recipe</span><span class="o">.</span><span class="n">package_nver</span>
<span class="n">projects</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">module_nver</span><span class="o">+</span><span class="n">package_nver</span><span class="p">),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">p</span><span class="p">:</span> <span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
<span class="n">deps</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">try</span><span class="p">:</span>
<span class="c"># This can possibly update repodata and reset the YumBase object.</span>
<span class="k">with</span> <span class="n">yumlock</span><span class="o">.</span><span class="n">lock_check</span><span class="p">:</span>
<span class="p">(</span><span class="n">installed_size</span><span class="p">,</span> <span class="n">anaconda_size</span><span class="p">,</span> <span class="n">deps</span><span class="p">)</span> <span class="o">=</span> <span class="n">projects_depsolve_with_size</span><span class="p">(</span><span class="n">yumlock</span><span class="o">.</span><span class="n">yb</span><span class="p">,</span> <span class="n">projects</span><span class="p">,</span> <span class="n">recipe</span><span class="o">.</span><span class="n">group_names</span><span class="p">,</span> <span class="n">with_core</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="k">except</span> <span class="n">ProjectsError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;start_build depsolve: </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">&quot;Problem depsolving </span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">recipe</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">],</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">)))</span>
<span class="c"># Read the kickstart template for this type</span>
<span class="n">ks_template_path</span> <span class="o">=</span> <span class="n">joinpaths</span><span class="p">(</span><span class="n">share_dir</span><span class="p">,</span> <span class="s">&quot;composer&quot;</span><span class="p">,</span> <span class="n">compose_type</span><span class="p">)</span> <span class="o">+</span> <span class="s">&quot;.ks&quot;</span>
<span class="n">ks_template</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">ks_template_path</span><span class="p">,</span> <span class="s">&quot;r&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="c"># How much space will the packages in the selected template take?</span>
<span class="n">ks_version</span> <span class="o">=</span> <span class="n">makeVersion</span><span class="p">(</span><span class="n">RHEL7</span><span class="p">)</span>
<span class="n">ks</span> <span class="o">=</span> <span class="n">KickstartParser</span><span class="p">(</span><span class="n">ks_version</span><span class="p">,</span> <span class="n">errorsAreFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">missingIncludeIsFatal</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">ks</span><span class="o">.</span><span class="n">readKickstartFromString</span><span class="p">(</span><span class="n">ks_template</span><span class="o">+</span><span class="s">&quot;</span><span class="se">\n</span><span class="si">%e</span><span class="s">nd</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
<span class="n">pkgs</span> <span class="o">=</span> <span class="p">[(</span><span class="n">name</span><span class="p">,</span> <span class="s">&quot;*&quot;</span><span class="p">)</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">packages</span><span class="o">.</span><span class="n">packageList</span><span class="p">]</span>
<span class="n">grps</span> <span class="o">=</span> <span class="p">[</span><span class="n">grp</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">grp</span> <span class="ow">in</span> <span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">packages</span><span class="o">.</span><span class="n">groupList</span><span class="p">]</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">with</span> <span class="n">yumlock</span><span class="o">.</span><span class="n">lock</span><span class="p">:</span>
<span class="p">(</span><span class="n">template_size</span><span class="p">,</span> <span class="n">anaconda_tmpl_size</span><span class="p">,</span> <span class="n">_</span><span class="p">)</span> <span class="o">=</span> <span class="n">projects_depsolve_with_size</span><span class="p">(</span><span class="n">yumlock</span><span class="o">.</span><span class="n">yb</span><span class="p">,</span> <span class="n">pkgs</span><span class="p">,</span> <span class="n">grps</span><span class="p">,</span> <span class="n">with_core</span><span class="o">=</span><span class="ow">not</span> <span class="n">ks</span><span class="o">.</span><span class="n">handler</span><span class="o">.</span><span class="n">packages</span><span class="o">.</span><span class="n">nocore</span><span class="p">)</span>
<span class="k">except</span> <span class="n">ProjectsError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;start_build depsolve: </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">&quot;Problem depsolving </span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">recipe</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">],</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">)))</span>
<span class="c"># Anaconda also stores the metadata on the disk once it is partitioned, try to take this into account by</span>
<span class="c"># adding the size of the lorax-composer metadata storage.</span>
<span class="k">with</span> <span class="n">yumlock</span><span class="o">.</span><span class="n">lock</span><span class="p">:</span>
<span class="n">metadata_size</span> <span class="o">=</span> <span class="n">get_md_size</span><span class="p">(</span><span class="n">yumlock</span><span class="o">.</span><span class="n">yb</span><span class="o">.</span><span class="n">conf</span><span class="o">.</span><span class="n">installroot</span><span class="p">)</span>
<span class="c"># Anaconda estimates size differently, only taking into account installed size and adding 35%</span>
<span class="c"># But we must make sure that our actual disk size is at least as big as the anaconda size, otherwise the install will fail</span>
<span class="n">anaconda_minimum</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="n">anaconda_size</span><span class="o">+</span><span class="n">anaconda_tmpl_size</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.35</span><span class="p">)</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;anaconda_size = </span><span class="si">%d</span><span class="s">, anaconda_template_size=</span><span class="si">%d</span><span class="s">, anaconda_minimum=</span><span class="si">%d</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">anaconda_size</span><span class="p">,</span> <span class="n">anaconda_tmpl_size</span><span class="p">,</span> <span class="n">anaconda_minimum</span><span class="p">)</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;installed_size = </span><span class="si">%d</span><span class="s">, template_size=</span><span class="si">%d</span><span class="s">, metadata_size=</span><span class="si">%d</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">installed_size</span><span class="p">,</span> <span class="n">template_size</span><span class="p">,</span> <span class="n">metadata_size</span><span class="p">)</span>
<span class="c"># Add 10% to the composer estimate</span>
<span class="n">installed_size</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="n">installed_size</span><span class="o">+</span><span class="n">template_size</span><span class="o">+</span><span class="n">metadata_size</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.10</span><span class="p">)</span>
<span class="c"># Select the largest size for the partition</span>
<span class="n">partition_size</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="mi">1024</span><span class="o">**</span><span class="mi">3</span><span class="p">,</span> <span class="n">anaconda_minimum</span><span class="p">,</span> <span class="n">installed_size</span><span class="p">)</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;/ partition size = </span><span class="si">%d</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">partition_size</span><span class="p">)</span>
<span class="c"># Create the results directory</span>
<span class="n">build_id</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">uuid4</span><span class="p">())</span>
<span class="n">results_dir</span> <span class="o">=</span> <span class="n">joinpaths</span><span class="p">(</span><span class="n">lib_dir</span><span class="p">,</span> <span class="s">&quot;results&quot;</span><span class="p">,</span> <span class="n">build_id</span><span class="p">)</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">results_dir</span><span class="p">)</span>
<span class="c"># Write the recipe commit hash</span>
<span class="n">commit_path</span> <span class="o">=</span> <span class="n">joinpaths</span><span class="p">(</span><span class="n">results_dir</span><span class="p">,</span> <span class="s">&quot;COMMIT&quot;</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">commit_path</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">commit_id</span><span class="p">)</span>
<span class="c"># Write the original recipe</span>
<span class="n">recipe_path</span> <span class="o">=</span> <span class="n">joinpaths</span><span class="p">(</span><span class="n">results_dir</span><span class="p">,</span> <span class="s">&quot;blueprint.toml&quot;</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">recipe_path</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">recipe</span><span class="o">.</span><span class="n">toml</span><span class="p">())</span>
<span class="c"># Write the frozen recipe</span>
<span class="n">frozen_recipe</span> <span class="o">=</span> <span class="n">recipe</span><span class="o">.</span><span class="n">freeze</span><span class="p">(</span><span class="n">deps</span><span class="p">)</span>
<span class="n">recipe_path</span> <span class="o">=</span> <span class="n">joinpaths</span><span class="p">(</span><span class="n">results_dir</span><span class="p">,</span> <span class="s">&quot;frozen.toml&quot;</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">recipe_path</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">frozen_recipe</span><span class="o">.</span><span class="n">toml</span><span class="p">())</span>
<span class="c"># Write out the dependencies to the results dir</span>
<span class="n">deps_path</span> <span class="o">=</span> <span class="n">joinpaths</span><span class="p">(</span><span class="n">results_dir</span><span class="p">,</span> <span class="s">&quot;deps.toml&quot;</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">deps_path</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">toml</span><span class="o">.</span><span class="n">dumps</span><span class="p">({</span><span class="s">&quot;packages&quot;</span><span class="p">:</span><span class="n">deps</span><span class="p">})</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&quot;UTF-8&quot;</span><span class="p">))</span>
<span class="c"># Save a copy of the original kickstart</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">ks_template_path</span><span class="p">,</span> <span class="n">results_dir</span><span class="p">)</span>
<span class="k">with</span> <span class="n">yumlock</span><span class="o">.</span><span class="n">lock</span><span class="p">:</span>
<span class="n">repos</span> <span class="o">=</span> <span class="n">yumlock</span><span class="o">.</span><span class="n">yb</span><span class="o">.</span><span class="n">repos</span><span class="o">.</span><span class="n">listEnabled</span><span class="p">()</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">repos</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">&quot;No enabled repos, canceling build.&quot;</span><span class="p">)</span>
<span class="c"># Create the final kickstart with repos and package list</span>
<span class="n">ks_path</span> <span class="o">=</span> <span class="n">joinpaths</span><span class="p">(</span><span class="n">results_dir</span><span class="p">,</span> <span class="s">&quot;final-kickstart.ks&quot;</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">ks_path</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">ks_url</span> <span class="o">=</span> <span class="n">repo_to_ks</span><span class="p">(</span><span class="n">repos</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s">&quot;url&quot;</span><span class="p">)</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;url = </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">ks_url</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;url </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">ks_url</span><span class="p">)</span>
<span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">r</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">repos</span><span class="p">[</span><span class="mi">1</span><span class="p">:]):</span>
<span class="n">ks_repo</span> <span class="o">=</span> <span class="n">repo_to_ks</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="s">&quot;baseurl&quot;</span><span class="p">)</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;repo composer-</span><span class="si">%s</span><span class="s"> = </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">idx</span><span class="p">,</span> <span class="n">ks_repo</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;repo --name=&quot;composer-</span><span class="si">%s</span><span class="s">&quot; </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">idx</span><span class="p">,</span> <span class="n">ks_repo</span><span class="p">))</span>
<span class="c"># Setup the disk for booting</span>
<span class="c"># TODO Add GPT and UEFI boot support</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;clearpart --all</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>
<span class="c"># Write the root partition and it&#39;s size in MB (rounded up)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;part / --size=</span><span class="si">%d</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">ceil</span><span class="p">(</span><span class="n">installed_size</span> <span class="o">/</span> <span class="mi">1024</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
<span class="c"># Some customizations modify the template before writing it</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">customize_ks_template</span><span class="p">(</span><span class="n">ks_template</span><span class="p">,</span> <span class="n">recipe</span><span class="p">))</span>
<span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">deps</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">dep_nevra</span><span class="p">(</span><span class="n">d</span><span class="p">)</span><span class="o">+</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%e</span><span class="s">nd</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
<span class="c"># Other customizations can be appended to the kickstart</span>
<span class="n">add_customizations</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">recipe</span><span class="p">)</span>
<span class="c"># Setup the config to pass to novirt_install</span>
<span class="n">log_dir</span> <span class="o">=</span> <span class="n">joinpaths</span><span class="p">(</span><span class="n">results_dir</span><span class="p">,</span> <span class="s">&quot;logs/&quot;</span><span class="p">)</span>
<span class="n">cfg_args</span> <span class="o">=</span> <span class="n">compose_args</span><span class="p">(</span><span class="n">compose_type</span><span class="p">)</span>
<span class="c"># Get the title, project, and release version from the host</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s">&quot;/etc/os-release&quot;</span><span class="p">):</span>
<span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;/etc/os-release is missing, cannot determine product or release version&quot;</span><span class="p">)</span>
<span class="n">os_release</span> <span class="o">=</span> <span class="n">SimpleConfigFile</span><span class="p">(</span><span class="s">&quot;/etc/os-release&quot;</span><span class="p">)</span>
<span class="n">os_release</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;os_release = </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">os_release</span><span class="p">)</span>
<span class="n">cfg_args</span><span class="p">[</span><span class="s">&quot;title&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">os_release</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;PRETTY_NAME&quot;</span><span class="p">)</span>
<span class="n">cfg_args</span><span class="p">[</span><span class="s">&quot;project&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">os_release</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;NAME&quot;</span><span class="p">)</span>
<span class="n">cfg_args</span><span class="p">[</span><span class="s">&quot;releasever&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">os_release</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;VERSION_ID&quot;</span><span class="p">)</span>
<span class="n">cfg_args</span><span class="p">[</span><span class="s">&quot;volid&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
<span class="n">cfg_args</span><span class="p">[</span><span class="s">&quot;extra_boot_args&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">get_kernel_append</span><span class="p">(</span><span class="n">recipe</span><span class="p">)</span>
<span class="n">cfg_args</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
<span class="s">&quot;compression&quot;</span><span class="p">:</span> <span class="s">&quot;xz&quot;</span><span class="p">,</span>
<span class="s">&quot;compress_args&quot;</span><span class="p">:</span> <span class="p">[],</span>
<span class="s">&quot;ks&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">ks_path</span><span class="p">],</span>
<span class="s">&quot;project&quot;</span><span class="p">:</span> <span class="s">&quot;Red Hat Enterprise Linux&quot;</span><span class="p">,</span>
<span class="s">&quot;releasever&quot;</span><span class="p">:</span> <span class="s">&quot;7&quot;</span><span class="p">,</span>
<span class="s">&quot;logfile&quot;</span><span class="p">:</span> <span class="n">log_dir</span>
<span class="p">})</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">joinpaths</span><span class="p">(</span><span class="n">results_dir</span><span class="p">,</span> <span class="s">&quot;config.toml&quot;</span><span class="p">),</span> <span class="s">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">toml</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">cfg_args</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&quot;UTF-8&quot;</span><span class="p">))</span>
<span class="c"># Set the initial status</span>
<span class="nb">open</span><span class="p">(</span><span class="n">joinpaths</span><span class="p">(</span><span class="n">results_dir</span><span class="p">,</span> <span class="s">&quot;STATUS&quot;</span><span class="p">),</span> <span class="s">&quot;w&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;WAITING&quot;</span><span class="p">)</span>
<span class="c"># Set the test mode, if requested</span>
<span class="k">if</span> <span class="n">test_mode</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="nb">open</span><span class="p">(</span><span class="n">joinpaths</span><span class="p">(</span><span class="n">results_dir</span><span class="p">,</span> <span class="s">&quot;TEST&quot;</span><span class="p">),</span> <span class="s">&quot;w&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">test_mode</span><span class="p">)</span>
<span class="n">write_timestamp</span><span class="p">(</span><span class="n">results_dir</span><span class="p">,</span> <span class="n">TS_CREATED</span><span class="p">)</span>
<span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;Adding </span><span class="si">%s</span><span class="s"> (</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">) to compose queue&quot;</span><span class="p">,</span> <span class="n">build_id</span><span class="p">,</span> <span class="n">recipe</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">],</span> <span class="n">compose_type</span><span class="p">)</span>
<span class="n">os</span><span class="o">.</span><span class="n">symlink</span><span class="p">(</span><span class="n">results_dir</span><span class="p">,</span> <span class="n">joinpaths</span><span class="p">(</span><span class="n">lib_dir</span><span class="p">,</span> <span class="s">&quot;queue/new/&quot;</span><span class="p">,</span> <span class="n">build_id</span><span class="p">))</span>
<span class="k">return</span> <span class="n">build_id</span>
<span class="c"># Supported output types</span></div>
<div class="viewcode-block" id="compose_types"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.compose_types">[docs]</a><span class="k">def</span> <span class="nf">compose_types</span><span class="p">(</span><span class="n">share_dir</span><span class="p">):</span>
<span class="sd">r&quot;&quot;&quot; Returns a list of the supported output types</span>
<span class="sd"> The output types come from the kickstart names in /usr/share/lorax/composer/\*ks</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="nb">sorted</span><span class="p">([</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">ks</span><span class="p">)[:</span><span class="o">-</span><span class="mi">3</span><span class="p">]</span> <span class="k">for</span> <span class="n">ks</span> <span class="ow">in</span> <span class="n">glob</span><span class="p">(</span><span class="n">joinpaths</span><span class="p">(</span><span class="n">share_dir</span><span class="p">,</span> <span class="s">&quot;composer/*.ks&quot;</span><span class="p">))])</span>
</div>
<div class="viewcode-block" id="compose_args"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.compose_args">[docs]</a><span class="k">def</span> <span class="nf">compose_args</span><span class="p">(</span><span class="n">compose_type</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Returns the settings to pass to novirt_install for the compose type</span>
<span class="sd"> :param compose_type: The type of compose to create, from `compose_types()`</span>
<span class="sd"> :type compose_type: str</span>
<span class="sd"> This will return a dict of options that match the ArgumentParser options for livemedia-creator.</span>
<span class="sd"> These are the ones the define the type of output, it&#39;s filename, etc.</span>
<span class="sd"> Other options will be filled in by `make_compose()`</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">_MAP</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;tar&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;make_iso&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_disk&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_fsimage&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_appliance&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ami&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_tar&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;make_pxe_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ostree_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;ostree&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_keep_size&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_size&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">&quot;qcow2&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;qcow2_args&quot;</span><span class="p">:</span> <span class="p">[],</span>
<span class="s">&quot;image_name&quot;</span><span class="p">:</span> <span class="n">default_image_name</span><span class="p">(</span><span class="s">&quot;xz&quot;</span><span class="p">,</span> <span class="s">&quot;root.tar&quot;</span><span class="p">),</span>
<span class="s">&quot;image_only&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;app_name&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_template&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_file&quot;</span><span class="p">:</span> <span class="bp">None</span>
<span class="p">},</span>
<span class="s">&quot;live-iso&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;make_iso&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;make_disk&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_fsimage&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_appliance&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ami&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_tar&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_pxe_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ostree_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;ostree&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_keep_size&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_size&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">&quot;qcow2&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;qcow2_args&quot;</span><span class="p">:</span> <span class="p">[],</span>
<span class="s">&quot;image_name&quot;</span><span class="p">:</span> <span class="s">&quot;live.iso&quot;</span><span class="p">,</span>
<span class="s">&quot;fs_label&quot;</span><span class="p">:</span> <span class="s">&quot;Anaconda&quot;</span><span class="p">,</span> <span class="c"># Live booting may expect this to be &#39;Anaconda&#39;</span>
<span class="s">&quot;image_only&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;app_name&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_template&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_file&quot;</span><span class="p">:</span> <span class="bp">None</span>
<span class="p">},</span>
<span class="s">&quot;partitioned-disk&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;make_iso&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_disk&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;make_fsimage&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_appliance&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ami&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_tar&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_pxe_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ostree_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;ostree&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_keep_size&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_size&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">&quot;qcow2&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;qcow2_args&quot;</span><span class="p">:</span> <span class="p">[],</span>
<span class="s">&quot;image_name&quot;</span><span class="p">:</span> <span class="s">&quot;disk.img&quot;</span><span class="p">,</span>
<span class="s">&quot;fs_label&quot;</span><span class="p">:</span> <span class="s">&quot;&quot;</span><span class="p">,</span>
<span class="s">&quot;image_only&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;app_name&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_template&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_file&quot;</span><span class="p">:</span> <span class="bp">None</span>
<span class="p">},</span>
<span class="s">&quot;qcow2&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;make_iso&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_disk&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;make_fsimage&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_appliance&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ami&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_tar&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_pxe_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ostree_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;ostree&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_keep_size&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_size&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">&quot;qcow2&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;qcow2_args&quot;</span><span class="p">:</span> <span class="p">[],</span>
<span class="s">&quot;image_name&quot;</span><span class="p">:</span> <span class="s">&quot;disk.qcow2&quot;</span><span class="p">,</span>
<span class="s">&quot;fs_label&quot;</span><span class="p">:</span> <span class="s">&quot;&quot;</span><span class="p">,</span>
<span class="s">&quot;image_only&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;app_name&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_template&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_file&quot;</span><span class="p">:</span> <span class="bp">None</span>
<span class="p">},</span>
<span class="s">&quot;ext4-filesystem&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;make_iso&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_disk&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_fsimage&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;make_appliance&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ami&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_tar&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_pxe_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ostree_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;ostree&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_keep_size&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_size&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">&quot;qcow2&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;qcow2_args&quot;</span><span class="p">:</span> <span class="p">[],</span>
<span class="s">&quot;image_name&quot;</span><span class="p">:</span> <span class="s">&quot;filesystem.img&quot;</span><span class="p">,</span>
<span class="s">&quot;fs_label&quot;</span><span class="p">:</span> <span class="s">&quot;&quot;</span><span class="p">,</span>
<span class="s">&quot;image_only&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;app_name&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_template&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_file&quot;</span><span class="p">:</span> <span class="bp">None</span>
<span class="p">},</span>
<span class="s">&quot;ami&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;make_iso&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_disk&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;make_fsimage&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_appliance&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ami&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_tar&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_pxe_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ostree_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;ostree&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_keep_size&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_size&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">&quot;qcow2&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;qcow2_args&quot;</span><span class="p">:</span> <span class="p">[],</span>
<span class="s">&quot;image_name&quot;</span><span class="p">:</span> <span class="s">&quot;disk.ami&quot;</span><span class="p">,</span>
<span class="s">&quot;fs_label&quot;</span><span class="p">:</span> <span class="s">&quot;&quot;</span><span class="p">,</span>
<span class="s">&quot;image_only&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;app_name&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_template&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_file&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="p">},</span>
<span class="s">&quot;vhd&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;make_iso&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_disk&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;make_fsimage&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_appliance&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ami&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_tar&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_pxe_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ostree_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;ostree&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_keep_size&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_size&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">&quot;qcow2&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;qcow2_args&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s">&quot;-O&quot;</span><span class="p">,</span> <span class="s">&quot;vpc&quot;</span><span class="p">,</span> <span class="s">&quot;-o&quot;</span><span class="p">,</span> <span class="s">&quot;subformat=fixed&quot;</span><span class="p">],</span>
<span class="s">&quot;image_name&quot;</span><span class="p">:</span> <span class="s">&quot;disk.vhd&quot;</span><span class="p">,</span>
<span class="s">&quot;fs_label&quot;</span><span class="p">:</span> <span class="s">&quot;&quot;</span><span class="p">,</span>
<span class="s">&quot;image_only&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;app_name&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_template&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_file&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="p">},</span>
<span class="s">&quot;vmdk&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;make_iso&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_disk&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;make_fsimage&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_appliance&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ami&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_tar&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_pxe_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ostree_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;ostree&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_keep_size&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_size&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">&quot;qcow2&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;qcow2_args&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s">&quot;-O&quot;</span><span class="p">,</span> <span class="s">&quot;vmdk&quot;</span><span class="p">],</span>
<span class="s">&quot;image_name&quot;</span><span class="p">:</span> <span class="s">&quot;disk.vmdk&quot;</span><span class="p">,</span>
<span class="s">&quot;fs_label&quot;</span><span class="p">:</span> <span class="s">&quot;&quot;</span><span class="p">,</span>
<span class="s">&quot;image_only&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;app_name&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_template&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_file&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="p">},</span>
<span class="s">&quot;openstack&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;make_iso&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_disk&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;make_fsimage&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_appliance&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ami&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_tar&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_pxe_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;make_ostree_live&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;ostree&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_keep_size&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="s">&quot;live_rootfs_size&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">&quot;qcow2&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;qcow2_args&quot;</span><span class="p">:</span> <span class="p">[],</span>
<span class="s">&quot;image_name&quot;</span><span class="p">:</span> <span class="s">&quot;disk.qcow2&quot;</span><span class="p">,</span>
<span class="s">&quot;fs_label&quot;</span><span class="p">:</span> <span class="s">&quot;&quot;</span><span class="p">,</span>
<span class="s">&quot;image_only&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s">&quot;app_name&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_template&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="s">&quot;app_file&quot;</span><span class="p">:</span> <span class="bp">None</span><span class="p">,</span>
<span class="p">},</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">_MAP</span><span class="p">[</span><span class="n">compose_type</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="move_compose_results"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.compose.move_compose_results">[docs]</a><span class="k">def</span> <span class="nf">move_compose_results</span><span class="p">(</span><span class="n">cfg</span><span class="p">,</span> <span class="n">results_dir</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Move the final image to the results_dir and cleanup the unneeded compose files</span>
<span class="sd"> :param cfg: Build configuration</span>
<span class="sd"> :type cfg: DataHolder</span>
<span class="sd"> :param results_dir: Directory to put the results into</span>
<span class="sd"> :type results_dir: str</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">cfg</span><span class="p">[</span><span class="s">&quot;make_tar&quot;</span><span class="p">]:</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">move</span><span class="p">(</span><span class="n">joinpaths</span><span class="p">(</span><span class="n">cfg</span><span class="p">[</span><span class="s">&quot;result_dir&quot;</span><span class="p">],</span> <span class="n">cfg</span><span class="p">[</span><span class="s">&quot;image_name&quot;</span><span class="p">]),</span> <span class="n">results_dir</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">cfg</span><span class="p">[</span><span class="s">&quot;make_iso&quot;</span><span class="p">]:</span>
<span class="c"># Output from live iso is always a boot.iso under images/, move and rename it</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">move</span><span class="p">(</span><span class="n">joinpaths</span><span class="p">(</span><span class="n">cfg</span><span class="p">[</span><span class="s">&quot;result_dir&quot;</span><span class="p">],</span> <span class="s">&quot;images/boot.iso&quot;</span><span class="p">),</span> <span class="n">joinpaths</span><span class="p">(</span><span class="n">results_dir</span><span class="p">,</span> <span class="n">cfg</span><span class="p">[</span><span class="s">&quot;image_name&quot;</span><span class="p">]))</span>
<span class="k">elif</span> <span class="n">cfg</span><span class="p">[</span><span class="s">&quot;make_disk&quot;</span><span class="p">]</span> <span class="ow">or</span> <span class="n">cfg</span><span class="p">[</span><span class="s">&quot;make_fsimage&quot;</span><span class="p">]:</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">move</span><span class="p">(</span><span class="n">joinpaths</span><span class="p">(</span><span class="n">cfg</span><span class="p">[</span><span class="s">&quot;result_dir&quot;</span><span class="p">],</span> <span class="n">cfg</span><span class="p">[</span><span class="s">&quot;image_name&quot;</span><span class="p">]),</span> <span class="n">joinpaths</span><span class="p">(</span><span class="n">results_dir</span><span class="p">,</span> <span class="n">cfg</span><span class="p">[</span><span class="s">&quot;image_name&quot;</span><span class="p">]))</span>
<span class="c"># Cleanup the compose directory, but only if it looks like a compose directory</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">cfg</span><span class="p">[</span><span class="s">&quot;result_dir&quot;</span><span class="p">])</span> <span class="o">==</span> <span class="s">&quot;compose&quot;</span><span class="p">:</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="n">cfg</span><span class="p">[</span><span class="s">&quot;result_dir&quot;</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;Incorrect compose directory, not cleaning up&quot;</span><span class="p">)</span></div>
</pre></div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li><a href="../../../index.html">Lorax 19.7.43 documentation</a> &raquo;</li>
<li><a href="../../index.html" >Module code</a> &raquo;</li>
<li><a href="../../pylorax.html" >pylorax</a> &raquo;</li>
<li><a href="../api.html" >pylorax.api</a> &raquo;</li>
</ul>
</div>
<div class="footer">
&copy; Copyright 2018, Red Hat, Inc..
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
</div>
</body>
</html>