Imported Upstream version 15.21.5
[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 \subpage plugin-system Receive notification if system content has changed
104
105 \ref plugin-services
106
107 \ref plugin-url-resolver
108
109 \ref plugin-appdata
110
111 <HR><!-- ====================================================================== -->
112 \section plugin-services Service plugins
113
114 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".
115
116 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").
117
118 \subsection plugin-services-example1 Example: Management console
119
120 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).
121
122 \subsection plugin-services-how How to write a Services plugin
123
124 When listing services, ZYpp will find each plugin service as a subscribed service.
125
126 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).
127
128 For our example:
129
130 \verbatim
131 # example plugin output
132 # comments are ignored
133 [repo1]
134 name=Base repository
135 summary=Standard repository
136 baseurl=http://server.com/repo1
137 type=rpm-md
138
139 # multiple repositories can be present in the output
140
141 [repo2]
142 ...
143
144 \endverbatim
145
146 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).
147
148
149 <HR><!-- ====================================================================== -->
150 \section plugin-url-resolver Url Resolver plugins
151
152 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:
153
154 ZYpp sees a repository whose url has the format:
155
156 \verbatim
157 plugin:foo?param1=val1&param2=val2
158 \endverbatim
159
160 ZYpp tries to execute a plugin named foo (in /usr/lib/zypp/plugins/urlresolver) and call it with the following protocol:
161
162 \verbatim
163    RESOLVEURL
164    param1:val1
165    param2:val2
166    ...
167    ^@
168 \endverbatim
169
170 The plugin answers:
171
172 \verbatim
173    RESOLVEDURL:
174    header1:val1
175    header2:val2
176    ...
177    http://realurl.com?opts=vals
178    ^@
179 \endverbatim
180
181 And this url is used instead.
182
183 \subsection plugin-urlresolver-example Example
184
185 You have a repository with url:
186
187    plugin:lan
188
189 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.
190
191 This type of plugin can be combined with service plugins, because a local service could return a list of repos like this:
192
193 \verbatim
194   [distro]
195   name=Distribution repository
196   baseurl=plugin:lan?repo=distro
197   [update]
198   name=Update repository
199   baseurl=plugin:lan?repo=update
200 \endverbatim
201
202 \note
203 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.
204
205 <HR><!-- ====================================================================== -->
206 \section plugin-appdata Appdata refresh plugins (repo change)
207
208 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:
209
210 \verbatim
211   -R REPO_ALIAS -t REPO_TYPE -p REPO_METADATA_PATH   -R NEXT_REPO....
212 \endverbatim
213
214 \note REPO_TYPE can be e.g. "rpm-md", "yast2", "plaindir" or "NONE" indicating the repo was not yet probed.
215
216 \note REPO_METADATA_PATH can be empty or a not existing directory, indicating valid metadata for the repo are not yet available.
217
218 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.
219 */