.\" Man page generated from reStructuredText. . .TH "PLAINBOX-TEMPLATE-UNITS" "7" "January 05, 2016" "0.25" "Plainbox" .SH NAME plainbox-template-units \- syntax and semantics of Plainbox template unit type . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .SH SYNOPSIS .sp This page documents the Plainbox template units syntax and runtime behavior .SH DESCRIPTION .sp The template unit is a variant of Plainbox unit types. A template is a skeleton for defining additional units, typically job definitions. A template is defined as a typical RFC822\-like Plainbox unit (like a typical job definition) with the exception that all the fields starting with the string \fBtemplate\-\fP are reserved for the template itself while all the other fields are a definition of all the eventual instances of the template. .SS Template\-Specific Fields .sp There are four fields that are specific to the template unit: .INDENT 0.0 .TP .B \fBtemplate\-unit\fP: Name of the unit type this template will generate. By default job definition units are generated (as if the field was specified with the value of \fBjob\fP) eventually but other values may be used as well. .sp This field is optional. .TP .B \fBtemplate\-resource\fP: Name of the resource job (if it is a compatible resource identifier) to use to parametrize the template. This must either be a name of a resource job available in the namespace the template unit belongs to \fIor\fP a valid resource identifier matching the definition in the \fBtemplate\-imports\fP field. .sp This field is mandatory. .TP .B \fBtemplate\-imports\fP: A resource import statement. It can be used to refer to arbitrary resource job by its full identifier and (optionally) give it a short variable name. .sp The syntax of each imports line is: .INDENT 7.0 .INDENT 3.5 .sp .nf .ft C IMPORT_STMT :: "from" "import" | "from" "import" AS .ft P .fi .UNINDENT .UNINDENT .sp The short syntax exposes \fBPARTIAL_ID\fP as the variable name available within all the fields defined within the template unit. If it is not a valid variable name then the second form must be used. .sp This field is sometimes optional. It becomes mandatory when the resource job definition is from another provider namespace or when it is not a valid resource identifier and needs to be aliased. .TP .B \fBtemplate\-filter\fP: A resource program that limits the set of records from which template instances will be made. The syntax of this field is the same as the syntax of typical job definition unit\(aqs \fBrequires\fP field, that is, it is a python expression. .sp When defined, the expression is evaluated once for each resource object and if it evaluates successfully to a True value then that particular resource object is used to instantiate a new unit. .sp This field is optional. .UNINDENT .SS Instantiation .sp When a template is instantiated, a single record object is used to fill in the parametric values to all the applicable fields. Each field is formatted using the python formatting language. Within each field the record is exposed as the variable named by the \fBtemplate_resource\fP field. Record data is exposed as attributes of that object. .sp The special parameter \fB__index__\fP can be used to iterate over the devices matching the \fBtemplate\-filter\fP field. .SS Migrating From Local Jobs .sp Migration from local jobs is mostly straightforward. Apart from one gotcha the process is as follows: .INDENT 0.0 .IP 1. 3 Look at the data that was used to \fIinstantiate\fP job definitions by the old local job. Write them down. .IP 2. 3 Ensure that all of the instantiated template data is exposed by exactly one resource. This may be the commonly\-used checkbox \fBdevice\fP resource job or any custom resource job but it has to be all contained in one resource. Data that used to be computed partially by the resource and partially by the local job needs to be computed as additional attributes (fields) of the resource instead. .IP 3. 3 Replace the boilerplate of the local job (typically a \fBcat\fP, here\-document piped to \fBrun\-templates\fP and \fBfilter\-templates\fP) with the equivalent \fBtemplate\-resource\fP and \fBtemplate\-filter\fP fields. .IP 4. 3 Remove the indentation so that all of the job definition is aligned to the left of the paragraph. .IP 5. 3 Re\-validate the provider to ensure that everything looks okay. .IP 6. 3 Re\-test the job by running it. .UNINDENT .sp The only gotcha is related to step two. It is very common for local jobs to do some additional computation. For example many storage tests compute the path name of some \fBsysfs\fP file. This has to be converted to a readily\-available path that is provided by the resource job. .SH EXAMPLES .sp The following example contains a simplified template that instantiates to a simple storage test. The test is only instantiated for devices that are considered \fIphysical\fP\&. In this example we don\(aqt want to spam the user with a long list of loopback devices. This is implemented by exposing that data in the resource job itself: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C id: device plugin: resource command: echo \(aqpath: /dev/sda\(aq echo \(aqhas_media: yes\(aq echo \(aqphysical: yes\(aq echo echo \(aqpath: /dev/cdrom\(aq echo \(aqhas_media: no\(aq echo \(aqphysical: yes\(aq echo echo \(aqpath: /dev/loop0\(aq echo \(aqhas_media: yes\(aq echo \(aqphysical: no\(aq .ft P .fi .UNINDENT .UNINDENT .sp The template defines a test\-storage\-\fBXXX\fP test where \fBXXX\fP is replaced by the path of the device. Only devices which are \fIphysical\fP according to some definition are considered for testing. This means that the record related to \fB/dev/loop0\fP will be ignored and will not instantiate a test job for that device. This feature can be coupled with the existing resource requirement to let the user know that we did see their CD\-ROM device but it was not tested as there was no inserted media at the time: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C unit: template template\-resource: device template\-filter: device.physical == \(aqyes\(aq requires: device.has_media == \(aqyes\(aq id: test\-storage\-{path} plugin: shell command: perform\-testing\-on \-\-device {path} .ft P .fi .UNINDENT .UNINDENT .SH AUTHOR Zygmunt Krynicki & Checkbox Contributors .SH COPYRIGHT 2012-2014 Canonical Ltd .\" Generated by docutils manpage writer. .