lorax/docs/html/_modules/lifted/providers.html

444 lines
34 KiB
HTML
Raw Normal View History

2019-10-16 21:01:30 +00:00
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
2019-11-05 22:36:46 +00:00
<title>lifted.providers &mdash; Lorax 32.2 documentation</title>
2019-10-16 21:01:30 +00:00
<script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
<script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></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>
<script type="text/javascript" src="../../_static/language_data.js"></script>
<script type="text/javascript" src="../../_static/js/theme.js"></script>
<link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
<link rel="index" title="Index" href="../../genindex.html" />
<link rel="search" title="Search" href="../../search.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../../index.html" class="icon icon-home"> Lorax
</a>
<div class="version">
2019-11-05 22:36:46 +00:00
32.2
2019-10-16 21:01:30 +00:00
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../intro.html">Introduction to Lorax</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../intro.html#before-lorax">Before Lorax</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../lorax.html">Lorax</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../livemedia-creator.html">livemedia-creator</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../lorax-composer.html">lorax-composer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../composer-cli.html">composer-cli</a></li>
2019-11-05 22:36:46 +00:00
<li class="toctree-l1"><a class="reference internal" href="../../mkksiso.html">mkksiso</a></li>
2019-10-16 21:01:30 +00:00
<li class="toctree-l1"><a class="reference internal" href="../../product-images.html">Product and Updates Images</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules.html">src</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../../index.html">Lorax</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../../index.html">Docs</a> &raquo;</li>
<li><a href="../index.html">Module code</a> &raquo;</li>
<li>lifted.providers</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<h1>Source code for lifted.providers</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="c1"># Copyright (C) 2019 Red Hat, Inc.</span>
<span class="c1">#</span>
<span class="c1"># This program is free software; you can redistribute it and/or modify</span>
<span class="c1"># it under the terms of the GNU General Public License as published by</span>
<span class="c1"># the Free Software Foundation; either version 2 of the License, or</span>
<span class="c1"># (at your option) any later version.</span>
<span class="c1">#</span>
<span class="c1"># This program is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
<span class="c1"># GNU General Public License for more details.</span>
<span class="c1">#</span>
<span class="c1"># You should have received a copy of the GNU General Public License</span>
<span class="c1"># along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="c1">#</span>
<span class="kn">from</span> <span class="nn">glob</span> <span class="k">import</span> <span class="n">glob</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">stat</span>
<span class="kn">import</span> <span class="nn">pylorax.api.toml</span> <span class="k">as</span> <span class="nn">toml</span>
<span class="k">def</span> <span class="nf">_get_profile_path</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">,</span> <span class="n">profile</span><span class="p">,</span> <span class="n">exists</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Helper to return the directory and path for a provider&#39;s profile file</span>
<span class="sd"> :param ucfg: upload config</span>
<span class="sd"> :type ucfg: object</span>
<span class="sd"> :param provider_name: the name of the cloud provider, e.g. &quot;azure&quot;</span>
<span class="sd"> :type provider_name: str</span>
<span class="sd"> :param profile: the name of the profile to save</span>
<span class="sd"> :type profile: str != &quot;&quot;</span>
<span class="sd"> :returns: Full path of the profile .toml file</span>
<span class="sd"> :rtype: str</span>
<span class="sd"> :raises: ValueError when passed invalid settings or an invalid profile name</span>
<span class="sd"> :raises: RuntimeError when the provider or profile couldn&#39;t be found</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1"># Make sure no path elements are present</span>
<span class="n">profile</span> <span class="o">=</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">profile</span><span class="p">)</span>
<span class="n">provider_name</span> <span class="o">=</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">provider_name</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">profile</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Profile name cannot be empty!&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">provider_name</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Provider name cannot be empty!&quot;</span><span class="p">)</span>
<span class="n">directory</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">ucfg</span><span class="p">[</span><span class="s2">&quot;settings_dir&quot;</span><span class="p">],</span> <span class="n">provider_name</span><span class="p">)</span>
<span class="c1"># create the settings directory if it doesn&#39;t exist</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">directory</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">directory</span><span class="p">,</span> <span class="n">f</span><span class="s2">&quot;</span><span class="si">{profile}</span><span class="s2">.toml&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">exists</span> <span class="ow">and</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">isfile</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="n">f</span><span class="s1">&#39;Couldn</span><span class="se">\&#39;</span><span class="s1">t find profile &quot;</span><span class="si">{profile}</span><span class="s1">&quot;!&#39;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<div class="viewcode-block" id="resolve_provider"><a class="viewcode-back" href="../../lifted.html#lifted.providers.resolve_provider">[docs]</a><span class="k">def</span> <span class="nf">resolve_provider</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Get information about the specified provider as defined in that</span>
<span class="sd"> provider&#39;s `provider.toml`, including the provider&#39;s display name and expected</span>
<span class="sd"> settings.</span>
<span class="sd"> At a minimum, each setting has a display name (that likely differs from its</span>
<span class="sd"> snake_case name) and a type. Currently, there are two types of settings:</span>
<span class="sd"> string and boolean. String settings can optionally have a &quot;placeholder&quot;</span>
<span class="sd"> value for use on the front end and a &quot;regex&quot; for making sure that a value</span>
<span class="sd"> follows an expected pattern.</span>
<span class="sd"> :param ucfg: upload config</span>
<span class="sd"> :type ucfg: object</span>
<span class="sd"> :param provider_name: the name of the provider to look for</span>
<span class="sd"> :type provider_name: str</span>
<span class="sd"> :raises: RuntimeError when the provider couldn&#39;t be found</span>
<span class="sd"> :returns: the provider</span>
<span class="sd"> :rtype: dict</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1"># Make sure no path elements are present</span>
<span class="n">provider_name</span> <span class="o">=</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">provider_name</span><span class="p">)</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">ucfg</span><span class="p">[</span><span class="s2">&quot;providers_dir&quot;</span><span class="p">],</span> <span class="n">provider_name</span><span class="p">,</span> <span class="s2">&quot;provider.toml&quot;</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="k">as</span> <span class="n">provider_file</span><span class="p">:</span>
<span class="n">provider</span> <span class="o">=</span> <span class="n">toml</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">provider_file</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="n">f</span><span class="s1">&#39;Couldn</span><span class="se">\&#39;</span><span class="s1">t find provider &quot;</span><span class="si">{provider_name}</span><span class="s1">&quot;!&#39;</span><span class="p">)</span> <span class="kn">from</span> <span class="nn">error</span>
<span class="k">return</span> <span class="n">provider</span></div>
<div class="viewcode-block" id="load_profiles"><a class="viewcode-back" href="../../lifted.html#lifted.providers.load_profiles">[docs]</a><span class="k">def</span> <span class="nf">load_profiles</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return all settings profiles associated with a provider</span>
<span class="sd"> :param ucfg: upload config</span>
<span class="sd"> :type ucfg: object</span>
<span class="sd"> :param provider_name: name a provider to find profiles for</span>
<span class="sd"> :type provider_name: str</span>
<span class="sd"> :returns: a dict of settings dicts, keyed by profile name</span>
<span class="sd"> :rtype: dict</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1"># Make sure no path elements are present</span>
<span class="n">provider_name</span> <span class="o">=</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">provider_name</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">load_path</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
<span class="k">return</span> <span class="n">toml</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get_name</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
<span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</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">path</span><span class="p">))[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">paths</span> <span class="o">=</span> <span class="n">glob</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">join</span><span class="p">(</span><span class="n">ucfg</span><span class="p">[</span><span class="s2">&quot;settings_dir&quot;</span><span class="p">],</span> <span class="n">provider_name</span><span class="p">,</span> <span class="s2">&quot;*&quot;</span><span class="p">))</span>
<span class="k">return</span> <span class="p">{</span><span class="n">get_name</span><span class="p">(</span><span class="n">path</span><span class="p">):</span> <span class="n">load_path</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">paths</span><span class="p">}</span></div>
<div class="viewcode-block" id="resolve_playbook_path"><a class="viewcode-back" href="../../lifted.html#lifted.providers.resolve_playbook_path">[docs]</a><span class="k">def</span> <span class="nf">resolve_playbook_path</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Given a provider&#39;s name, return the path to its playbook</span>
<span class="sd"> :param ucfg: upload config</span>
<span class="sd"> :type ucfg: object</span>
<span class="sd"> :param provider_name: the name of the provider to find the playbook for</span>
<span class="sd"> :type provider_name: str</span>
<span class="sd"> :raises: RuntimeError when the provider couldn&#39;t be found</span>
<span class="sd"> :returns: the path to the playbook</span>
<span class="sd"> :rtype: str</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1"># Make sure no path elements are present</span>
<span class="n">provider_name</span> <span class="o">=</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">provider_name</span><span class="p">)</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">ucfg</span><span class="p">[</span><span class="s2">&quot;providers_dir&quot;</span><span class="p">],</span> <span class="n">provider_name</span><span class="p">,</span> <span class="s2">&quot;playbook.yaml&quot;</span><span class="p">)</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">isfile</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="n">f</span><span class="s1">&#39;Couldn</span><span class="se">\&#39;</span><span class="s1">t find playbook for &quot;</span><span class="si">{provider_name}</span><span class="s1">&quot;!&#39;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">path</span></div>
<div class="viewcode-block" id="list_providers"><a class="viewcode-back" href="../../lifted.html#lifted.providers.list_providers">[docs]</a><span class="k">def</span> <span class="nf">list_providers</span><span class="p">(</span><span class="n">ucfg</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;List the names of the available upload providers</span>
<span class="sd"> :param ucfg: upload config</span>
<span class="sd"> :type ucfg: object</span>
<span class="sd"> :returns: a list of all available provider_names</span>
<span class="sd"> :rtype: list of str</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">paths</span> <span class="o">=</span> <span class="n">glob</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">join</span><span class="p">(</span><span class="n">ucfg</span><span class="p">[</span><span class="s2">&quot;providers_dir&quot;</span><span class="p">],</span> <span class="s2">&quot;*&quot;</span><span class="p">))</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">path</span><span class="p">)</span> <span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">paths</span><span class="p">)</span></div>
<div class="viewcode-block" id="validate_settings"><a class="viewcode-back" href="../../lifted.html#lifted.providers.validate_settings">[docs]</a><span class="k">def</span> <span class="nf">validate_settings</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">,</span> <span class="n">settings</span><span class="p">,</span> <span class="n">image_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Raise a ValueError if any settings are invalid</span>
<span class="sd"> :param ucfg: upload config</span>
<span class="sd"> :type ucfg: object</span>
<span class="sd"> :param provider_name: the name of the provider to validate the settings against</span>
<span class="sd"> :type provider_name: str</span>
<span class="sd"> :param settings: the settings to validate</span>
<span class="sd"> :type settings: dict</span>
<span class="sd"> :param image_name: optionally check whether an image_name is valid</span>
<span class="sd"> :type image_name: str</span>
<span class="sd"> :raises: ValueError when the passed settings are invalid</span>
<span class="sd"> :raises: RuntimeError when provider_name can&#39;t be found</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">image_name</span> <span class="o">==</span> <span class="s2">&quot;&quot;</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Image name cannot be empty!&quot;</span><span class="p">)</span>
<span class="n">type_map</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;string&quot;</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="s2">&quot;boolean&quot;</span><span class="p">:</span> <span class="nb">bool</span><span class="p">}</span>
<span class="n">settings_info</span> <span class="o">=</span> <span class="n">resolve_provider</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">)[</span><span class="s2">&quot;settings-info&quot;</span><span class="p">]</span>
<span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">settings</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">settings_info</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">f</span><span class="s1">&#39;Received unexpected setting: &quot;</span><span class="si">{key}</span><span class="s1">&quot;!&#39;</span><span class="p">)</span>
<span class="n">setting_type</span> <span class="o">=</span> <span class="n">settings_info</span><span class="p">[</span><span class="n">key</span><span class="p">][</span><span class="s2">&quot;type&quot;</span><span class="p">]</span>
<span class="n">correct_type</span> <span class="o">=</span> <span class="n">type_map</span><span class="p">[</span><span class="n">setting_type</span><span class="p">]</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">correct_type</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
<span class="n">f</span><span class="s1">&#39;Expected a </span><span class="si">{correct_type}</span><span class="s1"> for &quot;</span><span class="si">{key}</span><span class="s1">&quot;, received a {type(value)}!&#39;</span>
<span class="p">)</span>
<span class="k">if</span> <span class="n">setting_type</span> <span class="o">==</span> <span class="s2">&quot;string&quot;</span> <span class="ow">and</span> <span class="s2">&quot;regex&quot;</span> <span class="ow">in</span> <span class="n">settings_info</span><span class="p">[</span><span class="n">key</span><span class="p">]:</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">settings_info</span><span class="p">[</span><span class="n">key</span><span class="p">][</span><span class="s2">&quot;regex&quot;</span><span class="p">],</span> <span class="n">value</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">f</span><span class="s1">&#39;Value &quot;</span><span class="si">{value}</span><span class="s1">&quot; is invalid for setting &quot;</span><span class="si">{key}</span><span class="s1">&quot;!&#39;</span><span class="p">)</span></div>
<div class="viewcode-block" id="save_settings"><a class="viewcode-back" href="../../lifted.html#lifted.providers.save_settings">[docs]</a><span class="k">def</span> <span class="nf">save_settings</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">,</span> <span class="n">profile</span><span class="p">,</span> <span class="n">settings</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Save (and overwrite) settings for a given provider</span>
<span class="sd"> :param ucfg: upload config</span>
<span class="sd"> :type ucfg: object</span>
<span class="sd"> :param provider_name: the name of the cloud provider, e.g. &quot;azure&quot;</span>
<span class="sd"> :type provider_name: str</span>
<span class="sd"> :param profile: the name of the profile to save</span>
<span class="sd"> :type profile: str != &quot;&quot;</span>
<span class="sd"> :param settings: settings to save for that provider</span>
<span class="sd"> :type settings: dict</span>
<span class="sd"> :raises: ValueError when passed invalid settings or an invalid profile name</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">_get_profile_path</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">,</span> <span class="n">profile</span><span class="p">,</span> <span class="n">exists</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="n">validate_settings</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">,</span> <span class="n">settings</span><span class="p">,</span> <span class="n">image_name</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="c1"># touch the TOML file if it doesn&#39;t exist</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">isfile</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
<span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s2">&quot;a&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="c1"># make sure settings files aren&#39;t readable by others, as they will contain</span>
<span class="c1"># sensitive credentials</span>
<span class="n">current</span> <span class="o">=</span> <span class="n">stat</span><span class="o">.</span><span class="n">S_IMODE</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">lstat</span><span class="p">(</span><span class="n">path</span><span class="p">)</span><span class="o">.</span><span class="n">st_mode</span><span class="p">)</span>
<span class="n">os</span><span class="o">.</span><span class="n">chmod</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">current</span> <span class="o">&amp;</span> <span class="o">~</span><span class="n">stat</span><span class="o">.</span><span class="n">S_IROTH</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">settings_file</span><span class="p">:</span>
<span class="n">toml</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="n">settings_file</span><span class="p">)</span></div>
<div class="viewcode-block" id="load_settings"><a class="viewcode-back" href="../../lifted.html#lifted.providers.load_settings">[docs]</a><span class="k">def</span> <span class="nf">load_settings</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">,</span> <span class="n">profile</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Load settings for a provider&#39;s profile</span>
<span class="sd"> :param ucfg: upload config</span>
<span class="sd"> :type ucfg: object</span>
<span class="sd"> :param provider_name: the name of the cloud provider, e.g. &quot;azure&quot;</span>
<span class="sd"> :type provider_name: str</span>
<span class="sd"> :param profile: the name of the profile to save</span>
<span class="sd"> :type profile: str != &quot;&quot;</span>
<span class="sd"> :returns: The profile settings for the selected provider</span>
<span class="sd"> :rtype: dict</span>
<span class="sd"> :raises: ValueError when passed invalid settings or an invalid profile name</span>
<span class="sd"> :raises: RuntimeError when the provider or profile couldn&#39;t be found</span>
<span class="sd"> :raises: ValueError when the passed settings are invalid</span>
<span class="sd"> This also calls validate_settings on the loaded settings, potentially</span>
<span class="sd"> raising an error if the saved settings are invalid.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">_get_profile_path</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">,</span> <span class="n">profile</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
<span class="n">settings</span> <span class="o">=</span> <span class="n">toml</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
<span class="n">validate_settings</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">,</span> <span class="n">settings</span><span class="p">)</span>
<span class="k">return</span> <span class="n">settings</span></div>
<div class="viewcode-block" id="delete_profile"><a class="viewcode-back" href="../../lifted.html#lifted.providers.delete_profile">[docs]</a><span class="k">def</span> <span class="nf">delete_profile</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">,</span> <span class="n">profile</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Delete a provider&#39;s profile settings file</span>
<span class="sd"> :param ucfg: upload config</span>
<span class="sd"> :type ucfg: object</span>
<span class="sd"> :param provider_name: the name of the cloud provider, e.g. &quot;azure&quot;</span>
<span class="sd"> :type provider_name: str</span>
<span class="sd"> :param profile: the name of the profile to save</span>
<span class="sd"> :type profile: str != &quot;&quot;</span>
<span class="sd"> :raises: ValueError when passed invalid settings or an invalid profile name</span>
<span class="sd"> :raises: RuntimeError when the provider or profile couldn&#39;t be found</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">_get_profile_path</span><span class="p">(</span><span class="n">ucfg</span><span class="p">,</span> <span class="n">provider_name</span><span class="p">,</span> <span class="n">profile</span><span class="p">)</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">exists</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
<span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">path</span><span class="p">)</span></div>
</pre></div>
</div>
</div>
<footer>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2018, Red Hat, Inc.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>