update plugin doc (add zypp-plugin-python section and commit plugin page)
[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.codehaus.org/Protocol (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 hollowed 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 plugin-toc Supported plugins
87
88 \subpage plugin-commit Escort installation of packages
89
90 \ref plugin-services
91
92 \ref plugin-url-resolver
93
94
95 <HR><!-- ====================================================================== -->
96 \section plugin-services Service plugins
97
98 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".
99
100 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").
101
102 \subsection plugin-services-example1 Example: Management console
103
104 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).
105
106 \subsection plugin-services-how How to write a Services plugin
107
108 When listing services, ZYpp will find each plugin service as a subscribed service.
109
110 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).
111
112 For our example:
113
114 \verbatim
115 # example plugin output
116 # comments are ignored
117 [repo1]
118 name=Base repository
119 summary=Standard repository
120 baseurl=http://server.com/repo1
121 type=rpm-md
122
123 # multiple repositories can be present in the output
124
125 [repo2]
126 ...
127
128 \endverbatim
129
130 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).
131
132
133 <HR><!-- ====================================================================== -->
134 \section plugin-url-resolver Url Resolver plugins
135
136 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:
137
138 ZYpp sees a repository whose url has the format:
139
140 \verbatim
141 plugin:foo?param1=val1&param2=val2
142 \endverbatim
143
144 ZYpp tries to executa a plugin named foo (in /usr/lib/zypp/plugins/urlresolver) and calla it with the following protocol:
145
146 \verbatim
147    RESOLVEURL
148    param1:val1
149    param2:val2
150    ...
151    ^@
152 \endverbatim
153
154 The plugin answers:
155
156 \verbatim
157    RESOLVEDURL:
158    header1:val1
159    header2:val2
160    ...
161    http://realurl.com?opts=vals
162    ^@
163 \endverbatim
164
165 And this url is used instead.
166
167 \subsection plugin-urlresolver-example Example
168
169 You have a repository with url:
170
171    plugin:lan
172
173 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.
174
175 This type of plugin can be combined with service plugins, because a local service could return a list of repos like this:
176
177 \verbatim
178   [distro]
179   name=Distribution repository
180   baseurl=plugin:lan?repo=distro
181   [update]
182   name=Update repository
183   baseurl=plugin:lan?repo=update
184 \endverbatim
185
186 \note
187 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.
188
189 <HR><!-- ====================================================================== -->
190 \section plugins-impl Developers: Implementation
191
192 Plugins are implemented in the following classes:
193
194 - \ref zypp::PluginScript (Plugin as an external program)
195 - \ref zypp::PluginScriptException
196 - \ref zypp::PluginFrame (Message for the stateful protocol)
197 - \ref zypp::PluginFrameException
198 - \ref zypp::repo::PluginServices (Finds Service plugins)
199
200 The plugins default location is obtained from \ref zypp::ZConfig::pluginsPath()
201
202 */