Imported Upstream version 15.0.0
[platform/upstream/libzypp.git] / doc / autoinclude / Plugins.doc
1 /**
2
3 \page zypp-plugins Extending ZYpp: Plugins and Hooks
4
5 \author Duncan Mac-Vicar P. <dmacvicar@suse.de>
6 \author Michael Andres <ma@suse.de>
7
8 <HR><!-- ====================================================================== -->
9 \section plugins-intro Introduction
10
11 Plugins allow to extend the ZYpp package manager without the need to change
12 code. Plugins are designed as external programs so that they can be written in any language.
13
14 \section plugin-protocols Plugin protocols
15
16 Depending on the complexity and need for future extension, plugins talk
17 to ZYpp using two methods.
18
19 \subsection plugin-protocol-stateless Stateless
20
21 This type of plugin receive input reading the standard input, and answer ZYpp writing to the standard output. This means the plugin is executed once per hook and they are stateless (unless the plugin keeps the state out of process).
22
23 \subsection plugin-protocol-stateful Stateful
24
25 This type of plugin is called by ZYpp and a conversation using a simple protocol. The protocol is based on STOMP http://stomp.github.com (Streaming Text Orientated Messaging Protocol). Messages (called "frames") look like the following:
26
27 \verbatim
28 COMMAND
29 param1:val1
30 param2:val2
31 ...
32 Thus a COMMAND followed by key:value header lines
33 and a multiline body separated from header
34 by an empty line and terminated by NUL.
35 ^@
36 \endverbatim
37
38 \note ^@ is a null (control-@ in ASCII) byte.
39
40 <HR><!-- ====================================================================== -->
41 \section plugin-writing Writing plugins
42
43 A python class is offered as a helper to handle communication between the plugin and
44 libzypp. It is available by installing \c zypp-plugin-python.
45
46 \verbatim
47 zypper in -C zypp-plugin-python
48 \endverbatim
49
50 The plugin must define a method for each message it may receive from \ref libzypp. Message header list and body are passed to the method as arguments.
51
52 \verbatim
53 #!/usr/bin/env python
54 #
55 # zypp plugin
56 #
57 import os
58 import sys
59 from zypp_plugin import Plugin
60
61 class MyPlugin(Plugin):
62
63   def SAMPLE( self, headers, body ):
64     # called upon revieving a SAMPLE message
65     ...
66     if ( ok ):
67       self.ack()
68     else:
69       self.error( { "aheader":"header value" }, "body\n(multiline text ok)" )
70
71 plugin = MyPlugin()
72 plugin.main()
73 \endverbatim
74
75 Two methods \c ack and \c error are available to send back a standard \c ACK or \c ERROR message. You may optionally pass header entries and a multiline body. For sending custom messages use \c answer, which takes the command name as 1st argument, followed by optional header entries and a multiline body.
76
77 Plugin closes \c stdin and exits when receiving a \c _DISCONNECT message. Upon an \c ACK response to \c _DISCONNECT libzypp will not attempt to kill the script. An exit value different than \c 0 may be set via an \c 'exit' header in \c ACK.
78
79 \verbatim
80   def _DISCONNECT( self, headers, body ):
81     sys.stding.close()
82     self.ack( {'exit':'99'}, 'Famous last words.' )
83 \endverbatim
84
85 <HR><!-- ====================================================================== -->
86 \section plugins-impl Developers: Implementation
87
88 Plugins are implemented in the following classes:
89
90 - \ref zypp::PluginScript (Plugin as an external program)
91 - \ref zypp::PluginScriptException
92 - \ref zypp::PluginFrame (Message for the stateful protocol)
93 - \ref zypp::PluginFrameException
94 - \ref zypp::repo::PluginServices (Finds Service plugins)
95
96 The plugins default location is obtained from \ref zypp::ZConfig::pluginsPath()
97
98 <HR><!-- ====================================================================== -->
99 \section plugin-toc Supported plugins
100
101 \subpage plugin-commit Escort installation of packages
102
103 \ref plugin-services
104
105 \ref plugin-url-resolver
106
107 \ref plugin-appdata
108
109 <HR><!-- ====================================================================== -->
110 \section plugin-services Service plugins
111
112 ZYpp will find a subscribed service for each executable located in /usr/lib/zypp/plugins/services and will set the alias as the executable name. The type will be set to "plugin".
113
114 Service plugins are used to provide a client a list of repositories from a central location where more complex logic is needed than a simple remote xml index accessible via http (in that case you can use \ref services-remote "Remote Services").
115
116 \subsection plugin-services-example1 Example: Management console
117
118 You have a custom mass management application that controls the repositories each client muss have. While you can use \ref services-remote "Remote Services" and subscribe each client to an url in the server providing a dynamic repoindex.xml based on the client, if you need to pass custom information in order for the server to calculate the repository list (e.g. number of CPUs) or the protocol that the client and the server and client speak is proprietary, you may implement the service locally, as an executable that will be installed in each client /usr/lib/zypp/plugins/services directory (it may be installed from a package).
119
120 \subsection plugin-services-how How to write a Services plugin
121
122 When listing services, ZYpp will find each plugin service as a subscribed service.
123
124 Service plugins are Stateless. When services are refreshed, ZYpp will call each plugin and the repository list will be taken from the output of the script in INI format (same as how they are stored in /etc/zypp/repos.d).
125
126 For our example:
127
128 \verbatim
129 # example plugin output
130 # comments are ignored
131 [repo1]
132 name=Base repository
133 summary=Standard repository
134 baseurl=http://server.com/repo1
135 type=rpm-md
136
137 # multiple repositories can be present in the output
138
139 [repo2]
140 ...
141
142 \endverbatim
143
144 The repositories will be added on service refresh with the alias present in the output, prefixed by the service alias (in this case, the executable name).
145
146
147 <HR><!-- ====================================================================== -->
148 \section plugin-url-resolver Url Resolver plugins
149
150 Url resolver plugins convert urls of scheme "plugin" into the output of the plugin named $name using the protocol. Thanks to the protocol, each header returned is also added as HTTP headers. The current protocol sequence is:
151
152 ZYpp sees a repository whose url has the format:
153
154 \verbatim
155 plugin:foo?param1=val1&param2=val2
156 \endverbatim
157
158 ZYpp tries to executa a plugin named foo (in /usr/lib/zypp/plugins/urlresolver) and call it with the following protocol:
159
160 \verbatim
161    RESOLVEURL
162    param1:val1
163    param2:val2
164    ...
165    ^@
166 \endverbatim
167
168 The plugin answers:
169
170 \verbatim
171    RESOLVEDURL:
172    header1:val1
173    header2:val2
174    ...
175    http://realurl.com?opts=vals
176    ^@
177 \endverbatim
178
179 And this url is used instead.
180
181 \subsection plugin-urlresolver-example Example
182
183 You have a repository with url:
184
185    plugin:lan
186
187 The script looks which distribution you have installed, and via SLP finds the right repositories in the lan and selects the update one and returns it url. But in addition, it adds a header with the update status that can be collected on the server side.
188
189 This type of plugin can be combined with service plugins, because a local service could return a list of repos like this:
190
191 \verbatim
192   [distro]
193   name=Distribution repository
194   baseurl=plugin:lan?repo=distro
195   [update]
196   name=Update repository
197   baseurl=plugin:lan?repo=update
198 \endverbatim
199
200 \note
201 In this example, the service plugin could have inmediately resolved the urls and returned http://realurl, but the url resolver allows also to add HTTP headers to the request.
202
203 <HR><!-- ====================================================================== -->
204 \section plugin-appdata Appdata refresh plugins (repo change)
205
206 Stateless plugins found in /usr/lib/zypp/plugins/appdata are called whenever any of the system repos has changed (added/removed/renamed/modified) or has been refreshed. Detailed information \b what exactly has changed is not available. (scripts are executed IFF euid is '0' and --root is not used). For every enabled system repo we pass alias type and metadata path on the commandline like this:
207
208 \verbatim
209   -R REPO_ALIAS -t REPO_TYPE -p REPO_METADATA_PATH   -R NEXT_REPO....
210 \endverbatim
211
212 \note REPO_TYPE can be e.g. "rpm-md", "yast2", "plaindir" or "NONE" indicating the repo was not yet probed.
213
214 \note REPO_METADATA_PATH can be empty or a not existing directory, indicating valid metadata for the repo are not yet available.
215
216 Scripts are executed 'fire and forget' whenever a RepoManager instance that performed changes goes out of scope. So it's up to the script to protect against concurrency.
217 */