<liclass="toctree-l2"><aclass="reference internal"href="#input-from-stdin">15.13. Input from stdin</a><ul>
<liclass="toctree-l3"><aclass="reference internal"href="#stdin-inside-a-loop">15.13.1. Stdin inside a loop</a></li>
</ul>
</li>
<liclass="toctree-l2"><aclass="reference internal"href="#writing-the-manifest">15.14. Writing the manifest</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="#singleton-one-instance-only">15.15. Singleton - one instance only</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="#install-type-with-install-command">15.16. Install - type with install command</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="#nonparallel-only-one-instance-can-be-run-at-a-time">15.17. Nonparallel - only one instance can be run at a time</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="#the-type-explorers">15.18. The type explorers</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="#writing-the-gencode-script">15.19. Writing the gencode script</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="#variable-access-from-the-generated-scripts">15.20. Variable access from the generated scripts</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="#hints-for-typewriters">15.24. Hints for typewriters</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="#how-to-include-a-type-into-upstream-cdist">15.25. How to include a type into upstream cdist</a></li>
<h1><spanclass="section-number">15. </span>cdist type<aclass="headerlink"href="#cdist-type"title="Permalink to this headline">¶</a></h1>
<divclass="section"id="description">
<h2><spanclass="section-number">15.1. </span>Description<aclass="headerlink"href="#description"title="Permalink to this headline">¶</a></h2>
<p>Types are the main component of cdist and define functionality. If you
use cdist, you'll write a type for every functionality you would like
to use.</p>
</div>
<divclass="section"id="synopsis">
<h2><spanclass="section-number">15.2. </span>Synopsis<aclass="headerlink"href="#synopsis"title="Permalink to this headline">¶</a></h2>
<divclass="highlight-sh notranslate"><divclass="highlight"><pre><span></span>__TYPE ID --parameter value <spanclass="o">[</span>--parameter value ...<spanclass="o">]</span>
__TYPE --parameter value <spanclass="o">[</span>--parameter value ...<spanclass="o">]</span><spanclass="o">(</span><spanclass="k">for</span> singletons<spanclass="o">)</span>
</pre></div>
</div>
</div>
<divclass="section"id="how-to-use-a-type">
<h2><spanclass="section-number">15.3. </span>How to use a type<aclass="headerlink"href="#how-to-use-a-type"title="Permalink to this headline">¶</a></h2>
<p>You can use types from the initial manifest or the type manifest like a
<p>A list of supported types can be found in the <aclass="reference external"href="cdist-reference.html">cdist reference</a> manpage.</p>
</div>
<divclass="section"id="singleton-types">
<h2><spanclass="section-number">15.4. </span>Singleton types<aclass="headerlink"href="#singleton-types"title="Permalink to this headline">¶</a></h2>
<p>If a type is flagged as a singleton, it may be used only
once per host. This is useful for types which can be used only once on a
system. Singleton types do not take an object name as argument.</p>
<p>Example:</p>
<divclass="highlight-sh notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># __issue type manages /etc/issue</span>
__issue
<spanclass="c1"># Probably your own type - singletons may use parameters</span>
__myfancysingleton --colour green
</pre></div>
</div>
</div>
<divclass="section"id="config-types">
<h2><spanclass="section-number">15.5. </span>Config types<aclass="headerlink"href="#config-types"title="Permalink to this headline">¶</a></h2>
<p>By default types are used with config command. These are types that are not
flagged by any known command flag. If a type is marked then it will be skipped
with config command.</p>
</div>
<divclass="section"id="install-types">
<h2><spanclass="section-number">15.6. </span>Install types<aclass="headerlink"href="#install-types"title="Permalink to this headline">¶</a></h2>
<p>If a type is flagged with 'install' flag then it is used only with install command.
With other commands, i.e. config, these types are skipped if used.</p>
</div>
<divclass="section"id="nonparallel-types">
<h2><spanclass="section-number">15.7. </span>Nonparallel types<aclass="headerlink"href="#nonparallel-types"title="Permalink to this headline">¶</a></h2>
<p>If a type is flagged with 'nonparallel' flag then its objects cannot be run in parallel
when using -j option. Example of such a type is __package_dpkg type where dpkg itself
prevents to be run in more than one instance.</p>
</div>
<divclass="section"id="deprecated-types">
<h2><spanclass="section-number">15.8. </span>Deprecated types<aclass="headerlink"href="#deprecated-types"title="Permalink to this headline">¶</a></h2>
<p>If a type is flagged with 'deprecated' marker then it is considered deprecated.
When it is used cdist writes warning line. If 'deprecated' marker has content
then this content is printed as a deprecation messages, e.g.:</p>
<divclass="highlight-sh notranslate"><divclass="highlight"><pre><span></span>$ ls -l deprecated
-rw-r--r-- <spanclass="m">1</span> darko darko <spanclass="m">71</span> May <spanclass="m">20</span><spanclass="m">18</span>:30 deprecated
$ cat deprecated
This <spanclass="nb">type</span> is deprecated. It will be removed in the next minor release.
WARNING: <spanclass="m">185</span>.203.112.26: Type __foo is deprecated: This <spanclass="nb">type</span> is deprecated. It will be removed in the next minor release.
</pre></div>
</div>
<p>If 'deprecated' marker has no content then general message is printed, e.g.:</p>
<divclass="highlight-sh notranslate"><divclass="highlight"><pre><span></span>$ ls -l deprecated
-rw-r--r-- <spanclass="m">1</span> darko darko <spanclass="m">0</span> May <spanclass="m">20</span><spanclass="m">18</span>:36 deprecated
WARNING: <spanclass="m">185</span>.203.112.26: Type __bar is deprecated.
</pre></div>
</div>
</div>
<divclass="section"id="how-to-write-a-new-type">
<h2><spanclass="section-number">15.9. </span>How to write a new type<aclass="headerlink"href="#how-to-write-a-new-type"title="Permalink to this headline">¶</a></h2>
<p>A type consists of</p>
<ulclass="simple">
<li><p>parameter (optional)</p></li>
<li><p>manifest (optional)</p></li>
<li><p>singleton (optional)</p></li>
<li><p>explorer (optional)</p></li>
<li><p>gencode (optional)</p></li>
<li><p>nonparallel (optional)</p></li>
</ul>
<p>Types are stored below cdist/conf/type/. Their name should always be prefixed with
two underscores (__) to prevent collisions with other executables in $PATH.</p>
<p>To implement a new type, create the directory <strong>cdist/conf/type/__NAME</strong>.</p>
<p>Type manifest and gencode can be written in any language. They just need to be
executable and have a proper shebang. If they are not executable then cdist assumes
they are written in shell so they are executed using '/bin/sh -e' or 'CDIST_LOCAL_SHELL'.</p>
<p>For executable shell code it is suggested that shebang is '#!/bin/sh -e'.</p>
<p>For creating type skeleton you can use helper script
<h2><spanclass="section-number">15.10. </span>Defining parameters<aclass="headerlink"href="#defining-parameters"title="Permalink to this headline">¶</a></h2>
<p>Every type consists of required, optional and boolean parameters, which must
each be declared in a newline separated file in <strong>parameter/required</strong>,
<h2><spanclass="section-number">15.11. </span>Using parameters<aclass="headerlink"href="#using-parameters"title="Permalink to this headline">¶</a></h2>
<p>The parameters given to a type can be accessed and used in all type scripts
(e.g manifest, gencode, explorer). Note that boolean parameters are
represented by file existence. File exists -> True,
file does not exist -> False</p>
<p>Example: (e.g. in cdist/conf/type/__nginx_vhost/manifest)</p>
<spanclass="k">for</span><spanclass="nb">alias</span> in <spanclass="k">$(</span>cat <spanclass="s2">"</span><spanclass="nv">$__object</span><spanclass="s2">/parameter/server_alias"</span><spanclass="k">)</span><spanclass="p">;</span><spanclass="k">do</span>
<h2><spanclass="section-number">15.12. </span>Deprecated parameters<aclass="headerlink"href="#deprecated-parameters"title="Permalink to this headline">¶</a></h2>
<p>To deprecate type parameters one can declare a file for each deprecated
parameter under <strong>parameter/deprecated</strong> directory.</p>
<p>When such parameter is used cdist writes warning line with deprecation message.
If such file has content then this content is printed as deprecation message.
If there is no content then generic parameter deprecation message is printed.</p>
<p>Example:</p>
<divclass="highlight-sh notranslate"><divclass="highlight"><pre><span></span>$ ls parameter/deprecated/
eggs spam
$ cat parameter/deprecated/eggs
eggs parameter is deprecated, please use multiple egg parameter.
WARNING: <spanclass="m">185</span>.203.112.26: eggs parameter of <spanclass="nb">type</span> __foo is deprecated: eggs parameter is deprecated, please use multiple egg parameter.
WARNING: <spanclass="m">185</span>.203.112.26: spam parameter of <spanclass="nb">type</span> __foo is deprecated.
WARNING: <spanclass="m">185</span>.203.112.26: eggs parameter of <spanclass="nb">type</span> __foo is deprecated: eggs parameter is deprecated, please use multiple egg parameter.
</pre></div>
</div>
</div>
<divclass="section"id="input-from-stdin">
<h2><spanclass="section-number">15.13. </span>Input from stdin<aclass="headerlink"href="#input-from-stdin"title="Permalink to this headline">¶</a></h2>
<p>Every type can access what has been written on stdin when it has been called.
The result is saved into the <strong>stdin</strong> file in the object directory.</p>
<p>Example use of a type: (e.g. in cdist/conf/type/__archlinux_hostname)</p>
<h3><spanclass="section-number">15.13.1. </span>Stdin inside a loop<aclass="headerlink"href="#stdin-inside-a-loop"title="Permalink to this headline">¶</a></h3>
<p>Since cdist saves type's stdin content in the object as <strong>$__object/stdin</strong>,
so it can be accessed in manifest and gencode-* scripts, this can lead to
unexpected behavior. For example, suppose you have some type with the following
<h2><spanclass="section-number">15.14. </span>Writing the manifest<aclass="headerlink"href="#writing-the-manifest"title="Permalink to this headline">¶</a></h2>
<p>In the manifest of a type you can use other types, so your type extends
their functionality. A good example is the __package type, which in
<spanclass="nb">echo</span><spanclass="s2">"Don't know how to manage packages on: </span><spanclass="nv">$os</span><spanclass="s2">"</span>><spanclass="p">&</span><spanclass="m">2</span>
<h2><spanclass="section-number">15.15. </span>Singleton - one instance only<aclass="headerlink"href="#singleton-one-instance-only"title="Permalink to this headline">¶</a></h2>
<p>If you want to ensure that a type can only be used once per target, you can
mark it as a singleton: Just create the (empty) file "singleton" in your type
<h2><spanclass="section-number">15.16. </span>Install - type with install command<aclass="headerlink"href="#install-type-with-install-command"title="Permalink to this headline">¶</a></h2>
<p>If you want a type to be used with install command, you must mark it as
install: create the (empty) file "install" in your type directory:</p>
<h2><spanclass="section-number">15.17. </span>Nonparallel - only one instance can be run at a time<aclass="headerlink"href="#nonparallel-only-one-instance-can-be-run-at-a-time"title="Permalink to this headline">¶</a></h2>
<p>If objects of a type must not or cannot be run in parallel when using -j
option, you must mark it as nonparallel: create the (empty) file "nonparallel"
<p>For example, package types are nonparallel types.</p>
</div>
<divclass="section"id="the-type-explorers">
<h2><spanclass="section-number">15.18. </span>The type explorers<aclass="headerlink"href="#the-type-explorers"title="Permalink to this headline">¶</a></h2>
<p>If a type needs to explore specific details, it can provide type specific
explorers, which will be executed on the target for every created object.</p>
<p>The explorers are stored under the "explorer" directory below the type.
It could for instance contain code to check the md5sum of a file on the
client, like this (shortened version from the type __file):</p>
<h2><spanclass="section-number">15.19. </span>Writing the gencode script<aclass="headerlink"href="#writing-the-gencode-script"title="Permalink to this headline">¶</a></h2>
<p>There are two gencode scripts: <strong>gencode-local</strong> and <strong>gencode-remote</strong>.
The output of gencode-local is executed locally, whereas
the output of gencode-remote is executed on the target.
The gencode scripts can make use of the parameters, the global explorers
and the type specific explorers.</p>
<p>If the gencode scripts encounters an error, it should print diagnostic
messages to stderr and exit non-zero. If you need to debug the gencode
script, you can write to stderr:</p>
<divclass="highlight-sh notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># Debug output to stderr</span>
<h2><spanclass="section-number">15.20. </span>Variable access from the generated scripts<aclass="headerlink"href="#variable-access-from-the-generated-scripts"title="Permalink to this headline">¶</a></h2>
<p>In the generated scripts, you have access to the following cdist variables</p>
<ulclass="simple">
<li><p>__object</p></li>
<li><p>__object_id</p></li>
</ul>
<p>but only for read operations, means there is no back copy of this
files after the script execution.</p>
<p>So when you generate a script with the following content, it will work:</p>
<h2><spanclass="section-number">15.21. </span>Environment variable usage idiom<aclass="headerlink"href="#environment-variable-usage-idiom"title="Permalink to this headline">¶</a></h2>
<p>In type scripts you can support environment variables with default values if
environment variable is unset or null by using <strong>${parameter:-[word]}</strong>
parameter expansion.</p>
<p>Example using mktemp in a portable way that supports TMPDIR environment variable.</p>
<h2><spanclass="section-number">15.22. </span>Log level in types<aclass="headerlink"href="#log-level-in-types"title="Permalink to this headline">¶</a></h2>
<p>cdist log level can be accessed from __cdist_log_level variable.One of:</p>
<p>It is available for initial manifest, explorer, type manifest,
type explorer, type gencode.</p>
</div>
<divclass="section"id="detecting-dry-run">
<h2><spanclass="section-number">15.23. </span>Detecting dry run<aclass="headerlink"href="#detecting-dry-run"title="Permalink to this headline">¶</a></h2>
<p>If <codeclass="docutils literal notranslate"><spanclass="pre">$__cdist_dry_run</span></code> environment variable is set, then it's dry run.</p>
<p>It is available for initial manifest, explorer, type manifest,
type explorer, type gencode.</p>
</div>
<divclass="section"id="hints-for-typewriters">
<h2><spanclass="section-number">15.24. </span>Hints for typewriters<aclass="headerlink"href="#hints-for-typewriters"title="Permalink to this headline">¶</a></h2>
<p>It must be assumed that the target is pretty dumb and thus does not have high
level tools like ruby installed. If a type requires specific tools to be present
on the target, there must be another type that provides this tool and the first
type should create an object of the specific type.</p>
<p>If your type wants to save temporary data, that may be used by other types
later on (for instance __file), you can save them in the subdirectory
"files" below $__object (but you must create it yourself).
cdist will not touch this directory.</p>
<p>If your type contains static files, it's also recommended to place them in
a folder named "files" within the type (again, because cdist guarantees to
<h2><spanclass="section-number">15.25. </span>How to include a type into upstream cdist<aclass="headerlink"href="#how-to-include-a-type-into-upstream-cdist"title="Permalink to this headline">¶</a></h2>
<p>If you think your type may be useful for others, ensure it works with the
current master branch of cdist and have a look at <aclass="reference external"href="cdist-hacker.html">cdist hacking</a> on
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> using a <ahref="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.